projects
/
fur
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Clean up expression transformation a bit
[fur]
/
normalization.py
diff --git
a/normalization.py
b/normalization.py
index
1da6569
..
3fa52ac
100644
(file)
--- a/
normalization.py
+++ b/
normalization.py
@@
-1,6
+1,7
@@
import collections
import parsing
import collections
import parsing
+import util
NormalVariableExpression = collections.namedtuple(
'NormalVariableExpression',
NormalVariableExpression = collections.namedtuple(
'NormalVariableExpression',
@@
-30,7
+31,16
@@
NormalFunctionCallExpression = collections.namedtuple(
'NormalFunctionCallExpression',
[
'function',
'NormalFunctionCallExpression',
[
'function',
- 'arguments',
+ 'argument_count',
+ 'argument_items',
+ ],
+)
+
+NormalArrayVariableInitializationStatement = collections.namedtuple(
+ 'NormalArrayVariableInitializationStatement',
+ [
+ 'variable',
+ 'items',
],
)
],
)
@@
-66,6
+76,14
@@
NormalIfElseStatement = collections.namedtuple(
],
)
],
)
+NormalFunctionDefinitionStatement = collections.namedtuple(
+ 'NormalFunctionDefinitionStatement',
+ [
+ 'name',
+ 'statement_list',
+ ],
+)
+
NormalProgram = collections.namedtuple(
'NormalProgram',
[
NormalProgram = collections.namedtuple(
'NormalProgram',
[
@@
-73,6
+91,7
@@
NormalProgram = collections.namedtuple(
],
)
],
)
+# TODO Get rid of this
def fake_normalization(counter, thing):
return (counter, (), thing)
def fake_normalization(counter, thing):
return (counter, (), thing)
@@
-98,12
+117,21
@@
def normalize_function_call_expression(counter, expression):
))
counter += 1
))
counter += 1
+ arguments_variable = '${}'.format(counter)
+ counter += 1
+
+ prestatements.append(NormalArrayVariableInitializationStatement(
+ variable=arguments_variable,
+ items=tuple(arguments),
+ ))
+
return (
counter,
tuple(prestatements),
NormalFunctionCallExpression(
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),
),
)
),
)
@@
-274,8
+302,12
@@
def normalize_expression(counter, expression):
}[type(expression)](counter, expression)
def normalize_expression_statement(counter, statement):
}[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,
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 (
}[type(statement.expression)](counter, statement.expression)
return (
@@
-284,22
+316,35
@@
def normalize_expression_statement(counter, statement):
NormalExpressionStatement(expression=normalized),
)
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 {
def normalize_statement(counter, statement):
return {
+ parsing.FurAssignmentStatement: fake_normalization, # TODO unfake this
parsing.FurExpressionStatement: normalize_expression_statement,
parsing.FurExpressionStatement: normalize_expression_statement,
- parsing.Fur
AssignmentStatement: fake_normalization
,
+ parsing.Fur
FunctionDefinitionStatement: normalize_function_definition_statement
,
}[type(statement)](counter, statement)
}[type(statement)](counter, statement)
-def normalize(program):
+@util.force_generator(tuple)
+def normalize_statement_list(statement_list):
counter = 0
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:
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(
return NormalProgram(
- statement_list=
statement_list
,
+ statement_list=
normalize_statement_list(program.statement_list)
,
)
)