from core.builtin_concepts import ParserResultConcept, BuiltinConcepts from core.concept import Concept, ConceptParts from evaluators.BaseEvaluator import OneReturnValueEvaluator class ConceptEvaluator(OneReturnValueEvaluator): """ The concept evaluatuor is the main class that know what to do with a concept It verifies the PRE If ok, can execute or not the BODY Then checks the POST conditions """ NAME = "Concept" evaluation_steps = [ BuiltinConcepts.BEFORE_EVALUATION, BuiltinConcepts.EVALUATION, BuiltinConcepts.AFTER_EVALUATION ] 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, Concept) def eval(self, context, return_value): sheerka = context.sheerka concept = return_value.value.value context.log(self.verbose_log, f"Evaluating concept {concept}.", self.name) # If the concept that is requested is in the context(at least its name), drop the call. # Why ? # If we evaluate Concept("foo", body="a").set_prop("a", "'property_a'") # The body should be 'property_a', and not a concept called a in our universe if context.obj and concept.name in context.obj.props: return sheerka.ret(self.name, False, sheerka.new(BuiltinConcepts.NOT_FOR_ME), parents=[return_value]) evaluated = sheerka.evaluate_concept(context, concept, self.verbose_log) if evaluated.key != concept.key: # evaluated.key != concept.key means that we have transformed the concept # When you successfully evaluate an error, the status should not be false return sheerka.ret( self.name, False, evaluated, parents=[return_value]) if ConceptParts.BODY not in evaluated.cached_asts: return sheerka.ret(self.name, True, evaluated, parents=[return_value]) else: return sheerka.ret(self.name, True, evaluated.body, parents=[return_value])