Implemented FunctionParser

This commit is contained in:
2020-09-17 14:11:09 +02:00
parent 8a866880bc
commit 177a6b1d5f
40 changed files with 1752 additions and 561 deletions
+55 -36
View File
@@ -348,8 +348,8 @@ as:
"def concept one as 1",
"def concept two as 2",
"def concept number",
"one isa number",
"two isa number",
"set_isa(one, number)",
"set_isa(two, number)",
"def concept twenties from bnf 'twenty' number as 20 + number"
]),
("When using isa and concept twenty", [
@@ -357,8 +357,8 @@ as:
"def concept two as 2",
"def concept twenty as 20",
"def concept number",
"one isa number",
"two isa number",
"set_isa(one, number)",
"set_isa(two, number)",
"def concept twenties from bnf twenty number as 20 + number"
]),
])
@@ -408,8 +408,8 @@ as:
sheerka.evaluate_user_input("def concept one as 1")
sheerka.evaluate_user_input("def concept two as 2")
sheerka.evaluate_user_input("def concept number")
sheerka.evaluate_user_input("one isa number")
sheerka.evaluate_user_input("two isa number")
sheerka.evaluate_user_input("set_isa(one, number)")
sheerka.evaluate_user_input("set_isa(two, number)")
sheerka.evaluate_user_input("def concept twenties from bnf 'twenty' number as 20 + number")
res = sheerka.evaluate_user_input("twenty one")
@@ -450,8 +450,8 @@ as:
"def concept one as 1",
"def concept twenty as 20",
"def concept number",
"one isa number",
"twenty isa number",
"set_isa(one, number)",
"set_isa(twenty, number)",
"def concept twenties from bnf twenty number as twenty + number"
]
@@ -563,7 +563,7 @@ as:
definitions = [
"def concept two as 2",
"def concept number",
"two isa number",
"set_isa(two, number)",
"def concept plus_one from bnf number=n1 'plus_one' as n1 + 1",
]
@@ -574,15 +574,6 @@ as:
assert res[0].status
assert res[0].body == 3
def test_i_can_say_that_a_concept_isa_another_concept(self):
sheerka = self.get_sheerka()
sheerka.evaluate_user_input("def concept foo")
sheerka.evaluate_user_input("def concept bar")
res = sheerka.evaluate_user_input("foo isa bar")
assert len(res) == 1
assert res[0].status
assert sheerka.isinstance(res[0].body, BuiltinConcepts.SUCCESS)
def test_eval_does_not_break_valid_result(self):
sheerka = self.get_sheerka()
@@ -662,9 +653,9 @@ as:
"def concept three as 3",
"def concept twenty as 20",
"def concept number",
"one isa number",
"two isa number",
"three isa number",
"set_isa(one, number)",
"set_isa(two, number)",
"set_isa(three, number)",
"def concept twenties from bnf twenty number where number <= 2 as twenty + number"
]
@@ -759,7 +750,7 @@ as:
definitions = [
"def concept one as 1",
"def concept number",
"one isa number",
"set_isa(one, number)",
"def concept hundreds from bnf number=n1 'hundred' ('and' number=n2)? where n1<10 and n2<100 as n1 * 100 + n2",
]
@@ -782,7 +773,7 @@ as:
sheerka.evaluate_user_input("def concept two as 2")
sheerka.evaluate_user_input("def concept twenties from bnf 'twenty' (one|two)=unit as 20 + unit")
res = sheerka.evaluate_user_input("twenties isa number")
res = sheerka.evaluate_user_input("set_isa(twenties, number)")
assert len(res) == 1
assert res[0].status
@@ -950,11 +941,11 @@ as:
"def concept two as 2",
"def concept twenty as 20",
"def concept number",
"one isa number",
"two isa number",
"twenty isa number",
"set_isa(one, number)",
"set_isa(two, number)",
"set_isa(twenty, number)",
"def concept twenties from bnf twenty number where number < 10 as twenty + number",
"twenties isa number",
"set_isa(twenties, number)",
]
sheerka = self.init_scenario(init)
@@ -975,7 +966,7 @@ as:
sheerka = self.init_scenario(init)
res = sheerka.evaluate_user_input("last_created_concept() isa number")
res = sheerka.evaluate_user_input("set_isa(last_created_concept(), number)")
assert res[0].status
assert sheerka.isa(sheerka.new("one"), sheerka.new("number"))
@@ -1021,7 +1012,7 @@ as:
"def concept one",
"def concept foo",
"def concept number",
"one isa number",
"set_isa(one, number)",
"def concept x is a y as isa(x,y) pre in_context(BuiltinConcepts.EVAL_QUESTION_REQUESTED)",
"def concept x is a y as set_isa(x,y)",
]
@@ -1041,7 +1032,7 @@ as:
init = [
"def concept one as 1",
"def concept number",
"one isa number",
"set_isa(one, number)",
"def concept one as 10", # to make sure that it won't be rejected because of the cast
"def concept x is a y as isa(x,y) pre in_context(BuiltinConcepts.EVAL_QUESTION_REQUESTED)",
"def concept x is a y as set_isa(x,y)",
@@ -1069,7 +1060,7 @@ as:
"def concept one",
"def concept foo",
"def concept number",
"one isa number",
"set_isa(one, number)",
"def concept q from q ? as question(q)",
"def concept is_a from x is a y as isa(x,y) pre in_context(BuiltinConcepts.EVAL_QUESTION_REQUESTED)",
"set_is_greater_than(BuiltinConcepts.PRECEDENCE, c:is_a:, c:q:)"
@@ -1125,6 +1116,34 @@ as:
assert len(res) == 1
assert res[0].status
def test_i_can_eval_concepts_fed_with_functions(self):
init = [
"def concept inc a as a + 1",
"def concept one as 1"
]
def times_five(i):
return i * 5
sheerka = self.init_scenario(init)
sheerka.locals["times_five"] = times_five
res = sheerka.evaluate_user_input("eval inc times_five(one)")
assert len(res) == 1
assert res[0].status
assert res[0].body == 6
def test_i_can_define_a_concept_when_where_clause_contains_the_name_of_the_variable(self):
init = [
"def concept x is a y as isa(x,y) pre is_question()",
]
sheerka = self.init_scenario(init)
res = sheerka.evaluate_user_input("def concept a x b where a is a number as a + b")
assert len(res) == 1
assert res[0].status
assert sheerka.isinstance(res[0].body, BuiltinConcepts.NEW_CONCEPT)
class TestSheerkaNonRegFile(TestUsingFileBasedSheerka):
def test_i_can_def_several_concepts(self):
@@ -1197,15 +1216,15 @@ class TestSheerkaNonRegFile(TestUsingFileBasedSheerka):
self.init_scenario([
"def concept one as 1",
"def concept number",
"one isa number",
"set_isa(one, number)",
"def concept twenty as 20",
"twenty isa number",
"set_isa(twenty, number)",
"def concept twenties from bnf twenty number where number < 10 as twenty + number",
"twenties isa number",
"set_isa(twenties, number)",
"def concept thirty as 30",
"thirty isa number",
"set_isa(thirty, number)",
"def concept thirties from bnf thirty number where number < 10 as thirty + number",
"thirties isa number",
"set_isa(thirties, number)",
])
sheerka = self.get_sheerka() # another instance