X-Git-Url: https://code.kerkeslager.com/?a=blobdiff_plain;f=transformation.py;h=34710bf7c631f5684c84b33898b5a3ef245dacff;hb=204200cd277191bd0a272528b2a811cbdb7990a0;hp=41cd7128c32782c78e3d36ee478d9be0cb38da82;hpb=d6af7d074bf65e782e42055623a197863b5f8000;p=fur diff --git a/transformation.py b/transformation.py index 41cd712..34710bf 100644 --- a/transformation.py +++ b/transformation.py @@ -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', @@ -65,6 +65,7 @@ CFunctionCallExpression = collections.namedtuple( ], ) +# TODO We are currently not changing variables, just preventing them from being accessed. CSymbolAssignmentStatement = collections.namedtuple( 'CSymbolAssignmentStatement', [ @@ -121,6 +122,8 @@ CFunctionDeclaration = collections.namedtuple( ], ) +# TODO If a function definition doesn't end with an expression, we have issues currently because we try to return statement. +# TODO Closures currently wrap entire defining environment, even symbols that are not used, which makes garbage collection ineffective. CFunctionDefinition = collections.namedtuple( 'CFunctionDefinition', [ @@ -153,8 +156,8 @@ BUILTINS = { def transform_variable_expression(accumulators, expression): return CVariableExpression(variable=expression.variable) -def transform_string_literal(accumulators, expression): - value = expression.value +def transform_string_literal_expression(accumulators, expression): + value = expression.string try: index = accumulators.string_literal_list.index(value) @@ -165,17 +168,17 @@ def transform_string_literal(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, ) @@ -252,11 +255,7 @@ def transform_infix_expression(accumulators, expression): ) def transform_integer_literal_expression(accumulators, expression): - return CIntegerLiteral(value=expression.value) - -def transform_parenthesized_expression(accumulators, expression): - # Parentheses can be removed because everything in the C output is explicitly parenthesized - return transform_expression(accumulators, expression.internal) + return CIntegerLiteral(value=expression.integer) def transform_negation_expression(accumulators, expression): return CNegationExpression( @@ -270,12 +269,13 @@ def transform_expression(accumulators, expression): parsing.FurInfixExpression: transform_infix_expression, parsing.FurIntegerLiteralExpression: transform_integer_literal_expression, parsing.FurNegationExpression: transform_negation_expression, - parsing.FurParenthesizedExpression: transform_parenthesized_expression, - parsing.FurStringLiteralExpression: transform_string_literal, - parsing.FurSymbolExpression: transform_symbol_expression, + parsing.FurStringLiteralExpression: transform_string_literal_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) @@ -297,13 +297,15 @@ def transform_symbol_assignment_statement(accumulators, assignment_statement): ) def transform_function_call_expression(accumulators, function_call): - if function_call.function.value in BUILTINS.keys(): - # TODO Check that the builtin is actually callable - accumulators.builtin_set.add(function_call.function.value) + 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.symbol in BUILTINS.keys(): + # TODO Check that the builtin is actually callable + accumulators.builtin_set.add(function_call.function.symbol) # TODO Use the symbol from SYMBOL LIST return CFunctionCallExpression( - name=function_call.function.value, + name=transform_expression(accumulators, function_call.function), argument_count=function_call.argument_count, argument_items=transform_expression(accumulators, function_call.argument_items), )