Normalize symbol expressions
authorDavid Kerkeslager <kerkeslager@gmail.com>
Sat, 12 Aug 2017 19:08:28 +0000 (15:08 -0400)
committerDavid Kerkeslager <kerkeslager@gmail.com>
Sat, 12 Aug 2017 19:10:55 +0000 (15:10 -0400)
normalization.py
parsing.py
transformation.py

index a9fa3ec..51c1d7b 100644 (file)
@@ -24,6 +24,13 @@ NormalStringLiteralExpression = collections.namedtuple(
     ],
 )
 
+NormalSymbolExpression = collections.namedtuple(
+    'NormalSymbolExpression',
+    [
+        'symbol',
+    ],
+)
+
 NormalNegationExpression = collections.namedtuple(
     'NormalNegationExpression',
     [
@@ -114,16 +121,21 @@ NormalProgram = collections.namedtuple(
     ],
 )
 
-# TODO Get rid of this
 def fake_normalization(counter, thing):
     return (counter, (), thing)
 
 def normalize_integer_literal_expression(counter, expression):
+    # TODO Store this in a C variable
     return (counter, (), NormalIntegerLiteralExpression(integer=expression.integer))
 
 def normalize_string_literal_expression(counter, expression):
+    # TODO Store this in a C variable
     return (counter, (), NormalStringLiteralExpression(string=expression.string))
 
+def normalize_symbol_expression(counter, expression):
+    # TODO Store this in a C variable
+    return (counter, (), NormalSymbolExpression(symbol=expression.symbol))
+
 def normalize_function_call_expression(counter, expression):
     assert isinstance(expression, parsing.FurFunctionCallExpression)
 
@@ -331,7 +343,7 @@ def normalize_expression(counter, expression):
         parsing.FurIntegerLiteralExpression: normalize_integer_literal_expression,
         parsing.FurNegationExpression: normalize_negation_expression,
         parsing.FurStringLiteralExpression: normalize_string_literal_expression,
-        parsing.FurSymbolExpression: fake_normalization,
+        parsing.FurSymbolExpression: normalize_symbol_expression,
     }[type(expression)](counter, expression)
 
 def normalize_expression_statement(counter, statement):
index a543819..b2a14a8 100644 (file)
@@ -53,7 +53,7 @@ FurStringLiteralExpression = collections.namedtuple(
 FurSymbolExpression = collections.namedtuple(
     'FurSymbolExpression',
     [
-        'value',
+        'symbol',
     ],
 )
 
@@ -92,7 +92,7 @@ def _string_literal_expression_parser(index, tokens):
 
 def _symbol_expression_parser(index, tokens):
     if tokens[index].type == 'symbol':
-        return (True, index + 1, FurSymbolExpression(value=tokens[index].match))
+        return (True, index + 1, FurSymbolExpression(symbol=tokens[index].match))
 
     return (False, index, None)
 
@@ -415,7 +415,7 @@ def _function_definition_statement_parser(index, tokens):
 
     return True, index, FurFunctionDefinitionStatement(
         name=name,
-        argument_name_list=tuple(an.value for an in argument_name_list),
+    argument_name_list=tuple(an.symbol for an in argument_name_list),
         statement_list=statement_list,
     )
 
index cc24304..0249890 100644 (file)
@@ -1,7 +1,7 @@
 import collections
 
 import normalization
-import parsing
+import parsing # TODO Remove this import, as we should be normalizing everything before it gets here
 
 CIntegerLiteral = collections.namedtuple(
     'CIntegerLiteral',
@@ -165,17 +165,17 @@ def transform_string_literal_expression(accumulators, expression):
     return CStringLiteral(index=index, value=value)
 
 def transform_symbol_expression(accumulators, expression):
-    if expression.value in ['true', 'false']:
-        return CConstantExpression(value=expression.value)
+    if expression.symbol in ['true', 'false']:
+        return CConstantExpression(value=expression.symbol)
 
     try:
-        symbol_list_index = accumulators.symbol_list.index(expression.value)
+        symbol_list_index = accumulators.symbol_list.index(expression.symbol)
     except ValueError:
         symbol_list_index = len(accumulators.symbol_list)
-        accumulators.symbol_list.append(expression.value)
+        accumulators.symbol_list.append(expression.symbol)
 
     return CSymbolExpression(
-        symbol=expression.value,
+        symbol=expression.symbol,
         symbol_list_index=symbol_list_index,
     )
 
@@ -267,12 +267,12 @@ def transform_expression(accumulators, expression):
         parsing.FurIntegerLiteralExpression: transform_integer_literal_expression,
         parsing.FurNegationExpression: transform_negation_expression,
         parsing.FurStringLiteralExpression: transform_string_literal_expression,
-        parsing.FurSymbolExpression: transform_symbol_expression,
         normalization.NormalFunctionCallExpression: transform_function_call_expression,
         normalization.NormalInfixExpression: transform_infix_expression,
         normalization.NormalIntegerLiteralExpression: transform_integer_literal_expression,
         normalization.NormalNegationExpression: transform_negation_expression,
         normalization.NormalStringLiteralExpression: transform_string_literal_expression,
+        normalization.NormalSymbolExpression: transform_symbol_expression,
         normalization.NormalVariableExpression: transform_variable_expression,
     }[type(expression)](accumulators, expression)
 
@@ -294,11 +294,11 @@ def transform_symbol_assignment_statement(accumulators, assignment_statement):
     )
 
 def transform_function_call_expression(accumulators, function_call):
-    if isinstance(function_call.function, parsing.FurSymbolExpression):
+    if isinstance(function_call.function, normalization.NormalSymbolExpression):
         # TODO Move this check to transformation of symbol expressions so we can have builtins that aren't functions
-        if function_call.function.value in BUILTINS.keys():
+        if function_call.function.symbol in BUILTINS.keys():
             # TODO Check that the builtin is actually callable
-            accumulators.builtin_set.add(function_call.function.value)
+            accumulators.builtin_set.add(function_call.function.symbol)
 
     # TODO Use the symbol from SYMBOL LIST
     return CFunctionCallExpression(