],
)
-FurAdditionExpression = collections.namedtuple(
- 'FurAdditionExpression',
+FurAdditionLevelExpression = collections.namedtuple(
+ 'FurAdditionLevelExpression',
[
+ 'operator',
'left',
'right',
],
)
-FurSubtractionExpression = collections.namedtuple(
- 'FurSubtractionExpression',
+FurMultiplicationLevelExpression = collections.namedtuple(
+ 'FurMultiplicationLevelExpression',
[
+ 'operator',
'left',
'right',
],
)
-FurMultiplicationExpression = collections.namedtuple(
- 'FurMultiplicationExpression',
- [
- 'left',
- 'right',
- ],
-)
-
-FurIntegerDivisionExpression = collections.namedtuple(
- 'FurIntegerDivisionExpression',
- [
- 'left',
- 'right',
- ],
-)
-
-FurModularDivisionExpression = collections.namedtuple(
- 'FurModularDivisionExpression',
- [
- 'left',
- 'right',
- ],
-)
-
-FurEqualityExpression = collections.namedtuple(
- 'FurEqualityExpression',
- [
- 'left',
- 'right',
- ],
-)
-
-FurInequalityExpression = collections.namedtuple(
- 'FurInequalityExpression',
- [
- 'left',
- 'right',
- ],
-)
-
-FurLessThanOrEqualExpression = collections.namedtuple(
- 'FurLessThanOrEqualExpression',
- [
- 'left',
- 'right',
- ],
-)
-
-FurGreaterThanOrEqualExpression = collections.namedtuple(
- 'FurGreaterThanOrEqualExpression',
- [
- 'left',
- 'right',
- ],
-)
-
-FurLessThanExpression = collections.namedtuple(
- 'FurLessThanExpression',
- [
- 'left',
- 'right',
- ],
-)
-
-FurGreaterThanExpression = collections.namedtuple(
- 'FurGreaterThanExpression',
+FurEqualityLevelExpression = collections.namedtuple(
+ 'FurEqualityLevelExpression',
[
+ 'operator',
'left',
'right',
],
_symbol_expression_parser,
)(index, tokens)
-def _left_recursive_infix_operator_parser(token_type, operand_parser, operator_to_expression_type_mapping):
+def _left_recursive_infix_operator_parser(token_type, operand_parser, result_expression_type):
def result_parser(index, tokens):
failure = (False, index, None)
success, try_index, value = operand_parser(index + 1, tokens)
if success:
- result = operator_to_expression_type_mapping[tokens[index].match](left=result, right=value)
+ result = result_expression_type(
+ operator=tokens[index].match,
+ left=result,
+ right=value,
+ )
index = try_index
return True, index, result
return _left_recursive_infix_operator_parser(
'multiplication_level_operator',
_literal_level_expression_parser,
- {
- '*': FurMultiplicationExpression,
- '//': FurIntegerDivisionExpression,
- '%': FurModularDivisionExpression,
- },
+ FurMultiplicationLevelExpression,
)(index, tokens)
def _addition_level_expression_parser(index, tokens):
return _left_recursive_infix_operator_parser(
'addition_level_operator',
_multiplication_level_expression_parser,
- {
- '+': FurAdditionExpression,
- '-': FurSubtractionExpression,
- },
+ FurAdditionLevelExpression,
)(index, tokens)
def _equality_level_expression_parser(index, tokens):
return _left_recursive_infix_operator_parser(
'equality_level_operator',
_addition_level_expression_parser,
- {
- '==': FurEqualityExpression,
- '!=': FurInequalityExpression,
- '>=': FurGreaterThanOrEqualExpression,
- '<=': FurLessThanOrEqualExpression,
- '>': FurGreaterThanExpression,
- '<': FurLessThanExpression,
- },
+ FurEqualityLevelExpression,
)(index, tokens)
def _comma_separated_list_parser(index, tokens):
],
)
-
CModularDivisionExpression = collections.namedtuple(
'CModularDivisionExpression',
[
],
)
-EQUALITY_LEVEL_TYPE_MAPPING = {
- parsing.FurEqualityExpression: CEqualityExpression,
- parsing.FurInequalityExpression: CInequalityExpression,
- parsing.FurLessThanOrEqualExpression: CLessThanOrEqualExpression,
- parsing.FurGreaterThanOrEqualExpression: CGreaterThanOrEqualExpression,
- parsing.FurLessThanExpression: CLessThanExpression,
- parsing.FurGreaterThanExpression: CGreaterThanExpression,
+EQUALITY_LEVEL_OPERATOR_MAPPING = {
+ '==': CEqualityExpression,
+ '!=': CInequalityExpression,
+ '<=': CLessThanOrEqualExpression,
+ '>=': CGreaterThanOrEqualExpression,
+ '<': CLessThanExpression,
+ '>': CGreaterThanExpression,
}
def transform_equality_level_expression(builtin_dependencies, symbol_list, expression):
# Transform expressions like 1 < 2 < 3 into expressions like 1 < 2 && 2 < 3
- if type(expression.left) in EQUALITY_LEVEL_TYPE_MAPPING:
+ if isinstance(expression.left, parsing.FurEqualityLevelExpression):
left = transform_equality_level_expression(
builtin_dependencies,
symbol_list,
# TODO Don't evaluate the middle expression twice
return CAndExpression(
left=left,
- right=EQUALITY_LEVEL_TYPE_MAPPING[type(expression)](
+ right=EQUALITY_LEVEL_OPERATOR_MAPPING[expression.operator](
left=middle,
right=right,
),
)
- return EQUALITY_LEVEL_TYPE_MAPPING[type(expression)](
+ return EQUALITY_LEVEL_OPERATOR_MAPPING[expression.operator](
left=transform_expression(builtin_dependencies, symbol_list, expression.left),
right=transform_expression(builtin_dependencies, symbol_list, expression.right),
)
if type(expression) in LITERAL_TYPE_MAPPING:
return LITERAL_TYPE_MAPPING[type(expression)](value=expression.value)
- if type(expression) in EQUALITY_LEVEL_TYPE_MAPPING:
+ if isinstance(expression, parsing.FurEqualityLevelExpression):
return transform_equality_level_expression(builtin_dependencies, symbol_list, expression)
- INFIX_TYPE_MAPPING = {
- parsing.FurAdditionExpression: CAdditionExpression,
- parsing.FurSubtractionExpression: CSubtractionExpression,
- parsing.FurMultiplicationExpression: CMultiplicationExpression,
- parsing.FurIntegerDivisionExpression: CIntegerDivisionExpression,
- parsing.FurModularDivisionExpression: CModularDivisionExpression,
+ INFIX_OPERATOR_TO_TYPE_MAPPING = {
+ '+': CAdditionExpression,
+ '-': CSubtractionExpression,
+ '*': CMultiplicationExpression,
+ '//': CIntegerDivisionExpression,
+ '%': CModularDivisionExpression,
}
- return INFIX_TYPE_MAPPING[type(expression)](
+ return INFIX_OPERATOR_TO_TYPE_MAPPING[expression.operator](
left=transform_expression(builtin_dependencies, symbol_list, expression.left),
right=transform_expression(builtin_dependencies, symbol_list, expression.right),
)