From: David Kerkeslager Date: Sun, 6 Aug 2017 19:43:14 +0000 (-0400) Subject: Get function names for operators in transformation phase rather than generation phase X-Git-Url: https://code.kerkeslager.com/?a=commitdiff_plain;h=582116dd2ac470acf1d08ba58cd4fd52697035d5;p=fur Get function names for operators in transformation phase rather than generation phase --- diff --git a/generation.py b/generation.py index 8e93e02..4ac6948 100644 --- a/generation.py +++ b/generation.py @@ -55,26 +55,14 @@ def generate_expression(c_argument): if type(c_argument) in LITERAL_TYPE_MAPPING: return LITERAL_TYPE_MAPPING[type(c_argument)](c_argument) - INFIX_TYPE_MAPPING = { - transformation.CAdditionExpression: 'add', - transformation.CSubtractionExpression: 'subtract', - transformation.CMultiplicationExpression: 'multiply', - transformation.CIntegerDivisionExpression: 'integerDivide', - transformation.CModularDivisionExpression: 'modularDivide', - transformation.CEqualityExpression: 'equals', - transformation.CInequalityExpression: 'notEquals', - transformation.CGreaterThanExpression: 'greaterThan', - transformation.CLessThanExpression: 'lessThan', - transformation.CGreaterThanOrEqualExpression: 'greaterThanOrEqual', - transformation.CLessThanOrEqualExpression: 'lessThanOrEqual', - transformation.CAndExpression: 'and', - } - - return 'builtin${}({}, {})'.format( - INFIX_TYPE_MAPPING[type(c_argument)], - generate_expression(c_argument.left), - generate_expression(c_argument.right), - ) + if isinstance(c_argument, transformation.CFunctionCallForFurInfixOperator): + return 'builtin${}({}, {})'.format( + c_argument.name, + generate_expression(c_argument.left), + generate_expression(c_argument.right), + ) + + raise Exception('Could not handle expresssion "{}"'.format(c_argument)) def generate_negation_expression(c_negation_expression): return 'builtin$negate({})'.format( diff --git a/transformation.py b/transformation.py index e84a4bc..f7d849d 100644 --- a/transformation.py +++ b/transformation.py @@ -38,97 +38,10 @@ CNegationExpression = collections.namedtuple( ], ) -CAdditionExpression = collections.namedtuple( - 'CAdditionExpression', - [ - 'left', - 'right', - ], -) - -CSubtractionExpression = collections.namedtuple( - 'CSubtractionExpression', - [ - 'left', - 'right', - ], -) - -CMultiplicationExpression = collections.namedtuple( - 'CMultiplicationExpression', - [ - 'left', - 'right', - ], -) - -CIntegerDivisionExpression = collections.namedtuple( - 'CIntegerDivisionExpression', - [ - 'left', - 'right', - ], -) - -CEqualityExpression = collections.namedtuple( - 'CEqualityExpression', - [ - 'left', - 'right', - ], -) - -CInequalityExpression = collections.namedtuple( - 'CInequalityExpression', - [ - 'left', - 'right', - ], -) - -CGreaterThanOrEqualExpression = collections.namedtuple( - 'CGreaterThanOrEqualExpression', - [ - 'left', - 'right', - ], -) - -CLessThanOrEqualExpression = collections.namedtuple( - 'CLessThanOrEqualExpression', - [ - 'left', - 'right', - ], -) - -CGreaterThanExpression = collections.namedtuple( - 'CGreaterThanExpression', - [ - 'left', - 'right', - ], -) - -CLessThanExpression = collections.namedtuple( - 'CLessThanExpression', - [ - 'left', - 'right', - ], -) - -CAndExpression = collections.namedtuple( - 'CAndExpression', - [ - 'left', - 'right', - ], -) - -CModularDivisionExpression = collections.namedtuple( - 'CModularDivisionExpression', +CFunctionCallForFurInfixOperator = collections.namedtuple( + 'CFunctionCallForFurInfixOperator', [ + 'name', 'left', 'right', ], @@ -161,13 +74,13 @@ CProgram = collections.namedtuple( ], ) -EQUALITY_LEVEL_OPERATOR_MAPPING = { - '==': CEqualityExpression, - '!=': CInequalityExpression, - '<=': CLessThanOrEqualExpression, - '>=': CGreaterThanOrEqualExpression, - '<': CLessThanExpression, - '>': CGreaterThanExpression, +EQUALITY_LEVEL_OPERATOR_TO_FUNCTION_NAME_MAPPING = { + '==': 'equals', + '!=': 'notEquals', + '<=': 'lessThanOrEqual', + '>=': 'greaterThanOrEqual', + '<': 'lessThan', + '>': 'greaterThan', } def transform_equality_level_expression(builtin_dependencies, symbol_list, expression): @@ -188,15 +101,18 @@ def transform_equality_level_expression(builtin_dependencies, symbol_list, expre ) # TODO Don't evaluate the middle expression twice - return CAndExpression( + return CFunctionCallForFurInfixOperator( + name='and', left=left, - right=EQUALITY_LEVEL_OPERATOR_MAPPING[expression.operator]( + right=CFunctionCallForFurInfixOperator( + name=EQUALITY_LEVEL_OPERATOR_TO_FUNCTION_NAME_MAPPING[expression.operator], left=middle, right=right, ), ) - return EQUALITY_LEVEL_OPERATOR_MAPPING[expression.operator]( + return CFunctionCallForFurInfixOperator( + name=EQUALITY_LEVEL_OPERATOR_TO_FUNCTION_NAME_MAPPING[expression.operator], left=transform_expression(builtin_dependencies, symbol_list, expression.left), right=transform_expression(builtin_dependencies, symbol_list, expression.right), ) @@ -242,15 +158,16 @@ def transform_expression(builtin_dependencies, symbol_list, expression): if isinstance(expression, parsing.FurEqualityLevelExpression): return transform_equality_level_expression(builtin_dependencies, symbol_list, expression) - INFIX_OPERATOR_TO_TYPE_MAPPING = { - '+': CAdditionExpression, - '-': CSubtractionExpression, - '*': CMultiplicationExpression, - '//': CIntegerDivisionExpression, - '%': CModularDivisionExpression, + INFIX_OPERATOR_TO_FUNCTION_NAME = { + '+': 'add', + '-': 'subtract', + '*': 'multiply', + '//': 'integerDivide', + '%': 'modularDivide', } - return INFIX_OPERATOR_TO_TYPE_MAPPING[expression.operator]( + return CFunctionCallForFurInfixOperator( + name=INFIX_OPERATOR_TO_FUNCTION_NAME[expression.operator], left=transform_expression(builtin_dependencies, symbol_list, expression.left), right=transform_expression(builtin_dependencies, symbol_list, expression.right), )