From 1852539af30c364a11d2e2a0bb7944102293dcf6 Mon Sep 17 00:00:00 2001 From: David Kerkeslager Date: Tue, 8 Aug 2017 14:07:34 -0400 Subject: [PATCH] Normalize parenthesized and negated expressions --- normalization.py | 26 ++++++++++++++++++++++++-- parsing.py | 1 + transformation.py | 5 +++-- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/normalization.py b/normalization.py index 1dbeddb..1da6569 100644 --- a/normalization.py +++ b/normalization.py @@ -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) diff --git a/parsing.py b/parsing.py index 23bf65e..adc96e9 100644 --- a/parsing.py +++ b/parsing.py @@ -64,6 +64,7 @@ FurNegationExpression = collections.namedtuple( ], ) +# TODO We don't need to wrap this type FurParenthesizedExpression = collections.namedtuple( 'FurParenthesizedExpression', [ diff --git a/transformation.py b/transformation.py index efc7d99..4c1ef3b 100644 --- a/transformation.py +++ b/transformation.py @@ -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): -- 2.20.1