@@ -0,0 +1,30 @@
|
||||
from core.BuiltinConcepts import BuiltinConcepts
|
||||
from core.ErrorContext import ErrorContext
|
||||
from core.ExecutionContext import ExecutionContext, ExecutionContextActions
|
||||
from core.ReturnValue import ReturnValue
|
||||
from evaluators.base_evaluator import EvaluatorEvalResult, EvaluatorMatchResult, OneReturnValueEvaluator
|
||||
from parsers.ParserInput import ParserInput
|
||||
|
||||
|
||||
class CreateParserInput(OneReturnValueEvaluator):
|
||||
NAME = "CreateParserInput"
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(self.NAME, ExecutionContextActions.BEFORE_EVALUATION, 50)
|
||||
|
||||
def matches(self, context: ExecutionContext, return_value: ReturnValue) -> EvaluatorMatchResult:
|
||||
if return_value.status and \
|
||||
context.sheerka.isinstance(return_value.value, BuiltinConcepts.USER_INPUT):
|
||||
return EvaluatorMatchResult(True)
|
||||
return EvaluatorMatchResult(False)
|
||||
|
||||
def eval(self, context, evaluator_context, return_value):
|
||||
parser_input = ParserInput(return_value.value.get_value("command"))
|
||||
if parser_input.init():
|
||||
parser_input_concept = context.sheerka.newn(BuiltinConcepts.PARSER_INPUT, pi=parser_input)
|
||||
new_ret_val = ReturnValue(self.NAME, True, parser_input_concept, parents=[return_value])
|
||||
return EvaluatorEvalResult([new_ret_val], [return_value])
|
||||
else:
|
||||
error = ErrorContext(self.NAME, context, parser_input)
|
||||
new_ret_val = ReturnValue(self.NAME, False, error, parents=[return_value])
|
||||
return EvaluatorEvalResult([new_ret_val], [return_value])
|
||||
@@ -0,0 +1,73 @@
|
||||
from dataclasses import dataclass
|
||||
|
||||
from core.ExecutionContext import ExecutionContext, ExecutionContextActions
|
||||
from core.ReturnValue import ReturnValue
|
||||
|
||||
|
||||
@dataclass
|
||||
class EvaluatorMatchResult:
|
||||
status: bool
|
||||
obj: object = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class EvaluatorEvalResult:
|
||||
new: list[ReturnValue] = None
|
||||
eaten: list[ReturnValue] = None
|
||||
|
||||
|
||||
class BaseEvaluator:
|
||||
"""
|
||||
Base class to evaluate ReturnValues
|
||||
"""
|
||||
|
||||
def __init__(self, name, step: ExecutionContextActions, priority: int, enabled=True):
|
||||
self.name = name
|
||||
self.step = step
|
||||
self.priority = priority
|
||||
self.enabled = enabled
|
||||
|
||||
def __repr__(self):
|
||||
return f"{self.name} ({self.priority})"
|
||||
|
||||
def __eq__(self, other):
|
||||
if not isinstance(other, BaseEvaluator):
|
||||
return False
|
||||
|
||||
return self.name == other.name and \
|
||||
self.priority == other.priority and \
|
||||
self.step == other.step and \
|
||||
self.enabled == other.enabled
|
||||
|
||||
def __hash__(self):
|
||||
return hash((self.name, self.priority, self.step, self.enabled))
|
||||
|
||||
|
||||
class OneReturnValueEvaluator(BaseEvaluator):
|
||||
"""
|
||||
Evaluate one specific return value
|
||||
"""
|
||||
|
||||
def matches(self, context: ExecutionContext,
|
||||
return_value: ReturnValue) -> EvaluatorMatchResult:
|
||||
pass
|
||||
|
||||
def eval(self, context: ExecutionContext,
|
||||
evaluation_context: object,
|
||||
return_value: ReturnValue) -> EvaluatorEvalResult:
|
||||
pass
|
||||
|
||||
|
||||
class AllReturnValuesEvaluator(BaseEvaluator):
|
||||
"""
|
||||
Evaluates the groups of ReturnValues
|
||||
"""
|
||||
|
||||
def matches(self, context: ExecutionContext,
|
||||
return_values: list[ReturnValue]) -> EvaluatorMatchResult:
|
||||
pass
|
||||
|
||||
def eval(self, context: ExecutionContext,
|
||||
evaluation_context: object,
|
||||
return_values: list[ReturnValue]) -> EvaluatorEvalResult:
|
||||
pass
|
||||
Reference in New Issue
Block a user