From 777a46709a4353837a34df7fc0f7d9f3d47902a7 Mon Sep 17 00:00:00 2001 From: David Kerkeslager Date: Sat, 12 Aug 2017 14:56:12 -0400 Subject: [PATCH] Normalize literal expressions --- normalization.py | 24 ++++++++++++++++++++++-- parsing.py | 12 ++++++------ transformation.py | 10 ++++++---- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/normalization.py b/normalization.py index a5863a1..a9fa3ec 100644 --- a/normalization.py +++ b/normalization.py @@ -10,6 +10,20 @@ NormalVariableExpression = collections.namedtuple( ], ) +NormalIntegerLiteralExpression = collections.namedtuple( + 'NormalIntegerLiteralExpression', + [ + 'integer', + ], +) + +NormalStringLiteralExpression = collections.namedtuple( + 'NormalStringLiteralExpression', + [ + 'string', + ], +) + NormalNegationExpression = collections.namedtuple( 'NormalNegationExpression', [ @@ -104,6 +118,12 @@ NormalProgram = collections.namedtuple( def fake_normalization(counter, thing): return (counter, (), thing) +def normalize_integer_literal_expression(counter, expression): + return (counter, (), NormalIntegerLiteralExpression(integer=expression.integer)) + +def normalize_string_literal_expression(counter, expression): + return (counter, (), NormalStringLiteralExpression(string=expression.string)) + def normalize_function_call_expression(counter, expression): assert isinstance(expression, parsing.FurFunctionCallExpression) @@ -308,9 +328,9 @@ def normalize_expression(counter, expression): NormalVariableExpression: fake_normalization, parsing.FurFunctionCallExpression: normalize_function_call_expression, parsing.FurInfixExpression: normalize_infix_expression, - parsing.FurIntegerLiteralExpression: fake_normalization, + parsing.FurIntegerLiteralExpression: normalize_integer_literal_expression, parsing.FurNegationExpression: normalize_negation_expression, - parsing.FurStringLiteralExpression: fake_normalization, + parsing.FurStringLiteralExpression: normalize_string_literal_expression, parsing.FurSymbolExpression: fake_normalization, }[type(expression)](counter, expression) diff --git a/parsing.py b/parsing.py index 91098f9..a543819 100644 --- a/parsing.py +++ b/parsing.py @@ -39,14 +39,14 @@ def _zero_or_more_parser(formatter, parser): FurIntegerLiteralExpression = collections.namedtuple( 'FurIntegerLiteralExpression', [ - 'value', + 'integer', ], ) FurStringLiteralExpression = collections.namedtuple( 'FurStringLiteralExpression', [ - 'value', + 'string', ], ) @@ -82,11 +82,11 @@ def _integer_literal_expression_parser(index, tokens): value = int(tokens[index].match) index += 1 - return True, index, FurIntegerLiteralExpression(value=value) + return True, index, FurIntegerLiteralExpression(integer=value) def _string_literal_expression_parser(index, tokens): if tokens[index].type == 'single_quoted_string_literal': - return (True, index + 1, FurStringLiteralExpression(value=tokens[index].match[1:-1])) + return (True, index + 1, FurStringLiteralExpression(string=tokens[index].match[1:-1])) return (False, index, None) @@ -462,7 +462,7 @@ if __name__ == '__main__': ( True, 1, - FurStringLiteralExpression(value='Hello, world'), + FurStringLiteralExpression(string='Hello, world'), ), ) @@ -475,7 +475,7 @@ if __name__ == '__main__': 4, FurFunctionCallExpression( name='print', - arguments=(FurStringLiteralExpression(value='Hello, world'),), + arguments=(FurStringLiteralExpression(string='Hello, world'),), ), ), ) diff --git a/transformation.py b/transformation.py index 83fc36b..cc24304 100644 --- a/transformation.py +++ b/transformation.py @@ -153,8 +153,8 @@ BUILTINS = { def transform_variable_expression(accumulators, expression): return CVariableExpression(variable=expression.variable) -def transform_string_literal(accumulators, expression): - value = expression.value +def transform_string_literal_expression(accumulators, expression): + value = expression.string try: index = accumulators.string_literal_list.index(value) @@ -252,7 +252,7 @@ def transform_infix_expression(accumulators, expression): ) def transform_integer_literal_expression(accumulators, expression): - return CIntegerLiteral(value=expression.value) + return CIntegerLiteral(value=expression.integer) def transform_negation_expression(accumulators, expression): return CNegationExpression( @@ -266,11 +266,13 @@ def transform_expression(accumulators, expression): parsing.FurInfixExpression: transform_infix_expression, parsing.FurIntegerLiteralExpression: transform_integer_literal_expression, parsing.FurNegationExpression: transform_negation_expression, - parsing.FurStringLiteralExpression: transform_string_literal, + 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.NormalNegationExpression: transform_negation_expression, + normalization.NormalStringLiteralExpression: transform_string_literal_expression, normalization.NormalVariableExpression: transform_variable_expression, }[type(expression)](accumulators, expression) -- 2.20.1