Added variable-length args to function calls
[fur] / normalization.py
index 0d967d7..a41eb8d 100644 (file)
@@ -9,6 +9,13 @@ NormalVariableExpression = collections.namedtuple(
     ],
 )
 
+NormalNegationExpression = collections.namedtuple(
+    'NormalNegationExpression',
+    [
+        'internal_expression',
+    ],
+)
+
 NormalInfixExpression = collections.namedtuple(
     'NormalInfixExpression',
     [
@@ -23,12 +30,29 @@ NormalFunctionCallExpression = collections.namedtuple(
     'NormalFunctionCallExpression',
     [
         'function',
-        'arguments',
+        'argument_count',
+        'argument_items',
+    ],
+)
+
+NormalArrayVariableInitializationStatement = collections.namedtuple(
+    'NormalArrayVariableInitializationStatement',
+    [
+        'variable',
+        'items',
     ],
 )
 
-NormalVariableAssignmentStatement = collections.namedtuple(
-    'NormalVariableAssignmentStatement',
+NormalVariableInitializationStatement = collections.namedtuple(
+    'NormalVariableInitializationStatement',
+    [
+        'variable',
+        'expression',
+    ],
+)
+
+NormalVariableReassignmentStatement = collections.namedtuple(
+    'NormalVariableReassignmentStatement',
     [
         'variable',
         'expression',
@@ -42,6 +66,15 @@ NormalExpressionStatement = collections.namedtuple(
     ],
 )
 
+NormalIfElseStatement = collections.namedtuple(
+    'NormalIfElseStatement',
+    [
+        'condition_expression',
+        'if_statements',
+        'else_statements',
+    ],
+)
+
 NormalProgram = collections.namedtuple(
     'NormalProgram',
     [
@@ -65,7 +98,7 @@ def normalize_function_call_expression(counter, expression):
             prestatements.append(s)
 
         variable = '${}'.format(counter)
-        prestatements.append(NormalVariableAssignmentStatement(
+        prestatements.append(NormalVariableInitializationStatement(
             variable=variable,
             expression=normalized_argument,
         ))
@@ -74,12 +107,21 @@ def normalize_function_call_expression(counter, expression):
         ))
         counter += 1
 
+    arguments_variable = '${}'.format(counter)
+    counter += 1
+
+    prestatements.append(NormalArrayVariableInitializationStatement(
+        variable=arguments_variable,
+        items=tuple(arguments),
+    ))
+
     return (
         counter,
         tuple(prestatements),
         NormalFunctionCallExpression(
-            expression.function, # TODO Normalize the function
-            arguments=tuple(arguments),
+            function=expression.function, # TODO Normalize the function
+            argument_count=len(arguments),
+            argument_items=NormalVariableExpression(variable=arguments_variable),
         ),
     )
 
@@ -93,11 +135,11 @@ def normalize_basic_infix_operation(counter, expression):
     counter += 1
 
     root_prestatements = (
-        NormalVariableAssignmentStatement(
+        NormalVariableInitializationStatement(
             variable=left_variable,
             expression=left_expression,
         ),
-        NormalVariableAssignmentStatement(
+        NormalVariableInitializationStatement(
             variable=right_variable,
             expression=right_expression,
         ),
@@ -130,11 +172,11 @@ def normalize_comparison_expression(counter, expression):
     counter += 1
 
     root_prestatements = (
-        NormalVariableAssignmentStatement(
+        NormalVariableInitializationStatement(
             variable=left_variable,
             expression=left_expression,
         ),
-        NormalVariableAssignmentStatement(
+        NormalVariableInitializationStatement(
             variable=right_variable,
             expression=right_expression,
         ),
@@ -178,8 +220,39 @@ def normalize_comparison_expression(counter, expression):
     return (counter, result_prestatements, result_expression)
 
 def normalize_boolean_expression(counter, expression):
-    # TODO Unfake this
-    return fake_normalization(counter, expression)
+    counter, left_prestatements, left_expression = normalize_expression(counter, expression.left)
+    counter, right_prestatements, right_expression = normalize_expression(counter, expression.right)
+
+    result_variable = '${}'.format(counter)
+    if_else_prestatment = NormalVariableInitializationStatement(variable=result_variable, expression=left_expression)
+    counter += 1
+
+    condition_expression=NormalVariableExpression(variable=result_variable)
+    short_circuited_statements = right_prestatements + (NormalVariableReassignmentStatement(variable=result_variable, expression=right_expression),)
+
+    if expression.operator == 'and':
+        if_else_statement = NormalIfElseStatement(
+            condition_expression=condition_expression,
+            if_statements=short_circuited_statements,
+            else_statements=(),
+        )
+
+    elif expression.operator == 'or':
+        if_else_statement = NormalIfElseStatement(
+            condition_expression=condition_expression,
+            if_statements=(),
+            else_statements=short_circuited_statements,
+        )
+
+    else:
+        raise Exception('Unable to handle operator "{}"'.format(expression.operator))
+
+    return (
+        counter,
+        left_prestatements + (if_else_prestatment, if_else_statement),
+        NormalVariableExpression(variable=result_variable),
+    )
+
 
 def normalize_infix_expression(counter, expression):
     return {
@@ -190,13 +263,30 @@ def normalize_infix_expression(counter, expression):
         'or_level': normalize_boolean_expression,
     }[expression.order](counter, expression)
 
+def normalize_negation_expression(counter, expression):
+    counter, prestatements, internal_expression = normalize_expression(counter, expression.value)
+
+    internal_variable = '${}'.format(counter)
+    counter += 1
+
+    return (
+        counter,
+        prestatements + (NormalVariableInitializationStatement(variable=internal_variable, expression=internal_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,
+        NormalVariableExpression: fake_normalization,
         parsing.FurFunctionCallExpression: normalize_function_call_expression,
         parsing.FurInfixExpression: normalize_infix_expression,
         parsing.FurIntegerLiteralExpression: fake_normalization,
-        parsing.FurNegationExpression: fake_normalization, # TODO Don't fake this
-        parsing.FurParenthesizedExpression: fake_normalization, # TODO Don't fake this
+        parsing.FurNegationExpression: normalize_negation_expression,
+        parsing.FurParenthesizedExpression: normalize_parenthesized_expression,
         parsing.FurStringLiteralExpression: fake_normalization,
         parsing.FurSymbolExpression: fake_normalization,
     }[type(expression)](counter, expression)