Minor code enhancements
This commit is contained in:
+4
-3
@@ -933,19 +933,20 @@ class Sheerka(Concept):
|
||||
defs = self.sdp.get(self.CONCEPTS_DEFINITIONS_ENTRY)
|
||||
self.log.info(defs)
|
||||
|
||||
def dump_desc(self, concept_name):
|
||||
def dump_desc(self, *concept_names):
|
||||
first = True
|
||||
for concept_name in concept_names:
|
||||
if isinstance(concept_name, Concept):
|
||||
concepts = concept_name
|
||||
else:
|
||||
concepts = self.get(concept_name)
|
||||
if self.isinstance(concepts, BuiltinConcepts.UNKNOWN_CONCEPT):
|
||||
self.log.error("Concept unknown")
|
||||
self.log.error(f"Concept '{concept_name}' is unknown")
|
||||
return False
|
||||
|
||||
if not hasattr(concepts, "__iter__"):
|
||||
concepts = [concepts]
|
||||
|
||||
first = True
|
||||
for c in concepts:
|
||||
if not first:
|
||||
self.log.info("")
|
||||
|
||||
@@ -59,7 +59,12 @@ class PythonEvaluator(OneReturnValueEvaluator):
|
||||
return sheerka.ret(self.name, False, error, parents=[return_value])
|
||||
|
||||
def get_locals(self, context, node):
|
||||
my_locals = {"sheerka": context.sheerka}
|
||||
my_locals = {
|
||||
"sheerka": context.sheerka,
|
||||
"desc": context.sheerka.dump_desc,
|
||||
"concepts": context.sheerka.dump_concepts,
|
||||
"definitions": context.sheerka.dump_definitions,
|
||||
}
|
||||
if context.obj:
|
||||
context.log(self.verbose_log,
|
||||
f"Concept '{context.obj}' is in context. Adding its properties to locals if any.", self.name)
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
from core.builtin_concepts import BuiltinConcepts
|
||||
from parsers.BaseParser import BaseParser
|
||||
from parsers.ConceptLexerParser import UnrecognizedTokensNode, ConceptNode
|
||||
from parsers.ConceptLexerParser import ConceptNode
|
||||
from parsers.MultipleConceptsParser import MultipleConceptsParser
|
||||
from parsers.PythonParser import PythonParser
|
||||
|
||||
multiple_concepts_parser = MultipleConceptsParser()
|
||||
|
||||
|
||||
class PythonWithConceptsParser(BaseParser):
|
||||
def __init__(self, **kwargs):
|
||||
@@ -22,16 +25,10 @@ class PythonWithConceptsParser(BaseParser):
|
||||
if not sheerka.isinstance(text, BuiltinConcepts.PARSER_RESULT):
|
||||
return None
|
||||
|
||||
if not text.parser == multiple_concepts_parser:
|
||||
return None
|
||||
|
||||
nodes = text.body
|
||||
if not isinstance(nodes, list):
|
||||
return None
|
||||
|
||||
if len(nodes) == 0:
|
||||
return None
|
||||
|
||||
if not isinstance(nodes[0], (ConceptNode, UnrecognizedTokensNode)):
|
||||
return None
|
||||
|
||||
source = ""
|
||||
to_parse = ""
|
||||
identifiers = {}
|
||||
@@ -107,7 +104,6 @@ class PythonWithConceptsParser(BaseParser):
|
||||
if id(concept) in self.identifiers:
|
||||
return self.identifiers[id(concept)]
|
||||
|
||||
|
||||
identifier = "__C__" + (concept.key or concept.name)
|
||||
if concept.id:
|
||||
identifier += "__" + concept.id
|
||||
|
||||
@@ -7,10 +7,13 @@ from core.concept import Concept
|
||||
from core.sheerka import Sheerka, ExecutionContext
|
||||
from core.tokenizer import Token, TokenKind, Tokenizer
|
||||
from parsers.ConceptLexerParser import ConceptNode, UnrecognizedTokensNode
|
||||
from parsers.MultipleConceptsParser import MultipleConceptsParser
|
||||
from parsers.PythonParser import PythonNode, PythonErrorNode
|
||||
from parsers.PythonWithConceptsParser import PythonWithConceptsParser
|
||||
from sdp.sheerkaDataProvider import Event
|
||||
|
||||
multiple_concepts_parser = MultipleConceptsParser()
|
||||
|
||||
|
||||
def get_context():
|
||||
sheerka = Sheerka(skip_builtins_in_db=True)
|
||||
@@ -31,7 +34,7 @@ def get_ret_from(*args):
|
||||
result.append(UnrecognizedTokensNode(index, index + len(tokens) - 1, tokens))
|
||||
index += len(tokens)
|
||||
|
||||
return ReturnValueConcept("who", False, ParserResultConcept(parser="name", value=result))
|
||||
return ReturnValueConcept("who", False, ParserResultConcept(parser=multiple_concepts_parser, value=result))
|
||||
|
||||
|
||||
def to_str_ast(expression):
|
||||
|
||||
@@ -427,6 +427,42 @@ def test_i_can_eval_a_mix_with_bnf_and_python_when_rule_name():
|
||||
assert res[0].status
|
||||
assert res[0].body == 22
|
||||
|
||||
res = sheerka.evaluate_user_input("twenty one + twenty two")
|
||||
assert len(res) == 1
|
||||
assert res[0].status
|
||||
assert res[0].body == 43
|
||||
|
||||
res = sheerka.evaluate_user_input("twenty one + one")
|
||||
assert len(res) == 1
|
||||
assert res[0].status
|
||||
assert res[0].body == 22
|
||||
|
||||
|
||||
def test_i_can_eval_a_mix_with_bnf_and_python_when_rule_name_2():
|
||||
sheerka = get_sheerka()
|
||||
|
||||
sheerka.evaluate_user_input("def concept one as 1")
|
||||
sheerka.evaluate_user_input("def concept two as 2")
|
||||
sheerka.evaluate_user_input("def concept twenty as 20")
|
||||
sheerka.evaluate_user_input("def concept twenties from bnf twenty (one | two)=unit as twenty + unit")
|
||||
|
||||
assert sheerka.evaluate_user_input("eval twenty one")[0].body == 21
|
||||
|
||||
res = sheerka.evaluate_user_input("twenty one + 1")
|
||||
assert len(res) == 1
|
||||
assert res[0].status
|
||||
assert res[0].body == 22
|
||||
|
||||
res = sheerka.evaluate_user_input("twenty one + twenty two")
|
||||
assert len(res) == 1
|
||||
assert res[0].status
|
||||
assert res[0].body == 43
|
||||
|
||||
res = sheerka.evaluate_user_input("twenty one + one")
|
||||
assert len(res) == 1
|
||||
assert res[0].status
|
||||
assert res[0].body == 22
|
||||
|
||||
|
||||
def test_i_can_eval_a_more_complicated_mix_with_bnf_and_python():
|
||||
sheerka = get_sheerka()
|
||||
|
||||
Reference in New Issue
Block a user