Fixed infinite recursion when parsing complex BNF node

This commit is contained in:
2020-06-23 15:22:27 +02:00
parent 912455c343
commit 7310bc5522
28 changed files with 1082 additions and 276 deletions
+52 -15
View File
@@ -1,6 +1,6 @@
import pytest
from core.builtin_concepts import BuiltinConcepts
from core.concept import Concept, PROPERTIES_TO_SERIALIZE, simplec, CMV, CB, CC, CV
from core.concept import Concept, PROPERTIES_TO_SERIALIZE, simplec, CMV
from evaluators.MutipleSameSuccessEvaluator import MultipleSameSuccessEvaluator
from evaluators.PythonEvaluator import PythonEvalError
from parsers.BaseNodeParser import SyaAssociativity
@@ -12,16 +12,6 @@ from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
class TestSheerkaNonRegMemory(TestUsingMemoryBasedSheerka):
def init_scenario(self, init_expressions):
sheerka = self.get_sheerka()
for expression in init_expressions:
res = sheerka.evaluate_user_input(expression)
assert len(res) == 1, f"Failed to execute '{expression}'"
assert res[0].status, f"Error while executing '{expression}'"
return sheerka
@pytest.mark.parametrize("text, expected", [
("1 + 1", 2),
("sheerka.test()", 'I have access to Sheerka !')
@@ -880,10 +870,10 @@ as:
sheerka = self.init_scenario(definitions)
res = sheerka.evaluate_user_input("is_greater_than('some_prop', two, one)")
res = sheerka.evaluate_user_input("set_is_greater_than('some_prop', two, one)")
assert res[0].status
res = sheerka.evaluate_user_input("is_less_than('some_prop', two, three)")
res = sheerka.evaluate_user_input("set_is_less_than('some_prop', two, three)")
assert res[0].status
res = sheerka.evaluate_user_input("get_concepts_weights('some_prop')")
@@ -891,7 +881,7 @@ as:
assert res[0].body == {'1001': 1, '1002': 2, '1003': 3}
# test i use a concept to define relation
sheerka.evaluate_user_input("def concept a > b as is_greater_than('some_prop', a, b)")
sheerka.evaluate_user_input("def concept a > b as set_is_greater_than('some_prop', a, b)")
res = sheerka.evaluate_user_input("eval four > three")
assert res[0].status
@@ -901,7 +891,7 @@ as:
sheerka, context, one, two, plus = self.init_concepts(
Concept("one", body="1"),
Concept("two", body="2"),
self.from_def_concept("<", "a < b", ["a", "b"], body="is_less_than('some_prop', a, b)")
self.from_def_concept("<", "a < b", ["a", "b"], body="set_is_less_than('some_prop', a, b)")
)
expression = "c:one: < c:two:"
@@ -946,6 +936,33 @@ as:
assert isinstance(error1.error, TypeError)
assert error1.error.args[0] == "unsupported operand type(s) for +: 'Concept' and 'int'"
def test_i_can_evaluate_bnf_concept_defined_with_group_after_restart(self):
"""
BNF Concepts defined with group and being themselves part a s group get messed up after restart
:return:
"""
init = [
"def concept one as 1",
"def concept two as 2",
"def concept twenty as 20",
"def concept number",
"one isa number",
"two isa number",
"twenty isa number",
"def concept twenties from bnf twenty number where number < 10 as twenty + number",
"twenties isa number",
]
sheerka = self.init_scenario(init)
# simulate that sheerka was stopped and restarted
sheerka.cache_manager.clear(sheerka.CONCEPTS_GRAMMARS_ENTRY)
sheerka.cache_manager.get(sheerka.CONCEPTS_BY_KEY_ENTRY, "twenties").compiled = {}
res = sheerka.evaluate_user_input("eval twenty one")
assert res[0].status
assert res[0].body == 21
class TestSheerkaNonRegFile(TestUsingFileBasedSheerka):
def test_i_can_def_several_concepts(self):
@@ -1013,3 +1030,23 @@ class TestSheerkaNonRegFile(TestUsingFileBasedSheerka):
evaluated = sheerka.evaluate_concept(self.get_context(eval_body=True), res[0].value)
assert evaluated.body == "one two three"
assert evaluated.get_value("a") == sheerka.new(concept_a.key, body="one two").init_key()
def test_i_can_eval_sophisticated_bnf_concepts_after_restart(self):
self.init_scenario([
"def concept one as 1",
"def concept number",
"one isa number",
"def concept twenty as 20",
"twenty isa number",
"def concept twenties from bnf twenty number where number < 10 as twenty + number",
"twenties isa number",
"def concept thirty as 30",
"thirty isa number",
"def concept thirties from bnf thirty number where number < 10 as thirty + number",
"thirties isa number",
])
sheerka = self.get_sheerka() # another instance
assert sheerka.evaluate_user_input("eval twenty one")[0].body == 21
assert sheerka.evaluate_user_input("eval thirty one")[0].body == 31