From b4d5d11b84a72679554b19716acee0032abbd7d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Pluta?= Date: Tue, 9 Jul 2019 20:54:56 +0200 Subject: [PATCH] Add new functions: 'concat', 'range' and 'Map' constructor --- smnp/environment/factory.py | 5 ++++- smnp/library/function/concat.py | 10 +++++++++ smnp/library/function/map.py | 29 ++++++++++++++++++++++++++ smnp/library/function/range.py | 36 +++++++++++++++++++++++++++++++++ smnp/type/model.py | 1 + 5 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 smnp/library/function/concat.py create mode 100644 smnp/library/function/map.py create mode 100644 smnp/library/function/range.py diff --git a/smnp/environment/factory.py b/smnp/environment/factory.py index 5ce572d..c803fbe 100644 --- a/smnp/environment/factory.py +++ b/smnp/environment/factory.py @@ -1,6 +1,6 @@ from smnp.environment.environment import Environment from smnp.library.function import display, sleep, semitones, interval, combine, flat, wait, rand, tuplet, synth, pause, \ - transpose, type, exit, duration, octave, debug, get, sound, play + transpose, type, exit, duration, octave, debug, get, sound, play, map, concat, range from smnp.type.model import Type @@ -21,6 +21,9 @@ def createEnvironment(): pause.function, transpose.function, sound.function, + map.function, + concat.function, + range.function, debug.function ] diff --git a/smnp/library/function/concat.py b/smnp/library/function/concat.py new file mode 100644 index 0000000..71d5e76 --- /dev/null +++ b/smnp/library/function/concat.py @@ -0,0 +1,10 @@ +from smnp.library.model import Function +from smnp.library.signature import varargSignature, ofType +from smnp.type.model import Type + +_signature = varargSignature(ofType(Type.STRING)) +def _function(env, vararg): + return Type.string("".join([ arg.value for arg in vararg ])) + + +function = Function(_signature, _function, 'concat') \ No newline at end of file diff --git a/smnp/library/function/map.py b/smnp/library/function/map.py new file mode 100644 index 0000000..a325fcb --- /dev/null +++ b/smnp/library/function/map.py @@ -0,0 +1,29 @@ +from smnp.library.model import Function, CombinedFunction +from smnp.library.signature import varargSignature, listMatches, ofTypes, allTypes, signature, listOfMatchers +from smnp.type.model import Type + +_signature1 = varargSignature(listMatches(ofTypes(Type.INTEGER, Type.STRING, Type.NOTE), allTypes())) +def _function1(env, vararg): + map = {} + for entry in vararg: + key, value = entry.value + map[key] = value + + return Type.map(map) + + +_signature2 = signature(listOfMatchers(listMatches(ofTypes(Type.INTEGER, Type.STRING, Type.NOTE), allTypes()))) +def _function2(env, list): + map = {} + for entry in list.value: + key, value = entry.value + map[key] = value + + return Type.map(map) + + +function = CombinedFunction( + 'Map', + Function(_signature1, _function1), + Function(_signature2, _function2) +) \ No newline at end of file diff --git a/smnp/library/function/range.py b/smnp/library/function/range.py new file mode 100644 index 0000000..2ffd0db --- /dev/null +++ b/smnp/library/function/range.py @@ -0,0 +1,36 @@ +from smnp.library.model import CombinedFunction, Function +from smnp.library.signature import signature, ofType +from smnp.note.model import Note +from smnp.type.model import Type + + +_signature1 = signature(ofType(Type.INTEGER)) +def _function1(env, upper): + return Type.list(list(range(upper.value + 1))) + + +_signature2 = signature(ofType(Type.INTEGER), ofType(Type.INTEGER)) +def _function2(env, lower, upper): + return Type.list(list(range(lower.value, upper.value + 1))) + + +_signature3 = signature(ofType(Type.INTEGER), ofType(Type.INTEGER), ofType(Type.INTEGER)) +def _function3(env, lower, upper, step): + return Type.list(list(range(lower.value, upper.value + 1, step.value))) + + +_signature4 = signature(ofType(Type.NOTE), ofType(Type.NOTE)) +def _function4(env, lower, upper): + return Type.list([Type.note(n) for n in Note.range(lower.value, upper.value)]) + + +# TODO +# signature5 = range(note lower, note upper, integer step) OR step = "diatonic" | "chromatic" | "augmented" | "diminish" + +function = CombinedFunction( + 'range', + Function(_signature1, _function1), + Function(_signature2, _function2), + Function(_signature3, _function3), + Function(_signature4, _function4), +) diff --git a/smnp/type/model.py b/smnp/type/model.py index b37bad8..91d4e1b 100644 --- a/smnp/type/model.py +++ b/smnp/type/model.py @@ -47,6 +47,7 @@ class Type(Enum): @staticmethod def note(value): return Value(Type.NOTE, value, { + "pitch": Type.string(str(value.note)), "octave": Type.integer(value.octave), "duration": Type.integer(value.duration), "dot": Type.string('.' if value.dot else '')