Add a string concatenation operator
[fur] / transformation.py
index cc24304..e06bcf0 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,6 +115,8 @@ 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',
     [
@@ -144,10 +140,11 @@ CProgram = collections.namedtuple(
 )
 
 BUILTINS = {
-    'false':    [],
-    'pow':      ['math.h'],
-    'print':    ['stdio.h'],
-    'true':     [],
+    'concatenate':      [],
+    'false':            [],
+    'pow':              ['math.h'],
+    'print':            ['stdio.h'],
+    'true':             [],
 }
 
 def transform_variable_expression(accumulators, expression):
@@ -165,17 +162,17 @@ def transform_string_literal_expression(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)
 
     try:
-        symbol_list_index = accumulators.symbol_list.index(expression.value)
+        symbol_list_index = accumulators.symbol_list.index(expression.symbol)
     except ValueError:
         symbol_list_index = len(accumulators.symbol_list)
-        accumulators.symbol_list.append(expression.value)
+        accumulators.symbol_list.append(expression.symbol)
 
     return CSymbolExpression(
-        symbol=expression.value,
+        symbol=expression.symbol,
         symbol_list_index=symbol_list_index,
     )
 
@@ -189,7 +186,11 @@ CInfixDeclaration = collections.namedtuple(
     ],
 )
 
-INFIX_OPERATOR_TO_DECLARATION = {
+FUR_INFIX_OPERATOR_TO_C_FUNCTION = {
+    '++':   'concatenate',
+}
+
+FUR_INFIX_OPERATOR_TO_C_INFIX_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='*'),
@@ -206,7 +207,7 @@ INFIX_OPERATOR_TO_DECLARATION = {
 }
 
 def transform_comparison_level_expression(accumulators, expression):
-    accumulators.operator_set.add(INFIX_OPERATOR_TO_DECLARATION[expression.operator])
+    accumulators.operator_set.add(FUR_INFIX_OPERATOR_TO_C_INFIX_OPERATOR[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':
@@ -227,26 +228,35 @@ def transform_comparison_level_expression(accumulators, expression):
             name='and',
             left=left,
             right=CFunctionCallForFurInfixOperator(
-                name=INFIX_OPERATOR_TO_DECLARATION[expression.operator].name,
+                name=FUR_INFIX_OPERATOR_TO_C_INFIX_OPERATOR[expression.operator].name,
                 left=middle,
                 right=right,
             ),
         )
 
     return CFunctionCallForFurInfixOperator(
-        name=INFIX_OPERATOR_TO_DECLARATION[expression.operator].name,
+        name=FUR_INFIX_OPERATOR_TO_C_INFIX_OPERATOR[expression.operator].name,
         left=transform_expression(accumulators, expression.left),
         right=transform_expression(accumulators, expression.right),
     )
 
+def transform_infix_operator_without_c_equivalent(accumulators, expression):
+    return CFunctionCallForFurInfixOperator(
+        name='concatenate',
+        left=transform_expression(accumulators, expression.left),
+        right=transform_expression(accumulators, expression.right),
+    )
 def transform_infix_expression(accumulators, expression):
+    if expression.operator in FUR_INFIX_OPERATOR_TO_C_FUNCTION:
+        return transform_infix_operator_without_c_equivalent(accumulators, expression)
+
     if expression.order == 'comparison_level':
         return transform_comparison_level_expression(accumulators, expression)
 
-    accumulators.operator_set.add(INFIX_OPERATOR_TO_DECLARATION[expression.operator])
+    accumulators.operator_set.add(FUR_INFIX_OPERATOR_TO_C_INFIX_OPERATOR[expression.operator])
 
     return CFunctionCallForFurInfixOperator(
-        name=INFIX_OPERATOR_TO_DECLARATION[expression.operator].name,
+        name=FUR_INFIX_OPERATOR_TO_C_INFIX_OPERATOR[expression.operator].name,
         left=transform_expression(accumulators, expression.left),
         right=transform_expression(accumulators, expression.right),
     )
@@ -259,20 +269,59 @@ def transform_negation_expression(accumulators, expression):
         value=transform_expression(accumulators, expression.internal_expression),
     )
 
+CListConstructExpression = collections.namedtuple(
+    'CListConstructExpression',
+    [
+        'allocate',
+    ],
+)
+
+CListAppendStatement = collections.namedtuple(
+    'CListAppendStatement',
+    [
+        'list_expression',
+        'item_expression',
+    ],
+)
+
+CListGetExpression = collections.namedtuple(
+    'CListGetExpression',
+    [
+        'list_expression',
+        'index_expression',
+    ],
+)
+
+def transform_list_construct_expression(accumulators, expression):
+    return CListConstructExpression(allocate=expression.allocate)
+
+def transform_list_get_expression(accumulators, expression):
+    return CListGetExpression(
+        list_expression=transform_expression(accumulators, expression.list_expression),
+        index_expression=transform_expression(accumulators, expression.index_expression),
+    )
+
+def transform_list_append_statement(accumulators, expression):
+    return CListAppendStatement(
+        list_expression=transform_expression(accumulators, expression.list_expression),
+        item_expression=transform_expression(accumulators, expression.item_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.FurStringLiteralExpression: transform_string_literal_expression,
-        parsing.FurSymbolExpression: transform_symbol_expression,
         normalization.NormalFunctionCallExpression: transform_function_call_expression,
         normalization.NormalInfixExpression: transform_infix_expression,
         normalization.NormalIntegerLiteralExpression: transform_integer_literal_expression,
+        normalization.NormalListConstructExpression: transform_list_construct_expression,
+        normalization.NormalListGetExpression: transform_list_get_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)
 
@@ -294,15 +343,9 @@ def transform_symbol_assignment_statement(accumulators, assignment_statement):
     )
 
 def transform_function_call_expression(accumulators, function_call):
-    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=transform_expression(accumulators, function_call.function),
+        function_expression=transform_expression(accumulators, function_call.function_expression),
         argument_count=function_call.argument_count,
         argument_items=transform_expression(accumulators, function_call.argument_items),
     )
@@ -315,8 +358,8 @@ def transform_expression_statement(accumulators, statement):
 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):
@@ -359,6 +402,7 @@ def transform_statement(accumulators, statement):
         normalization.NormalExpressionStatement: transform_expression_statement,
         normalization.NormalFunctionDefinitionStatement: transform_function_definition_statement,
         normalization.NormalIfElseStatement: transform_if_else_statement,
+        normalization.NormalListAppendStatement: transform_list_append_statement,
         normalization.NormalVariableInitializationStatement: transform_variable_initialization_statement,
         normalization.NormalVariableReassignmentStatement: transform_variable_reassignment_statement,
     }[type(statement)](accumulators, statement)
@@ -388,6 +432,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]: