I can define variables

This commit is contained in:
2020-06-10 07:42:09 +02:00
parent af3a3ffe92
commit 9eae784581
3 changed files with 22 additions and 9 deletions
+2
View File
@@ -86,6 +86,8 @@ class Sheerka(Concept):
}
self.sheerka_pipeables = {}
self.locals = {}
@property
def resolved_concepts_by_first_keyword(self):
"""
+6 -7
View File
@@ -44,7 +44,7 @@ class PythonEvaluator(OneReturnValueEvaluator):
def __init__(self):
super().__init__(self.NAME, [BuiltinConcepts.EVALUATION], 50)
self.locals = {}
self.globals = {}
def matches(self, context, return_value):
return return_value.status and \
@@ -65,19 +65,18 @@ class PythonEvaluator(OneReturnValueEvaluator):
not_for_me = context.sheerka.new(BuiltinConcepts.NOT_FOR_ME, body=node)
return sheerka.ret(self.name, False, not_for_me, parents=[return_value])
# get locals
# get globals
my_globals = self.get_globals(context, node)
my_locals = {}
context.log(f"globals={my_globals}", self.name)
# eval
if isinstance(node.ast_, ast.Expression):
context.log("Evaluating using 'eval'.", self.name)
compiled = compile(node.ast_, "<string>", "eval")
evaluated = eval(compiled, my_globals, my_locals)
evaluated = eval(compiled, my_globals, sheerka.locals)
else:
context.log("Evaluating using 'exec'.", self.name)
evaluated = self.exec_with_return(node.ast_, my_globals, my_locals)
evaluated = self.exec_with_return(node.ast_, my_globals, sheerka.locals)
context.log(f"{evaluated=}", self.name)
return sheerka.ret(self.name, True, evaluated, parents=[return_value])
@@ -99,8 +98,8 @@ class PythonEvaluator(OneReturnValueEvaluator):
self.update_globals_with_context(my_globals, context)
self.update_globals_with_node(my_globals, context, node)
if self.locals: # when extra values are given. Add them
my_globals.update(self.locals)
if self.globals: # when extra values are given. Add them
my_globals.update(self.globals)
my_globals["sheerka"] = Expando(method_from_sheerka) # it's the last, so I cannot be overridden
return my_globals
+14 -2
View File
@@ -121,7 +121,7 @@ class TestPythonEvaluator(TestUsingMemoryBasedSheerka):
parsed = PythonParser().parse(context, ParserInput("get_concept_name(c:foo:)"))
python_evaluator = PythonEvaluator()
python_evaluator.locals["get_concept_name"] = get_concept_name
python_evaluator.globals["get_concept_name"] = get_concept_name
evaluated = python_evaluator.eval(context, parsed)
assert evaluated.status
@@ -130,7 +130,7 @@ class TestPythonEvaluator(TestUsingMemoryBasedSheerka):
# sanity, does not work otherwise
parsed = PythonParser().parse(context, ParserInput("get_concept_name(foo)"))
python_evaluator = PythonEvaluator()
python_evaluator.locals["get_concept_name"] = get_concept_name
python_evaluator.globals["get_concept_name"] = get_concept_name
evaluated = python_evaluator.eval(context, parsed)
assert not evaluated.status
@@ -150,6 +150,18 @@ class TestPythonEvaluator(TestUsingMemoryBasedSheerka):
assert evaluated.status
assert sheerka.get_concepts_weights(BuiltinConcepts.PRECEDENCE) == {'1001': 1, '1002': 2}
def test_i_can_define_variables(self):
sheerka, context = self.init_concepts()
parsed = PythonParser().parse(context, ParserInput("a=10"))
python_evaluator = PythonEvaluator()
python_evaluator.eval(context, parsed)
parsed = PythonParser().parse(context, ParserInput("a"))
evaluated = python_evaluator.eval(context, parsed)
assert evaluated.status
assert evaluated.body == 10
# @pytest.mark.parametrize("text, concept_key, concept_id, use_concept", [
# ("__C__key__C__", "key", None, False),