From 68eb624cc15463008b9ed38e17315e9a1f8bdeb0 Mon Sep 17 00:00:00 2001 From: David Kerkeslager Date: Thu, 4 Jan 2018 16:31:21 -0500 Subject: [PATCH] Added a CPS conversion pass (which currently does not actually convert) --- conversion.py | 252 +++++++++++++++++++++++++++++++++++++++++++++- transformation.py | 38 +++---- 2 files changed, 270 insertions(+), 20 deletions(-) diff --git a/conversion.py b/conversion.py index 8c47a4d..965f86e 100644 --- a/conversion.py +++ b/conversion.py @@ -1,5 +1,140 @@ import collections +import normalization + +CPSFunctionCallExpression = collections.namedtuple( + 'CPSFunctionCallExpression', + ( + 'metadata', + 'function_expression', + 'argument_count', + ), +) + +CPSIntegerLiteralExpression = collections.namedtuple( + 'CPSIntegerLiteralExpression', + ( + 'integer', + ), +) + +CPSListConstructExpression = collections.namedtuple( + 'CPSListConstructExpression', + ( + 'allocate', + ), +) + +CPSStringLiteralExpression = collections.namedtuple( + 'CPSStringLiteralExpression', + ( + 'string', + ), +) + +CPSStructureLiteralExpression = collections.namedtuple( + 'CPSStructureLiteralExpression', + ( + 'field_count', + 'symbol_list_variable', + 'value_list_variable', + ), +) + +CPSSymbolExpression = collections.namedtuple( + 'CPSSymbolExpression', + ( + 'symbol', + ), +) + +CPSVariableExpression = collections.namedtuple( + 'CPSVariableExpression', + ( + 'variable', + ), +) + +CPSArrayVariableInitializationStatement = collections.namedtuple( + 'CPSArrayVariableInitializationStatement', + ( + 'variable', + 'items', + ), +) + +CPSAssignmentStatement = collections.namedtuple( + 'CPSAssignmentStatement', + ( + 'target', + 'expression', + ), +) + +CPSExpressionStatement = collections.namedtuple( + 'CPSExpressionStatement', + ( + 'expression', + ), +) + +CPSFunctionDefinitionStatement = collections.namedtuple( + 'CPSFunctionDefinitionStatement', + ( + 'name', + 'argument_name_list', + 'statement_list', + ) +) + +CPSIfElseStatement = collections.namedtuple( + 'CPSIfElseStatement', + ( + 'condition_expression', + 'if_statement_list', + 'else_statement_list', + ), +) + +CPSListAppendStatement = collections.namedtuple( + 'CPSListAppendStatement', + ( + 'list_expression', + 'item_expression', + ), +) + +CPSPushStatement = collections.namedtuple( + 'CPSPushStatement', + ( + 'expression', + ), +) + +CPSVariableInitializationStatement = collections.namedtuple( + 'CPSVariableInitializationStatement', + ( + 'variable', + 'expression', + ), +) + +CPSSymbolArrayVariableInitializationStatement = collections.namedtuple( + 'CPSSymbolArrayVariableInitializationStatement', + ( + 'variable', + 'symbol_list', + ), +) + +CPSVariableReassignmentStatement = collections.namedtuple( + 'CPSVariableReassignmentStatement', + ( + 'variable', + 'expression', + ), +) + CPSProgram = collections.namedtuple( 'CPSProgram', ( @@ -7,7 +142,122 @@ CPSProgram = collections.namedtuple( ), ) +def convert_function_call_expression(expression): + return CPSFunctionCallExpression( + metadata=expression.metadata, + function_expression=convert_expression(expression.function_expression), + argument_count=expression.argument_count, + ) + +def convert_integer_literal_expression(expression): + return CPSIntegerLiteralExpression(integer=expression.integer) + +def convert_list_construct_expression(expression): + return CPSListConstructExpression(allocate=expression.allocate) + +def convert_string_literal_expression(expression): + return CPSStringLiteralExpression(string=expression.string) + +def convert_structure_literal_expression(expression): + return CPSStructureLiteralExpression( + field_count=expression.field_count, + symbol_list_variable=expression.symbol_list_variable, + value_list_variable=expression.value_list_variable, + ) + +def convert_symbol_expression(expression): + return CPSSymbolExpression(symbol=expression.symbol) + +def convert_variable_expression(expression): + return CPSVariableExpression(variable=expression.variable) + +def convert_expression(expression): + return { + normalization.NormalFunctionCallExpression: convert_function_call_expression, + normalization.NormalIntegerLiteralExpression: convert_integer_literal_expression, + normalization.NormalListConstructExpression: convert_list_construct_expression, + normalization.NormalStringLiteralExpression: convert_string_literal_expression, + normalization.NormalStructureLiteralExpression: convert_structure_literal_expression, + normalization.NormalSymbolExpression: convert_symbol_expression, + normalization.NormalVariableExpression: convert_variable_expression, + }[type(expression)](expression) + +def convert_array_variable_initialization_statement(statement): + return CPSArrayVariableInitializationStatement( + variable=statement.variable, + items=tuple(convert_expression(e) for e in statement.items), + ) + +def convert_assignment_statement(statement): + return CPSAssignmentStatement( + target=statement.target, + expression=convert_expression(statement.expression), + ) + +def convert_expression_statement(statement): + return CPSExpressionStatement( + expression=convert_expression(statement.expression), + ) + +def convert_function_definition_statement(statement): + return CPSFunctionDefinitionStatement( + name=statement.name, + argument_name_list=statement.argument_name_list, + statement_list=tuple(convert_statement(s) for s in statement.statement_list), + ) + +def convert_if_else_statement(statement): + return CPSIfElseStatement( + condition_expression=convert_expression(statement.condition_expression), + if_statement_list=tuple(convert_statement(s) for s in statement.if_statement_list), + else_statement_list=tuple(convert_statement(s) for s in statement.else_statement_list), + ) + +def convert_list_append_statement(statement): + return CPSListAppendStatement( + list_expression=convert_expression(statement.list_expression), + item_expression=convert_expression(statement.item_expression), + ) + + +def convert_push_statement(statement): + return CPSPushStatement( + expression=convert_expression(statement.expression), + ) + +def convert_variable_initialization_statement(statement): + return CPSVariableInitializationStatement( + variable=statement.variable, + expression=convert_expression(statement.expression), + ) + +def convert_variable_reassignment_statement(statement): + return CPSVariableReassignmentStatement( + variable=statement.variable, + expression=convert_expression(statement.expression), + ) + +def convert_symbol_array_variable_initialization_statement(statement): + return CPSSymbolArrayVariableInitializationStatement( + variable=statement.variable, + symbol_list=statement.symbol_list, + ) + +def convert_statement(statement): + return { + normalization.NormalArrayVariableInitializationStatement: convert_array_variable_initialization_statement, + normalization.NormalAssignmentStatement: convert_assignment_statement, + normalization.NormalExpressionStatement: convert_expression_statement, + normalization.NormalFunctionDefinitionStatement: convert_function_definition_statement, + normalization.NormalIfElseStatement: convert_if_else_statement, + normalization.NormalListAppendStatement: convert_list_append_statement, + normalization.NormalPushStatement: convert_push_statement, + normalization.NormalVariableInitializationStatement: convert_variable_initialization_statement, + normalization.NormalVariableReassignmentStatement: convert_variable_reassignment_statement, + normalization.NormalSymbolArrayVariableInitializationStatement: convert_symbol_array_variable_initialization_statement, + }[type(statement)](statement) + def convert(program): return CPSProgram( - statement_list=program.statement_list, + statement_list=tuple(convert_statement(s) for s in program.statement_list), ) diff --git a/transformation.py b/transformation.py index eb6d9cb..56b61f3 100644 --- a/transformation.py +++ b/transformation.py @@ -1,7 +1,7 @@ import collections +import conversion import normalization -import parsing # TODO Remove this import, as we should be normalizing everything before it gets here CIntegerLiteral = collections.namedtuple( 'CIntegerLiteral', @@ -157,7 +157,7 @@ BUILTINS = { } def transform_variable_expression(accumulators, expression): - assert isinstance(expression, normalization.NormalVariableExpression) + assert isinstance(expression, conversion.CPSVariableExpression) return CVariableExpression(variable=expression.variable) def transform_string_literal_expression(accumulators, expression): @@ -236,14 +236,14 @@ def transform_list_append_statement(accumulators, expression): def transform_expression(accumulators, expression): return { - normalization.NormalFunctionCallExpression: transform_function_call_expression, - normalization.NormalIntegerLiteralExpression: transform_integer_literal_expression, - normalization.NormalListConstructExpression: transform_list_construct_expression, + conversion.CPSFunctionCallExpression: transform_function_call_expression, + conversion.CPSIntegerLiteralExpression: transform_integer_literal_expression, + conversion.CPSListConstructExpression: transform_list_construct_expression, normalization.NormalListGetExpression: transform_list_get_expression, - normalization.NormalStructureLiteralExpression: transform_structure_literal_expression, - normalization.NormalStringLiteralExpression: transform_string_literal_expression, - normalization.NormalSymbolExpression: transform_symbol_expression, - normalization.NormalVariableExpression: transform_variable_expression, + conversion.CPSStructureLiteralExpression: transform_structure_literal_expression, + conversion.CPSStringLiteralExpression: transform_string_literal_expression, + conversion.CPSSymbolExpression: transform_symbol_expression, + conversion.CPSVariableExpression: transform_variable_expression, }[type(expression)](accumulators, expression) def transform_symbol_assignment_statement(accumulators, assignment_statement): @@ -338,16 +338,16 @@ def transform_push_statement(accumulators, statement): def transform_statement(accumulators, statement): return { - normalization.NormalArrayVariableInitializationStatement: transform_array_variable_initialization_statement, - normalization.NormalAssignmentStatement: transform_symbol_assignment_statement, - normalization.NormalExpressionStatement: transform_expression_statement, - normalization.NormalFunctionDefinitionStatement: transform_function_definition_statement, - normalization.NormalIfElseStatement: transform_if_else_statement, - normalization.NormalListAppendStatement: transform_list_append_statement, - normalization.NormalPushStatement: transform_push_statement, - normalization.NormalSymbolArrayVariableInitializationStatement: transform_symbol_array_variable_initialization_statement, - normalization.NormalVariableInitializationStatement: transform_variable_initialization_statement, - normalization.NormalVariableReassignmentStatement: transform_variable_reassignment_statement, + conversion.CPSArrayVariableInitializationStatement: transform_array_variable_initialization_statement, + conversion.CPSAssignmentStatement: transform_symbol_assignment_statement, + conversion.CPSExpressionStatement: transform_expression_statement, + conversion.CPSFunctionDefinitionStatement: transform_function_definition_statement, + conversion.CPSIfElseStatement: transform_if_else_statement, + conversion.CPSListAppendStatement: transform_list_append_statement, + conversion.CPSPushStatement: transform_push_statement, + conversion.CPSSymbolArrayVariableInitializationStatement: transform_symbol_array_variable_initialization_statement, + conversion.CPSVariableInitializationStatement: transform_variable_initialization_statement, + conversion.CPSVariableReassignmentStatement: transform_variable_reassignment_statement, }[type(statement)](accumulators, statement) -- 2.20.1