Refactored sheerka execution flow + Enhanced log management
This commit is contained in:
@@ -5,12 +5,9 @@ from core.concept import Concept
|
||||
from evaluators.BaseEvaluator import OneReturnValueEvaluator
|
||||
from parsers.ConceptLexerParser import ParsingExpression, ParsingExpressionVisitor
|
||||
from parsers.DefaultParser import DefConceptNode
|
||||
import logging
|
||||
|
||||
from parsers.PythonParser import PythonNode
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ConceptOrRuleNameVisitor(ParsingExpressionVisitor):
|
||||
"""
|
||||
@@ -49,7 +46,7 @@ class AddConceptEvaluator(OneReturnValueEvaluator):
|
||||
isinstance(return_value.value.value, DefConceptNode)
|
||||
|
||||
def eval(self, context, return_value):
|
||||
log.debug("Adding a new concept")
|
||||
context.log(self.log, "Adding a new concept", self.name)
|
||||
def_concept_node = return_value.value.value
|
||||
sheerka = context.sheerka
|
||||
|
||||
@@ -89,6 +86,9 @@ class AddConceptEvaluator(OneReturnValueEvaluator):
|
||||
concept.bnf = def_concept_node.definition.value.value
|
||||
|
||||
ret = sheerka.create_new_concept(context, concept)
|
||||
if not ret.status:
|
||||
error_cause = sheerka.value(ret.body)
|
||||
context.log(self.log, f"Failed to add concept '{concept.name}'. Reason: {error_cause}", self.name)
|
||||
return sheerka.ret(self.name, ret.status, ret.value, parents=[return_value])
|
||||
|
||||
@staticmethod
|
||||
|
||||
@@ -1,14 +1,22 @@
|
||||
from core.sheerka import ExecutionContext
|
||||
from core.sheerka_logger import get_logger
|
||||
|
||||
|
||||
class BaseEvaluator:
|
||||
"""
|
||||
Base class to evaluate ReturnValues
|
||||
"""
|
||||
|
||||
PREFIX = "Evaluators:"
|
||||
PREFIX = "evaluators."
|
||||
enabled = True
|
||||
|
||||
def __init__(self, name, priority: int):
|
||||
self.log = get_logger(self.PREFIX + self.__class__.__name__)
|
||||
self.init_log = get_logger("init." + self.PREFIX + self.__class__.__name__)
|
||||
self.verbose_log = get_logger("verbose." + self.PREFIX + self.__class__.__name__)
|
||||
|
||||
def __init__(self, name, priority: int, enabled=True):
|
||||
self.name = self.PREFIX + name
|
||||
self.priority = priority
|
||||
self.enabled = enabled
|
||||
|
||||
|
||||
class OneReturnValueEvaluator(BaseEvaluator):
|
||||
@@ -16,10 +24,10 @@ class OneReturnValueEvaluator(BaseEvaluator):
|
||||
Evaluate one specific return value
|
||||
"""
|
||||
|
||||
def matches(self, context, return_value):
|
||||
def matches(self, context: ExecutionContext, return_value):
|
||||
pass
|
||||
|
||||
def eval(self, context, return_value):
|
||||
def eval(self, context: ExecutionContext, return_value):
|
||||
pass
|
||||
|
||||
|
||||
@@ -28,8 +36,8 @@ class AllReturnValuesEvaluator(BaseEvaluator):
|
||||
Evaluates the groups of ReturnValues
|
||||
"""
|
||||
|
||||
def matches(self, context, return_values):
|
||||
def matches(self, context: ExecutionContext, return_values):
|
||||
pass
|
||||
|
||||
def eval(self, context, return_values):
|
||||
def eval(self, context: ExecutionContext, return_values):
|
||||
pass
|
||||
|
||||
@@ -2,9 +2,6 @@ from core.builtin_concepts import ParserResultConcept, BuiltinConcepts
|
||||
import core.builtin_helpers
|
||||
from core.concept import Concept, ConceptParts
|
||||
from evaluators.BaseEvaluator import OneReturnValueEvaluator
|
||||
import logging
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ConceptEvaluator(OneReturnValueEvaluator):
|
||||
@@ -15,7 +12,11 @@ class ConceptEvaluator(OneReturnValueEvaluator):
|
||||
Then checks the POST conditions
|
||||
"""
|
||||
NAME = "Concept"
|
||||
evaluation_steps = [BuiltinConcepts.EVALUATION, BuiltinConcepts.AFTER_EVALUATION]
|
||||
evaluation_steps = [
|
||||
BuiltinConcepts.BEFORE_EVALUATION,
|
||||
BuiltinConcepts.EVALUATION,
|
||||
BuiltinConcepts.AFTER_EVALUATION
|
||||
]
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(self.NAME, 50)
|
||||
@@ -40,7 +41,7 @@ class ConceptEvaluator(OneReturnValueEvaluator):
|
||||
|
||||
# Evaluate the properties
|
||||
for prop in concept.props:
|
||||
sub_context = context.push(self.name, f"Evaluating property '{prop}'", concept)
|
||||
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)
|
||||
@@ -60,11 +61,11 @@ class ConceptEvaluator(OneReturnValueEvaluator):
|
||||
if body is None:
|
||||
raise NotImplementedError("Seems weird !")
|
||||
|
||||
sub_context = context.push(self.name, "Evaluating body", concept)
|
||||
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.chain_process(context, parsing_result, self.evaluation_steps)
|
||||
res = sheerka.execute(context, parsing_result, self.evaluation_steps, self.log)
|
||||
res = core.builtin_helpers.expect_one(context, res)
|
||||
return res
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
from core.builtin_concepts import ParserResultConcept, BuiltinConcepts
|
||||
from evaluators.BaseEvaluator import OneReturnValueEvaluator
|
||||
|
||||
import logging
|
||||
|
||||
from parsers.ConceptLexerParser import ConceptNode, TerminalNode, NonTerminalNode, ConceptMatch
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
from parsers.ConceptLexerParser import ConceptNode, NonTerminalNode, ConceptMatch
|
||||
|
||||
|
||||
class ConceptNodeEvaluator(OneReturnValueEvaluator):
|
||||
|
||||
@@ -1,12 +1,8 @@
|
||||
from core.builtin_concepts import BuiltinConcepts
|
||||
import core.builtin_helpers
|
||||
from evaluators.BaseEvaluator import AllReturnValuesEvaluator, BaseEvaluator
|
||||
import logging
|
||||
|
||||
from parsers.BaseParser import BaseParser
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class MultipleSameSuccessEvaluator(AllReturnValuesEvaluator):
|
||||
"""
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
from core.builtin_concepts import BuiltinConcepts
|
||||
from evaluators.BaseEvaluator import AllReturnValuesEvaluator
|
||||
import logging
|
||||
|
||||
from parsers.BaseParser import BaseParser
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class OneSuccessEvaluator(AllReturnValuesEvaluator):
|
||||
"""
|
||||
|
||||
@@ -7,10 +7,6 @@ from parsers.PythonParser import PythonNode
|
||||
import ast
|
||||
import core.ast.nodes
|
||||
|
||||
import logging
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class PythonEvaluator(OneReturnValueEvaluator):
|
||||
NAME = "Python"
|
||||
@@ -31,17 +27,21 @@ class PythonEvaluator(OneReturnValueEvaluator):
|
||||
sheerka = context.sheerka
|
||||
node = return_value.value.value
|
||||
try:
|
||||
log.debug(f"Evaluating python node {node}")
|
||||
context.log(self.verbose_log, f"Evaluating python node {node}", self.name)
|
||||
my_locals = self.get_locals(context, node.ast_)
|
||||
context.log(self.verbose_log, f"locals={my_locals}", self.name)
|
||||
|
||||
if isinstance(node.ast_, ast.Expression):
|
||||
context.log(self.verbose_log, "Evaluating using 'eval'", self.name)
|
||||
compiled = compile(node.ast_, "<string>", "eval")
|
||||
evaluated = eval(compiled, {}, my_locals)
|
||||
else:
|
||||
context.log(self.verbose_log, "Evaluating using 'exec'", self.name)
|
||||
evaluated = self.exec_with_return(node.ast_, my_locals)
|
||||
|
||||
return sheerka.ret(self.name, True, evaluated, parents=[return_value])
|
||||
except Exception as error:
|
||||
context.log_error(self.verbose_log, error, self.name)
|
||||
error = sheerka.new(BuiltinConcepts.ERROR, body=error)
|
||||
return sheerka.ret(self.name, False, error, parents=[return_value])
|
||||
|
||||
@@ -60,7 +60,7 @@ class PythonEvaluator(OneReturnValueEvaluator):
|
||||
if context.sheerka.isinstance(concept, BuiltinConcepts.UNKNOWN_CONCEPT):
|
||||
continue
|
||||
|
||||
sub_context = context.push(self.name, "Evaluating body", concept)
|
||||
sub_context = context.push(self.name, desc="Evaluating body", obj=concept)
|
||||
context.sheerka.eval_concept(sub_context, concept, ["body"])
|
||||
|
||||
if not context.sheerka.isa(concept.body, BuiltinConcepts.ERROR):
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
import logging
|
||||
|
||||
from core.builtin_concepts import BuiltinConcepts
|
||||
import core.builtin_helpers
|
||||
from evaluators.BaseEvaluator import AllReturnValuesEvaluator, BaseEvaluator
|
||||
import logging
|
||||
|
||||
from parsers.BaseParser import BaseParser
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class TooManySuccessEvaluator(AllReturnValuesEvaluator):
|
||||
"""
|
||||
@@ -49,6 +47,11 @@ class TooManySuccessEvaluator(AllReturnValuesEvaluator):
|
||||
|
||||
def eval(self, context, return_values):
|
||||
sheerka = context.sheerka
|
||||
if self.verbose_log.isEnabledFor(logging.DEBUG):
|
||||
for s in self.success:
|
||||
context.log(self.verbose_log, s, self.name)
|
||||
context.log(self.verbose_log, f"value={sheerka.value(s.value)}", self.name)
|
||||
|
||||
if not core.builtin_helpers.is_same_success(sheerka, self.success):
|
||||
too_many_success = sheerka.new(BuiltinConcepts.TOO_MANY_SUCCESS, body=self.success)
|
||||
return sheerka.ret(self.name, False, too_many_success, parents=return_values)
|
||||
|
||||
Reference in New Issue
Block a user