Added if expression statements
[fur] / transformation.py
index 32b253e..0b58907 100644 (file)
@@ -1,7 +1,7 @@
 import collections
 
 import normalization
-import parsing
+import parsing # TODO Remove this import, as we should be normalizing everything before it gets here
 
 CIntegerLiteral = collections.namedtuple(
     'CIntegerLiteral',
@@ -18,13 +18,6 @@ CStringLiteral = collections.namedtuple(
     ],
 )
 
-CConstantExpression = collections.namedtuple(
-    'CConstantExpression',
-    [
-        'value'
-    ],
-)
-
 CVariableExpression = collections.namedtuple(
     'CVariableExpression',
     [
@@ -59,12 +52,13 @@ CFunctionCallForFurInfixOperator = collections.namedtuple(
 CFunctionCallExpression = collections.namedtuple(
     'CFunctionCallExpression',
     [
-        'name',
+        'function_expression',
         'argument_count',
         'argument_items',
     ],
 )
 
+# TODO We are currently not changing variables, just preventing them from being accessed.
 CSymbolAssignmentStatement = collections.namedtuple(
     'CSymbolAssignmentStatement',
     [
@@ -109,8 +103,8 @@ CIfElseStatement = collections.namedtuple(
     'CIfElseStatement',
     [
         'condition_expression',
-        'if_statements',
-        'else_statements',
+        'if_statement_list',
+        'else_statement_list',
     ],
 )
 
@@ -121,10 +115,13 @@ CFunctionDeclaration = collections.namedtuple(
     ],
 )
 
+# TODO If a function definition doesn't end with an expression, we have issues currently because we try to return statement.
+# TODO Closures currently wrap entire defining environment, even symbols that are not used, which makes garbage collection ineffective.
 CFunctionDefinition = collections.namedtuple(
     'CFunctionDefinition',
     [
         'name',
+        'argument_name_list',
         'statement_list',
     ],
 )
@@ -142,47 +139,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'],
@@ -193,8 +149,8 @@ BUILTINS = {
 def transform_variable_expression(accumulators, expression):
     return CVariableExpression(variable=expression.variable)
 
-def transform_string_literal(accumulators, expression):
-    value = expression.value
+def transform_string_literal_expression(accumulators, expression):
+    value = expression.string
 
     try:
         index = accumulators.string_literal_list.index(value)
@@ -205,37 +161,80 @@ def transform_string_literal(accumulators, expression):
     return CStringLiteral(index=index, value=value)
 
 def transform_symbol_expression(accumulators, expression):
-    if expression.value in ['true', 'false']:
-        return CConstantExpression(value=expression.value)
+    if expression.symbol in BUILTINS:
+        accumulators.builtin_set.add(expression.symbol)
 
-    if expression.value not in accumulators.symbol_list:
-        symbol_list.append(expression.value)
+    try:
+        symbol_list_index = accumulators.symbol_list.index(expression.symbol)
+    except ValueError:
+        symbol_list_index = len(accumulators.symbol_list)
+        accumulators.symbol_list.append(expression.symbol)
 
     return CSymbolExpression(
-        symbol=expression.value,
-        symbol_list_index=accumulators.symbol_list.index(expression.value),
+        symbol=expression.symbol,
+        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)
@@ -249,11 +248,7 @@ 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)
+    return CIntegerLiteral(value=expression.integer)
 
 def transform_negation_expression(accumulators, expression):
     return CNegationExpression(
@@ -263,27 +258,30 @@ def transform_negation_expression(accumulators, expression):
 def transform_expression(accumulators, expression):
     # TODO Clean up handlers for parsing expressions
     return {
-        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,
+        parsing.FurStringLiteralExpression: transform_string_literal_expression,
         normalization.NormalFunctionCallExpression: transform_function_call_expression,
         normalization.NormalInfixExpression: transform_infix_expression,
+        normalization.NormalIntegerLiteralExpression: transform_integer_literal_expression,
         normalization.NormalNegationExpression: transform_negation_expression,
+        normalization.NormalStringLiteralExpression: transform_string_literal_expression,
+        normalization.NormalSymbolExpression: transform_symbol_expression,
         normalization.NormalVariableExpression: transform_variable_expression,
     }[type(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,37 +289,23 @@ 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)
-
     # TODO Use the symbol from SYMBOL LIST
     return CFunctionCallExpression(
-        name=function_call.function.value,
+        function_expression=transform_expression(accumulators, function_call.function_expression),
         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):
     return CIfElseStatement(
         condition_expression=transform_expression(accumulators, statement.condition_expression),
-        if_statements=tuple(transform_statement(accumulators, s) for s in statement.if_statements),
-        else_statements=tuple(transform_statement(accumulators, s) for s in statement.else_statements),
+        if_statement_list=tuple(transform_statement(accumulators, s) for s in statement.if_statement_list),
+        else_statement_list=tuple(transform_statement(accumulators, s) for s in statement.else_statement_list),
     )
 
 def transform_array_variable_initialization_statement(accumulators, statement):
@@ -347,8 +331,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 +342,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,
@@ -391,6 +377,11 @@ def transform(program):
         transform_statement(accumulators, statement) for statement in program.statement_list
     ]
 
+    # This prevents warnings about normalized variables being entire C statements
+    last_statement = statement_list[-1]
+    if isinstance(last_statement, normalization.NormalExpressionStatement) and isinstance(last_statement.expression, normalization.NormalVariableExpression):
+        del statement_list[-1]
+
     standard_library_set = set()
     for builtin in accumulators.builtin_set:
         for standard_library in BUILTINS[builtin]: