Enable variable assignment

This commit is contained in:
Bartłomiej Pluta
2019-07-08 18:27:29 +02:00
parent 05dfe46f9f
commit 61a5a3565e
2 changed files with 31 additions and 14 deletions

View File

@@ -1,15 +1,29 @@
from smnp.error.runtime import RuntimeException
from smnp.runtime.evaluator import evaluate
from smnp.type.model import Type
from smnp.runtime.evaluator import Evaluator
from smnp.runtime.evaluators.expression import expressionEvaluator
def evaluateAssignment(assignment, environment):
target = assignment.target.identifier
value = evaluate(assignment.value, environment)
if value.type == Type.VOID:
raise RuntimeException(f"Expected expression, found '{value.type.name}'", assignment.value.pos)
scopeOfExistingVariable = environment.findVariableScope(target)
if scopeOfExistingVariable is not None:
scopeOfExistingVariable[target] = value
else:
environment.scopes[-1][target] = value
class AssignmentEvaluator(Evaluator):
@classmethod
def evaluator(cls, node, environment):
target = node.target.value
value = expressionEvaluator(doAssert=True)(node.value, environment).value #TODO check if it isn't necessary to verify 'result' attr of EvaluatioNResult
scopeOfExistingVariable = environment.findVariableScope(target)
if scopeOfExistingVariable is None:
environment.scopes[-1][target] = value
else:
scopeOfExistingVariable[target] = value
return value
#
# def evaluateAssignment(assignment, environment):
# target = assignment.target.identifier
# value = evaluate(assignment.value, environment)
# if value.type == Type.VOID:
# raise RuntimeException(f"Expected expression, found '{value.type.name}'", assignment.value.pos)
# scopeOfExistingVariable = environment.findVariableScope(target)
# if scopeOfExistingVariable is not None:
# scopeOfExistingVariable[target] = value
# else:
# environment.scopes[-1][target] = value

View File

@@ -1,4 +1,5 @@
from smnp.ast.node.access import AccessNode
from smnp.ast.node.assignment import AssignmentNode
from smnp.ast.node.identifier import IdentifierNode
from smnp.ast.node.integer import IntegerLiteralNode
from smnp.ast.node.invocation import FunctionCallNode
@@ -20,6 +21,7 @@ def expressionEvaluator(doAssert=False):
from smnp.runtime.evaluators.function import FunctionCallEvaluator
from smnp.runtime.evaluators.access import AccessEvaluator
from smnp.runtime.evaluators.assignment import AssignmentEvaluator
result = Evaluator.oneOf(
Evaluator.forNodes(FunctionCallEvaluator.evaluate, FunctionCallNode),
Evaluator.forNodes(StringEvaluator.evaluate, StringLiteralNode),
@@ -27,7 +29,8 @@ def expressionEvaluator(doAssert=False):
Evaluator.forNodes(NoteEvaluator.evaluate, NoteLiteralNode),
Evaluator.forNodes(IdentifierEvaluator.evaluate, IdentifierNode),
Evaluator.forNodes(ListEvaluator.evaluate, ListNode),
Evaluator.forNodes(AccessEvaluator.evaluate, AccessNode)
Evaluator.forNodes(AccessEvaluator.evaluate, AccessNode),
Evaluator.forNodes(AssignmentEvaluator.evaluate, AssignmentNode)
)(node, environment)
if doAssert and result.result and result.value.type == Type.VOID: