Use order instead of operator to normalize infix expressions
authorDavid Kerkeslager <kerkeslager@gmail.com>
Tue, 8 Aug 2017 14:52:13 +0000 (10:52 -0400)
committerDavid Kerkeslager <kerkeslager@gmail.com>
Tue, 8 Aug 2017 14:52:13 +0000 (10:52 -0400)
normalization.py
parsing.py
tokenization.py
transformation.py

index 3ece2bb..e3760f6 100644 (file)
@@ -117,20 +117,12 @@ def normalize_basic_infix_operation(counter, expression):
 def normalize_infix_expression(counter, expression):
     # TODO Unfake this normalization
     return {
-        '+':    normalize_basic_infix_operation,
-        '-':    normalize_basic_infix_operation,
-        '*':    normalize_basic_infix_operation,
-        '//':   normalize_basic_infix_operation,
-        '%':    normalize_basic_infix_operation,
-        '==':   fake_normalization,
-        '<=':   fake_normalization,
-        '>=':   fake_normalization,
-        '!=':   fake_normalization,
-        '<':    fake_normalization,
-        '>':    fake_normalization,
-        'and':  fake_normalization,
-        'or':   fake_normalization,
-    }[expression.operator](counter, expression)
+        'multiplication_level': normalize_basic_infix_operation,
+        'addition_level': normalize_basic_infix_operation,
+        'comparison_level': fake_normalization,
+        'and_level': fake_normalization,
+        'or_level': fake_normalization,
+    }[expression.order](counter, expression)
 
 def normalize_expression(counter, expression):
     return {
index aa4f427..23bf65e 100644 (file)
@@ -190,17 +190,17 @@ def _addition_level_expression_parser(index, tokens):
         'addition_level',
     )(index, tokens)
 
-def _equality_level_expression_parser(index, tokens):
+def _comparison_level_expression_parser(index, tokens):
     return _left_recursive_infix_operator_parser(
-        lambda token: token.type == 'equality_level_operator',
+        lambda token: token.type == 'comparison_level_operator',
         _addition_level_expression_parser,
-        'equality_level',
+        'comparison_level',
     )(index, tokens)
 
 def _and_level_expression_parser(index, tokens):
     return _left_recursive_infix_operator_parser(
         lambda token: token.type == 'symbol' and token.match == 'and',
-        _equality_level_expression_parser,
+        _comparison_level_expression_parser,
         'and_level',
     )(index, tokens)
 
index 3131c35..a736912 100644 (file)
@@ -39,7 +39,7 @@ _TOKEN_MATCHERS = [
     ('integer_literal',                 r'\d+'),
     ('symbol',                          r'[a-z]+'),
     ('single_quoted_string_literal',    r"'.*?'"),
-    ('equality_level_operator',         r'(<=|>=|==|!=|<|>)'),
+    ('comparison_level_operator',         r'(<=|>=|==|!=|<|>)'),
     ('assignment_operator',             r'='),
     ('addition_level_operator',         r'(\+|-)'),
     ('multiplication_level_operator',   r'(\*|//|%)'),
@@ -205,7 +205,7 @@ if __name__ == '__main__':
             self.assertEqual(
                 tokenize('=='),
                 (Token(
-                    type='equality_level_operator',
+                    type='comparison_level_operator',
                     match='==',
                     index=0,
                     line=1,
@@ -216,7 +216,7 @@ if __name__ == '__main__':
             self.assertEqual(
                 tokenize('>='),
                 (Token(
-                    type='equality_level_operator',
+                    type='comparison_level_operator',
                     match='>=',
                     index=0,
                     line=1,
@@ -227,7 +227,7 @@ if __name__ == '__main__':
             self.assertEqual(
                 tokenize('<='),
                 (Token(
-                    type='equality_level_operator',
+                    type='comparison_level_operator',
                     match='<=',
                     index=0,
                     line=1,
@@ -238,7 +238,7 @@ if __name__ == '__main__':
             self.assertEqual(
                 tokenize('>'),
                 (Token(
-                    type='equality_level_operator',
+                    type='comparison_level_operator',
                     match='>',
                     index=0,
                     line=1,
@@ -249,7 +249,7 @@ if __name__ == '__main__':
             self.assertEqual(
                 tokenize('<'),
                 (Token(
-                    type='equality_level_operator',
+                    type='comparison_level_operator',
                     match='<',
                     index=0,
                     line=1,
@@ -260,7 +260,7 @@ if __name__ == '__main__':
             self.assertEqual(
                 tokenize('!='),
                 (Token(
-                    type='equality_level_operator',
+                    type='comparison_level_operator',
                     match='!=',
                     index=0,
                     line=1,
index 5de9bbf..b6d21c8 100644 (file)
@@ -108,10 +108,10 @@ EQUALITY_LEVEL_OPERATOR_TO_FUNCTION_NAME_MAPPING = {
     '>':    'greaterThan',
 }
 
-def transform_equality_level_expression(accumulators, expression):
+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 == 'equality_level':
-        left = transform_equality_level_expression(
+    if isinstance(expression.left, parsing.FurInfixExpression) and expression.left.order == 'comparison_level':
+        left = transform_comparison_level_expression(
             accumulators,
             expression.left
         )
@@ -151,8 +151,8 @@ def transform_variable_expression(accumulators, expression):
     return CVariableExpression(variable=expression.variable)
 
 def transform_infix_expression(accumulators, expression):
-    if expression.order == 'equality_level':
-        return transform_equality_level_expression(accumulators, expression)
+    if expression.order == 'comparison_level':
+        return transform_comparison_level_expression(accumulators, expression)
 
     INFIX_OPERATOR_TO_FUNCTION_NAME = {
         '+':    'add',