Files
smnp-py/smnp/runtime/evaluators/assignment.py
2019-07-08 19:33:14 +02:00

33 lines
1.4 KiB
Python

from smnp.error.runtime import RuntimeException
from smnp.runtime.evaluator import Evaluator
from smnp.runtime.evaluators.expression import expressionEvaluator
class AssignmentEvaluator(Evaluator):
@classmethod
def evaluator(cls, node, environment):
target = node.target.value
if target.startswith("_"):
raise RuntimeException("Usage of variables with names starting with '_' is not allowed", node.target.pos)
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