X-Git-Url: https://code.kerkeslager.com/?a=blobdiff_plain;f=normalization.py;h=71f1fbadca7f38c41cf319431b1c744c565b73d6;hb=e60f2b20f17609da9b093ac1e08d688bf87bfb4a;hp=9fdabd9391bddcbe0c3d9b62f1e9f6e19eeaab79;hpb=521bb843144e1c835f9eeff8172eea580ef02c4d;p=fur diff --git a/normalization.py b/normalization.py index 9fdabd9..71f1fba 100644 --- a/normalization.py +++ b/normalization.py @@ -17,6 +17,15 @@ NormalIntegerLiteralExpression = collections.namedtuple( ], ) +NormalLambdaExpression = collections.namedtuple( + 'NormalLambdaExpression', + ( + 'name', + 'argument_name_list', + 'statement_list', + ), +) + NormalStringLiteralExpression = collections.namedtuple( 'NormalStringLiteralExpression', [ @@ -103,15 +112,6 @@ NormalIfElseStatement = collections.namedtuple( ], ) -NormalFunctionDefinitionStatement = collections.namedtuple( - 'NormalFunctionDefinitionStatement', - [ - 'name', - 'argument_name_list', - 'statement_list', - ], -) - NormalProgram = collections.namedtuple( 'NormalProgram', [ @@ -120,13 +120,31 @@ NormalProgram = collections.namedtuple( ) def normalize_integer_literal_expression(counter, expression): + return ( + counter, + (), + NormalIntegerLiteralExpression(integer=expression.integer), + ) + +def normalize_lambda_expression(counter, expression): variable = '${}'.format(counter) + + _, statement_list = normalize_statement_list( + 0, + expression.statement_list, + assign_result_to='result', + ) + return ( counter + 1, ( NormalVariableInitializationStatement( variable=variable, - expression=NormalIntegerLiteralExpression(integer=expression.integer), + expression=NormalLambdaExpression( + name=expression.name, + argument_name_list=expression.argument_name_list, + statement_list=statement_list, + ), ), ), NormalVariableExpression(variable=variable), @@ -183,16 +201,10 @@ def normalize_list_literal_expression(counter, expression): ) def normalize_string_literal_expression(counter, expression): - variable = '${}'.format(counter) return ( - counter + 1, - ( - NormalVariableInitializationStatement( - variable=variable, - expression=NormalStringLiteralExpression(string=expression.string), - ), - ), - NormalVariableExpression(variable=variable), + counter, + (), + NormalStringLiteralExpression(string=expression.string), ) NormalStructureLiteralExpression = collections.namedtuple( @@ -262,16 +274,10 @@ def normalize_structure_literal_expression(counter, expression): def normalize_symbol_expression(counter, expression): - variable = '${}'.format(counter) return ( - counter + 1, - ( - NormalVariableInitializationStatement( - variable=variable, - expression=NormalSymbolExpression(symbol=expression.symbol), - ), - ), - NormalVariableExpression(variable=variable), + counter, + (), + NormalSymbolExpression(symbol=expression.symbol), ) def normalize_function_call_expression(counter, expression): @@ -283,21 +289,11 @@ def normalize_function_call_expression(counter, expression): for s in argument_prestatements: prestatements.append(s) - variable = '${}'.format(counter) - prestatements.append( - NormalVariableInitializationStatement( - variable=variable, - expression=normalized_argument, - ) - ) prestatements.append( NormalPushStatement( - expression=NormalVariableExpression( - variable=variable, - ), + expression=normalized_argument, ), ) - counter += 1 counter, function_prestatements, function_expression = normalize_expression( counter, @@ -307,19 +303,6 @@ def normalize_function_call_expression(counter, expression): for ps in function_prestatements: prestatements.append(ps) - if not isinstance(function_expression, NormalVariableExpression): - function_variable = '${}'.format(counter) - - prestatements.append( - NormalVariableInitializationStatement( - variable=function_variable, - expression=function_expression, - ) - ) - - function_expression = NormalVariableExpression(variable=function_variable) - counter += 1 - result_variable = '${}'.format(counter) prestatements.append( @@ -380,6 +363,7 @@ def normalize_expression(counter, expression): desugaring.DesugaredFunctionCallExpression: normalize_function_call_expression, desugaring.DesugaredIfExpression: normalize_if_expression, desugaring.DesugaredIntegerLiteralExpression: normalize_integer_literal_expression, + desugaring.DesugaredLambdaExpression: normalize_lambda_expression, desugaring.DesugaredListLiteralExpression: normalize_list_literal_expression, desugaring.DesugaredStringLiteralExpression: normalize_string_literal_expression, desugaring.DesugaredStructureLiteralExpression: normalize_structure_literal_expression, @@ -399,22 +383,6 @@ def normalize_expression_statement(counter, statement): NormalExpressionStatement(expression=normalized), ) -def normalize_function_definition_statement(counter, statement): - _, statement_list = normalize_statement_list( - 0, - statement.statement_list, - assign_result_to='result', - ) - return ( - counter, - (), - NormalFunctionDefinitionStatement( - name=statement.name, - argument_name_list=statement.argument_name_list, - statement_list=statement_list, - ), - ) - def normalize_assignment_statement(counter, statement): counter, prestatements, normalized_expression = normalize_expression(counter, statement.expression) return ( @@ -430,7 +398,6 @@ def normalize_statement(counter, statement): return { desugaring.DesugaredAssignmentStatement: normalize_assignment_statement, desugaring.DesugaredExpressionStatement: normalize_expression_statement, - desugaring.DesugaredFunctionDefinitionStatement: normalize_function_definition_statement, }[type(statement)](counter, statement) @util.force_generator(tuple)