Enable creating custom methods
This commit is contained in:
@@ -33,15 +33,16 @@ class Environment():
|
|||||||
return (False, None)
|
return (False, None)
|
||||||
|
|
||||||
def _invokeCustomMethod(self, object, name, args):
|
def _invokeCustomMethod(self, object, name, args):
|
||||||
for function in self.customMethods:
|
for method in self.customMethods:
|
||||||
if function.name == name:
|
if method.type.value == object.type and method.name == name:
|
||||||
signatureCheckresult = function.signature.check(args)
|
signatureCheckresult = method.signature.check(args)
|
||||||
if signatureCheckresult[0]:
|
if signatureCheckresult[0]:
|
||||||
self.scopes.append({argName: argValue for argName, argValue in zip(function.arguments, args)})
|
self.scopes.append({argName: argValue for argName, argValue in zip(method.arguments, args)})
|
||||||
result = BodyEvaluator.evaluate(function.body, self).value # TODO check if it isn't necessary to verify 'result' attr of EvaluatioNResult
|
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)
|
self.scopes.pop(-1)
|
||||||
return (True, result)
|
return (True, result)
|
||||||
raise IllegalFunctionInvocationException(f"{function.name}{function.signature.string}",
|
raise IllegalFunctionInvocationException(f"{method.name}{method.signature.string}",
|
||||||
f"{name}{types(args)}")
|
f"{name}{types(args)}")
|
||||||
return (False, None)
|
return (False, None)
|
||||||
|
|
||||||
@@ -80,6 +81,9 @@ class Environment():
|
|||||||
def addCustomFunction(self, name, signature, arguments, body):
|
def addCustomFunction(self, name, signature, arguments, body):
|
||||||
self.customFunctions.append(CustomFunction(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):
|
def findVariable(self, name, type=None, pos=None):
|
||||||
for scope in reversed(self.scopes):
|
for scope in reversed(self.scopes):
|
||||||
if name in scope:
|
if name in scope:
|
||||||
@@ -126,6 +130,16 @@ class Environment():
|
|||||||
|
|
||||||
class CustomFunction:
|
class CustomFunction:
|
||||||
def __init__(self, name, signature, arguments, body):
|
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.name = name
|
||||||
self.signature = signature
|
self.signature = signature
|
||||||
self.arguments = arguments
|
self.arguments = arguments
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ def main():
|
|||||||
|
|
||||||
ast = parse(tokens)
|
ast = parse(tokens)
|
||||||
|
|
||||||
ast.print()
|
#ast.print()
|
||||||
|
|
||||||
env = createEnvironment()
|
env = createEnvironment()
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
from smnp.ast.node.extend import ExtendNode
|
||||||
from smnp.ast.node.function import FunctionDefinitionNode
|
from smnp.ast.node.function import FunctionDefinitionNode
|
||||||
from smnp.ast.node.program import Program
|
from smnp.ast.node.program import Program
|
||||||
from smnp.error.runtime import RuntimeException
|
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.expression import expressionEvaluator
|
||||||
|
|
||||||
from smnp.runtime.evaluators.function import FunctionDefinitionEvaluator
|
from smnp.runtime.evaluators.function import FunctionDefinitionEvaluator
|
||||||
|
from smnp.runtime.evaluators.extend import ExtendEvaluator
|
||||||
result = Evaluator.oneOf(
|
result = Evaluator.oneOf(
|
||||||
Evaluator.forNodes(ProgramEvaluator.evaluate, Program),
|
Evaluator.forNodes(ProgramEvaluator.evaluate, Program),
|
||||||
Evaluator.forNodes(FunctionDefinitionEvaluator.evaluate, FunctionDefinitionNode),
|
Evaluator.forNodes(FunctionDefinitionEvaluator.evaluate, FunctionDefinitionNode),
|
||||||
|
Evaluator.forNodes(ExtendEvaluator.evaluate, ExtendNode),
|
||||||
expressionEvaluator()
|
expressionEvaluator()
|
||||||
)(node, environment)
|
)(node, environment)
|
||||||
|
|
||||||
|
|||||||
25
smnp/runtime/evaluators/extend.py
Normal file
25
smnp/runtime/evaluators/extend.py
Normal file
@@ -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)
|
||||||
10
smnp/runtime/evaluators/type.py
Normal file
10
smnp/runtime/evaluators/type.py
Normal file
@@ -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)
|
||||||
@@ -10,6 +10,7 @@ class Type(Enum):
|
|||||||
LIST = (list, lambda x: f"[{', '.join([e.stringify() for e in x])}]")
|
LIST = (list, lambda x: f"[{', '.join([e.stringify() for e in x])}]")
|
||||||
PERCENT = (float, lambda x: f"{int(x * 100)}%")
|
PERCENT = (float, lambda x: f"{int(x * 100)}%")
|
||||||
NOTE = (Note, lambda x: x.note.name)
|
NOTE = (Note, lambda x: x.note.name)
|
||||||
|
TYPE = (None, lambda x: str(x.type.name.lower()))
|
||||||
VOID = (type(None), lambda x: _failStringify(Type.VOID))
|
VOID = (type(None), lambda x: _failStringify(Type.VOID))
|
||||||
|
|
||||||
def stringify(self, element):
|
def stringify(self, element):
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ class Value:
|
|||||||
def __init__(self, objectType, value):
|
def __init__(self, objectType, value):
|
||||||
self.value = 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
|
self.type = objectType
|
||||||
|
|
||||||
elif type(value) == Value:
|
elif type(value) == Value:
|
||||||
|
|||||||
Reference in New Issue
Block a user