Normalize parenthesized and negated expressions
authorDavid Kerkeslager <kerkeslager@gmail.com>
Tue, 8 Aug 2017 18:07:34 +0000 (14:07 -0400)
committerDavid Kerkeslager <kerkeslager@gmail.com>
Tue, 8 Aug 2017 18:07:34 +0000 (14:07 -0400)
normalization.py
parsing.py
transformation.py

index 1dbeddb..1da6569 100644 (file)
@@ -9,6 +9,13 @@ NormalVariableExpression = collections.namedtuple(
     ],
 )
 
+NormalNegationExpression = collections.namedtuple(
+    'NormalNegationExpression',
+    [
+        'internal_expression',
+    ],
+)
+
 NormalInfixExpression = collections.namedtuple(
     'NormalInfixExpression',
     [
@@ -238,6 +245,21 @@ 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,
@@ -245,8 +267,8 @@ def normalize_expression(counter, expression):
         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)
index 23bf65e..adc96e9 100644 (file)
@@ -64,6 +64,7 @@ FurNegationExpression = collections.namedtuple(
     ],
 )
 
+# TODO We don't need to wrap this type
 FurParenthesizedExpression = collections.namedtuple(
     'FurParenthesizedExpression',
     [
index efc7d99..4c1ef3b 100644 (file)
@@ -233,6 +233,7 @@ def transform_expression(accumulators, expression):
         parsing.FurInfixExpression: transform_infix_expression, # TODO Shouldn't need this
         normalization.NormalFunctionCallExpression: transform_function_call_expression,
         normalization.NormalInfixExpression: transform_infix_expression,
+        normalization.NormalNegationExpression: transform_negation_expression,
         normalization.NormalVariableExpression: transform_variable_expression,
     }[type(expression)](accumulators, expression)
 
@@ -250,9 +251,9 @@ def transform_symbol_assignment_statement(accumulators, assignment_statement):
         ),
     )
 
-def transform_negation_expression(accumulators, negation_expression):
+def transform_negation_expression(accumulators, expression):
     return CNegationExpression(
-        value=transform_expression(accumulators, negation_expression.value),
+        value=transform_expression(accumulators, expression.internal_expression),
     )
 
 def transform_function_call_expression(accumulators, function_call):