Improve environment #1

This commit is contained in:
Bartłomiej Pluta
2019-07-04 11:43:07 +02:00
parent ce101df380
commit b60dedc769
16 changed files with 28 additions and 8 deletions

View File

@@ -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:

View File

@@ -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}"
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"

View File

View File

@@ -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

View File

@@ -1,4 +1,4 @@
from smnp.environment.function.tools import returnElementOrList
from smnp.library.tools import returnElementOrList
def changeDuration(args, env):

View File

@@ -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

View File

@@ -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)}")

View File

@@ -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)