7dcaa9c111
Fixed #77 : Parser: ShortTermMemoryParser should be called separately Fixed #78 : Remove VariableNode usage Fixed #79 : ConceptManager: Implement compile caching Fixed #80 : SheerkaExecute : parsers_key is not correctly computed Fixed #81 : ValidateConceptEvaluator : Validate concept's where and pre clauses right after the parsing Fixed #82 : SheerkaIsAManager: isa() failed when the set as a body Fixed #83 : ValidateConceptEvaluator : Support BNF and SYA Concepts Fixed #84 : ExpressionParser: Implement the parser as a standard parser Fixed #85 : Services: Give order to services Fixed #86 : cannot manage smart_get_attr(the short, color)
132 lines
3.4 KiB
Python
132 lines
3.4 KiB
Python
import ast
|
|
|
|
from core.builtin_concepts import ReturnValueConcept, BuiltinConcepts, ParserResultConcept
|
|
from core.concept import Concept
|
|
from evaluators.BaseEvaluator import BaseEvaluator
|
|
from parsers.BaseParser import BaseParser
|
|
from parsers.PythonParser import PythonNode
|
|
|
|
reduced_requested = ReturnValueConcept("Sheerka", True, Concept(name=BuiltinConcepts.REDUCE_REQUESTED,
|
|
key=BuiltinConcepts.REDUCE_REQUESTED))
|
|
|
|
|
|
def ret_val(value="value", who="who", status=True):
|
|
"""
|
|
ReturnValueConcept
|
|
:param value:
|
|
:param who:
|
|
:param status:
|
|
:return:
|
|
"""
|
|
return ReturnValueConcept(who, status, value)
|
|
|
|
|
|
def p_ret_val(value="value", parser="parser", status=True):
|
|
"""
|
|
ReturnValueConcept from parser
|
|
:param value:
|
|
:param parser:
|
|
:param status:
|
|
:return:
|
|
"""
|
|
return ReturnValueConcept(BaseParser.get_name(parser), status, value)
|
|
|
|
|
|
def e_ret_val(value="value", evaluator="evaluator", status=True):
|
|
"""
|
|
ReturnValueConcept from evaluator
|
|
:param value:
|
|
:param evaluator:
|
|
:param status:
|
|
:return:
|
|
"""
|
|
return ReturnValueConcept(BaseEvaluator.PREFIX + evaluator, status, value)
|
|
|
|
|
|
def p_ret_val_false(value="value", parser="parser"):
|
|
"""
|
|
Failed ReturnValueConcept from parser
|
|
:param value:
|
|
:param parser:
|
|
:return:
|
|
"""
|
|
return p_ret_val(value, parser, status=False)
|
|
|
|
|
|
def p_ret_val_true(value="value", parser="parser"):
|
|
"""
|
|
Successful ReturnValueConcept from parser
|
|
:param value:
|
|
:param parser:
|
|
:return:
|
|
"""
|
|
return p_ret_val(value, parser, status=True)
|
|
|
|
|
|
def e_ret_val_false(value="value", parser="parser"):
|
|
"""
|
|
Failed ReturnValueConcept from evaluator
|
|
:param value:
|
|
:param parser:
|
|
:return:
|
|
"""
|
|
return e_ret_val(value, parser, status=False)
|
|
|
|
|
|
def e_ret_val_true(value="value", parser="parser"):
|
|
"""
|
|
Successful ReturnValueConcept from evaluator
|
|
:param value:
|
|
:param parser:
|
|
:return:
|
|
"""
|
|
return e_ret_val(value, parser, status=True)
|
|
|
|
|
|
def e_ret_val_new(key, evaluator="evaluator", status=True, **kwargs):
|
|
"""
|
|
Successful ReturnValueConcept from evaluator that returns a concept
|
|
:param key:
|
|
:param evaluator:
|
|
:param status:
|
|
:param kwargs:
|
|
:return:
|
|
"""
|
|
body = new_concept(key, **kwargs)
|
|
return e_ret_val(body, evaluator, status)
|
|
|
|
|
|
def pr_ret_val(value, parser="parser", source=None, status=True):
|
|
"""
|
|
ParserResult ReturnValue
|
|
eg: ReturnValue with a ParserResult
|
|
:param value:
|
|
:param parser:
|
|
:param source:
|
|
:param status:
|
|
:return:
|
|
"""
|
|
source = source or (value.name if isinstance(value, Concept) else "source")
|
|
parser_result = ParserResultConcept(BaseParser.get_name(parser), source=source, value=value)
|
|
return p_ret_val(value=parser_result, parser=parser, status=status)
|
|
|
|
|
|
def python_ret_val(source):
|
|
"""
|
|
ReturnValueConcept with a PythonNode
|
|
:param source:
|
|
:return:
|
|
"""
|
|
python_node = PythonNode(source.strip(), ast.parse(source.strip(), f"<source>", 'eval'))
|
|
return pr_ret_val(python_node, parser="Python", source=source)
|
|
|
|
|
|
def new_concept(key, **kwargs):
|
|
res = Concept(key=key, name=key, is_builtin=False, is_unique=False)
|
|
for k, v in kwargs.items():
|
|
to_use = "#" + k + "#" if k in ("body", "pre", "post", "ret") else k
|
|
res.set_value(to_use, v)
|
|
|
|
res.get_hints().is_evaluated = True
|
|
return res
|