Working on #51 : Working
This commit is contained in:
@@ -18,7 +18,8 @@ from core.tokenizer import Keywords, TokenKind, Token, IterParser
|
||||
from core.utils import index_tokens, COLORS, get_text_from_tokens, merge_dictionaries, merge_sets
|
||||
from evaluators.ConceptEvaluator import ConceptEvaluator
|
||||
from evaluators.PythonEvaluator import PythonEvaluator, Expando
|
||||
from parsers.BaseExpressionParser import AndNode, ExpressionVisitor, VariableNode, ComparisonNode, FunctionNode
|
||||
from parsers.BaseExpressionParser import AndNode, ExpressionVisitor, VariableNode, ComparisonNode, FunctionNode, \
|
||||
ComparisonType
|
||||
from parsers.BaseNodeParser import SourceCodeWithConceptNode, ConceptNode, SourceCodeNode
|
||||
from parsers.LogicalOperatorParser import LogicalOperatorParser
|
||||
from parsers.PythonParser import PythonNode
|
||||
@@ -1377,8 +1378,7 @@ class PythonConditionExprVisitor(ExpressionVisitor):
|
||||
self.variables[target] = var_name
|
||||
return var_name
|
||||
|
||||
def init_or_get_variable_from_name(self, variable_path: List[str], obj_variables):
|
||||
|
||||
def get_variable_from_name(self, variable_path: List[str]):
|
||||
if len(variable_path) > 1:
|
||||
left = variable_path[:-1]
|
||||
right = [variable_path[-1]]
|
||||
@@ -1389,6 +1389,15 @@ class PythonConditionExprVisitor(ExpressionVisitor):
|
||||
return self.variables[var_name], ".".join(right)
|
||||
|
||||
right.insert(0, left.pop())
|
||||
else:
|
||||
return variable_path[0], ".".join(variable_path[1:])
|
||||
|
||||
return variable_path
|
||||
|
||||
def init_or_get_variable_from_name(self, variable_path: List[str], obj_variables):
|
||||
var_root, var_attr = self.get_variable_from_name(variable_path)
|
||||
if var_root != variable_path[0]:
|
||||
return var_root, var_attr
|
||||
|
||||
if variable_path[0] not in self.variables:
|
||||
self.add_variable(variable_path[0])
|
||||
@@ -1413,12 +1422,24 @@ class PythonConditionExprVisitor(ExpressionVisitor):
|
||||
return PythonConditionExprVisitorObj(source, source, {}, {expr_node.name})
|
||||
|
||||
def visit_ComparisonNode(self, expr_node: ComparisonNode):
|
||||
if isinstance(expr_node.left, VariableNode):
|
||||
source = expr_node.get_source()
|
||||
return PythonConditionExprVisitorObj(source, source, {}, {expr_node.left.name})
|
||||
else:
|
||||
if not isinstance(expr_node.left, VariableNode):
|
||||
raise FailedToCompileError([expr_node])
|
||||
|
||||
res = evaluate(self.context,
|
||||
expr_node.right.get_source(),
|
||||
evaluators=CONDITIONS_VISITOR_EVALUATORS,
|
||||
desc=None,
|
||||
eval_body=False,
|
||||
eval_where=False,
|
||||
is_question=False,
|
||||
expect_success=False,
|
||||
stm=None)
|
||||
res = expect_one(self.context, res)
|
||||
if not res.status:
|
||||
return FailedToCompileError([f"Cannot recognize '{expr_node.right.get_source()}'"])
|
||||
|
||||
return self.create_comparison_condition(expr_node.left.unpack(), expr_node.comp, res.value)
|
||||
|
||||
def visit_AndNode(self, expr_node: AndNode):
|
||||
current_visitor_obj = self.visit(expr_node.parts[0])
|
||||
for node in expr_node.parts[1:]:
|
||||
@@ -1473,3 +1494,16 @@ class PythonConditionExprVisitor(ExpressionVisitor):
|
||||
concept_variables.update({k: v for k, v in concept.variables().items() if v is not NotInit})
|
||||
|
||||
return PythonConditionExprVisitorObj(source, source, {}, obj_variables)
|
||||
|
||||
def create_comparison_condition(self, var_path, op, value):
|
||||
var_root, var_attr = self.get_variable_from_name(var_path)
|
||||
left = var_root + "." + var_attr
|
||||
if op == ComparisonType.EQUALS:
|
||||
if isinstance(value, Expando):
|
||||
source = f"isinstance({left}, Expando) and {left} == {value.get_name()}"
|
||||
return PythonConditionExprVisitorObj(source, source, {}, set())
|
||||
if isinstance(value, Concept):
|
||||
return self.recognize_concept(var_path, value, {})
|
||||
else:
|
||||
source = ComparisonNode.rebuild_source(left, op, value)
|
||||
return PythonConditionExprVisitorObj(source, source, {}, {var_path[0]})
|
||||
|
||||
Reference in New Issue
Block a user