X-Git-Url: https://code.kerkeslager.com/?a=blobdiff_plain;f=transformation.py;h=1a4a8bb5f232d8d445f69ac3f6433b01fc0fdafc;hb=8b306504c3a04a4334d3e8de6abed6c3ce182585;hp=81c1e5d62ed48e7450ee6477b43306eaef4f83d2;hpb=059e6ff380d17a715ffbd2d55ac59e39c931a954;p=fur diff --git a/transformation.py b/transformation.py index 81c1e5d..1a4a8bb 100644 --- a/transformation.py +++ b/transformation.py @@ -134,6 +134,7 @@ CProgram = collections.namedtuple( [ 'builtin_set', 'function_definition_list', + 'operator_declarations', 'statements', 'standard_libraries', 'string_literal_list', @@ -141,47 +142,6 @@ CProgram = collections.namedtuple( ], ) -EQUALITY_LEVEL_OPERATOR_TO_FUNCTION_NAME_MAPPING = { - '==': 'equals', - '!=': 'notEquals', - '<=': 'lessThanOrEqual', - '>=': 'greaterThanOrEqual', - '<': 'lessThan', - '>': 'greaterThan', -} - -def transform_comparison_level_expression(accumulators, expression): - # 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( - accumulators, - expression.left - ) - - middle = left.right - - right = transform_expression( - accumulators, - expression.right, - ) - - # TODO Don't evaluate the middle expression twice - return CFunctionCallForFurInfixOperator( - name='and', - left=left, - right=CFunctionCallForFurInfixOperator( - name=EQUALITY_LEVEL_OPERATOR_TO_FUNCTION_NAME_MAPPING[expression.operator], - left=middle, - right=right, - ), - ) - - return CFunctionCallForFurInfixOperator( - name=EQUALITY_LEVEL_OPERATOR_TO_FUNCTION_NAME_MAPPING[expression.operator], - left=transform_expression(accumulators, expression.left), - right=transform_expression(accumulators, expression.right), - ) - BUILTINS = { 'false': [], 'pow': ['math.h'], @@ -215,22 +175,74 @@ def transform_symbol_expression(accumulators, expression): 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( + accumulators, + expression.left + ) + + middle = left.right + + right = transform_expression( + accumulators, + expression.right, + ) + + # TODO Don't evaluate the middle expression twice + return CFunctionCallForFurInfixOperator( + name='and', + left=left, + right=CFunctionCallForFurInfixOperator( + name=INFIX_OPERATOR_TO_DECLARATION[expression.operator].name, + left=middle, + right=right, + ), + ) + + return CFunctionCallForFurInfixOperator( + name=INFIX_OPERATOR_TO_DECLARATION[expression.operator].name, + left=transform_expression(accumulators, expression.left), + right=transform_expression(accumulators, expression.right), + ) + 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), ) @@ -242,6 +254,11 @@ 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) +def transform_negation_expression(accumulators, expression): + return CNegationExpression( + value=transform_expression(accumulators, expression.internal_expression), + ) + def transform_expression(accumulators, expression): # TODO Clean up handlers for parsing expressions return { @@ -272,11 +289,6 @@ 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 @@ -386,6 +398,7 @@ 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,