88 lines
3.1 KiB
Python
88 lines
3.1 KiB
Python
import copy
|
|
|
|
from core.ast.visitors import UnreferencedNamesVisitor
|
|
from core.builtin_concepts import BuiltinConcepts, ParserResultConcept
|
|
from evaluators.BaseEvaluator import OneReturnValueEvaluator
|
|
from parsers.PythonParser import PythonNode
|
|
import ast
|
|
import core.ast.nodes
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
class PythonEvaluator(OneReturnValueEvaluator):
|
|
NAME = "Python"
|
|
|
|
def __init__(self):
|
|
super().__init__(self.NAME, 50)
|
|
|
|
def matches(self, context, return_value):
|
|
return return_value.status and \
|
|
isinstance(return_value.value, ParserResultConcept) and \
|
|
isinstance(return_value.value.value, PythonNode)
|
|
|
|
def eval(self, context, return_value):
|
|
sheerka = context.sheerka
|
|
node = return_value.value.value
|
|
try:
|
|
log.debug(f"Evaluating python node {node}")
|
|
my_locals = self.get_locals(context, node.ast_)
|
|
|
|
if isinstance(node.ast_, ast.Expression):
|
|
compiled = compile(node.ast_, "<string>", "eval")
|
|
evaluated = eval(compiled, {}, my_locals)
|
|
else:
|
|
evaluated = self.exec_with_return(node.ast_, my_locals)
|
|
|
|
return sheerka.ret(self.name, True, evaluated, parents=[return_value])
|
|
except Exception as error:
|
|
error = sheerka.new(BuiltinConcepts.ERROR, body=error)
|
|
return sheerka.ret(self.name, False, error, parents=[return_value])
|
|
|
|
def get_locals(self, context, ast_):
|
|
my_locals = {"sheerka": context.sheerka}
|
|
if context.obj:
|
|
for prop_name, prop_value in context.obj.props.items():
|
|
my_locals[prop_name] = prop_value.value
|
|
|
|
node_concept = core.ast.nodes.python_to_concept(ast_)
|
|
unreferenced_names_visitor = UnreferencedNamesVisitor(context.sheerka)
|
|
unreferenced_names_visitor.visit(node_concept)
|
|
|
|
for name in unreferenced_names_visitor.names:
|
|
concept = context.sheerka.new(name)
|
|
if context.sheerka.isinstance(concept, BuiltinConcepts.UNKNOWN_CONCEPT):
|
|
continue
|
|
|
|
sub_context = context.push(self.name, "Evaluating body", concept)
|
|
context.sheerka.eval_concept(sub_context, concept, ["body"])
|
|
|
|
if not context.sheerka.isa(concept.body, BuiltinConcepts.ERROR):
|
|
my_locals[name] = concept.body
|
|
|
|
return my_locals
|
|
|
|
@staticmethod
|
|
def expr_to_expression(expr):
|
|
expr.lineno = 0
|
|
expr.col_offset = 0
|
|
result = ast.Expression(expr.value, lineno=0, col_offset=0)
|
|
|
|
return result
|
|
|
|
def exec_with_return(self, code_ast, my_locals):
|
|
|
|
init_ast = copy.deepcopy(code_ast)
|
|
init_ast.body = code_ast.body[:-1]
|
|
|
|
last_ast = copy.deepcopy(code_ast)
|
|
last_ast.body = code_ast.body[-1:]
|
|
|
|
exec(compile(init_ast, "<ast>", "exec"), {}, my_locals)
|
|
if type(last_ast.body[0]) == ast.Expr:
|
|
return eval(compile(self.expr_to_expression(last_ast.body[0]), "<ast>", "eval"), {}, my_locals)
|
|
else:
|
|
exec(compile(last_ast, "<ast>", "exec"), {}, my_locals)
|