NormalVariableExpression(variable=center_variable),
)
-def normalize_comparison_expression(counter, expression):
- stack = []
+def desugar_ternary_comparison(counter, expression):
+ counter, left_prestatements, left_expression = normalize_expression(counter, expression.left.left)
+ counter, middle_prestatements, middle_expression = normalize_expression(counter, expression.left.right)
+
+ left_variable = '${}'.format(counter)
+ counter += 1
+ middle_variable = '${}'.format(counter)
+ counter += 1
+
+ juncture_prestatements = (
+ NormalVariableInitializationStatement(
+ variable=left_variable,
+ expression=left_expression,
+ ),
+ NormalVariableInitializationStatement(
+ variable=middle_variable,
+ expression=middle_expression,
+ )
+ )
+
+ counter, boolean_expression_prestatements, boolean_expression = normalize_boolean_expression(
+ counter,
+ parsing.FurInfixExpression(
+ order='and_level',
+ operator='and',
+ left=parsing.FurInfixExpression(
+ order='comparison_level',
+ operator=expression.left.operator,
+ left=NormalVariableExpression(variable=left_variable),
+ right=NormalVariableExpression(variable=middle_variable),
+ ),
+ right=parsing.FurInfixExpression(
+ order='comparison_level',
+ operator=expression.operator,
+ left=NormalVariableExpression(variable=middle_variable),
+ right=expression.right,
+ ),
+ )
+ )
+
+ return (
+ counter,
+ left_prestatements + middle_prestatements + juncture_prestatements + boolean_expression_prestatements,
+ boolean_expression,
+ )
- while isinstance(expression.left, parsing.FurInfixExpression) and expression.order == 'comparison_level':
- stack.append((expression.operator, expression.order, expression.right))
- expression = expression.left
+def normalize_comparison_expression(counter, expression):
+ if isinstance(expression.left, parsing.FurInfixExpression) and expression.order == 'comparison_level':
+ return desugar_ternary_comparison(counter, expression)
counter, left_prestatements, left_expression = normalize_expression(counter, expression.left)
counter, right_prestatements, right_expression = normalize_expression(counter, expression.right)
),
)
- 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):
def transform_comparison_level_expression(accumulators, expression):
accumulators.operator_set.add(FUR_INFIX_OPERATOR_TO_C_INFIX_OPERATOR[expression.operator])
- # Transform expressions like 1 < 2 < 3 into expressions like 1 < 2 && 2 < 3
- if isinstance(expression.left, parsing.FurInfixExpression) and expression.left.order == 'comparison_level':
- left = transform_comparison_level_expression(
- accumulators,
- expression.left
- )
-
- middle = left.right
-
- right = transform_expression(
- accumulators,
- expression.right,
- )
-
- # TODO Don't evaluate the middle expression twice
- return CFunctionCallForFurInfixOperator(
- name='and',
- left=left,
- right=CFunctionCallForFurInfixOperator(
- name=FUR_INFIX_OPERATOR_TO_C_INFIX_OPERATOR[expression.operator].name,
- left=middle,
- right=right,
- ),
- )
-
return CFunctionCallForFurInfixOperator(
name=FUR_INFIX_OPERATOR_TO_C_INFIX_OPERATOR[expression.operator].name,
left=transform_expression(accumulators, expression.left),