Prevent assignment to a builtin variable
[fur] / normalization.py
index 1da6569..a5863a1 100644 (file)
@@ -1,6 +1,7 @@
 import collections
 
 import parsing
+import util
 
 NormalVariableExpression = collections.namedtuple(
     'NormalVariableExpression',
@@ -30,7 +31,16 @@ NormalFunctionCallExpression = collections.namedtuple(
     'NormalFunctionCallExpression',
     [
         'function',
-        'arguments',
+        'argument_count',
+        'argument_items',
+    ],
+)
+
+NormalArrayVariableInitializationStatement = collections.namedtuple(
+    'NormalArrayVariableInitializationStatement',
+    [
+        'variable',
+        'items',
     ],
 )
 
@@ -57,6 +67,14 @@ NormalExpressionStatement = collections.namedtuple(
     ],
 )
 
+NormalAssignmentStatement = collections.namedtuple(
+    'NormalAssignmentStatement',
+    [
+        'target',
+        'expression',
+    ],
+)
+
 NormalIfElseStatement = collections.namedtuple(
     'NormalIfElseStatement',
     [
@@ -66,6 +84,15 @@ NormalIfElseStatement = collections.namedtuple(
     ],
 )
 
+NormalFunctionDefinitionStatement = collections.namedtuple(
+    'NormalFunctionDefinitionStatement',
+    [
+        'name',
+        'argument_name_list',
+        'statement_list',
+    ],
+)
+
 NormalProgram = collections.namedtuple(
     'NormalProgram',
     [
@@ -73,6 +100,7 @@ NormalProgram = collections.namedtuple(
     ],
 )
 
+# TODO Get rid of this
 def fake_normalization(counter, thing):
     return (counter, (), thing)
 
@@ -98,12 +126,29 @@ def normalize_function_call_expression(counter, expression):
         ))
         counter += 1
 
+    arguments_variable = '${}'.format(counter)
+    counter += 1
+
+    prestatements.append(NormalArrayVariableInitializationStatement(
+        variable=arguments_variable,
+        items=tuple(arguments),
+    ))
+
+    counter, function_prestatements, function_expression = normalize_expression(
+        counter,
+        expression.function,
+    )
+
+    for ps in function_prestatements:
+        prestatements.append(ps)
+
     return (
         counter,
         tuple(prestatements),
         NormalFunctionCallExpression(
-            expression.function, # TODO Normalize the function
-            arguments=tuple(arguments),
+            function=function_expression,
+            argument_count=len(arguments),
+            argument_items=NormalVariableExpression(variable=arguments_variable),
         ),
     )
 
@@ -257,9 +302,6 @@ def normalize_negation_expression(counter, expression):
         NormalNegationExpression(internal_expression=NormalVariableExpression(variable=internal_variable)),
     )
 
-def normalize_parenthesized_expression(counter, expression):
-    return normalize_expression(counter, expression.internal)
-
 def normalize_expression(counter, expression):
     return {
         NormalInfixExpression: fake_normalization,
@@ -268,14 +310,17 @@ def normalize_expression(counter, expression):
         parsing.FurInfixExpression: normalize_infix_expression,
         parsing.FurIntegerLiteralExpression: fake_normalization,
         parsing.FurNegationExpression: normalize_negation_expression,
-        parsing.FurParenthesizedExpression: normalize_parenthesized_expression,
         parsing.FurStringLiteralExpression: fake_normalization,
         parsing.FurSymbolExpression: fake_normalization,
     }[type(expression)](counter, expression)
 
 def normalize_expression_statement(counter, statement):
+    # TODO Verify all expression types are supported and just call normalize_expression
     counter, prestatements, normalized = {
         parsing.FurFunctionCallExpression: normalize_function_call_expression,
+        parsing.FurSymbolExpression: normalize_expression,
+        parsing.FurInfixExpression: normalize_expression,
+        parsing.FurIntegerLiteralExpression: normalize_expression,
     }[type(statement.expression)](counter, statement.expression)
 
     return (
@@ -284,22 +329,47 @@ def normalize_expression_statement(counter, statement):
         NormalExpressionStatement(expression=normalized),
     )
 
+def normalize_function_definition_statement(counter, statement):
+    return (
+        counter,
+        (),
+        NormalFunctionDefinitionStatement(
+            name=statement.name,
+            argument_name_list=statement.argument_name_list,
+            statement_list=normalize_statement_list(statement.statement_list),
+        ),
+    )
+
+def normalize_assignment_statement(counter, statement):
+    counter, prestatements, normalized_expression = normalize_expression(counter, statement.expression)
+    return (
+        counter,
+        prestatements,
+        NormalAssignmentStatement(
+            target=statement.target,
+            expression=normalized_expression,
+        ),
+    )
+
 def normalize_statement(counter, statement):
     return {
+        parsing.FurAssignmentStatement: normalize_assignment_statement,
         parsing.FurExpressionStatement: normalize_expression_statement,
-        parsing.FurAssignmentStatement: fake_normalization,
+        parsing.FurFunctionDefinitionStatement: normalize_function_definition_statement,
     }[type(statement)](counter, statement)
 
-def normalize(program):
+@util.force_generator(tuple)
+def normalize_statement_list(statement_list):
     counter = 0
-    statement_list = []
 
-    for statement in program.statement_list:
+    for statement in statement_list:
         counter, prestatements, normalized = normalize_statement(counter, statement)
         for s in prestatements:
-            statement_list.append(s)
-        statement_list.append(normalized)
+            yield s
+        yield normalized
+
+def normalize(program):
 
     return NormalProgram(
-        statement_list=statement_list,
+        statement_list=normalize_statement_list(program.statement_list),
     )