From f378cbf85e3dbd26d5a97b6ac9b1aeb0811ecf74 Mon Sep 17 00:00:00 2001 From: David Kerkeslager Date: Fri, 27 Sep 2019 20:36:28 -0400 Subject: [PATCH] Generate structures --- conversion.py | 29 +++++------ crossplatform_ir_generation.py | 20 ++++++++ normalization.py | 90 ++++++++++------------------------ 3 files changed, 57 insertions(+), 82 deletions(-) diff --git a/conversion.py b/conversion.py index b71e834..2b6ccbd 100644 --- a/conversion.py +++ b/conversion.py @@ -45,8 +45,6 @@ CPSStructureLiteralExpression = collections.namedtuple( 'CPSStructureLiteralExpression', ( 'field_count', - 'symbol_list_variable', - 'value_list_variable', ), ) @@ -57,6 +55,13 @@ CPSSymbolExpression = collections.namedtuple( ), ) +CPSSymbolLiteralExpression = collections.namedtuple( + 'CPSSymbolLiteralExpression', + ( + 'symbol', + ), +) + CPSVariableExpression = collections.namedtuple( 'CPSVariableExpression', ( @@ -160,13 +165,14 @@ def convert_string_literal_expression(expression): 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_symbol_literal_expression(expression): + return CPSSymbolLiteralExpression(symbol=expression.symbol) + def convert_variable_expression(expression): return CPSVariableExpression(variable=expression.variable) @@ -180,15 +186,10 @@ def convert_expression(expression): normalization.NormalStringLiteralExpression: convert_string_literal_expression, normalization.NormalStructureLiteralExpression: convert_structure_literal_expression, normalization.NormalSymbolExpression: convert_symbol_expression, + normalization.NormalSymbolLiteralExpression: convert_symbol_literal_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, @@ -228,21 +229,13 @@ def convert_variable_initialization_statement(statement): 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.NormalListAppendStatement: convert_list_append_statement, normalization.NormalPushStatement: convert_push_statement, normalization.NormalVariableInitializationStatement: convert_variable_initialization_statement, - normalization.NormalSymbolArrayVariableInitializationStatement: convert_symbol_array_variable_initialization_statement, }[type(statement)](statement) def convert_statement_list(statement_list): diff --git a/crossplatform_ir_generation.py b/crossplatform_ir_generation.py index 9d3ab4b..fad6222 100644 --- a/crossplatform_ir_generation.py +++ b/crossplatform_ir_generation.py @@ -114,6 +114,15 @@ def generate_string_literal_expression(counters, expression): return referenced_entry_list, instruction_list +def generate_structure_literal_expression(counters, expression): + referenced_entry_list = () + instruction_list = (CIRInstruction( + instruction='structure', + argument=expression.field_count, + ),) + + return referenced_entry_list, instruction_list + def generate_symbol_expression(counters, expression): referenced_entry_list = () instruction_list = (CIRInstruction( @@ -123,6 +132,15 @@ def generate_symbol_expression(counters, expression): return referenced_entry_list, instruction_list +def generate_symbol_literal_expression(counters, expression): + referenced_entry_list = () + instruction_list = (CIRInstruction( + instruction='push_value', + argument=generate_symbol_literal(expression.symbol), + ),) + + return referenced_entry_list, instruction_list + def generate_variable_expression(counters, expression): referenced_entry_list = () instruction_list = (CIRInstruction( @@ -140,7 +158,9 @@ def generate_expression(counters, expression): conversion.CPSLambdaExpression: generate_lambda_expression, conversion.CPSListConstructExpression: generate_list_construct_expression, conversion.CPSStringLiteralExpression: generate_string_literal_expression, + conversion.CPSStructureLiteralExpression: generate_structure_literal_expression, conversion.CPSSymbolExpression: generate_symbol_expression, + conversion.CPSSymbolLiteralExpression: generate_symbol_literal_expression, conversion.CPSVariableExpression: generate_variable_expression, }[type(expression)](counters, expression) diff --git a/normalization.py b/normalization.py index 309e84c..e6fea77 100644 --- a/normalization.py +++ b/normalization.py @@ -35,9 +35,16 @@ NormalStringLiteralExpression = collections.namedtuple( NormalSymbolExpression = collections.namedtuple( 'NormalSymbolExpression', - [ + ( 'symbol', - ], + ), +) + +NormalSymbolLiteralExpression = collections.namedtuple( + 'NormalSymbolLiteralExpression', + ( + 'symbol', + ), ) NormalPushStatement = collections.namedtuple( @@ -56,22 +63,6 @@ NormalFunctionCallExpression = collections.namedtuple( ], ) -NormalArrayVariableInitializationStatement = collections.namedtuple( - 'NormalArrayVariableInitializationStatement', - [ - 'variable', - 'items', - ], -) - -NormalSymbolArrayVariableInitializationStatement = collections.namedtuple( - 'NormalSymbolArrayVariableInitializationStatement', - [ - 'variable', - 'symbol_list', - ], -) - NormalVariableInitializationStatement = collections.namedtuple( 'NormalVariableInitializationStatement', [ @@ -192,70 +183,41 @@ def normalize_string_literal_expression(counter, expression): NormalStructureLiteralExpression = collections.namedtuple( 'NormalStructureLiteralExpression', - [ + ( 'field_count', - 'symbol_list_variable', - 'value_list_variable', - ], + ), ) def normalize_structure_literal_expression(counter, expression): prestatements = [] - field_symbol_array = [] - field_value_array = [] - for symbol_expression_pair in expression.fields: - counter, field_prestatements, field_expression = normalize_expression( + for field in expression.fields: + counter, field_expression_prestatements, field_expression = normalize_expression( counter, - symbol_expression_pair.expression, + field.expression, ) - for p in field_prestatements: + for p in field_expression_prestatements: prestatements.append(p) - field_symbol_array.append(symbol_expression_pair.symbol) - field_value_array.append(field_expression) + prestatements.append(NormalPushStatement( + expression=field_expression, + )) - symbol_array_variable = '${}'.format(counter) - counter += 1 - - prestatements.append( - NormalSymbolArrayVariableInitializationStatement( - variable=symbol_array_variable, - symbol_list=tuple(field_symbol_array), - ) - ) - - value_array_variable = '${}'.format(counter) - counter += 1 - - prestatements.append( - NormalArrayVariableInitializationStatement( - variable=value_array_variable, - items=tuple(field_value_array), - ) - ) - - variable = '${}'.format(counter) - - prestatements.append( - NormalVariableInitializationStatement( - variable=variable, - expression=NormalStructureLiteralExpression( - field_count=len(expression.fields), - symbol_list_variable=symbol_array_variable, - value_list_variable=value_array_variable, + prestatements.append(NormalPushStatement( + expression=NormalSymbolLiteralExpression( + symbol=field.symbol, ), - ) - ) + )) return ( - counter + 1, + counter, tuple(prestatements), - NormalVariableExpression(variable=variable), + NormalStructureLiteralExpression( + field_count=len(expression.fields), + ), ) - def normalize_symbol_expression(counter, expression): return ( counter, -- 2.20.1