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',
],
)
-CConstantExpression = collections.namedtuple(
- 'CConstantExpression',
- [
- 'value'
- ],
-)
-
CVariableExpression = collections.namedtuple(
'CVariableExpression',
[
CFunctionCallExpression = collections.namedtuple(
'CFunctionCallExpression',
[
- 'name',
+ 'function_expression',
'argument_count',
'argument_items',
],
)
+# TODO We are currently not changing variables, just preventing them from being accessed.
CSymbolAssignmentStatement = collections.namedtuple(
'CSymbolAssignmentStatement',
[
],
)
+# 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',
[
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)
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 BUILTINS:
+ accumulators.builtin_set.add(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,
)
)
def transform_integer_literal_expression(accumulators, expression):
- return CIntegerLiteral(value=expression.value)
+ return CIntegerLiteral(value=expression.integer)
def transform_negation_expression(accumulators, expression):
return CNegationExpression(
def transform_expression(accumulators, expression):
# TODO Clean up handlers for parsing expressions
return {
- parsing.FurFunctionCallExpression: transform_function_call_expression,
parsing.FurInfixExpression: transform_infix_expression,
parsing.FurIntegerLiteralExpression: transform_integer_literal_expression,
parsing.FurNegationExpression: transform_negation_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)
)
def transform_function_call_expression(accumulators, function_call):
- if isinstance(function_call.function, parsing.FurSymbolExpression):
- # 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():
- # TODO Check that the builtin is actually callable
- accumulators.builtin_set.add(function_call.function.value)
-
# TODO Use the symbol from SYMBOL LIST
return CFunctionCallExpression(
- name=transform_expression(accumulators, function_call.function),
+ function_expression=transform_expression(accumulators, function_call.function_expression),
argument_count=function_call.argument_count,
argument_items=transform_expression(accumulators, function_call.argument_items),
)
transform_statement(accumulators, statement) for statement in program.statement_list
]
+ # This prevents warnings about normalized variables being entire C statements
+ last_statement = statement_list[-1]
+ if isinstance(last_statement, normalization.NormalExpressionStatement) and isinstance(last_statement.expression, normalization.NormalVariableExpression):
+ del statement_list[-1]
+
standard_library_set = set()
for builtin in accumulators.builtin_set:
for standard_library in BUILTINS[builtin]: