import collections
import parsing
+import util
NormalVariableExpression = collections.namedtuple(
'NormalVariableExpression',
'NormalFunctionCallExpression',
[
'function',
- 'arguments',
+ 'argument_count',
+ 'argument_items',
+ ],
+)
+
+NormalArrayVariableInitializationStatement = collections.namedtuple(
+ 'NormalArrayVariableInitializationStatement',
+ [
+ 'variable',
+ 'items',
],
)
],
)
+NormalFunctionDefinitionStatement = collections.namedtuple(
+ 'NormalFunctionDefinitionStatement',
+ [
+ 'name',
+ 'statement_list',
+ ],
+)
+
NormalProgram = collections.namedtuple(
'NormalProgram',
[
],
)
+# TODO Get rid of this
def fake_normalization(counter, thing):
return (counter, (), thing)
))
counter += 1
+ arguments_variable = '${}'.format(counter)
+ counter += 1
+
+ prestatements.append(NormalArrayVariableInitializationStatement(
+ variable=arguments_variable,
+ items=tuple(arguments),
+ ))
+
return (
counter,
tuple(prestatements),
NormalFunctionCallExpression(
- expression.function, # TODO Normalize the function
- arguments=tuple(arguments),
+ function=expression.function, # TODO Normalize the function
+ argument_count=len(arguments),
+ argument_items=NormalVariableExpression(variable=arguments_variable),
),
)
}[type(expression)](counter, expression)
def normalize_expression_statement(counter, statement):
+ # TODO Verify all expression types are supported and just call normalize_expression
counter, prestatements, normalized = {
parsing.FurFunctionCallExpression: normalize_function_call_expression,
+ parsing.FurSymbolExpression: normalize_expression,
+ parsing.FurInfixExpression: normalize_expression,
+ parsing.FurIntegerLiteralExpression: normalize_expression,
}[type(statement.expression)](counter, statement.expression)
return (
NormalExpressionStatement(expression=normalized),
)
+def normalize_function_definition_statement(counter, statement):
+ return (
+ counter,
+ (),
+ NormalFunctionDefinitionStatement(
+ name=statement.name,
+ statement_list=normalize_statement_list(statement.statement_list),
+ ),
+ )
+
def normalize_statement(counter, statement):
return {
+ parsing.FurAssignmentStatement: fake_normalization, # TODO unfake this
parsing.FurExpressionStatement: normalize_expression_statement,
- parsing.FurAssignmentStatement: fake_normalization,
+ parsing.FurFunctionDefinitionStatement: normalize_function_definition_statement,
}[type(statement)](counter, statement)
-def normalize(program):
+@util.force_generator(tuple)
+def normalize_statement_list(statement_list):
counter = 0
- statement_list = []
- for statement in program.statement_list:
+ for statement in statement_list:
counter, prestatements, normalized = normalize_statement(counter, statement)
for s in prestatements:
- statement_list.append(s)
- statement_list.append(normalized)
+ yield s
+ yield normalized
+
+def normalize(program):
return NormalProgram(
- statement_list=statement_list,
+ statement_list=normalize_statement_list(program.statement_list),
)