Don't compile in some unused operators
[fur] / transformation.py
index 5fb853c..32b253e 100644 (file)
@@ -134,6 +134,7 @@ CProgram = collections.namedtuple(
     [
         'builtin_set',
         'function_definition_list',
+        'operator_declarations',
         'statements',
         'standard_libraries',
         'string_literal_list',
@@ -192,70 +193,83 @@ BUILTINS = {
 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)
+def transform_string_literal(accumulators, expression):
+    value = expression.value
 
-    INFIX_OPERATOR_TO_FUNCTION_NAME = {
-        '+':    'add',
-        '-':    'subtract',
-        '*':    'multiply',
-        '//':   'integerDivide',
-        '%':    'modularDivide',
-        'and':  'and',
-        'or':   'or',
-    }
+    try:
+        index = accumulators.string_literal_list.index(value)
+    except ValueError:
+        index = len(accumulators.string_literal_list)
+        accumulators.string_literal_list.append(value)
 
-    return CFunctionCallForFurInfixOperator(
-        name=INFIX_OPERATOR_TO_FUNCTION_NAME[expression.operator],
-        left=transform_expression(accumulators, expression.left),
-        right=transform_expression(accumulators, expression.right),
-    )
+    return CStringLiteral(index=index, value=value)
 
-def transform_expression(accumulators, expression):
-    if isinstance(expression, parsing.FurParenthesizedExpression):
-        # Parentheses can be removed because everything in the C output is explicitly parenthesized
-        return transform_expression(accumulators, expression.internal)
+def transform_symbol_expression(accumulators, expression):
+    if expression.value in ['true', 'false']:
+        return CConstantExpression(value=expression.value)
 
-    if isinstance(expression, parsing.FurNegationExpression):
-        return transform_negation_expression(accumulators, expression)
+    if expression.value not in accumulators.symbol_list:
+        symbol_list.append(expression.value)
 
-    if isinstance(expression, parsing.FurFunctionCallExpression):
-        return transform_function_call_expression(accumulators, expression)
+    return CSymbolExpression(
+        symbol=expression.value,
+        symbol_list_index=accumulators.symbol_list.index(expression.value),
+    )
 
-    if isinstance(expression, parsing.FurSymbolExpression):
-        if expression.value in ['true', 'false']:
-            return CConstantExpression(value=expression.value)
+CInfixOperatorDeclaration = collections.namedtuple(
+    'CInfixOperatorDeclaration',
+    [
+        'name',
+        'input_type',
+        'result_type',
+        'c_operator',
+    ],
+)
 
-        if expression.value not in accumulators.symbol_list:
-            symbol_list.append(expression.value)
+INFIX_OPERATOR_TO_DECLARATION = {
+    '+':    CInfixOperatorDeclaration(name='add', input_type='INTEGER', result_type='INTEGER', c_operator='+'),
+    '-':    CInfixOperatorDeclaration(name='subtract', input_type='INTEGER', result_type='INTEGER', c_operator='-'),
+    '*':    CInfixOperatorDeclaration(name='multiply', input_type='INTEGER', result_type='INTEGER', c_operator='*'),
+    '//':   CInfixOperatorDeclaration(name='integerDivide', input_type='INTEGER', result_type='INTEGER', c_operator='/'),
+    '%':    CInfixOperatorDeclaration(name='modularDivide', input_type='INTEGER', result_type='INTEGER', c_operator='%'),
+    'and':  CInfixOperatorDeclaration(name='and', input_type='BOOLEAN', result_type='BOOLEAN', c_operator='&&'),
+    'or':   CInfixOperatorDeclaration(name='or', input_type='BOOLEAN', result_type='BOOLEAN', c_operator='||'),
+}
 
-        return CSymbolExpression(
-            symbol=expression.value,
-            symbol_list_index=accumulators.symbol_list.index(expression.value),
-        )
+def transform_infix_expression(accumulators, expression):
+    if expression.order == 'comparison_level':
+        return transform_comparison_level_expression(accumulators, expression)
 
-    if isinstance(expression, parsing.FurStringLiteralExpression):
-        value = expression.value
+    accumulators.operator_set.add(INFIX_OPERATOR_TO_DECLARATION[expression.operator])
 
-        try:
-            index = accumulators.string_literal_list.index(value)
-        except ValueError:
-            index = len(accumulators.string_literal_list)
-            accumulators.string_literal_list.append(value)
+    return CFunctionCallForFurInfixOperator(
+        name=INFIX_OPERATOR_TO_DECLARATION[expression.operator].name,
+        left=transform_expression(accumulators, expression.left),
+        right=transform_expression(accumulators, expression.right),
+    )
 
-        return CStringLiteral(index=index, value=value)
+def transform_integer_literal_expression(accumulators, expression):
+    return CIntegerLiteral(value=expression.value)
 
-    LITERAL_TYPE_MAPPING = {
-        parsing.FurIntegerLiteralExpression: CIntegerLiteral,
-    }
+def transform_parenthesized_expression(accumulators, expression):
+    # Parentheses can be removed because everything in the C output is explicitly parenthesized
+    return transform_expression(accumulators, expression.internal)
 
-    if type(expression) in LITERAL_TYPE_MAPPING:
-        return LITERAL_TYPE_MAPPING[type(expression)](value=expression.value)
+def transform_negation_expression(accumulators, expression):
+    return CNegationExpression(
+        value=transform_expression(accumulators, expression.internal_expression),
+    )
 
-    # TODO Handle all possible types in this form
+def transform_expression(accumulators, expression):
+    # TODO Clean up handlers for parsing expressions
     return {
-        parsing.FurInfixExpression: transform_infix_expression, # TODO Shouldn't need this
+        parsing.FurFunctionCallExpression: transform_function_call_expression,
+        parsing.FurInfixExpression: transform_infix_expression,
+        parsing.FurIntegerLiteralExpression: transform_integer_literal_expression,
+        parsing.FurNegationExpression: transform_negation_expression,
+        parsing.FurParenthesizedExpression: transform_parenthesized_expression,
+        parsing.FurStringLiteralExpression: transform_string_literal,
+        parsing.FurSymbolExpression: transform_symbol_expression,
         normalization.NormalFunctionCallExpression: transform_function_call_expression,
         normalization.NormalInfixExpression: transform_infix_expression,
         normalization.NormalNegationExpression: transform_negation_expression,
@@ -276,11 +290,6 @@ def transform_symbol_assignment_statement(accumulators, assignment_statement):
         ),
     )
 
-def transform_negation_expression(accumulators, expression):
-    return CNegationExpression(
-        value=transform_expression(accumulators, expression.internal_expression),
-    )
-
 def transform_function_call_expression(accumulators, function_call):
     if function_call.function.value in BUILTINS.keys():
         # TODO Check that the builtin is actually callable
@@ -363,6 +372,7 @@ Accumulators = collections.namedtuple(
     [
         'builtin_set',
         'function_definition_list',
+        'operator_set',
         'symbol_list',
         'string_literal_list',
     ],
@@ -372,6 +382,7 @@ def transform(program):
     accumulators = Accumulators(
         builtin_set=set(),
         function_definition_list=[],
+        operator_set=set(),
         symbol_list=[],
         string_literal_list=[],
     )
@@ -388,6 +399,7 @@ def transform(program):
     return CProgram(
         builtin_set=accumulators.builtin_set,
         function_definition_list=accumulators.function_definition_list,
+        operator_declarations=tuple(sorted(accumulators.operator_set)),
         statements=statement_list,
         standard_libraries=standard_library_set,
         string_literal_list=accumulators.string_literal_list,