Added basic implentation for where
This commit is contained in:
@@ -8,6 +8,7 @@ from evaluators.BaseEvaluator import OneReturnValueEvaluator
|
||||
from parsers.PythonParser import PythonNode
|
||||
import ast
|
||||
import core.ast.nodes
|
||||
import core.utils
|
||||
|
||||
|
||||
class PythonEvaluator(OneReturnValueEvaluator):
|
||||
@@ -40,9 +41,11 @@ 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
|
||||
my_locals = self.get_locals(context, node)
|
||||
context.log(self.verbose_log, f"locals={my_locals}", self.name)
|
||||
|
||||
# eval
|
||||
if isinstance(node.ast_, ast.Expression):
|
||||
context.log(self.verbose_log, "Evaluating using 'eval'.", self.name)
|
||||
compiled = compile(node.ast_, "<string>", "eval")
|
||||
@@ -53,6 +56,7 @@ class PythonEvaluator(OneReturnValueEvaluator):
|
||||
|
||||
context.log(self.verbose_log, f"{evaluated=}", self.name)
|
||||
return sheerka.ret(self.name, True, evaluated, parents=[return_value])
|
||||
|
||||
except Exception as error:
|
||||
context.log_error(self.verbose_log, error, self.name)
|
||||
error = sheerka.new(BuiltinConcepts.ERROR, body=error)
|
||||
@@ -65,16 +69,19 @@ class PythonEvaluator(OneReturnValueEvaluator):
|
||||
"concepts": context.sheerka.dump_handler.dump_concepts,
|
||||
"definitions": context.sheerka.dump_handler.dump_definitions,
|
||||
"history": context.sheerka.dump_handler.dump_history,
|
||||
"state": context.sheerka.dump_handler.dump_state,
|
||||
}
|
||||
if context.obj:
|
||||
context.log(self.verbose_log,
|
||||
f"Concept '{context.obj}' is in context. Adding its properties to locals if any.", self.name)
|
||||
f"Concept '{context.obj}' is in context. Adding it and its properties to locals.", self.name)
|
||||
|
||||
for prop_name, prop_value in context.obj.props.items():
|
||||
if not isinstance(prop_value.value, Concept):
|
||||
my_locals[prop_name] = prop_value.value
|
||||
else:
|
||||
if isinstance(prop_value.value, Concept):
|
||||
my_locals[prop_name] = context.sheerka.value(prop_value.value)
|
||||
else:
|
||||
my_locals[prop_name] = prop_value.value
|
||||
|
||||
my_locals["self"] = context.obj.body
|
||||
|
||||
node_concept = core.ast.nodes.python_to_concept(node.ast_)
|
||||
unreferenced_names_visitor = UnreferencedNamesVisitor(context.sheerka)
|
||||
@@ -89,16 +96,17 @@ class PythonEvaluator(OneReturnValueEvaluator):
|
||||
return_concept = False
|
||||
|
||||
else:
|
||||
concept_key, concept_id, return_concept = self.resolve_name(context, name)
|
||||
c_key, c_id, return_concept = self.resolve_name(name)
|
||||
|
||||
if concept_key in my_locals:
|
||||
if c_key in my_locals:
|
||||
context.log(self.verbose_log, f"Using value from property.", self.name)
|
||||
continue
|
||||
|
||||
context.log(self.verbose_log, f"Instantiating new concept.", self.name)
|
||||
concept = context.sheerka.new((concept_key, concept_id))
|
||||
context.log(self.verbose_log, f"Instantiating new concept with {c_key=}, {c_id=}.", self.name)
|
||||
new = context.sheerka.new
|
||||
concept = new((None, c_id)) if c_id else new(c_key)
|
||||
if context.sheerka.isinstance(concept, BuiltinConcepts.UNKNOWN_CONCEPT):
|
||||
context.log(self.verbose_log, f"'{concept_key}' is not a concept. Skipping.", self.name)
|
||||
context.log(self.verbose_log, f"({c_key=}, {c_id=}) is not a concept. Skipping.", self.name)
|
||||
continue
|
||||
|
||||
context.log(self.verbose_log, f"Evaluating '{concept}'", self.name)
|
||||
@@ -110,62 +118,68 @@ class PythonEvaluator(OneReturnValueEvaluator):
|
||||
if evaluated.key == concept.key:
|
||||
my_locals[name] = evaluated if return_concept else context.sheerka.value(evaluated)
|
||||
|
||||
if self.locals:
|
||||
if self.locals: # when exta values are given. Add them
|
||||
my_locals.update(self.locals)
|
||||
|
||||
return my_locals
|
||||
|
||||
def resolve_name(self, context, to_resolve):
|
||||
@staticmethod
|
||||
def resolve_name(to_resolve):
|
||||
"""
|
||||
Try to match
|
||||
__C__concept_key__C__
|
||||
or
|
||||
__C__concept_key__concept_id__C__
|
||||
|
||||
:param context:
|
||||
:param to_resolve:
|
||||
:return:
|
||||
"""
|
||||
if not to_resolve.startswith("__C__"):
|
||||
return to_resolve, None, False
|
||||
|
||||
context.log(self.verbose_log, f"Resolving name '{to_resolve}'.", self.name)
|
||||
|
||||
if len(to_resolve) >= 18 and to_resolve[:18] == "__C__USE_CONCEPT__":
|
||||
use_concept = True
|
||||
index = 18
|
||||
key, id_, use_concept = core.utils.decode_concept(to_resolve)
|
||||
if key or id_:
|
||||
return key, id_, use_concept
|
||||
else:
|
||||
use_concept = False
|
||||
index = 5
|
||||
|
||||
try:
|
||||
next_index = to_resolve.index("__", index)
|
||||
if next_index == index:
|
||||
context.log(self.verbose_log, f"Error: no key between '__'.", self.name)
|
||||
return None
|
||||
concept_key = to_resolve[index: next_index]
|
||||
except ValueError:
|
||||
context.log(self.verbose_log, f"Error: Missing trailing '__'.", self.name)
|
||||
return None
|
||||
|
||||
if next_index == len(to_resolve) - 5:
|
||||
context.log(self.verbose_log, f"Recognized concept '{concept_key}'", self.name)
|
||||
return concept_key, None, use_concept
|
||||
|
||||
index = next_index + 2
|
||||
try:
|
||||
next_index = to_resolve.index("__", index)
|
||||
if next_index == index:
|
||||
context.log(self.verbose_log, f"Error: no id between '__'.", self.name)
|
||||
return None
|
||||
|
||||
concept_id = to_resolve[index: next_index]
|
||||
except ValueError:
|
||||
context.log(self.verbose_log, f"Recognized concept '{concept_key}'.", self.name)
|
||||
return concept_key, None, use_concept
|
||||
|
||||
context.log(self.verbose_log, f"Recognized concept '{concept_key}' (id='{concept_id}').", self.name)
|
||||
return concept_key, concept_id, use_concept
|
||||
return to_resolve, None, False
|
||||
#
|
||||
# if not to_resolve.startswith("__C__"):
|
||||
# return to_resolve, None, False
|
||||
#
|
||||
# context.log(self.verbose_log, f"Resolving name '{to_resolve}'.", self.name)
|
||||
#
|
||||
# if len(to_resolve) >= 18 and to_resolve[:18] == "__C__USE_CONCEPT__":
|
||||
# use_concept = True
|
||||
# index = 18
|
||||
# else:
|
||||
# use_concept = False
|
||||
# index = 5
|
||||
#
|
||||
# try:
|
||||
# next_index = to_resolve.index("__", index)
|
||||
# if next_index == index:
|
||||
# context.log(self.verbose_log, f"Error: no key between '__'.", self.name)
|
||||
# return None
|
||||
# concept_key = to_resolve[index: next_index]
|
||||
# except ValueError:
|
||||
# context.log(self.verbose_log, f"Error: Missing trailing '__'.", self.name)
|
||||
# return None
|
||||
#
|
||||
# if next_index == len(to_resolve) - 5:
|
||||
# context.log(self.verbose_log, f"Recognized concept '{concept_key}'", self.name)
|
||||
# return concept_key, None, use_concept
|
||||
#
|
||||
# index = next_index + 2
|
||||
# try:
|
||||
# next_index = to_resolve.index("__", index)
|
||||
# if next_index == index:
|
||||
# context.log(self.verbose_log, f"Error: no id between '__'.", self.name)
|
||||
# return None
|
||||
#
|
||||
# concept_id = to_resolve[index: next_index]
|
||||
# except ValueError:
|
||||
# context.log(self.verbose_log, f"Recognized concept '{concept_key}'.", self.name)
|
||||
# return concept_key, None, use_concept
|
||||
#
|
||||
# context.log(self.verbose_log, f"Recognized concept '{concept_key}' (id='{concept_id}').", self.name)
|
||||
# return concept_key, concept_id, use_concept
|
||||
|
||||
@staticmethod
|
||||
def expr_to_expression(expr):
|
||||
|
||||
Reference in New Issue
Block a user