From ec8ce6f417bad0e61e82462787fbb7d7dbe25ea0 Mon Sep 17 00:00:00 2001 From: David Kerkeslager Date: Tue, 8 Aug 2017 10:52:13 -0400 Subject: [PATCH] Use order instead of operator to normalize infix expressions --- normalization.py | 20 ++++++-------------- parsing.py | 8 ++++---- tokenization.py | 14 +++++++------- transformation.py | 10 +++++----- 4 files changed, 22 insertions(+), 30 deletions(-) diff --git a/normalization.py b/normalization.py index 3ece2bb..e3760f6 100644 --- a/normalization.py +++ b/normalization.py @@ -117,20 +117,12 @@ def normalize_basic_infix_operation(counter, expression): def normalize_infix_expression(counter, expression): # TODO Unfake this normalization return { - '+': normalize_basic_infix_operation, - '-': normalize_basic_infix_operation, - '*': normalize_basic_infix_operation, - '//': normalize_basic_infix_operation, - '%': normalize_basic_infix_operation, - '==': fake_normalization, - '<=': fake_normalization, - '>=': fake_normalization, - '!=': fake_normalization, - '<': fake_normalization, - '>': fake_normalization, - 'and': fake_normalization, - 'or': fake_normalization, - }[expression.operator](counter, expression) + 'multiplication_level': normalize_basic_infix_operation, + 'addition_level': normalize_basic_infix_operation, + 'comparison_level': fake_normalization, + 'and_level': fake_normalization, + 'or_level': fake_normalization, + }[expression.order](counter, expression) def normalize_expression(counter, expression): return { diff --git a/parsing.py b/parsing.py index aa4f427..23bf65e 100644 --- a/parsing.py +++ b/parsing.py @@ -190,17 +190,17 @@ def _addition_level_expression_parser(index, tokens): 'addition_level', )(index, tokens) -def _equality_level_expression_parser(index, tokens): +def _comparison_level_expression_parser(index, tokens): return _left_recursive_infix_operator_parser( - lambda token: token.type == 'equality_level_operator', + lambda token: token.type == 'comparison_level_operator', _addition_level_expression_parser, - 'equality_level', + 'comparison_level', )(index, tokens) def _and_level_expression_parser(index, tokens): return _left_recursive_infix_operator_parser( lambda token: token.type == 'symbol' and token.match == 'and', - _equality_level_expression_parser, + _comparison_level_expression_parser, 'and_level', )(index, tokens) diff --git a/tokenization.py b/tokenization.py index 3131c35..a736912 100644 --- a/tokenization.py +++ b/tokenization.py @@ -39,7 +39,7 @@ _TOKEN_MATCHERS = [ ('integer_literal', r'\d+'), ('symbol', r'[a-z]+'), ('single_quoted_string_literal', r"'.*?'"), - ('equality_level_operator', r'(<=|>=|==|!=|<|>)'), + ('comparison_level_operator', r'(<=|>=|==|!=|<|>)'), ('assignment_operator', r'='), ('addition_level_operator', r'(\+|-)'), ('multiplication_level_operator', r'(\*|//|%)'), @@ -205,7 +205,7 @@ if __name__ == '__main__': self.assertEqual( tokenize('=='), (Token( - type='equality_level_operator', + type='comparison_level_operator', match='==', index=0, line=1, @@ -216,7 +216,7 @@ if __name__ == '__main__': self.assertEqual( tokenize('>='), (Token( - type='equality_level_operator', + type='comparison_level_operator', match='>=', index=0, line=1, @@ -227,7 +227,7 @@ if __name__ == '__main__': self.assertEqual( tokenize('<='), (Token( - type='equality_level_operator', + type='comparison_level_operator', match='<=', index=0, line=1, @@ -238,7 +238,7 @@ if __name__ == '__main__': self.assertEqual( tokenize('>'), (Token( - type='equality_level_operator', + type='comparison_level_operator', match='>', index=0, line=1, @@ -249,7 +249,7 @@ if __name__ == '__main__': self.assertEqual( tokenize('<'), (Token( - type='equality_level_operator', + type='comparison_level_operator', match='<', index=0, line=1, @@ -260,7 +260,7 @@ if __name__ == '__main__': self.assertEqual( tokenize('!='), (Token( - type='equality_level_operator', + type='comparison_level_operator', match='!=', index=0, line=1, diff --git a/transformation.py b/transformation.py index 5de9bbf..b6d21c8 100644 --- a/transformation.py +++ b/transformation.py @@ -108,10 +108,10 @@ EQUALITY_LEVEL_OPERATOR_TO_FUNCTION_NAME_MAPPING = { '>': 'greaterThan', } -def transform_equality_level_expression(accumulators, expression): +def transform_comparison_level_expression(accumulators, expression): # Transform expressions like 1 < 2 < 3 into expressions like 1 < 2 && 2 < 3 - if isinstance(expression.left, parsing.FurInfixExpression) and expression.left.order == 'equality_level': - left = transform_equality_level_expression( + if isinstance(expression.left, parsing.FurInfixExpression) and expression.left.order == 'comparison_level': + left = transform_comparison_level_expression( accumulators, expression.left ) @@ -151,8 +151,8 @@ def transform_variable_expression(accumulators, expression): return CVariableExpression(variable=expression.variable) def transform_infix_expression(accumulators, expression): - if expression.order == 'equality_level': - return transform_equality_level_expression(accumulators, expression) + if expression.order == 'comparison_level': + return transform_comparison_level_expression(accumulators, expression) INFIX_OPERATOR_TO_FUNCTION_NAME = { '+': 'add', -- 2.20.1