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