],
)
-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',
_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)
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,
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):
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,
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