From 6d56706354601bed556c40357cc213d3b55fec87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Pluta?= Date: Mon, 8 Jul 2019 17:48:02 +0200 Subject: [PATCH] Enable creating custom methods --- smnp/environment/environment.py | 26 ++++++++++++++++++++------ smnp/main.py | 2 +- smnp/runtime/evaluator.py | 3 +++ smnp/runtime/evaluators/extend.py | 25 +++++++++++++++++++++++++ smnp/runtime/evaluators/type.py | 10 ++++++++++ smnp/type/model.py | 1 + smnp/type/value.py | 2 +- 7 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 smnp/runtime/evaluators/extend.py create mode 100644 smnp/runtime/evaluators/type.py diff --git a/smnp/environment/environment.py b/smnp/environment/environment.py index f10a672..f2a74ce 100644 --- a/smnp/environment/environment.py +++ b/smnp/environment/environment.py @@ -33,15 +33,16 @@ class Environment(): return (False, None) def _invokeCustomMethod(self, object, name, args): - for function in self.customMethods: - if function.name == name: - signatureCheckresult = function.signature.check(args) + for method in self.customMethods: + if method.type.value == object.type and method.name == name: + signatureCheckresult = method.signature.check(args) if signatureCheckresult[0]: - self.scopes.append({argName: argValue for argName, argValue in zip(function.arguments, args)}) - result = BodyEvaluator.evaluate(function.body, self).value # TODO check if it isn't necessary to verify 'result' attr of EvaluatioNResult + self.scopes.append({argName: argValue for argName, argValue in zip(method.arguments, args)}) + self.scopes[-1][method.alias] = object + result = BodyEvaluator.evaluate(method.body, self).value # TODO check if it isn't necessary to verify 'result' attr of EvaluatioNResult self.scopes.pop(-1) return (True, result) - raise IllegalFunctionInvocationException(f"{function.name}{function.signature.string}", + raise IllegalFunctionInvocationException(f"{method.name}{method.signature.string}", f"{name}{types(args)}") return (False, None) @@ -80,6 +81,9 @@ class Environment(): def addCustomFunction(self, name, signature, arguments, body): self.customFunctions.append(CustomFunction(name, signature, arguments, body)) + def addCustomMethod(self, type, alias, name, signature, arguments, body): + self.customMethods.append(CustomMethod(type, alias, name, signature, arguments, body)) + def findVariable(self, name, type=None, pos=None): for scope in reversed(self.scopes): if name in scope: @@ -126,6 +130,16 @@ class Environment(): class CustomFunction: def __init__(self, name, signature, arguments, body): + self.name = name + self.signature = signature + self.arguments = arguments + self.body = body + + +class CustomMethod: + def __init__(self, type, alias, name, signature, arguments, body): + self.type = type + self.alias = alias self.name = name self.signature = signature self.arguments = arguments diff --git a/smnp/main.py b/smnp/main.py index aed51e0..2259a49 100644 --- a/smnp/main.py +++ b/smnp/main.py @@ -16,7 +16,7 @@ def main(): ast = parse(tokens) - ast.print() + #ast.print() env = createEnvironment() diff --git a/smnp/runtime/evaluator.py b/smnp/runtime/evaluator.py index 64bb0a0..c2a2fad 100644 --- a/smnp/runtime/evaluator.py +++ b/smnp/runtime/evaluator.py @@ -1,3 +1,4 @@ +from smnp.ast.node.extend import ExtendNode from smnp.ast.node.function import FunctionDefinitionNode from smnp.ast.node.program import Program from smnp.error.runtime import RuntimeException @@ -68,9 +69,11 @@ def evaluate(node, environment): from smnp.runtime.evaluators.expression import expressionEvaluator from smnp.runtime.evaluators.function import FunctionDefinitionEvaluator + from smnp.runtime.evaluators.extend import ExtendEvaluator result = Evaluator.oneOf( Evaluator.forNodes(ProgramEvaluator.evaluate, Program), Evaluator.forNodes(FunctionDefinitionEvaluator.evaluate, FunctionDefinitionNode), + Evaluator.forNodes(ExtendEvaluator.evaluate, ExtendNode), expressionEvaluator() )(node, environment) diff --git a/smnp/runtime/evaluators/extend.py b/smnp/runtime/evaluators/extend.py new file mode 100644 index 0000000..5ea2f31 --- /dev/null +++ b/smnp/runtime/evaluators/extend.py @@ -0,0 +1,25 @@ +from smnp.runtime.evaluator import Evaluator +from smnp.runtime.evaluators.function import argumentsNodeToMethodSignature +from smnp.runtime.evaluators.type import TypeEvaluator + + +class ExtendEvaluator(Evaluator): + + @classmethod + def evaluator(cls, node, environment): + type = TypeEvaluator.evaluate(node.type, environment).value #TODO check if it isn't necessary to verify 'result' attr of EvaluatioNResult + variable = node.variable.value + cls._evaluateExtend(node.methods, environment, type, variable) + + @classmethod + def _evaluateExtend(cls, node, environment, type, variable): + for child in node.children: + cls._evaluateMethodDefinition(child, environment, type, variable) + + @classmethod + def _evaluateMethodDefinition(cls, node, environment, type, variable): + name = node.name.value + signature = argumentsNodeToMethodSignature(node.arguments) + arguments = [arg.variable.value for arg in node.arguments] + body = node.body + environment.addCustomMethod(type, variable, name, signature, arguments, body) \ No newline at end of file diff --git a/smnp/runtime/evaluators/type.py b/smnp/runtime/evaluators/type.py new file mode 100644 index 0000000..dd34e1b --- /dev/null +++ b/smnp/runtime/evaluators/type.py @@ -0,0 +1,10 @@ +from smnp.runtime.evaluator import Evaluator +from smnp.type.model import Type +from smnp.type.value import Value + + +class TypeEvaluator(Evaluator): + + @classmethod + def evaluator(cls, node, environment): + return Value(Type.TYPE, node.type) \ No newline at end of file diff --git a/smnp/type/model.py b/smnp/type/model.py index c54949e..0c48fcb 100644 --- a/smnp/type/model.py +++ b/smnp/type/model.py @@ -10,6 +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) + TYPE = (None, lambda x: str(x.type.name.lower())) VOID = (type(None), lambda x: _failStringify(Type.VOID)) def stringify(self, element): diff --git a/smnp/type/value.py b/smnp/type/value.py index 154eaac..bed349d 100644 --- a/smnp/type/value.py +++ b/smnp/type/value.py @@ -5,7 +5,7 @@ class Value: def __init__(self, objectType, value): self.value = value - if type(value) == objectType.value[0]: + if objectType.value[0] is None or type(value) == objectType.value[0]: self.type = objectType elif type(value) == Value: