import copy from enum import Enum from core.ast.visitors import UnreferencedNamesVisitor from core.builtin_concepts import BuiltinConcepts, ParserResultConcept from core.concept import ConceptParts, Concept from evaluators.BaseEvaluator import OneReturnValueEvaluator from parsers.PythonParser import PythonNode import ast import core.ast.nodes import core.utils class PythonEvaluator(OneReturnValueEvaluator): NAME = "Python" """ Evaluate a Python node, ie, evaluate some Python code """ def __init__(self): super().__init__(self.NAME, [BuiltinConcepts.EVALUATION], 50) self.locals = {} 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: context.log(f"Evaluating python node {node}.", self.name) # Do not evaluate if the ast refers to a concept (leave it to ConceptEvaluator) if isinstance(node.ast_, ast.Expression) and isinstance(node.ast_.body, ast.Name): c = context.sheerka.get(node.ast_.body.id) if not context.sheerka.isinstance(c, BuiltinConcepts.UNKNOWN_CONCEPT): context.log("It's a simple concept. Not for me.", self.name) not_for_me = context.sheerka.new(BuiltinConcepts.NOT_FOR_ME, body=node) return sheerka.ret(self.name, False, not_for_me, parents=[return_value]) # get locals my_globals = self.get_globals(context, node) my_locals = {} context.log(f"globals={my_globals}", self.name) # eval if isinstance(node.ast_, ast.Expression): context.log("Evaluating using 'eval'.", self.name) compiled = compile(node.ast_, "", "eval") evaluated = eval(compiled, my_globals, my_locals) else: context.log("Evaluating using 'exec'.", self.name) evaluated = self.exec_with_return(node.ast_, my_globals, my_locals) context.log(f"{evaluated=}", self.name) return sheerka.ret(self.name, True, evaluated, parents=[return_value]) except Exception as error: context.log_error(error, self.name) error = sheerka.new(BuiltinConcepts.ERROR, body=error) return sheerka.ret(self.name, False, error, parents=[return_value]) def get_globals(self, context, node): my_locals = { "sheerka": context.sheerka, "desc": context.sheerka.dump_handler.dump_desc, "concepts": context.sheerka.dump_handler.dump_concepts, "definitions": context.sheerka.dump_handler.dump_definitions, "history": context.sheerka.dump_handler.dump_history, "state": context.sheerka.dump_handler.dump_state, "Concept": core.concept.Concept } if context.obj: context.log(f"Concept '{context.obj}' is in context. Adding it and its properties to locals.", self.name) for prop_name, prop_value in context.obj.props.items(): if isinstance(prop_value.value, Concept): my_locals[prop_name] = context.sheerka.value(prop_value.value) else: my_locals[prop_name] = prop_value.value my_locals["self"] = context.obj.body node_concept = core.ast.nodes.python_to_concept(node.ast_) unreferenced_names_visitor = UnreferencedNamesVisitor(context.sheerka) unreferenced_names_visitor.visit(node_concept) for name in unreferenced_names_visitor.names: context.log(f"Resolving '{name}'.", self.name) if name in node.concepts: context.log(f"Using value from node.", self.name) concept = node.concepts[name] return_concept = False else: c_key, c_id, return_concept = self.resolve_name(name) if c_key in my_locals: context.log(f"Using value from property.", self.name) continue context.log(f"Instantiating new concept with {c_key=}, {c_id=}.", self.name) new = context.sheerka.new concept = new((None, c_id)) if c_id else new(c_key) if context.sheerka.isinstance(concept, BuiltinConcepts.UNKNOWN_CONCEPT): context.log(f"({c_key=}, {c_id=}) is not a concept. Skipping.", self.name) continue context.log(f"Evaluating '{concept}'", self.name) with context.push(self.name, desc=f"Evaluating '{concept}'", obj=concept) as sub_context: evaluated = context.sheerka.evaluate_concept(sub_context, concept, True) sub_context.add_values(return_values=evaluated) if evaluated.key == concept.key: my_locals[name] = evaluated if return_concept else context.sheerka.value(evaluated) if self.locals: # when exta values are given. Add them my_locals.update(self.locals) return my_locals @staticmethod def resolve_name(to_resolve): """ Try to match __C__concept_key__C__ or __C__concept_key__concept_id__C__ :param to_resolve: :return: """ key, id_, use_concept = core.utils.decode_concept(to_resolve) if key or id_: return key, id_, use_concept else: return to_resolve, None, False @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_globals, 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, "", "exec"), {}, my_locals) if type(last_ast.body[0]) == ast.Expr: return eval(compile(self.expr_to_expression(last_ast.body[0]), "", "eval"), my_globals, my_locals) else: exec(compile(last_ast, "", "exec"), my_globals, my_locals)