From: David Kerkeslager Date: Tue, 8 Aug 2017 15:56:07 +0000 (-0400) Subject: Normalize comparison expressions, even ternary comparison expressions X-Git-Url: https://code.kerkeslager.com/?a=commitdiff_plain;h=8be4bc7974ca10fb7fe247748c95c5c240bf11f4;p=fur Normalize comparison expressions, even ternary comparison expressions --- diff --git a/normalization.py b/normalization.py index 8968aaf..0d967d7 100644 --- a/normalization.py +++ b/normalization.py @@ -115,8 +115,67 @@ def normalize_basic_infix_operation(counter, expression): ) def normalize_comparison_expression(counter, expression): - # TODO Unfake this - return fake_normalization(counter, expression) + stack = [] + + while isinstance(expression.left, parsing.FurInfixExpression) and expression.order == 'comparison_level': + stack.append((expression.operator, expression.order, expression.right)) + expression = expression.left + + counter, left_prestatements, left_expression = normalize_expression(counter, expression.left) + counter, right_prestatements, right_expression = normalize_expression(counter, expression.right) + + left_variable = '${}'.format(counter) + counter += 1 + right_variable = '${}'.format(counter) + counter += 1 + + root_prestatements = ( + NormalVariableAssignmentStatement( + variable=left_variable, + expression=left_expression, + ), + NormalVariableAssignmentStatement( + variable=right_variable, + expression=right_expression, + ), + ) + + counter, result_prestatements, result_expression = ( + counter, + left_prestatements + right_prestatements + root_prestatements, + # TODO Implement short-circuiting + NormalInfixExpression( + order=expression.order, # TODO Do we need this? + operator=expression.operator, + left=NormalVariableExpression(variable=left_variable), + right=NormalVariableExpression(variable=right_variable), + ), + ) + + while len(stack) > 0: + right_operator, right_order, right_expression = stack.pop() + and_right_expression = parsing.FurInfixExpression( + operator=right_operator, + order=right_order, + left=NormalVariableExpression(variable=right_variable), + right=right_expression, + ) + + and_expression = parsing.FurInfixExpression( + operator='and', + order='and_level', + left=result_expression, + right=and_right_expression, + ) + + counter, and_prestatements, result_expression = normalize_boolean_expression( + counter, + and_expression, + ) + + result_prestatements = result_prestatements + and_prestatements + + return (counter, result_prestatements, result_expression) def normalize_boolean_expression(counter, expression): # TODO Unfake this