Commits

Andrew Godwin committed ac0c4dc

Migrate comparison

  • Participants
  • Parent commits 7b4d84c

Comments (0)

Files changed (2)

File kugelblitz/translator/__init__.py

 from kugelblitz.translator.base import ast, BaseTranslator
 from kugelblitz.translator.exceptions import CompileError
 from kugelblitz.translator.toplevel import ModuleTranslator, FunctionTranslator
-from kugelblitz.translator.expressions import ExprTranslator, BinOpTranslator, BoolOpTranslator, UnaryOpTranslator
+from kugelblitz.translator.expressions import ExprTranslator, BinOpTranslator, BoolOpTranslator, UnaryOpTranslator, CompareTranslator
 from kugelblitz.translator.values import NumTranslator, ListTranslator, NameTranslator
 
 def wrap_old_translator(func):
             #ast.DictComp: None, Not in 2.6
             ast.GeneratorExp: None,
             ast.Yield: None,
-            ast.Compare: wrap_old_translator(translate_compare),
+            ast.Compare: CompareTranslator,
             ast.Call: wrap_old_translator(translate_call),
             ast.Repr: None,
             ast.Num: NumTranslator,
             ast.Tuple: ListTranslator,
             
             # cmpop
-            ast.Eq: lambda _: '==',
-            ast.NotEq: lambda _: '!=',
-            ast.Lt: lambda _: '<',
-            ast.LtE: lambda _: '<=',
-            ast.Gt: lambda _: '>',
-            ast.GtE: lambda _: '>=',
-            ast.Is: None,
-            ast.IsNot: None,
-            ast.In: None,
-            ast.NotIn: None,
         }[node.__class__](node)
     except TypeError:
         raise CompileError("No translator available for %s." % node.__class__.__name__)

File kugelblitz/translator/expressions.py

                 'left': self.sub_translate(self.node.left),
                 'op': self.ops[self.node.op.__class__],
                 'right': self.sub_translate(self.node.right),
-            }
+            }
+
+
+class CompareTranslator(BaseTranslator):
+    
+    ops = {
+        ast.Eq: '==',
+        ast.NotEq: '!=',
+        ast.Lt: '<',
+        ast.LtE: '<=',
+        ast.Gt: '>',
+        ast.GtE: '>=',
+        ast.Is: None,
+        ast.IsNot: None,
+        ast.In: None,
+        ast.NotIn: None,
+    }
+    
+    def translate(self):
+        assert len(self.node.ops) == 1, "Cannot have multiple comparison"
+        assert len(self.node.comparators) == 1, "Cannot have multiple comparison"
+        return "%(left)s %(op)s %(comparator)s" % {
+            "left": self.sub_translate(self.node.left),
+            "op": self.ops[self.node.ops[0].__class__],
+            "comparator": self.sub_translate(self.node.comparators[0]),
+        }