X-Git-Url: https://code.kerkeslager.com/?a=blobdiff_plain;f=transformation.py;h=1a4a8bb5f232d8d445f69ac3f6433b01fc0fdafc;hb=c9f47106ccf806533783a551b5c51d9280869635;hp=9784c155eb72c92fc15a792c14535af2f2439d79;hpb=dee52f20ded67843ceac0e92ee2038f8aaf21cc4;p=fur diff --git a/transformation.py b/transformation.py index 9784c15..1a4a8bb 100644 --- a/transformation.py +++ b/transformation.py @@ -114,10 +114,27 @@ CIfElseStatement = collections.namedtuple( ], ) +CFunctionDeclaration = collections.namedtuple( + 'CFunctionDeclaration', + [ + 'name', + ], +) + +CFunctionDefinition = collections.namedtuple( + 'CFunctionDefinition', + [ + 'name', + 'statement_list', + ], +) + CProgram = collections.namedtuple( 'CProgram', [ 'builtin_set', + 'function_definition_list', + 'operator_declarations', 'statements', 'standard_libraries', 'string_literal_list', @@ -125,16 +142,68 @@ CProgram = collections.namedtuple( ], ) -EQUALITY_LEVEL_OPERATOR_TO_FUNCTION_NAME_MAPPING = { - '==': 'equals', - '!=': 'notEquals', - '<=': 'lessThanOrEqual', - '>=': 'greaterThanOrEqual', - '<': 'lessThan', - '>': 'greaterThan', +BUILTINS = { + 'false': [], + 'pow': ['math.h'], + 'print': ['stdio.h'], + 'true': [], +} + +def transform_variable_expression(accumulators, expression): + return CVariableExpression(variable=expression.variable) + +def transform_string_literal(accumulators, expression): + value = expression.value + + try: + index = accumulators.string_literal_list.index(value) + except ValueError: + index = len(accumulators.string_literal_list) + accumulators.string_literal_list.append(value) + + return CStringLiteral(index=index, value=value) + +def transform_symbol_expression(accumulators, expression): + if expression.value in ['true', 'false']: + return CConstantExpression(value=expression.value) + + if expression.value not in accumulators.symbol_list: + symbol_list.append(expression.value) + + return CSymbolExpression( + symbol=expression.value, + symbol_list_index=accumulators.symbol_list.index(expression.value), + ) + +CInfixDeclaration = collections.namedtuple( + 'CInfixDeclaration', + [ + 'name', + 'in_type', + 'out_type', + 'operator', + ], +) + +INFIX_OPERATOR_TO_DECLARATION = { + '+': CInfixDeclaration(name='add', in_type='integer', out_type='integer', operator='+'), + '-': CInfixDeclaration(name='subtract', in_type='integer', out_type='integer', operator='-'), + '*': CInfixDeclaration(name='multiply', in_type='integer', out_type='integer', operator='*'), + '//': CInfixDeclaration(name='integerDivide', in_type='integer', out_type='integer', operator='/'), + '%': CInfixDeclaration(name='modularDivide', in_type='integer', out_type='integer', operator='%'), + 'and': CInfixDeclaration(name='and', in_type='boolean', out_type='boolean', operator='&&'), + 'or': CInfixDeclaration(name='or', in_type='boolean', out_type='boolean', operator='||'), + '==': CInfixDeclaration(name='equals', in_type='integer', out_type='boolean', operator='=='), + '!=': CInfixDeclaration(name='notEquals', in_type='integer', out_type='boolean', operator='!='), + '<=': CInfixDeclaration(name='lessThanOrEqual', in_type='integer', out_type='boolean', operator='<='), + '>=': CInfixDeclaration(name='greaterThanOrEqual', in_type='integer', out_type='boolean', operator='>='), + '<': CInfixDeclaration(name='lessThan', in_type='integer', out_type='boolean', operator='<'), + '>': CInfixDeclaration(name='greaterThan', in_type='integer', out_type='boolean', operator='>'), } def transform_comparison_level_expression(accumulators, expression): + accumulators.operator_set.add(INFIX_OPERATOR_TO_DECLARATION[expression.operator]) + # Transform expressions like 1 < 2 < 3 into expressions like 1 < 2 && 2 < 3 if isinstance(expression.left, parsing.FurInfixExpression) and expression.left.order == 'comparison_level': left = transform_comparison_level_expression( @@ -154,92 +223,52 @@ def transform_comparison_level_expression(accumulators, expression): name='and', left=left, right=CFunctionCallForFurInfixOperator( - name=EQUALITY_LEVEL_OPERATOR_TO_FUNCTION_NAME_MAPPING[expression.operator], + name=INFIX_OPERATOR_TO_DECLARATION[expression.operator].name, left=middle, right=right, ), ) return CFunctionCallForFurInfixOperator( - name=EQUALITY_LEVEL_OPERATOR_TO_FUNCTION_NAME_MAPPING[expression.operator], + name=INFIX_OPERATOR_TO_DECLARATION[expression.operator].name, left=transform_expression(accumulators, expression.left), right=transform_expression(accumulators, expression.right), ) -BUILTINS = { - 'false': [], - 'pow': ['math.h'], - 'print': ['stdio.h'], - 'true': [], -} - -def transform_variable_expression(accumulators, expression): - return CVariableExpression(variable=expression.variable) - def transform_infix_expression(accumulators, expression): if expression.order == 'comparison_level': return transform_comparison_level_expression(accumulators, expression) - INFIX_OPERATOR_TO_FUNCTION_NAME = { - '+': 'add', - '-': 'subtract', - '*': 'multiply', - '//': 'integerDivide', - '%': 'modularDivide', - 'and': 'and', - 'or': 'or', - } + accumulators.operator_set.add(INFIX_OPERATOR_TO_DECLARATION[expression.operator]) return CFunctionCallForFurInfixOperator( - name=INFIX_OPERATOR_TO_FUNCTION_NAME[expression.operator], + name=INFIX_OPERATOR_TO_DECLARATION[expression.operator].name, left=transform_expression(accumulators, expression.left), right=transform_expression(accumulators, expression.right), ) -def transform_expression(accumulators, expression): - if isinstance(expression, parsing.FurParenthesizedExpression): - # Parentheses can be removed because everything in the C output is explicitly parenthesized - return transform_expression(accumulators, expression.internal) - - if isinstance(expression, parsing.FurNegationExpression): - return transform_negation_expression(accumulators, expression) +def transform_integer_literal_expression(accumulators, expression): + return CIntegerLiteral(value=expression.value) - if isinstance(expression, parsing.FurFunctionCallExpression): - return transform_function_call_expression(accumulators, expression) +def transform_parenthesized_expression(accumulators, expression): + # Parentheses can be removed because everything in the C output is explicitly parenthesized + return transform_expression(accumulators, expression.internal) - if isinstance(expression, parsing.FurSymbolExpression): - if expression.value in ['true', 'false']: - return CConstantExpression(value=expression.value) - - if expression.value not in accumulators.symbol_list: - symbol_list.append(expression.value) - - return CSymbolExpression( - symbol=expression.value, - symbol_list_index=accumulators.symbol_list.index(expression.value), - ) - - if isinstance(expression, parsing.FurStringLiteralExpression): - value = expression.value - - try: - index = accumulators.string_literal_list.index(value) - except ValueError: - index = len(accumulators.string_literal_list) - accumulators.string_literal_list.append(value) - - return CStringLiteral(index=index, value=value) - - LITERAL_TYPE_MAPPING = { - parsing.FurIntegerLiteralExpression: CIntegerLiteral, - } - - if type(expression) in LITERAL_TYPE_MAPPING: - return LITERAL_TYPE_MAPPING[type(expression)](value=expression.value) +def transform_negation_expression(accumulators, expression): + return CNegationExpression( + value=transform_expression(accumulators, expression.internal_expression), + ) - # TODO Handle all possible types in this form +def transform_expression(accumulators, expression): + # TODO Clean up handlers for parsing expressions return { - parsing.FurInfixExpression: transform_infix_expression, # TODO Shouldn't need this + parsing.FurFunctionCallExpression: transform_function_call_expression, + parsing.FurInfixExpression: transform_infix_expression, + parsing.FurIntegerLiteralExpression: transform_integer_literal_expression, + parsing.FurNegationExpression: transform_negation_expression, + parsing.FurParenthesizedExpression: transform_parenthesized_expression, + parsing.FurStringLiteralExpression: transform_string_literal, + parsing.FurSymbolExpression: transform_symbol_expression, normalization.NormalFunctionCallExpression: transform_function_call_expression, normalization.NormalInfixExpression: transform_infix_expression, normalization.NormalNegationExpression: transform_negation_expression, @@ -260,28 +289,27 @@ def transform_symbol_assignment_statement(accumulators, assignment_statement): ), ) -def transform_negation_expression(accumulators, expression): - return CNegationExpression( - value=transform_expression(accumulators, expression.internal_expression), - ) - def transform_function_call_expression(accumulators, function_call): if function_call.function.value in BUILTINS.keys(): # TODO Check that the builtin is actually callable accumulators.builtin_set.add(function_call.function.value) - return CFunctionCallExpression( - name='builtin${}.instance.closure'.format(function_call.function.value), - argument_count=function_call.argument_count, - argument_items=transform_expression(accumulators, function_call.argument_items), - ) - - raise Exception() + # TODO Use the symbol from SYMBOL LIST + return CFunctionCallExpression( + name=function_call.function.value, + argument_count=function_call.argument_count, + argument_items=transform_expression(accumulators, function_call.argument_items), + ) def transform_expression_statement(accumulators, statement): + # TODO At some point we can verify that all expression types are supported and just call transform_expression expression = { parsing.FurFunctionCallExpression: transform_function_call_expression, + parsing.FurInfixExpression: transform_expression, + parsing.FurIntegerLiteralExpression: transform_expression, + parsing.FurSymbolExpression: transform_expression, normalization.NormalFunctionCallExpression: transform_function_call_expression, + normalization.NormalVariableExpression: transform_expression, }[type(statement.expression)](accumulators, statement.expression) return CExpressionStatement( @@ -313,13 +341,26 @@ def transform_variable_reassignment_statement(accumulators, statement): expression=transform_expression(accumulators, statement.expression), ) +def transform_function_definition_statement(accumulators, statement): + # TODO Allow defining the same function in different contexts + if any(fd.name == statement.name for fd in accumulators.function_definition_list): + raise Exception('A function with name "{}" already exists'.format(statement.name)) + + accumulators.function_definition_list.append(CFunctionDefinition( + name=statement.name, + statement_list=tuple(transform_statement(accumulators, s) for s in statement.statement_list) + )) + + return CFunctionDeclaration(name=statement.name) + def transform_statement(accumulators, statement): return { parsing.FurAssignmentStatement: transform_symbol_assignment_statement, parsing.FurExpressionStatement: transform_expression_statement, + normalization.NormalArrayVariableInitializationStatement: transform_array_variable_initialization_statement, normalization.NormalExpressionStatement: transform_expression_statement, + normalization.NormalFunctionDefinitionStatement: transform_function_definition_statement, normalization.NormalIfElseStatement: transform_if_else_statement, - normalization.NormalArrayVariableInitializationStatement: transform_array_variable_initialization_statement, normalization.NormalVariableInitializationStatement: transform_variable_initialization_statement, normalization.NormalVariableReassignmentStatement: transform_variable_reassignment_statement, }[type(statement)](accumulators, statement) @@ -329,6 +370,8 @@ Accumulators = collections.namedtuple( 'Accumulators', [ 'builtin_set', + 'function_definition_list', + 'operator_set', 'symbol_list', 'string_literal_list', ], @@ -337,6 +380,8 @@ Accumulators = collections.namedtuple( def transform(program): accumulators = Accumulators( builtin_set=set(), + function_definition_list=[], + operator_set=set(), symbol_list=[], string_literal_list=[], ) @@ -352,6 +397,8 @@ def transform(program): return CProgram( builtin_set=accumulators.builtin_set, + function_definition_list=accumulators.function_definition_list, + operator_declarations=tuple(sorted(accumulators.operator_set)), statements=statement_list, standard_libraries=standard_library_set, string_literal_list=accumulators.string_literal_list,