Prevent assignment to a builtin variable
[fur] / transformation.py
index 32b253e..83fc36b 100644 (file)
@@ -125,6 +125,7 @@ CFunctionDefinition = collections.namedtuple(
     'CFunctionDefinition',
     [
         'name',
+        'argument_name_list',
         'statement_list',
     ],
 )
@@ -142,47 +143,6 @@ CProgram = collections.namedtuple(
     ],
 )
 
-EQUALITY_LEVEL_OPERATOR_TO_FUNCTION_NAME_MAPPING = {
-    '==':   'equals',
-    '!=':   'notEquals',
-    '<=':   'lessThanOrEqual',
-    '>=':   'greaterThanOrEqual',
-    '<':    'lessThan',
-    '>':    'greaterThan',
-}
-
-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 == '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=EQUALITY_LEVEL_OPERATOR_TO_FUNCTION_NAME_MAPPING[expression.operator],
-                left=middle,
-                right=right,
-            ),
-        )
-
-    return CFunctionCallForFurInfixOperator(
-        name=EQUALITY_LEVEL_OPERATOR_TO_FUNCTION_NAME_MAPPING[expression.operator],
-        left=transform_expression(accumulators, expression.left),
-        right=transform_expression(accumulators, expression.right),
-    )
-
 BUILTINS = {
     'false':    [],
     'pow':      ['math.h'],
@@ -208,34 +168,77 @@ def transform_symbol_expression(accumulators, expression):
     if expression.value in ['true', 'false']:
         return CConstantExpression(value=expression.value)
 
-    if expression.value not in accumulators.symbol_list:
-        symbol_list.append(expression.value)
+    try:
+        symbol_list_index = accumulators.symbol_list.index(expression.value)
+    except ValueError:
+        symbol_list_index = len(accumulators.symbol_list)
+        accumulators.symbol_list.append(expression.value)
 
     return CSymbolExpression(
         symbol=expression.value,
-        symbol_list_index=accumulators.symbol_list.index(expression.value),
+        symbol_list_index=symbol_list_index,
     )
 
-CInfixOperatorDeclaration = collections.namedtuple(
-    'CInfixOperatorDeclaration',
+CInfixDeclaration = collections.namedtuple(
+    'CInfixDeclaration',
     [
         'name',
-        'input_type',
-        'result_type',
-        'c_operator',
+        'in_type',
+        'out_type',
+        'operator',
     ],
 )
 
 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='||'),
+    '+':    CInfixDeclaration(name='add', in_type='integer', out_type='integer', operator='+'),
+    '-':    CInfixDeclaration(name='subtract', in_type='integer', out_type='integer', operator='-'),
+    '*':    CInfixDeclaration(name='multiply', in_type='integer', out_type='integer', operator='*'),
+    '//':   CInfixDeclaration(name='integerDivide', in_type='integer', out_type='integer', operator='/'),
+    '%':    CInfixDeclaration(name='modularDivide', in_type='integer', out_type='integer', operator='%'),
+    'and':  CInfixDeclaration(name='and', in_type='boolean', out_type='boolean', operator='&&'),
+    'or':   CInfixDeclaration(name='or', in_type='boolean', out_type='boolean', operator='||'),
+    '==':   CInfixDeclaration(name='equals', in_type='integer', out_type='boolean', operator='=='),
+    '!=':   CInfixDeclaration(name='notEquals', in_type='integer', out_type='boolean', operator='!='),
+    '<=':   CInfixDeclaration(name='lessThanOrEqual', in_type='integer', out_type='boolean', operator='<='),
+    '>=':   CInfixDeclaration(name='greaterThanOrEqual', in_type='integer', out_type='boolean', operator='>='),
+    '<':    CInfixDeclaration(name='lessThan', in_type='integer', out_type='boolean', operator='<'),
+    '>':    CInfixDeclaration(name='greaterThan', in_type='integer', out_type='boolean', operator='>'),
 }
 
+def transform_comparison_level_expression(accumulators, expression):
+    accumulators.operator_set.add(INFIX_OPERATOR_TO_DECLARATION[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=INFIX_OPERATOR_TO_DECLARATION[expression.operator].name,
+                left=middle,
+                right=right,
+            ),
+        )
+
+    return CFunctionCallForFurInfixOperator(
+        name=INFIX_OPERATOR_TO_DECLARATION[expression.operator].name,
+        left=transform_expression(accumulators, expression.left),
+        right=transform_expression(accumulators, expression.right),
+    )
+
 def transform_infix_expression(accumulators, expression):
     if expression.order == 'comparison_level':
         return transform_comparison_level_expression(accumulators, expression)
@@ -251,10 +254,6 @@ def transform_infix_expression(accumulators, expression):
 def transform_integer_literal_expression(accumulators, expression):
     return CIntegerLiteral(value=expression.value)
 
-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)
-
 def transform_negation_expression(accumulators, expression):
     return CNegationExpression(
         value=transform_expression(accumulators, expression.internal_expression),
@@ -267,7 +266,6 @@ def transform_expression(accumulators, 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,
@@ -278,12 +276,15 @@ def transform_expression(accumulators, expression):
 
 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:
+    try:
+        symbol_list_index = accumulators.symbol_list.index(assignment_statement.target)
+    except ValueError:
+        symbol_list_index = len(accumulators.symbol_list)
         accumulators.symbol_list.append(assignment_statement.target)
 
     return CSymbolAssignmentStatement(
         target=assignment_statement.target,
-        target_symbol_list_index=accumulators.symbol_list.index(assignment_statement.target),
+        target_symbol_list_index=symbol_list_index,
         expression=transform_expression(
             accumulators,
             assignment_statement.expression,
@@ -291,30 +292,22 @@ def transform_symbol_assignment_statement(accumulators, assignment_statement):
     )
 
 def transform_function_call_expression(accumulators, function_call):
-    if function_call.function.value in BUILTINS.keys():
-        # TODO Check that the builtin is actually callable
-        accumulators.builtin_set.add(function_call.function.value)
+    if isinstance(function_call.function, parsing.FurSymbolExpression):
+        # TODO Move this check to transformation of symbol expressions so we can have builtins that aren't functions
+        if function_call.function.value in BUILTINS.keys():
+            # TODO Check that the builtin is actually callable
+            accumulators.builtin_set.add(function_call.function.value)
 
     # TODO Use the symbol from SYMBOL LIST
     return CFunctionCallExpression(
-        name=function_call.function.value,
+        name=transform_expression(accumulators, function_call.function),
         argument_count=function_call.argument_count,
         argument_items=transform_expression(accumulators, function_call.argument_items),
     )
 
 def transform_expression_statement(accumulators, statement):
-    # TODO At some point we can verify that all expression types are supported and just call transform_expression
-    expression = {
-        parsing.FurFunctionCallExpression: transform_function_call_expression,
-        parsing.FurInfixExpression: transform_expression,
-        parsing.FurIntegerLiteralExpression: transform_expression,
-        parsing.FurSymbolExpression: transform_expression,
-        normalization.NormalFunctionCallExpression: transform_function_call_expression,
-        normalization.NormalVariableExpression: transform_expression,
-    }[type(statement.expression)](accumulators, statement.expression)
-
     return CExpressionStatement(
-        expression=expression,
+        expression=transform_expression(accumulators, statement.expression),
     )
 
 def transform_if_else_statement(accumulators, statement):
@@ -347,8 +340,10 @@ def transform_function_definition_statement(accumulators, statement):
     if any(fd.name == statement.name for fd in accumulators.function_definition_list):
         raise Exception('A function with name "{}" already exists'.format(statement.name))
 
+    # TODO Add argument names to the symbol table
     accumulators.function_definition_list.append(CFunctionDefinition(
         name=statement.name,
+        argument_name_list=statement.argument_name_list,
         statement_list=tuple(transform_statement(accumulators, s) for s in statement.statement_list)
     ))
 
@@ -356,9 +351,9 @@ def transform_function_definition_statement(accumulators, statement):
 
 def transform_statement(accumulators, statement):
     return {
-        parsing.FurAssignmentStatement: transform_symbol_assignment_statement,
         parsing.FurExpressionStatement: transform_expression_statement,
         normalization.NormalArrayVariableInitializationStatement: transform_array_variable_initialization_statement,
+        normalization.NormalAssignmentStatement: transform_symbol_assignment_statement,
         normalization.NormalExpressionStatement: transform_expression_statement,
         normalization.NormalFunctionDefinitionStatement: transform_function_definition_statement,
         normalization.NormalIfElseStatement: transform_if_else_statement,