Refactored to use a single implementation for concept evaluation

This commit is contained in:
2019-12-21 15:08:06 +01:00
parent b24b858b81
commit 41e0885486
17 changed files with 920 additions and 644 deletions
+19 -37
View File
@@ -1,5 +1,4 @@
from core.builtin_concepts import ParserResultConcept, BuiltinConcepts
import core.builtin_helpers
from core.concept import Concept, ConceptParts
from evaluators.BaseEvaluator import OneReturnValueEvaluator
@@ -31,42 +30,25 @@ class ConceptEvaluator(OneReturnValueEvaluator):
concept = return_value.value.value
context.log(self.verbose_log, f"Evaluating concept {concept}.", self.name)
# pre condition should already be validated by the parser.
# It's a mandatory condition for the concept before it can be recognized
# 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])
if len(concept.cached_asts) == 0:
sheerka.initialize_concept_asts(context, concept, self.verbose_log)
evaluated = sheerka.evaluate_concept(context, concept, self.verbose_log)
# TODO; check pre
# if pre is not true, return Concept with a false value
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])
# Evaluate the properties
for prop in concept.props:
sub_context = context.push(self.name, desc=f"Evaluating property '{prop}'", obj=concept)
res = self.evaluate_parsing(sheerka, sub_context, concept.cached_asts[prop])
if res.status:
concept.set_prop(prop, res.value)
else:
return sheerka.ret(
self.name,
False,
sheerka.new(BuiltinConcepts.PROPERTY_EVAL_ERROR, body=prop, concept=concept, error=res.value),
parents=[return_value])
# Returns the concept when no body
if ConceptParts.BODY not in concept.cached_asts:
return sheerka.ret(self.name, True, concept, parents=[return_value])
# Evaluate the body otherwise
body = concept.cached_asts[ConceptParts.BODY]
if body is None:
raise NotImplementedError("Seems weird !")
sub_context = context.push(self.name, desc="Evaluating body", obj=concept)
res = self.evaluate_parsing(sheerka, sub_context, body)
return sheerka.ret(self.name, res.status, res.value, parents=[return_value])
def evaluate_parsing(self, sheerka, context, parsing_result):
res = sheerka.execute(context, parsing_result, self.evaluation_steps, self.log)
res = core.builtin_helpers.expect_one(context, res)
return res
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])