Added if expression statements
[fur] / transformation.py
index 81c1e5d..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',
     ],
 )
@@ -134,6 +131,7 @@ CProgram = collections.namedtuple(
     [
         'builtin_set',
         'function_definition_list',
+        'operator_declarations',
         'statements',
         'standard_libraries',
         'string_literal_list',
@@ -141,16 +139,71 @@ CProgram = collections.namedtuple(
     ],
 )
 
-EQUALITY_LEVEL_OPERATOR_TO_FUNCTION_NAME_MAPPING = {
-    '==':   'equals',
-    '!=':   'notEquals',
-    '<=':   'lessThanOrEqual',
-    '>=':   'greaterThanOrEqual',
-    '<':    'lessThan',
-    '>':    'greaterThan',
+BUILTINS = {
+    'false':    [],
+    'pow':      ['math.h'],
+    'print':    ['stdio.h'],
+    'true':     [],
+}
+
+def transform_variable_expression(accumulators, expression):
+    return CVariableExpression(variable=expression.variable)
+
+def transform_string_literal_expression(accumulators, expression):
+    value = expression.string
+
+    try:
+        index = accumulators.string_literal_list.index(value)
+    except ValueError:
+        index = len(accumulators.string_literal_list)
+        accumulators.string_literal_list.append(value)
+
+    return CStringLiteral(index=index, value=value)
+
+def transform_symbol_expression(accumulators, expression):
+    if expression.symbol in BUILTINS:
+        accumulators.builtin_set.add(expression.symbol)
+
+    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.symbol,
+        symbol_list_index=symbol_list_index,
+    )
+
+CInfixDeclaration = collections.namedtuple(
+    'CInfixDeclaration',
+    [
+        'name',
+        'in_type',
+        'out_type',
+        'operator',
+    ],
+)
+
+INFIX_OPERATOR_TO_DECLARATION = {
+    '+':    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(
@@ -170,145 +223,89 @@ def transform_comparison_level_expression(accumulators, expression):
             name='and',
             left=left,
             right=CFunctionCallForFurInfixOperator(
-                name=EQUALITY_LEVEL_OPERATOR_TO_FUNCTION_NAME_MAPPING[expression.operator],
+                name=INFIX_OPERATOR_TO_DECLARATION[expression.operator].name,
                 left=middle,
                 right=right,
             ),
         )
 
     return CFunctionCallForFurInfixOperator(
-        name=EQUALITY_LEVEL_OPERATOR_TO_FUNCTION_NAME_MAPPING[expression.operator],
+        name=INFIX_OPERATOR_TO_DECLARATION[expression.operator].name,
         left=transform_expression(accumulators, expression.left),
         right=transform_expression(accumulators, expression.right),
     )
 
-BUILTINS = {
-    'false':    [],
-    'pow':      ['math.h'],
-    'print':    ['stdio.h'],
-    'true':     [],
-}
-
-def transform_variable_expression(accumulators, expression):
-    return CVariableExpression(variable=expression.variable)
-
-def transform_string_literal(accumulators, expression):
-    value = expression.value
-
-    try:
-        index = accumulators.string_literal_list.index(value)
-    except ValueError:
-        index = len(accumulators.string_literal_list)
-        accumulators.string_literal_list.append(value)
-
-    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.value not in accumulators.symbol_list:
-        symbol_list.append(expression.value)
-
-    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)
 
-    INFIX_OPERATOR_TO_FUNCTION_NAME = {
-        '+':    'add',
-        '-':    'subtract',
-        '*':    'multiply',
-        '//':   'integerDivide',
-        '%':    'modularDivide',
-        'and':  'and',
-        'or':   'or',
-    }
+    accumulators.operator_set.add(INFIX_OPERATOR_TO_DECLARATION[expression.operator])
 
     return CFunctionCallForFurInfixOperator(
-        name=INFIX_OPERATOR_TO_FUNCTION_NAME[expression.operator],
+        name=INFIX_OPERATOR_TO_DECLARATION[expression.operator].name,
         left=transform_expression(accumulators, expression.left),
         right=transform_expression(accumulators, expression.right),
     )
 
 def transform_integer_literal_expression(accumulators, expression):
-    return CIntegerLiteral(value=expression.value)
+    return CIntegerLiteral(value=expression.integer)
 
-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),
+    )
 
 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,
         ),
     )
 
-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
-        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):
@@ -334,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)
     ))
 
@@ -343,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,
@@ -378,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]:
@@ -386,6 +390,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,