From 808637f6e5ee3f0672fd89d7642a882258d63402 Mon Sep 17 00:00:00 2001 From: David Kerkeslager Date: Sun, 6 Aug 2017 15:54:35 -0400 Subject: [PATCH] Store all Fur infix operator expressions in the same type --- parsing.py | 34 +++++++++------------------------- transformation.py | 33 ++++++++++++++++++--------------- 2 files changed, 27 insertions(+), 40 deletions(-) diff --git a/parsing.py b/parsing.py index b791d46..df29c0d 100644 --- a/parsing.py +++ b/parsing.py @@ -65,27 +65,10 @@ FurParenthesizedExpression = collections.namedtuple( ], ) -FurAdditionLevelExpression = collections.namedtuple( - 'FurAdditionLevelExpression', - [ - 'operator', - 'left', - 'right', - ], -) - -FurMultiplicationLevelExpression = collections.namedtuple( - 'FurMultiplicationLevelExpression', - [ - 'operator', - 'left', - 'right', - ], -) - -FurEqualityLevelExpression = collections.namedtuple( - 'FurEqualityLevelExpression', +FurInfixExpression = collections.namedtuple( + 'FurInfixExpression', [ + 'order', 'operator', 'left', 'right', @@ -159,7 +142,7 @@ def _literal_level_expression_parser(index, tokens): _symbol_expression_parser, )(index, tokens) -def _left_recursive_infix_operator_parser(token_type, operand_parser, result_expression_type): +def _left_recursive_infix_operator_parser(token_type, operand_parser, order): def result_parser(index, tokens): failure = (False, index, None) @@ -175,7 +158,8 @@ def _left_recursive_infix_operator_parser(token_type, operand_parser, result_exp success, try_index, value = operand_parser(index + 1, tokens) if success: - result = result_expression_type( + result = FurInfixExpression( + order=order, operator=tokens[index].match, left=result, right=value, @@ -190,21 +174,21 @@ def _multiplication_level_expression_parser(index, tokens): return _left_recursive_infix_operator_parser( 'multiplication_level_operator', _literal_level_expression_parser, - FurMultiplicationLevelExpression, + 'multiplication_level', )(index, tokens) def _addition_level_expression_parser(index, tokens): return _left_recursive_infix_operator_parser( 'addition_level_operator', _multiplication_level_expression_parser, - FurAdditionLevelExpression, + 'addition_level', )(index, tokens) def _equality_level_expression_parser(index, tokens): return _left_recursive_infix_operator_parser( 'equality_level_operator', _addition_level_expression_parser, - FurEqualityLevelExpression, + 'equality_level', )(index, tokens) def _comma_separated_list_parser(index, tokens): diff --git a/transformation.py b/transformation.py index f7d849d..f67d0a4 100644 --- a/transformation.py +++ b/transformation.py @@ -85,7 +85,7 @@ EQUALITY_LEVEL_OPERATOR_TO_FUNCTION_NAME_MAPPING = { def transform_equality_level_expression(builtin_dependencies, symbol_list, expression): # Transform expressions like 1 < 2 < 3 into expressions like 1 < 2 && 2 < 3 - if isinstance(expression.left, parsing.FurEqualityLevelExpression): + if isinstance(expression.left, parsing.FurInfixExpression) and expression.left.order == 'equality_level': left = transform_equality_level_expression( builtin_dependencies, symbol_list, @@ -155,22 +155,25 @@ def transform_expression(builtin_dependencies, symbol_list, expression): if type(expression) in LITERAL_TYPE_MAPPING: return LITERAL_TYPE_MAPPING[type(expression)](value=expression.value) - if isinstance(expression, parsing.FurEqualityLevelExpression): - return transform_equality_level_expression(builtin_dependencies, symbol_list, expression) + if isinstance(expression, parsing.FurInfixExpression): + if expression.order == 'equality_level': + return transform_equality_level_expression(builtin_dependencies, symbol_list, expression) - INFIX_OPERATOR_TO_FUNCTION_NAME = { - '+': 'add', - '-': 'subtract', - '*': 'multiply', - '//': 'integerDivide', - '%': 'modularDivide', - } + INFIX_OPERATOR_TO_FUNCTION_NAME = { + '+': 'add', + '-': 'subtract', + '*': 'multiply', + '//': 'integerDivide', + '%': 'modularDivide', + } - return CFunctionCallForFurInfixOperator( - name=INFIX_OPERATOR_TO_FUNCTION_NAME[expression.operator], - left=transform_expression(builtin_dependencies, symbol_list, expression.left), - right=transform_expression(builtin_dependencies, symbol_list, expression.right), - ) + return CFunctionCallForFurInfixOperator( + name=INFIX_OPERATOR_TO_FUNCTION_NAME[expression.operator], + left=transform_expression(builtin_dependencies, symbol_list, expression.left), + right=transform_expression(builtin_dependencies, symbol_list, expression.right), + ) + + raise Exception('Could not transform expression "{}"'.format(expression)) def transform_assignment_statement(builtin_dependencies, symbol_list, assignment_statement): # TODO Check that target is not a builtin -- 2.20.1