Use order instead of operator to normalize infix expressions
[fur] / transformation.py
index 98398f8..b6d21c8 100644 (file)
@@ -1,5 +1,6 @@
 import collections
 
+import normalization
 import parsing
 
 CIntegerLiteral = collections.namedtuple(
@@ -24,6 +25,13 @@ CConstantExpression = collections.namedtuple(
     ],
 )
 
+CVariableExpression = collections.namedtuple(
+    'CVariableExpression',
+    [
+        'variable',
+    ],
+)
+
 CSymbolExpression = collections.namedtuple(
     'CSymbolExpression',
     [
@@ -56,8 +64,8 @@ CFunctionCallExpression = collections.namedtuple(
     ],
 )
 
-CAssignmentStatement = collections.namedtuple(
-    'CAssignmentStatement',
+CSymbolAssignmentStatement = collections.namedtuple(
+    'CSymbolAssignmentStatement',
     [
         'target',
         'target_symbol_list_index',
@@ -65,6 +73,21 @@ CAssignmentStatement = collections.namedtuple(
     ],
 )
 
+CVariableAssignmentStatement = collections.namedtuple(
+    'CVariableAssignmentStatement',
+    [
+        'variable',
+        'expression',
+    ],
+)
+
+CExpressionStatement = collections.namedtuple(
+    'CExpressionStatement',
+    [
+        'expression',
+    ],
+)
+
 CProgram = collections.namedtuple(
     'CProgram',
     [
@@ -85,10 +108,10 @@ EQUALITY_LEVEL_OPERATOR_TO_FUNCTION_NAME_MAPPING = {
     '>':    'greaterThan',
 }
 
-def transform_equality_level_expression(accumulators, expression):
+def transform_comparison_level_expression(accumulators, expression):
     # Transform expressions like 1 < 2 < 3 into expressions like 1 < 2 && 2 < 3
-    if isinstance(expression.left, parsing.FurInfixExpression) and expression.left.order == 'equality_level':
-        left = transform_equality_level_expression(
+    if isinstance(expression.left, parsing.FurInfixExpression) and expression.left.order == 'comparison_level':
+        left = transform_comparison_level_expression(
             accumulators,
             expression.left
         )
@@ -124,6 +147,29 @@ BUILTINS = {
     'true':     [],
 }
 
+def transform_variable_expression(accumulators, expression):
+    return CVariableExpression(variable=expression.variable)
+
+def transform_infix_expression(accumulators, expression):
+    if expression.order == 'comparison_level':
+        return transform_comparison_level_expression(accumulators, expression)
+
+    INFIX_OPERATOR_TO_FUNCTION_NAME = {
+        '+':    'add',
+        '-':    'subtract',
+        '*':    'multiply',
+        '//':   'integerDivide',
+        '%':    'modularDivide',
+        'and':  'and',
+        'or':   'or',
+    }
+
+    return CFunctionCallForFurInfixOperator(
+        name=INFIX_OPERATOR_TO_FUNCTION_NAME[expression.operator],
+        left=transform_expression(accumulators, expression.left),
+        right=transform_expression(accumulators, expression.right),
+    )
+
 def transform_expression(accumulators, expression):
     if isinstance(expression, parsing.FurParenthesizedExpression):
         # Parentheses can be removed because everything in the C output is explicitly parenthesized
@@ -165,34 +211,20 @@ def transform_expression(accumulators, expression):
     if type(expression) in LITERAL_TYPE_MAPPING:
         return LITERAL_TYPE_MAPPING[type(expression)](value=expression.value)
 
-    if isinstance(expression, parsing.FurInfixExpression):
-        if expression.order == 'equality_level':
-            return transform_equality_level_expression(accumulators, expression)
-
-        INFIX_OPERATOR_TO_FUNCTION_NAME = {
-            '+':    'add',
-            '-':    'subtract',
-            '*':    'multiply',
-            '//':   'integerDivide',
-            '%':    'modularDivide',
-            'and':  'and',
-            'or':   'or',
-        }
-
-        return CFunctionCallForFurInfixOperator(
-            name=INFIX_OPERATOR_TO_FUNCTION_NAME[expression.operator],
-            left=transform_expression(accumulators, expression.left),
-            right=transform_expression(accumulators, expression.right),
-        )
-
-    raise Exception('Could not transform expression "{}"'.format(expression))
+    # TODO Handle all possible types in this form
+    return {
+        parsing.FurInfixExpression: transform_infix_expression, # TODO Shouldn't need this
+        normalization.NormalFunctionCallExpression: transform_function_call_expression,
+        normalization.NormalInfixExpression: transform_infix_expression,
+        normalization.NormalVariableExpression: transform_variable_expression,
+    }[type(expression)](accumulators, expression)
 
-def transform_assignment_statement(accumulators, assignment_statement):
+def transform_symbol_assignment_statement(accumulators, assignment_statement):
     # TODO Check that target is not a builtin
     if assignment_statement.target not in accumulators.symbol_list:
         accumulators.symbol_list.append(assignment_statement.target)
 
-    return CAssignmentStatement(
+    return CSymbolAssignmentStatement(
         target=assignment_statement.target,
         target_symbol_list_index=accumulators.symbol_list.index(assignment_statement.target),
         expression=transform_expression(
@@ -207,6 +239,7 @@ def transform_negation_expression(accumulators, negation_expression):
     )
 
 def transform_function_call_expression(accumulators, function_call):
+    # TODO Function should be a full expression
     if function_call.function.value in BUILTINS.keys():
         # TODO Check that the builtin is actually callable
         accumulators.builtin_set.add(function_call.function.value)
@@ -222,14 +255,27 @@ def transform_function_call_expression(accumulators, function_call):
     raise Exception()
 
 def transform_expression_statement(accumulators, statement):
-    return {
+    expression = {
         parsing.FurFunctionCallExpression: transform_function_call_expression,
+        normalization.NormalFunctionCallExpression: transform_function_call_expression,
     }[type(statement.expression)](accumulators, statement.expression)
 
+    return CExpressionStatement(
+        expression=expression,
+    )
+
+def transform_variable_assignment_statement(accumulators, statement):
+    return CVariableAssignmentStatement(
+        variable=statement.variable,
+        expression=transform_expression(accumulators, statement.expression),
+    )
+
 def transform_statement(accumulators, statement):
     return {
-        parsing.FurAssignmentStatement: transform_assignment_statement,
+        parsing.FurAssignmentStatement: transform_symbol_assignment_statement,
         parsing.FurExpressionStatement: transform_expression_statement,
+        normalization.NormalVariableAssignmentStatement: transform_variable_assignment_statement,
+        normalization.NormalExpressionStatement: transform_expression_statement,
     }[type(statement)](accumulators, statement)