Commits

Andrew Godwin committed 554ec74

Fix lambda and raise

Comments (0)

Files changed (3)

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.toplevel import ModuleTranslator, FunctionTranslator, LambdaTranslator
 from kugelblitz.translator.expressions import ExprTranslator, BinOpTranslator, BoolOpTranslator, UnaryOpTranslator, CompareTranslator
 from kugelblitz.translator.values import NumTranslator, ListTranslator, NameTranslator
 from kugelblitz.translator.assignment import AssignTranslator, AugAssignTranslator
-from kugelblitz.translator.control import IfTranslator, IfExprTranslator
+from kugelblitz.translator.control import IfTranslator, IfExprTranslator, RaiseTranslator
 
 def wrap_old_translator(func):
     class WrappedTranslator(BaseTranslator):
             ast.If: IfTranslator,
             ast.With: None,
             
-            ast.Raise: wrap_old_translator(translate_raise),
+            ast.Raise: RaiseTranslator,
             ast.TryExcept: None,
             ast.TryFinally: None,
             ast.Assert: None,
             ast.BoolOp: BoolOpTranslator,
             ast.BinOp: BinOpTranslator,
             ast.UnaryOp: UnaryOpTranslator,
-            ast.Lambda: wrap_old_translator(translate_lambda),
+            ast.Lambda: LambdaTranslator,
             ast.IfExp: IfExprTranslator,
             ast.Dict: None,
             #ast.Set: None, Not in 2.6
 def translate_delete(node):
     return ';\n'.join('delete %s' % translate(n) for n in node.targets)
 
-def translate_lambda(node):
-    return "function (%(args_def)s) {\nreturn %(body_def)s\n}" % {
-        'args_def': ", ".join([arg.id for arg in node.args.args]),
-        'body_def': translate_body([node.body]),
-    }
-
 def translate_attribute(node):
     return "%(left)s.%(right)s" % {
         "left": translate(node.value),
     else:
         raise CompileError("Unknown slice type %s" % type(node.slice))
 
-def translate_raise(node):
-    return "throw"
-
 if __name__ == "__main__":
     import sys
     print get_translator(ast.parse(sys.stdin.read()+"\n")).translate()

kugelblitz/translator/control.py

             'test': self.sub_translate(self.node.test),
             'body': self.sub_translate(self.node.body),
             'orelse': self.sub_translate(self.node.orelse),
-        }
+        }
+
+class RaiseTranslator(BodyTranslator):
+    
+    def translate(self):
+        return "throw"

kugelblitz/translator/toplevel.py

     """
     
     separator = '\n\n'
-    translates = [ast.Module]
     
     def translate(self):
         return self.translate_body(self.node.body)
     Translates a function. Includes function name in output.
     """
     
-    translates = [ast.FunctionDef]
-    
     def translate(self):
         args_def = ", ".join([arg.id for arg in self.node.args.args])
         return "var %(name)s = function (%(args_def)s) { %(body_def)s }" % {
             "body_def": self.translate_body(self.node.body),
             "name": self.node.name,
         }
+
+
+class LambdaTranslator(BodyTranslator):
+    """
+    Translates a function. Includes function name in output.
+    """
+    
+    def translate(self):
+        return "function (%(args_def)s) {\nreturn %(body_def)s\n}" % {
+            'args_def': ", ".join([arg.id for arg in self.node.args.args]),
+            'body_def': self.translate_body([self.node.body]),
+        }
     
 
 class MethodTranslator(FunctionTranslator):