Added parenthesized expressions
authorDavid Kerkeslager <kerkeslager@gmail.com>
Sun, 6 Aug 2017 17:21:47 +0000 (13:21 -0400)
committerDavid Kerkeslager <kerkeslager@gmail.com>
Sun, 6 Aug 2017 17:21:47 +0000 (13:21 -0400)
examples/11_parentheses.fur [new file with mode: 0644]
examples/11_parentheses.fur.output.txt [new file with mode: 0644]
parsing.py
transformation.py

diff --git a/examples/11_parentheses.fur b/examples/11_parentheses.fur
new file mode 100644 (file)
index 0000000..6cddd2c
--- /dev/null
@@ -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 (file)
index 0000000..bf0d87a
--- /dev/null
@@ -0,0 +1 @@
+4
\ No newline at end of file
index 290eef9..6338369 100644 (file)
@@ -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
index 668f4ff..b038b5d 100644 (file)
@@ -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)