Enhanced PythonEvaluator to accept concepts
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
from core.builtin_concepts import ParserResultConcept, BuiltinConcepts
|
||||
import core.builtin_helpers
|
||||
from core.concept import Concept, ConceptParts
|
||||
from evaluators.BaseEvaluator import OneReturnValueEvaluator
|
||||
import logging
|
||||
@@ -47,13 +48,14 @@ class ConceptEvaluator(OneReturnValueEvaluator):
|
||||
sheerka.new(BuiltinConcepts.PROPERTY_EVAL_ERROR, body=prop, concept=concept, error=res.value),
|
||||
parents=[return_value])
|
||||
|
||||
# Evaluate body
|
||||
# Returns the concept when no body
|
||||
if ConceptParts.BODY not in concept.codes:
|
||||
return sheerka.ret(self.name, True, concept, parents=[return_value])
|
||||
|
||||
# Evaluate the body otherwise
|
||||
body = concept.codes[ConceptParts.BODY]
|
||||
if body is None:
|
||||
return None # seems weird
|
||||
raise NotImplementedError("Seems weird !")
|
||||
|
||||
sub_context = context.push(self.name, "Evaluating body", concept)
|
||||
res = self.evaluate_parsing(sheerka, sub_context, body)
|
||||
@@ -61,5 +63,5 @@ class ConceptEvaluator(OneReturnValueEvaluator):
|
||||
|
||||
def evaluate_parsing(self, sheerka, context, parsing_result):
|
||||
res = sheerka.chain_process(context, parsing_result, self.evaluation_steps)
|
||||
res = sheerka.expect_one(context, res)
|
||||
res = core.builtin_helpers.expect_one(context, res)
|
||||
return res
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from core.builtin_concepts import BuiltinConcepts
|
||||
from core.concept import Concept
|
||||
import core.builtin_helpers
|
||||
from evaluators.BaseEvaluator import AllReturnValuesEvaluator, BaseEvaluator
|
||||
import logging
|
||||
|
||||
@@ -36,7 +37,7 @@ class MultipleSameSuccessEvaluator(AllReturnValuesEvaluator):
|
||||
elif ret.who.startswith(BaseEvaluator.PREFIX):
|
||||
if ret.status:
|
||||
nb_successful_evaluators += 1
|
||||
self.success.append(ret.value)
|
||||
self.success.append(ret)
|
||||
elif ret.who.startswith(BaseParser.PREFIX):
|
||||
if ret.status:
|
||||
only_parsers_in_error = False
|
||||
@@ -46,19 +47,9 @@ class MultipleSameSuccessEvaluator(AllReturnValuesEvaluator):
|
||||
return after_evaluation and nb_successful_evaluators > 1 and only_parsers_in_error and not unlisted
|
||||
|
||||
def eval(self, context, return_values):
|
||||
reference = self.get_value(self.success[0])
|
||||
|
||||
for return_value in self.success[1:]:
|
||||
actual = self.get_value(return_value)
|
||||
if actual != reference:
|
||||
return None
|
||||
|
||||
sheerka = context.sheerka
|
||||
return sheerka.ret(self.name, True, reference, parents=return_values)
|
||||
if core.builtin_helpers.is_same_success(sheerka, self.success):
|
||||
reference = sheerka.value(self.success[0].value, allow_none_body=True)
|
||||
return sheerka.ret(self.name, True, reference, parents=return_values)
|
||||
|
||||
@staticmethod
|
||||
def get_value(obj):
|
||||
if not isinstance(obj, Concept):
|
||||
return obj
|
||||
|
||||
return obj if obj.body is None else obj.body
|
||||
return None
|
||||
|
||||
@@ -1,14 +1,18 @@
|
||||
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):
|
||||
@@ -22,22 +26,62 @@ class PythonEvaluator(OneReturnValueEvaluator):
|
||||
def eval(self, context, return_value):
|
||||
sheerka = context.sheerka
|
||||
node = return_value.value.value
|
||||
if isinstance(node.ast_, ast.Expression):
|
||||
try:
|
||||
log.debug(f"Evaluating python node {node}")
|
||||
compiled = compile(node.ast_, "<string>", "eval")
|
||||
evaluated = eval(compiled, {}, self.get_locals(context))
|
||||
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])
|
||||
else:
|
||||
return sheerka.ret(self.name, False, sheerka.new(BuiltinConcepts.ERROR), parents=[return_value])
|
||||
try:
|
||||
log.debug(f"Evaluating python node {node}")
|
||||
my_locals = self.get_locals(context, node.ast_)
|
||||
|
||||
@staticmethod
|
||||
def get_locals(context):
|
||||
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.transform(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)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from core.builtin_concepts import BuiltinConcepts
|
||||
from core.concept import Concept
|
||||
import core.builtin_helpers
|
||||
from evaluators.BaseEvaluator import AllReturnValuesEvaluator, BaseEvaluator
|
||||
import logging
|
||||
|
||||
@@ -46,20 +46,9 @@ class TooManySuccessEvaluator(AllReturnValuesEvaluator):
|
||||
return after_evaluation and nb_successful_evaluators > 1 and only_parsers_in_error and not unlisted
|
||||
|
||||
def eval(self, context, return_values):
|
||||
reference = self.get_value(self.success[0].value)
|
||||
|
||||
for return_value in self.success[1:]:
|
||||
actual = self.get_value(return_value.value)
|
||||
if actual != reference:
|
||||
sheerka = context.sheerka
|
||||
too_many_success = sheerka.new(BuiltinConcepts.TOO_MANY_SUCCESS, obj=self.success)
|
||||
return sheerka.ret(self.name, False, too_many_success, parents=return_values)
|
||||
sheerka = context.sheerka
|
||||
if not core.builtin_helpers.is_same_success(sheerka, self.success):
|
||||
too_many_success = sheerka.new(BuiltinConcepts.TOO_MANY_SUCCESS, obj=self.success)
|
||||
return sheerka.ret(self.name, False, too_many_success, parents=return_values)
|
||||
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def get_value(obj):
|
||||
if not isinstance(obj, Concept):
|
||||
return obj
|
||||
|
||||
return obj if obj.body is None else obj.body
|
||||
|
||||
Reference in New Issue
Block a user