diff --git a/smnp/environment/environment.py b/smnp/environment/environment.py index 6c3c7ce..32a009f 100644 --- a/smnp/environment/environment.py +++ b/smnp/environment/environment.py @@ -1,3 +1,4 @@ +from smnp.error.function import FunctionNotFoundException from smnp.error.runtime import RuntimeException @@ -9,6 +10,15 @@ class Environment(): self.customFunctions = {} self.callStack = [] #TODO remove + def invokeFunction(self, name, args): + for function in self.functions: # TODO to działa tylko dla wbudowanych funkcji + if function.name == name: + ret = function.call(self, args) + if ret is not None: + return ret + raise FunctionNotFoundException(name) + # TODO raise nie znaleziono funkcji + def findVariable(self, name, type=None, pos=None): for scope in reversed(self.scopes): if name in scope: diff --git a/smnp/error/function.py b/smnp/error/function.py index 149abed..c622f2b 100644 --- a/smnp/error/function.py +++ b/smnp/error/function.py @@ -3,4 +3,9 @@ from smnp.error.base import SmnpException class IllegalFunctionInvocationException(SmnpException): def __init__(self, expected, found): - self.msg = f"Illegal function invocation\n\nExpected signature:\n{expected}\n\nFound:\n{found}" \ No newline at end of file + self.msg = f"Illegal function invocation\n\nExpected signature:\n{expected}\n\nFound:\n{found}" + + +class FunctionNotFoundException(SmnpException): + def __init__(self, function): + self.msg = f"Function '{function}' not found" diff --git a/smnp/environment/function/__init__.py b/smnp/library/__init__.py similarity index 100% rename from smnp/environment/function/__init__.py rename to smnp/library/__init__.py diff --git a/smnp/library/function/__init__.py b/smnp/library/function/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/smnp/environment/function/base.py b/smnp/library/function/base.py similarity index 100% rename from smnp/environment/function/base.py rename to smnp/library/function/base.py diff --git a/smnp/environment/function/interval.py b/smnp/library/function/interval.py similarity index 93% rename from smnp/environment/function/interval.py rename to smnp/library/function/interval.py index de69022..c1fe692 100644 --- a/smnp/environment/function/interval.py +++ b/smnp/library/function/interval.py @@ -1,4 +1,4 @@ -from smnp.environment.function.tools import returnElementOrList +from smnp.library.tools import returnElementOrList from smnp.note.interval import intervalToString from smnp.note.model import Note diff --git a/smnp/environment/function/list.py b/smnp/library/function/list.py similarity index 100% rename from smnp/environment/function/list.py rename to smnp/library/function/list.py diff --git a/smnp/environment/function/mic.py b/smnp/library/function/mic.py similarity index 100% rename from smnp/environment/function/mic.py rename to smnp/library/function/mic.py diff --git a/smnp/environment/function/note.py b/smnp/library/function/note.py similarity index 94% rename from smnp/environment/function/note.py rename to smnp/library/function/note.py index e69e1d8..3c58b42 100644 --- a/smnp/environment/function/note.py +++ b/smnp/library/function/note.py @@ -1,4 +1,4 @@ -from smnp.environment.function.tools import returnElementOrList +from smnp.library.tools import returnElementOrList def changeDuration(args, env): diff --git a/smnp/environment/function/rand.py b/smnp/library/function/rand.py similarity index 100% rename from smnp/environment/function/rand.py rename to smnp/library/function/rand.py diff --git a/smnp/environment/function/synth.py b/smnp/library/function/synth.py similarity index 100% rename from smnp/environment/function/synth.py rename to smnp/library/function/synth.py diff --git a/smnp/environment/function/transposer.py b/smnp/library/function/transposer.py similarity index 92% rename from smnp/environment/function/transposer.py rename to smnp/library/function/transposer.py index 394ed7a..7decbcf 100644 --- a/smnp/environment/function/transposer.py +++ b/smnp/library/function/transposer.py @@ -1,5 +1,5 @@ -from smnp.environment.function.interval import semitonesList -from smnp.environment.function.tools import returnElementOrList +from smnp.library.function.interval import semitonesList +from smnp.library.tools import returnElementOrList from smnp.note.model import Note diff --git a/smnp/environment/function/model.py b/smnp/library/model.py similarity index 85% rename from smnp/environment/function/model.py rename to smnp/library/model.py index 0de4f74..2ecf8a4 100644 --- a/smnp/environment/function/model.py +++ b/smnp/library/model.py @@ -2,6 +2,7 @@ from enum import Enum, auto from smnp.error.function import IllegalFunctionInvocationException from smnp.type.model import Type +from smnp.type.value import Value class FunctionType(Enum): @@ -21,7 +22,9 @@ class Function: def call(self, env, args): result = self.signature.check(args) if result[0]: - return self.function(env, *result[1:]) + ret = self.function(env, *result[1:]) + if ret is None: + return Value(Type.VOID, None) raise IllegalFunctionInvocationException(self.stringSignature(), f"{self.name}{types(args)}") #TODO: argumenty do typów, nie wartości @@ -38,7 +41,9 @@ class CombinedFunction(Function): for function in self.functions: result = function.signature.check(args) if result[0]: - return function.function(env, *result[1:]) + ret = function.function(env, *result[1:]) + if ret is None: + return Value(Type.VOID, None) raise IllegalFunctionInvocationException(self.stringSignature(), f"{self.name}{types(args)}") diff --git a/smnp/environment/function/signature.py b/smnp/library/signature.py similarity index 100% rename from smnp/environment/function/signature.py rename to smnp/library/signature.py diff --git a/smnp/environment/function/tools.py b/smnp/library/tools.py similarity index 100% rename from smnp/environment/function/tools.py rename to smnp/library/tools.py diff --git a/smnp/type/model.py b/smnp/type/model.py index 0afd7bb..29c5a61 100644 --- a/smnp/type/model.py +++ b/smnp/type/model.py @@ -10,7 +10,7 @@ class Type(Enum): LIST = (list, lambda x: f"({', '.join([e.stringify() for e in x])})") PERCENT = (float, lambda x: f"{int(x * 100)}%") NOTE = (Note, lambda x: x.note.name) - VOID = (None, lambda x: _failStringify(x)) + VOID = (type(None), lambda x: _failStringify(x)) def stringify(self, element): return self.value[1](element)