From fd7f82862860375ee5ed87fd5341bc8022049b61 Mon Sep 17 00:00:00 2001 From: David Kerkeslager Date: Sun, 6 Aug 2017 13:21:47 -0400 Subject: [PATCH] Added parenthesized expressions --- examples/11_parentheses.fur | 1 + examples/11_parentheses.fur.output.txt | 1 + parsing.py | 32 +++++++++++++++++++++++++- transformation.py | 4 ++++ 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 examples/11_parentheses.fur create mode 100644 examples/11_parentheses.fur.output.txt diff --git a/examples/11_parentheses.fur b/examples/11_parentheses.fur new file mode 100644 index 0000000..6cddd2c --- /dev/null +++ b/examples/11_parentheses.fur @@ -0,0 +1 @@ +print((1 + 1) * 2) diff --git a/examples/11_parentheses.fur.output.txt b/examples/11_parentheses.fur.output.txt new file mode 100644 index 0000000..bf0d87a --- /dev/null +++ b/examples/11_parentheses.fur.output.txt @@ -0,0 +1 @@ +4 \ No newline at end of file diff --git a/parsing.py b/parsing.py index 290eef9..6338369 100644 --- a/parsing.py +++ b/parsing.py @@ -58,6 +58,13 @@ FurNegationExpression = collections.namedtuple( ], ) +FurParenthesizedExpression = collections.namedtuple( + 'FurParenthesizedExpression', + [ + 'internal', + ], +) + FurAdditionExpression = collections.namedtuple( 'FurAdditionExpression', [ @@ -120,6 +127,28 @@ def _symbol_expression_parser(index, tokens): return (False, index, None) +def _parenthesized_expression_parser(index, tokens): + failure = (False, index, None) + + if tokens[index].type == 'open_parenthese': + index += 1 + else: + return failure + + success, index, internal = _expression_parser(index, tokens) + if not success: + return failure + + if tokens[index].type == 'close_parenthese': + index += 1 + else: + raise Exception('Expected ")" on line {}, found "{}"'.format( + tokens[index].line, + tokens[index].match, + )) + + return True, index, FurParenthesizedExpression(internal=internal) + def _negation_expression_parser(index, tokens): failure = (False, index, None) @@ -137,6 +166,7 @@ def _literal_level_expression_parser(index, tokens): return _or_parser( _negation_expression_parser, _function_call_expression_parser, + _parenthesized_expression_parser, _integer_literal_expression_parser, _string_literal_expression_parser, _symbol_expression_parser, @@ -264,7 +294,7 @@ def _function_call_expression_parser(index, tokens): return True, index, FurFunctionCallExpression(function=function, arguments=arguments) -_expression_parser = _multiplication_level_expression_parser +_expression_parser = _addition_level_expression_parser def _assignment_statement_parser(index, tokens): # TODO Use a FurSymbolExpression for the target diff --git a/transformation.py b/transformation.py index 668f4ff..b038b5d 100644 --- a/transformation.py +++ b/transformation.py @@ -113,6 +113,10 @@ BUILTINS = { } def transform_expression(builtin_dependencies, symbol_list, expression): + if isinstance(expression, parsing.FurParenthesizedExpression): + # Parentheses can be removed because everything in the C output is explicitly parenthesized + return transform_expression(builtin_dependencies, symbol_list, expression.internal) + if isinstance(expression, parsing.FurNegationExpression): return transform_negation_expression(builtin_dependencies, symbol_list, expression) -- 2.20.1