Add constant symbol list, which solves all the symbol allocation problems
[fur] / parsing.py
index 0d7caa6..290eef9 100644 (file)
@@ -44,6 +44,13 @@ FurStringLiteralExpression = collections.namedtuple(
     ],
 )
 
+FurSymbolExpression = collections.namedtuple(
+    'FurSymbolExpression',
+    [
+        'value',
+    ],
+)
+
 FurNegationExpression = collections.namedtuple(
     'FurNegationExpression',
     [
@@ -102,14 +109,16 @@ def _integer_literal_expression_parser(index, tokens):
     return True, index, FurIntegerLiteralExpression(value=value)
 
 def _string_literal_expression_parser(index, tokens):
-    failure = (False, index, None)
+    if tokens[index].type == 'single_quoted_string_literal':
+        return (True, index + 1, FurStringLiteralExpression(value=tokens[index].match[1:-1]))
 
-    if tokens[index].type != 'single_quoted_string_literal':
-        return failure
-    value = tokens[index].match[1:-1]
-    index += 1
+    return (False, index, None)
+
+def _symbol_expression_parser(index, tokens):
+    if tokens[index].type == 'symbol':
+        return (True, index + 1, FurSymbolExpression(value=tokens[index].match))
 
-    return True, index, FurStringLiteralExpression(value=value)
+    return (False, index, None)
 
 def _negation_expression_parser(index, tokens):
     failure = (False, index, None)
@@ -130,6 +139,7 @@ def _literal_level_expression_parser(index, tokens):
         _function_call_expression_parser,
         _integer_literal_expression_parser,
         _string_literal_expression_parser,
+        _symbol_expression_parser,
     )(index, tokens)
 
 def _multiplication_level_expression_parser(index, tokens):
@@ -207,11 +217,19 @@ def _comma_separated_list_parser(index, tokens):
 FurFunctionCallExpression = collections.namedtuple(
     'FurFunctionCallExpression',
     [
-        'name',
+        'function',
         'arguments',
     ],
 )
 
+FurAssignmentStatement = collections.namedtuple(
+    'FurAssignmentStatement',
+    [
+        'target',
+        'expression',
+    ],
+)
+
 FurProgram = collections.namedtuple(
     'FurProgram',
     [
@@ -220,12 +238,13 @@ FurProgram = collections.namedtuple(
 )
 
 def _function_call_expression_parser(index, tokens):
+    # TODO Use a FurSymbolExpression for the name
     failure = (False, index, None)
 
-    if tokens[index].type != 'symbol':
+    success, index, function = _symbol_expression_parser(index, tokens)
+
+    if not success:
         return failure
-    name = tokens[index].match
-    index += 1
 
     if tokens[index].type != 'open_parenthese':
         return failure
@@ -243,12 +262,44 @@ def _function_call_expression_parser(index, tokens):
         ))
     index += 1
 
-    return True, index, FurFunctionCallExpression(name=name, arguments=arguments)
+    return True, index, FurFunctionCallExpression(function=function, arguments=arguments)
+
+_expression_parser = _multiplication_level_expression_parser
+
+def _assignment_statement_parser(index, tokens):
+    # TODO Use a FurSymbolExpression for the target
+    failure = (False, index, None)
+
+    if tokens[index].type != 'symbol':
+        return failure
+    target = tokens[index].match
+    index += 1
+
+    if tokens[index].type != 'assignment_operator':
+        return failure
+    assignment_operator_index = index
+
+    success, index, expression = _expression_parser(index + 1, tokens)
+
+    if not success:
+        raise Exception(
+            'Expected expression after assignment operator on line {}'.format(
+                tokens[assignment_operator_index].line
+            )
+        )
+
+    return True, index, FurAssignmentStatement(target=target, expression=expression)
+
+def _statement_parser(index, tokens):
+    return _or_parser(
+        _assignment_statement_parser,
+        _expression_parser,
+    )(index, tokens)
 
 def _program_formatter(statement_list):
     return FurProgram(statement_list=statement_list)
 
-_program_parser = _zero_or_more_parser(_program_formatter, _function_call_expression_parser)
+_program_parser = _zero_or_more_parser(_program_formatter, _statement_parser)
 
 def _parse(parser, tokens):
     success, index, result = parser(0, tokens)