Files
Sheerka-Old/evaluators/ConceptEvaluator.py
T

56 lines
2.3 KiB
Python

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, return_body=False):
super().__init__(self.NAME, [BuiltinConcepts.EVALUATION], 50)
self.return_body = return_body
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, True, context.obj.props[concept.name].value, 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 not self.return_body or 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])