laurent@371: #!/usr/bin/env python
laurent@371: # Copyright 2006 James Tauber and contributors
laurent@371: #
laurent@371: # Licensed under the Apache License, Version 2.0 (the "License");
laurent@371: # you may not use this file except in compliance with the License.
laurent@371: # You may obtain a copy of the License at
laurent@371: #
laurent@371: #     http://www.apache.org/licenses/LICENSE-2.0
laurent@371: #
laurent@371: # Unless required by applicable law or agreed to in writing, software
laurent@371: # distributed under the License is distributed on an "AS IS" BASIS,
laurent@371: # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
laurent@371: # See the License for the specific language governing permissions and
laurent@371: # limitations under the License.
laurent@371: 
laurent@371: 
laurent@371: import sys
laurent@371: from types import StringType
laurent@371: import compiler
laurent@371: from compiler import ast
laurent@371: import os
laurent@371: import copy
laurent@371: 
laurent@371: # the standard location for builtins (e.g. pyjslib) can be
laurent@371: # over-ridden by changing this.  it defaults to sys.prefix
laurent@371: # so that on a system-wide install of pyjamas the builtins
laurent@371: # can be found in e.g. {sys.prefix}/share/pyjamas
laurent@371: #
laurent@371: # over-rides can be done by either explicitly modifying
laurent@371: # pyjs.prefix or by setting an environment variable, PYJSPREFIX.
laurent@371: 
laurent@371: prefix = sys.prefix
laurent@371: 
laurent@371: if os.environ.has_key('PYJSPREFIX'):
laurent@371:     prefix = os.environ['PYJSPREFIX']
laurent@371: 
laurent@371: # pyjs.path is the list of paths, just like sys.path, from which
laurent@371: # library modules will be searched for, for compile purposes.
laurent@371: # obviously we don't want to use sys.path because that would result
laurent@371: # in compiling standard python modules into javascript!
laurent@371: 
laurent@371: path = [os.path.abspath('')]
laurent@371: 
laurent@371: if os.environ.has_key('PYJSPATH'):
laurent@371:     for p in os.environ['PYJSPATH'].split(os.pathsep):
laurent@371:         p = os.path.abspath(p)
laurent@371:         if os.path.isdir(p):
laurent@371:             path.append(p)
laurent@371: 
laurent@371: # this is the python function used to wrap native javascript
laurent@371: NATIVE_JS_FUNC_NAME = "JS"
laurent@371: 
laurent@371: UU = ""
laurent@371: 
laurent@371: PYJSLIB_BUILTIN_FUNCTIONS=("cmp",
laurent@371:                            "map",
laurent@371:                            "filter",
laurent@371:                            "dir",
laurent@371:                            "getattr",
laurent@371:                            "setattr",
laurent@371:                            "hasattr",
laurent@371:                            "int",
laurent@371:                            "float",
laurent@371:                            "str",
laurent@371:                            "repr",
laurent@371:                            "range",
laurent@371:                            "len",
laurent@371:                            "hash",
laurent@371:                            "abs",
laurent@371:                            "ord",
laurent@371:                            "chr",
laurent@371:                            "enumerate",
laurent@371:                            "min",
laurent@371:                            "max",
laurent@371:                            "bool",
laurent@371:                            "type",
laurent@371:                            "isinstance")
laurent@371: 
laurent@371: PYJSLIB_BUILTIN_CLASSES=("BaseException",
laurent@371:                          "Exception",
laurent@371:                          "StandardError",
laurent@371:                          "StopIteration",
laurent@371:                          "AttributeError",
laurent@371:                          "TypeError",
laurent@371:                          "KeyError",
laurent@371:                          "LookupError",
laurent@371:                          "list",
laurent@371:                          "dict",
laurent@371:                          "object",
laurent@371:                          "tuple",
laurent@371:                         )
laurent@371: 
laurent@371: def pyjs_builtin_remap(name):
laurent@371:     # XXX HACK!
laurent@371:     if name == 'list':
laurent@371:         name = 'List'
laurent@371:     if name == 'object':
laurent@371:         name = '__Object'
laurent@371:     if name == 'dict':
laurent@371:         name = 'Dict'
laurent@371:     if name == 'tuple':
laurent@371:         name = 'Tuple'
laurent@371:     return name
laurent@371: 
laurent@371: # XXX: this is a hack: these should be dealt with another way
laurent@371: # however, console is currently the only global name which is causing
laurent@371: # problems.
laurent@371: PYJS_GLOBAL_VARS=("console")
laurent@371: 
laurent@371: # This is taken from the django project.
laurent@371: # Escape every ASCII character with a value less than 32.
laurent@371: JS_ESCAPES = (
laurent@371:     ('\\', r'\x5C'),
laurent@371:     ('\'', r'\x27'),
laurent@371:     ('"', r'\x22'),
laurent@371:     ('>', r'\x3E'),
laurent@371:     ('<', r'\x3C'),
laurent@371:     ('&', r'\x26'),
laurent@371:     (';', r'\x3B')
laurent@371:     ) + tuple([('%c' % z, '\\x%02X' % z) for z in range(32)])
laurent@371: 
laurent@371: def escapejs(value):
laurent@371:     """Hex encodes characters for use in JavaScript strings."""
laurent@371:     for bad, good in JS_ESCAPES:
laurent@371:         value = value.replace(bad, good)
laurent@371:     return value
laurent@371: 
laurent@371: def uuprefix(name, leave_alone=0):
laurent@371:     name = name.split(".")
laurent@371:     name = name[:leave_alone] + map(lambda x: "__%s" % x, name[leave_alone:])
laurent@371:     return '.'.join(name)
laurent@371: 
laurent@371: class Klass:
laurent@371: 
laurent@371:     klasses = {}
laurent@371: 
laurent@371:     def __init__(self, name, name_):
laurent@371:         self.name = name
laurent@371:         self.name_ = name_
laurent@371:         self.klasses[name] = self
laurent@371:         self.functions = set()
laurent@371: 
laurent@371:     def set_base(self, base_name):
laurent@371:         self.base = self.klasses.get(base_name)
laurent@371: 
laurent@371:     def add_function(self, function_name):
laurent@371:         self.functions.add(function_name)
laurent@371: 
laurent@371: 
laurent@371: class TranslationError(Exception):
laurent@371:     def __init__(self, message, node):
laurent@371:         self.message = "line %s:\n%s\n%s" % (node.lineno, message, node)
laurent@371: 
laurent@371:     def __str__(self):
laurent@371:         return self.message
laurent@371: 
laurent@371: def strip_py(name):
laurent@371:     return name
laurent@371: 
laurent@371: def mod_var_name_decl(raw_module_name):
laurent@371:     """ function to get the last component of the module e.g.
laurent@371:         pyjamas.ui.DOM into the "namespace".  i.e. doing
laurent@371:         "import pyjamas.ui.DOM" actually ends up with _two_
laurent@371:         variables - one pyjamas.ui.DOM, the other just "DOM".
laurent@371:         but "DOM" is actually local, hence the "var" prefix.
laurent@371: 
laurent@371:         for PyV8, this might end up causing problems - we'll have
laurent@371:         to see: gen_mod_import and mod_var_name_decl might have
laurent@371:         to end up in a library-specific module, somewhere.
laurent@371:     """
laurent@371:     name = raw_module_name.split(".")
laurent@371:     if len(name) == 1:
laurent@371:         return ''
laurent@371:     child_name = name[-1]
laurent@371:     return "var %s = %s;\n" % (child_name, raw_module_name)
laurent@371: 
laurent@371: def gen_mod_import(parentName, importName, dynamic=1):
laurent@371:     #pyjs_ajax_eval("%(n)s.cache.js", null, true);
laurent@371:     return """
laurent@371:     pyjslib.import_module(sys.loadpath, '%(p)s', '%(n)s', %(d)d, false);
laurent@371:     """ % ({'p': parentName, 'd': dynamic, 'n': importName}) + \
laurent@371:     mod_var_name_decl(importName)
laurent@371: 
laurent@371: class Translator:
laurent@371: 
laurent@371:     def __init__(self, mn, module_name, raw_module_name, src, debug, mod, output,
laurent@371:                  dynamic=0, optimize=False,
laurent@371:                  findFile=None):
laurent@371: 
laurent@371:         if module_name:
laurent@371:             self.module_prefix = module_name + "."
laurent@371:         else:
laurent@371:             self.module_prefix = ""
laurent@371:         self.raw_module_name = raw_module_name
laurent@371:         src = src.replace("\r\n", "\n")
laurent@371:         src = src.replace("\n\r", "\n")
laurent@371:         src = src.replace("\r",   "\n")
laurent@371:         self.src = src.split("\n")
laurent@371:         self.debug = debug
laurent@371:         self.imported_modules = []
laurent@371:         self.imported_modules_as = []
laurent@371:         self.imported_js = set()
laurent@371:         self.top_level_functions = set()
laurent@371:         self.top_level_classes = set()
laurent@371:         self.top_level_vars = set()
laurent@371:         self.local_arg_stack = [[]]
laurent@371:         self.output = output
laurent@371:         self.imported_classes = {}
laurent@371:         self.method_imported_globals = set()
laurent@371:         self.method_self = None
laurent@371:         self.nextTupleAssignID = 1
laurent@371:         self.dynamic = dynamic
laurent@371:         self.optimize = optimize
laurent@371:         self.findFile = findFile
laurent@371: 
laurent@371:         if module_name.find(".") >= 0:
laurent@371:             vdec = ''
laurent@371:         else:
laurent@371:             vdec = 'var '
laurent@371:         print >>self.output, UU+"%s%s = function (__mod_name__) {" % (vdec, module_name)
laurent@371: 
laurent@371:         print >>self.output, "    if("+module_name+".__was_initialized__) return;"
laurent@371:         print >>self.output, "    "+UU+module_name+".__was_initialized__ = true;"
laurent@371:         print >>self.output, UU+"if (__mod_name__ == null) __mod_name__ = '%s';" % (mn)
laurent@371:         print >>self.output, UU+"%s.__name__ = __mod_name__;" % (raw_module_name)
laurent@371: 
laurent@371:         decl = mod_var_name_decl(raw_module_name)
laurent@371:         if decl:
laurent@371:             print >>self.output, decl
laurent@371: 
laurent@371: 
laurent@371:         if self.debug:
laurent@371:             haltException = self.module_prefix + "HaltException"
laurent@371:             print >>self.output, haltException + ' = function () {'
laurent@371:             print >>self.output, '  this.message = "Program Halted";'
laurent@371:             print >>self.output, '  this.name = "' + haltException + '";'
laurent@371:             print >>self.output, '}'
laurent@371:             print >>self.output, ''
laurent@371:             print >>self.output, haltException + ".prototype.__str__ = function()"
laurent@371:             print >>self.output, '{'
laurent@371:             print >>self.output, 'return this.message ;'
laurent@371:             print >>self.output, '}'
laurent@371: 
laurent@371:             print >>self.output, haltException + ".prototype.toString = function()"
laurent@371:             print >>self.output, '{'
laurent@371:             print >>self.output, 'return this.name + ": \\"" + this.message + "\\"";'
laurent@371:             print >>self.output, '}'
laurent@371: 
laurent@371:             isHaltFunction = self.module_prefix + "IsHaltException"
laurent@371:             print >>self.output, """
laurent@371:     %s = function (s) {
laurent@371:       var suffix="HaltException";
laurent@371:       if (s.length < suffix.length) {
laurent@371:         //alert(s + " " + suffix);
laurent@371:         return false;
laurent@371:       } else {
laurent@371:         var ss = s.substring(s.length, (s.length - suffix.length));
laurent@371:         //alert(s + " " + suffix + " " + ss);
laurent@371:         return ss == suffix;
laurent@371:       }
laurent@371:     }
laurent@371:                 """ % isHaltFunction
laurent@371:         for child in mod.node:
laurent@371:             if isinstance(child, ast.Function):
laurent@371:                 self.top_level_functions.add(child.name)
laurent@371:             elif isinstance(child, ast.Class):
laurent@371:                 self.top_level_classes.add(child.name)
laurent@371: 
laurent@371:         for child in mod.node:
laurent@371:             if isinstance(child, ast.Function):
laurent@371:                 self._function(child, False)
laurent@371:             elif isinstance(child, ast.Class):
laurent@371:                 self._class(child)
laurent@371:             elif isinstance(child, ast.Import):
laurent@371:                 importName = child.names[0][0]
laurent@371:                 if importName == '__pyjamas__': # special module to help make pyjamas modules loadable in the python interpreter
laurent@371:                     pass
laurent@371:                 elif importName.endswith('.js'):
laurent@371:                    self.imported_js.add(importName)
laurent@371:                 else:
laurent@371:                    self.add_imported_module(strip_py(importName))
laurent@371:             elif isinstance(child, ast.From):
laurent@371:                 if child.modname == '__pyjamas__': # special module to help make pyjamas modules loadable in the python interpreter
laurent@371:                     pass
laurent@371:                 else:
laurent@371:                     self.add_imported_module(child.modname)
laurent@371:                     self._from(child)
laurent@371:             elif isinstance(child, ast.Discard):
laurent@371:                 self._discard(child, None)
laurent@371:             elif isinstance(child, ast.Assign):
laurent@371:                 self._assign(child, None, True)
laurent@371:             elif isinstance(child, ast.AugAssign):
laurent@371:                 self._augassign(child, None)
laurent@371:             elif isinstance(child, ast.If):
laurent@371:                 self._if(child, None)
laurent@371:             elif isinstance(child, ast.For):
laurent@371:                 self._for(child, None)
laurent@371:             elif isinstance(child, ast.While):
laurent@371:                 self._while(child, None)
laurent@371:             elif isinstance(child, ast.Subscript):
laurent@371:                 self._subscript_stmt(child, None)
laurent@371:             elif isinstance(child, ast.Global):
laurent@371:                 self._global(child, None)
laurent@371:             elif isinstance(child, ast.Printnl):
laurent@371:                self._print(child, None)
laurent@371:             elif isinstance(child, ast.Print):
laurent@371:                self._print(child, None)
laurent@371:             elif isinstance(child, ast.TryExcept):
laurent@371:                 self._tryExcept(child, None)
laurent@371:             elif isinstance(child, ast.Raise):
laurent@371:                 self._raise(child, None)
laurent@371:             elif isinstance(child, ast.Stmt):
laurent@371:                 self._stmt(child, None)
laurent@371:             else:
laurent@371:                 raise TranslationError("unsupported type (in __init__)", child)
laurent@371: 
laurent@371:         # Initialize all classes for this module
laurent@371:         #print >> self.output, "__"+self.modpfx()+\
laurent@371:         #          "classes_initialize = function() {\n"
laurent@371:         #for className in self.top_level_classes:
laurent@371:         #    print >> self.output, "\t"+UU+self.modpfx()+"__"+className+"_initialize();"
laurent@371:         #print >> self.output, "};\n"
laurent@371: 
laurent@371:         print >> self.output, "return this;\n"
laurent@371:         print >> self.output, "}; /* end %s */ \n"  % module_name
laurent@371: 
laurent@371:     def module_imports(self):
laurent@371:         return self.imported_modules + self.imported_modules_as
laurent@371: 
laurent@371:     def add_local_arg(self, varname):
laurent@371:         local_vars = self.local_arg_stack[-1]
laurent@371:         if varname not in local_vars:
laurent@371:             local_vars.append(varname)
laurent@371: 
laurent@371:     def add_imported_module(self, importName):
laurent@371: 
laurent@371:         if importName in self.imported_modules:
laurent@371:             return
laurent@371:         self.imported_modules.append(importName)
laurent@371:         name = importName.split(".")
laurent@371:         if len(name) != 1:
laurent@371:             # add the name of the module to the namespace,
laurent@371:             # but don't add the short name to imported_modules
laurent@371:             # because then the short name would be attempted to be
laurent@371:             # added to the dependencies, and it's half way up the
laurent@371:             # module import directory structure!
laurent@371:             child_name = name[-1]
laurent@371:             self.imported_modules_as.append(child_name) 
laurent@371:         print >> self.output, gen_mod_import(self.raw_module_name,
laurent@371:                                              strip_py(importName),
laurent@371:                                              self.dynamic)
laurent@371: 
laurent@371:     def _default_args_handler(self, node, arg_names, current_klass,
laurent@371:                               output=None):
laurent@371:         if len(node.defaults):
laurent@371:             output = output or self.output
laurent@371:             default_pos = len(arg_names) - len(node.defaults)
laurent@371:             if arg_names and arg_names[0] == self.method_self:
laurent@371:                 default_pos -= 1
laurent@371:             for default_node in node.defaults:
laurent@371:                 if isinstance(default_node, ast.Const):
laurent@371:                     default_value = self._const(default_node)
laurent@371:                 elif isinstance(default_node, ast.Name):
laurent@371:                     default_value = self._name(default_node, current_klass)
laurent@371:                 elif isinstance(default_node, ast.UnarySub):
laurent@371:                     default_value = self._unarysub(default_node, current_klass)
laurent@371:                 else:
laurent@371:                     raise TranslationError("unsupported type (in _method)", default_node)
laurent@371: 
laurent@371:                 default_name = arg_names[default_pos]
laurent@371:                 default_pos += 1
laurent@371:                 print >> output, "    if (typeof %s == 'undefined') %s=%s;" % (default_name, default_name, default_value)
laurent@371: 
laurent@371:     def _varargs_handler(self, node, varargname, arg_names, current_klass):
laurent@371:         print >>self.output, "    var", varargname, '= new pyjslib.Tuple();'
laurent@371:         print >>self.output, "    for(var __va_arg="+str(len(arg_names))+"; __va_arg < arguments.length; __va_arg++) {"
laurent@371:         print >>self.output, "        var __arg = arguments[__va_arg];"
laurent@371:         print >>self.output, "        "+varargname+".append(__arg);"
laurent@371:         print >>self.output, "    }"
laurent@371: 
laurent@371:     def _kwargs_parser(self, node, function_name, arg_names, current_klass):
laurent@371:         if len(node.defaults) or node.kwargs:
laurent@371:             default_pos = len(arg_names) - len(node.defaults)
laurent@371:             if arg_names and arg_names[0] == self.method_self:
laurent@371:                 default_pos -= 1
laurent@371:             print >>self.output, function_name+'.parse_kwargs = function (', ", ".join(["__kwargs"]+arg_names), ") {"
laurent@371:             for default_node in node.defaults:
laurent@371:                 default_value = self.expr(default_node, current_klass)
laurent@371: #                if isinstance(default_node, ast.Const):
laurent@371: #                    default_value = self._const(default_node)
laurent@371: #                elif isinstance(default_node, ast.Name):
laurent@371: #                    default_value = self._name(default_node)
laurent@371: #                elif isinstance(default_node, ast.UnarySub):
laurent@371: #                    default_value = self._unarysub(default_node, current_klass)
laurent@371: #                else:
laurent@371: #                    raise TranslationError("unsupported type (in _method)", default_node)
laurent@371: 
laurent@371:                 default_name = arg_names[default_pos]
laurent@371:                 print >>self.output, "    if (typeof %s == 'undefined')"%(default_name)
laurent@371:                 print >>self.output, "        %s=__kwargs.%s;"% (default_name, default_name)
laurent@371:                 default_pos += 1
laurent@371: 
laurent@371:             #self._default_args_handler(node, arg_names, current_klass)
laurent@371:             if node.kwargs: arg_names += ["pyjslib.Dict(__kwargs)"]
laurent@371:             print >>self.output, "    var __r = "+"".join(["[", ", ".join(arg_names), "]"])+";"
laurent@371:             if node.varargs:
laurent@371:                 self._varargs_handler(node, "__args", arg_names, current_klass)
laurent@371:                 print >>self.output, "    __r.push.apply(__r, __args.getArray())"
laurent@371:             print >>self.output, "    return __r;"
laurent@371:             print >>self.output, "};"
laurent@371: 
laurent@371:     def _function(self, node, local=False):
laurent@371:         if local:
laurent@371:             function_name = node.name
laurent@371:             self.add_local_arg(function_name)
laurent@371:         else:
laurent@371:             function_name = UU + self.modpfx() + node.name
laurent@371: 
laurent@371:         arg_names = list(node.argnames)
laurent@371:         normal_arg_names = list(arg_names)
laurent@371:         if node.kwargs: kwargname = normal_arg_names.pop()
laurent@371:         if node.varargs: varargname = normal_arg_names.pop()
laurent@371:         declared_arg_names = list(normal_arg_names)
laurent@371:         if node.kwargs: declared_arg_names.append(kwargname)
laurent@371: 
laurent@371:         function_args = "(" + ", ".join(declared_arg_names) + ")"
laurent@371:         print >>self.output, "%s = function%s {" % (function_name, function_args)
laurent@371:         self._default_args_handler(node, normal_arg_names, None)
laurent@371: 
laurent@371:         local_arg_names = normal_arg_names + declared_arg_names 
laurent@371: 
laurent@371:         if node.varargs:
laurent@371:             self._varargs_handler(node, varargname, declared_arg_names, None)
laurent@371:             local_arg_names.append(varargname)
laurent@371: 
laurent@371:         # stack of local variable names for this function call
laurent@371:         self.local_arg_stack.append(local_arg_names)
laurent@371: 
laurent@371:         for child in node.code:
laurent@371:             self._stmt(child, None)
laurent@371: 
laurent@371:         # remove the top local arg names
laurent@371:         self.local_arg_stack.pop()
laurent@371: 
laurent@371:         # we need to return null always, so it is not undefined
laurent@371:         lastStmt = [p for p in node.code][-1]
laurent@371:         if not isinstance(lastStmt, ast.Return):
laurent@371:             if not self._isNativeFunc(lastStmt):
laurent@371:                 print >>self.output, "    return null;"
laurent@371: 
laurent@371:         print >>self.output, "};"
laurent@371:         print >>self.output, "%s.__name__ = '%s';\n" % (function_name, node.name)
laurent@371: 
laurent@371: 
laurent@371:         self._kwargs_parser(node, function_name, normal_arg_names, None)
laurent@371: 
laurent@371: 
laurent@371:     def _return(self, node, current_klass):
laurent@371:         expr = self.expr(node.value, current_klass)
laurent@371:         # in python a function call always returns None, so we do it
laurent@371:         # here too
laurent@371:         print >>self.output, "    return " + expr + ";"
laurent@371: 
laurent@371: 
laurent@371:     def _break(self, node, current_klass):
laurent@371:         print >>self.output, "    break;"
laurent@371: 
laurent@371: 
laurent@371:     def _continue(self, node, current_klass):
laurent@371:         print >>self.output, "    continue;"
laurent@371: 
laurent@371: 
laurent@371:     def _callfunc(self, v, current_klass):
laurent@371: 
laurent@371:         if isinstance(v.node, ast.Name):
laurent@371:             if v.node.name in self.top_level_functions:
laurent@371:                 call_name = self.modpfx() + v.node.name
laurent@371:             elif v.node.name in self.top_level_classes:
laurent@371:                 call_name = self.modpfx() + v.node.name
laurent@371:             elif self.imported_classes.has_key(v.node.name):
laurent@371:                 call_name = self.imported_classes[v.node.name] + '.' + v.node.name
laurent@371:             elif v.node.name in PYJSLIB_BUILTIN_FUNCTIONS:
laurent@371:                 call_name = 'pyjslib.' + v.node.name
laurent@371:             elif v.node.name in PYJSLIB_BUILTIN_CLASSES:
laurent@371:                 name = pyjs_builtin_remap(v.node.name)
laurent@371:                 call_name = 'pyjslib.' + name
laurent@371:             elif v.node.name == "callable":
laurent@371:                 call_name = "pyjslib.isFunction"
laurent@371:             else:
laurent@371:                 call_name = v.node.name
laurent@371:             call_args = []
laurent@371:         elif isinstance(v.node, ast.Getattr):
laurent@371:             attr_name = v.node.attrname
laurent@371: 
laurent@371:             if isinstance(v.node.expr, ast.Name):
laurent@371:                 call_name = self._name2(v.node.expr, current_klass, attr_name)
laurent@371:                 call_args = []
laurent@371:             elif isinstance(v.node.expr, ast.Getattr):
laurent@371:                 call_name = self._getattr2(v.node.expr, current_klass, attr_name)
laurent@371:                 call_args = []
laurent@371:             elif isinstance(v.node.expr, ast.CallFunc):
laurent@371:                 call_name = self._callfunc(v.node.expr, current_klass) + "." + v.node.attrname
laurent@371:                 call_args = []
laurent@371:             elif isinstance(v.node.expr, ast.Subscript):
laurent@371:                 call_name = self._subscript(v.node.expr, current_klass) + "." + v.node.attrname
laurent@371:                 call_args = []
laurent@371:             elif isinstance(v.node.expr, ast.Const):
laurent@371:                 call_name = self.expr(v.node.expr, current_klass) + "." + v.node.attrname
laurent@371:                 call_args = []
laurent@371:             else:
laurent@371:                 raise TranslationError("unsupported type (in _callfunc)", v.node.expr)
laurent@371:         else:
laurent@371:             raise TranslationError("unsupported type (in _callfunc)", v.node)
laurent@371: 
laurent@371:         call_name = strip_py(call_name)
laurent@371: 
laurent@371:         kwargs = []
laurent@371:         star_arg_name = None
laurent@371:         if v.star_args:
laurent@371:             star_arg_name = self.expr(v.star_args, current_klass)
laurent@371: 
laurent@371:         for ch4 in v.args:
laurent@371:             if isinstance(ch4, ast.Keyword):
laurent@371:                 kwarg = ch4.name + ":" + self.expr(ch4.expr, current_klass)
laurent@371:                 kwargs.append(kwarg)
laurent@371:             else:
laurent@371:                 arg = self.expr(ch4, current_klass)
laurent@371:                 call_args.append(arg)
laurent@371: 
laurent@371:         if kwargs:
laurent@371:             fn_args = ", ".join(['{' + ', '.join(kwargs) + '}']+call_args)
laurent@371:         else:
laurent@371:             fn_args = ", ".join(call_args)
laurent@371: 
laurent@371:         if kwargs or star_arg_name:
laurent@371:             if not star_arg_name:
laurent@371:                 star_arg_name = 'null'
laurent@371:             try: call_this, method_name = call_name.rsplit(".", 1)
laurent@371:             except ValueError:
laurent@371:                 # Must be a function call ...
laurent@371:                 return ("pyjs_kwargs_function_call("+call_name+", "
laurent@371:                                   + star_arg_name 
laurent@371:                                   + ", ["+fn_args+"]"
laurent@371:                                   + ")" )
laurent@371:             else:
laurent@371:                 return ("pyjs_kwargs_method_call("+call_this+", '"+method_name+"', "
laurent@371:                                   + star_arg_name 
laurent@371:                                   + ", ["+fn_args+"]"
laurent@371:                                   + ")")
laurent@371:         else:
laurent@371:             return call_name + "(" + ", ".join(call_args) + ")"
laurent@371: 
laurent@371:     def _print(self, node, current_klass):
laurent@371:         if self.optimize:
laurent@371:             return
laurent@371:         call_args = []
laurent@371:         for ch4 in node.nodes:
laurent@371:             arg = self.expr(ch4, current_klass)
laurent@371:             call_args.append(arg)
laurent@371: 
laurent@371:         print >>self.output, "pyjslib.printFunc([", ', '.join(call_args), "],", int(isinstance(node, ast.Printnl)), ");"
laurent@371: 
laurent@371:     def _tryExcept(self, node, current_klass):
laurent@371:         if len(node.handlers) != 1:
laurent@371:             raise TranslationError("except statements in this form are" +
laurent@371:                                    " not supported", node)
laurent@371: 
laurent@371:         expr = node.handlers[0][0]
laurent@371:         as_ = node.handlers[0][1]
laurent@371:         if as_:
laurent@371:             errName = as_.name
laurent@371:         else:
laurent@371:             errName = 'err'
laurent@371: 
laurent@371:         # XXX TODO: check that this should instead be added as a _separate_
laurent@371:         # local scope, temporary to the function.  oh dearie me.
laurent@371:         self.add_local_arg(errName)
laurent@371: 
laurent@371:         print >>self.output, "    try {"
laurent@371:         for stmt in node.body.nodes:
laurent@371:             self._stmt(stmt, current_klass)
laurent@371:         print >> self.output, "    } catch(%s) {" % errName
laurent@371:         if expr:
laurent@371:             l = []
laurent@371:             if isinstance(expr, ast.Tuple):
laurent@371:                 for x in expr.nodes:
laurent@371:                     l.append("(%(err)s.__name__ == %(expr)s.__name__)" % dict (err=errName, expr=self.expr(x, current_klass)))
laurent@371:             else:
laurent@371:                 l = [ " (%(err)s.__name__ == %(expr)s.__name__) " % dict (err=errName, expr=self.expr(expr, current_klass)) ]
laurent@371:             print >> self.output, "   if(%s) {" % '||\n\t\t'.join(l)
laurent@371:         for stmt in node.handlers[0][2]:
laurent@371:             self._stmt(stmt, current_klass)
laurent@371:         if expr:
laurent@371:             #print >> self.output, "} else { throw(%s); } " % errName
laurent@371:             print >> self.output, "}"
laurent@371:         if node.else_ != None:
laurent@371:             print >>self.output, "    } finally {"
laurent@371:             for stmt in node.else_:
laurent@371:                 self._stmt(stmt, current_klass)
laurent@371:         print >>self.output, "    }"
laurent@371: 
laurent@371:     # XXX: change use_getattr to True to enable "strict" compilation
laurent@371:     # but incurring a 100% performance penalty. oops.
laurent@371:     def _getattr(self, v, current_klass, use_getattr=False):
laurent@371:         attr_name = v.attrname
laurent@371:         if isinstance(v.expr, ast.Name):
laurent@371:             obj = self._name(v.expr, current_klass, return_none_for_module=True)
laurent@371:             if obj == None and v.expr.name in self.module_imports():
laurent@371:                 # XXX TODO: distinguish between module import classes
laurent@371:                 # and variables.  right now, this is a hack to get
laurent@371:                 # the sys module working.
laurent@371:                 #if v.expr.name == 'sys':
laurent@371:                 return v.expr.name+'.'+attr_name
laurent@371:                 #return v.expr.name+'.__'+attr_name+'.prototype.__class__'
laurent@371:             if not use_getattr or attr_name == '__class__' or \
laurent@371:                     attr_name == '__name__':
laurent@371:                 return obj + "." + attr_name
laurent@371:             return "pyjslib.getattr(%s, '%s')" % (obj, attr_name)
laurent@371:         elif isinstance(v.expr, ast.Getattr):
laurent@371:             return self._getattr(v.expr, current_klass) + "." + attr_name
laurent@371:         elif isinstance(v.expr, ast.Subscript):
laurent@371:             return self._subscript(v.expr, self.modpfx()) + "." + attr_name
laurent@371:         elif isinstance(v.expr, ast.CallFunc):
laurent@371:             return self._callfunc(v.expr, self.modpfx()) + "." + attr_name
laurent@371:         else:
laurent@371:             raise TranslationError("unsupported type (in _getattr)", v.expr)
laurent@371: 
laurent@371: 
laurent@371:     def modpfx(self):
laurent@371:         return strip_py(self.module_prefix)
laurent@371:         
laurent@371:     def _name(self, v, current_klass, top_level=False,
laurent@371:                                       return_none_for_module=False):
laurent@371: 
laurent@371:         if v.name == 'ilikesillynamesfornicedebugcode':
laurent@371:             print top_level, current_klass, repr(v)
laurent@371:             print self.top_level_vars
laurent@371:             print self.top_level_functions
laurent@371:             print self.local_arg_stack
laurent@371:             print "error..."
laurent@371: 
laurent@371:         local_var_names = None
laurent@371:         las = len(self.local_arg_stack)
laurent@371:         if las > 0:
laurent@371:             local_var_names = self.local_arg_stack[-1]
laurent@371: 
laurent@371:         if v.name == "True":
laurent@371:             return "true"
laurent@371:         elif v.name == "False":
laurent@371:             return "false"
laurent@371:         elif v.name == "None":
laurent@371:             return "null"
laurent@371:         elif v.name == '__name__' and current_klass is None:
laurent@371:             return self.modpfx() + v.name
laurent@371:         elif v.name == self.method_self:
laurent@371:             return "this"
laurent@371:         elif v.name in self.top_level_functions:
laurent@371:             return UU+self.modpfx() + v.name
laurent@371:         elif v.name in self.method_imported_globals:
laurent@371:             return UU+self.modpfx() + v.name
laurent@371:         elif not current_klass and las == 1 and v.name in self.top_level_vars:
laurent@371:             return UU+self.modpfx() + v.name
laurent@371:         elif v.name in local_var_names:
laurent@371:             return v.name
laurent@371:         elif self.imported_classes.has_key(v.name):
laurent@371:             return UU+self.imported_classes[v.name] + '.__' + v.name + ".prototype.__class__"
laurent@371:         elif v.name in self.top_level_classes:
laurent@371:             return UU+self.modpfx() + "__" + v.name + ".prototype.__class__"
laurent@371:         elif v.name in self.module_imports() and return_none_for_module:
laurent@371:             return None
laurent@371:         elif v.name in PYJSLIB_BUILTIN_CLASSES:
laurent@371:             return "pyjslib." + pyjs_builtin_remap( v.name )
laurent@371:         elif current_klass:
laurent@371:             if v.name not in local_var_names and \
laurent@371:                v.name not in self.top_level_vars and \
laurent@371:                v.name not in PYJS_GLOBAL_VARS and \
laurent@371:                v.name not in self.top_level_functions:
laurent@371: 
laurent@371:                 cls_name = current_klass
laurent@371:                 if hasattr(cls_name, "name"):
laurent@371:                     cls_name_ = cls_name.name_
laurent@371:                     cls_name = cls_name.name
laurent@371:                 else:
laurent@371:                     cls_name_ = current_klass + "_" # XXX ???
laurent@371:                 name = UU+cls_name_ + ".prototype.__class__." \
laurent@371:                                    + v.name
laurent@371:                 if v.name == 'listener':
laurent@371:                     name = 'listener+' + name
laurent@371:                 return name
laurent@371: 
laurent@371:         return v.name
laurent@371: 
laurent@371:     def _name2(self, v, current_klass, attr_name):
laurent@371:         obj = v.name
laurent@371: 
laurent@371:         if obj in self.method_imported_globals:
laurent@371:             call_name = UU+self.modpfx() + obj + "." + attr_name
laurent@371:         elif self.imported_classes.has_key(obj):
laurent@371:             #attr_str = ""
laurent@371:             #if attr_name != "__init__":
laurent@371:             attr_str = ".prototype.__class__." + attr_name
laurent@371:             call_name = UU+self.imported_classes[obj] + '.__' + obj + attr_str
laurent@371:         elif obj in self.module_imports():
laurent@371:             call_name = obj + "." + attr_name
laurent@371:         elif obj[0] == obj[0].upper(): # XXX HACK ALERT
laurent@371:             call_name = UU + self.modpfx() + "__" + obj + ".prototype.__class__." + attr_name
laurent@371:         else:
laurent@371:             call_name = UU+self._name(v, current_klass) + "." + attr_name
laurent@371: 
laurent@371:         return call_name
laurent@371: 
laurent@371: 
laurent@371:     def _getattr2(self, v, current_klass, attr_name):
laurent@371:         if isinstance(v.expr, ast.Getattr):
laurent@371:             call_name = self._getattr2(v.expr, current_klass, v.attrname + "." + attr_name)
laurent@371:         elif isinstance(v.expr, ast.Name) and v.expr.name in self.module_imports():
laurent@371:             call_name = UU+v.expr.name + '.__' +v.attrname+".prototype.__class__."+attr_name
laurent@371:         else:
laurent@371:             obj = self.expr(v.expr, current_klass)
laurent@371:             call_name = obj + "." + v.attrname + "." + attr_name
laurent@371: 
laurent@371:         return call_name
laurent@371: 
laurent@371: 
laurent@371:     def _class(self, node):
laurent@371:         """
laurent@371:         Handle a class definition.
laurent@371: 
laurent@371:         In order to translate python semantics reasonably well, the following
laurent@371:         structure is used:
laurent@371: 
laurent@371:         A special object is created for the class, which inherits attributes
laurent@371:         from the superclass, or Object if there's no superclass.  This is the
laurent@371:         class object; the object which you refer to when specifying the
laurent@371:         class by name.  Static, class, and unbound methods are copied
laurent@371:         from the superclass object.
laurent@371: 
laurent@371:         A special constructor function is created with the same name as the
laurent@371:         class, which is used to create instances of that class.
laurent@371: 
laurent@371:         A javascript class (e.g. a function with a prototype attribute) is
laurent@371:         created which is the javascript class of created instances, and
laurent@371:         which inherits attributes from the class object. Bound methods are
laurent@371:         copied from the superclass into this class rather than inherited,
laurent@371:         because the class object contains unbound, class, and static methods
laurent@371:         that we don't necessarily want to inherit.
laurent@371: 
laurent@371:         The type of a method can now be determined by inspecting its
laurent@371:         static_method, unbound_method, class_method, or instance_method
laurent@371:         attribute; only one of these should be true.
laurent@371: 
laurent@371:         Much of this work is done in pyjs_extend, is pyjslib.py
laurent@371:         """
laurent@371:         class_name = self.modpfx() + uuprefix(node.name, 1)
laurent@371:         class_name_ = self.modpfx() + uuprefix(node.name)
laurent@371:         current_klass = Klass(class_name, class_name_)
laurent@371:         init_method = None
laurent@371:         for child in node.code:
laurent@371:             if isinstance(child, ast.Function):
laurent@371:                 current_klass.add_function(child.name)
laurent@371:                 if child.name == "__init__":
laurent@371:                     init_method = child
laurent@371: 
laurent@371: 
laurent@371:         if len(node.bases) == 0:
laurent@371:             base_class = "pyjslib.__Object"
laurent@371:         elif len(node.bases) == 1:
laurent@371:             if isinstance(node.bases[0], ast.Name):
laurent@371:                 if self.imported_classes.has_key(node.bases[0].name):
laurent@371:                     base_class_ = self.imported_classes[node.bases[0].name] + '.__' + node.bases[0].name
laurent@371:                     base_class = self.imported_classes[node.bases[0].name] + '.' + node.bases[0].name
laurent@371:                 else:
laurent@371:                     base_class_ = self.modpfx() + "__" + node.bases[0].name
laurent@371:                     base_class = self.modpfx() + node.bases[0].name
laurent@371:             elif isinstance(node.bases[0], ast.Getattr):
laurent@371:                 # the bases are not in scope of the class so do not
laurent@371:                 # pass our class to self._name
laurent@371:                 base_class_ = self._name(node.bases[0].expr, None) + \
laurent@371:                              ".__" + node.bases[0].attrname
laurent@371:                 base_class = self._name(node.bases[0].expr, None) + \
laurent@371:                              "." + node.bases[0].attrname
laurent@371:             else:
laurent@371:                 raise TranslationError("unsupported type (in _class)", node.bases[0])
laurent@371: 
laurent@371:             current_klass.set_base(base_class)
laurent@371:         else:
laurent@371:             raise TranslationError("more than one base (in _class)", node)
laurent@371: 
laurent@371:         print >>self.output, UU+class_name_ + " = function () {"
laurent@371:         # call superconstructor
laurent@371:         #if base_class:
laurent@371:         #    print >>self.output, "    __" + base_class + ".call(this);"
laurent@371:         print >>self.output, "}"
laurent@371: 
laurent@371:         if not init_method:
laurent@371:             init_method = ast.Function([], "__init__", ["self"], [], 0, None, [])
laurent@371:             #self._method(init_method, current_klass, class_name)
laurent@371: 
laurent@371:         # Generate a function which constructs the object
laurent@371:         clsfunc = ast.Function([],
laurent@371:            node.name,
laurent@371:            init_method.argnames[1:],
laurent@371:            init_method.defaults,
laurent@371:            init_method.flags,
laurent@371:            None,
laurent@371:            [ast.Discard(ast.CallFunc(ast.Name("JS"), [ast.Const(
laurent@371: #            I attempted lazy initialization, but then you can't access static class members
laurent@371: #            "    if(!__"+base_class+".__was_initialized__)"+
laurent@371: #            "        __" + class_name + "_initialize();\n" +
laurent@371:             "    var instance = new " + UU + class_name_ + "();\n" +
laurent@371:             "    if(instance.__init__) instance.__init__.apply(instance, arguments);\n" +
laurent@371:             "    return instance;"
laurent@371:             )]))])
laurent@371: 
laurent@371:         self._function(clsfunc, False)
laurent@371:         print >>self.output, UU+class_name_ + ".__initialize__ = function () {"
laurent@371:         print >>self.output, "    if("+UU+class_name_+".__was_initialized__) return;"
laurent@371:         print >>self.output, "    "+UU+class_name_+".__was_initialized__ = true;"
laurent@371:         cls_obj = UU+class_name_ + '.prototype.__class__'
laurent@371: 
laurent@371:         if class_name == "pyjslib.__Object":
laurent@371:             print >>self.output, "    "+cls_obj+" = {};"
laurent@371:         else:
laurent@371:             if base_class and base_class not in ("object", "pyjslib.__Object"):
laurent@371:                 print >>self.output, "    if(!"+UU+base_class_+".__was_initialized__)"
laurent@371:                 print >>self.output, "        "+UU+base_class_+".__initialize__();"
laurent@371:                 print >>self.output, "    pyjs_extend(" + UU+class_name_ + ", "+UU+base_class_+");"
laurent@371:             else:
laurent@371:                 print >>self.output, "    pyjs_extend(" + UU+class_name_ + ", "+UU+"pyjslib.__Object);"
laurent@371: 
laurent@371:         print >>self.output, "    "+cls_obj+".__new__ = "+UU+class_name+";"
laurent@371:         print >>self.output, "    "+cls_obj+".__name__ = '"+UU+node.name+"';"
laurent@371: 
laurent@371:         for child in node.code:
laurent@371:             if isinstance(child, ast.Pass):
laurent@371:                 pass
laurent@371:             elif isinstance(child, ast.Function):
laurent@371:                 self._method(child, current_klass, class_name, class_name_)
laurent@371:             elif isinstance(child, ast.Assign):
laurent@371:                 self.classattr(child, current_klass)
laurent@371:             elif isinstance(child, ast.Discard) and isinstance(child.expr, ast.Const):
laurent@371:                 # Probably a docstring, turf it
laurent@371:                 pass
laurent@371:             else:
laurent@371:                 raise TranslationError("unsupported type (in _class)", child)
laurent@371:         print >>self.output, "}"
laurent@371: 
laurent@371:         print >> self.output, class_name_+".__initialize__();"
laurent@371: 
laurent@371: 
laurent@371:     def classattr(self, node, current_klass):
laurent@371:         self._assign(node, current_klass, True)
laurent@371: 
laurent@371:     def _raise(self, node, current_klass):
laurent@371:         if node.expr2:
laurent@371:             raise TranslationError("More than one expression unsupported",
laurent@371:                                    node)
laurent@371:         print >> self.output, "throw (%s);" % self.expr(
laurent@371:             node.expr1, current_klass)
laurent@371: 
laurent@371:     def _method(self, node, current_klass, class_name, class_name_):
laurent@371:         # reset global var scope
laurent@371:         self.method_imported_globals = set()
laurent@371: 
laurent@371:         arg_names = list(node.argnames)
laurent@371: 
laurent@371:         classmethod = False
laurent@371:         staticmethod = False
laurent@371:         if node.decorators:
laurent@371:             for d in node.decorators:
laurent@371:                 if d.name == "classmethod":
laurent@371:                     classmethod = True
laurent@371:                 elif d.name == "staticmethod":
laurent@371:                     staticmethod = True
laurent@371: 
laurent@371:         if staticmethod:
laurent@371:             staticfunc = ast.Function([], class_name_+"."+node.name, node.argnames, node.defaults, node.flags, node.doc, node.code, node.lineno)
laurent@371:             self._function(staticfunc, True)
laurent@371:             print >>self.output, "    " + UU+class_name_ + ".prototype.__class__." + node.name + " = " + class_name_+"."+node.name+";";
laurent@371:             print >>self.output, "    " + UU+class_name_ + ".prototype.__class__." + node.name + ".static_method = true;";
laurent@371:             return
laurent@371:         else:
laurent@371:             if len(arg_names) == 0:
laurent@371:                 raise TranslationError("methods must take an argument 'self' (in _method)", node)
laurent@371:             self.method_self = arg_names[0]
laurent@371: 
laurent@371:             #if not classmethod and arg_names[0] != "self":
laurent@371:             #    raise TranslationError("first arg not 'self' (in _method)", node)
laurent@371: 
laurent@371:         normal_arg_names = arg_names[1:]
laurent@371:         if node.kwargs: kwargname = normal_arg_names.pop()
laurent@371:         if node.varargs: varargname = normal_arg_names.pop()
laurent@371:         declared_arg_names = list(normal_arg_names)
laurent@371:         if node.kwargs: declared_arg_names.append(kwargname)
laurent@371: 
laurent@371:         function_args = "(" + ", ".join(declared_arg_names) + ")"
laurent@371: 
laurent@371:         if classmethod:
laurent@371:             fexpr = UU + class_name_ + ".prototype.__class__." + node.name
laurent@371:         else:
laurent@371:             fexpr = UU + class_name_ + ".prototype." + node.name
laurent@371:         print >>self.output, "    "+fexpr + " = function" + function_args + " {"
laurent@371: 
laurent@371:         # default arguments
laurent@371:         self._default_args_handler(node, normal_arg_names, current_klass)
laurent@371: 
laurent@371:         local_arg_names = normal_arg_names + declared_arg_names 
laurent@371: 
laurent@371:         if node.varargs:
laurent@371:             self._varargs_handler(node, varargname, declared_arg_names, current_klass)
laurent@371:             local_arg_names.append(varargname)
laurent@371: 
laurent@371: 
laurent@371:         # stack of local variable names for this function call
laurent@371:         self.local_arg_stack.append(local_arg_names)
laurent@371: 
laurent@371:         for child in node.code:
laurent@371:             self._stmt(child, current_klass)
laurent@371: 
laurent@371:         # remove the top local arg names
laurent@371:         self.local_arg_stack.pop()
laurent@371: 
laurent@371:         print >>self.output, "    };"
laurent@371: 
laurent@371:         self._kwargs_parser(node, fexpr, normal_arg_names, current_klass)
laurent@371: 
laurent@371:         if classmethod:
laurent@371:             # Have to create a version on the instances which automatically passes the
laurent@371:             # class as "self"
laurent@371:             altexpr = UU + class_name_ + ".prototype." + node.name
laurent@371:             print >>self.output, "    "+altexpr + " = function() {"
laurent@371:             print >>self.output, "        return " + fexpr + ".apply(this.__class__, arguments);"
laurent@371:             print >>self.output, "    };"
laurent@371:             print >>self.output, "    "+fexpr+".class_method = true;"
laurent@371:             print >>self.output, "    "+altexpr+".instance_method = true;"
laurent@371:         else:
laurent@371:             # For instance methods, we need an unbound version in the class object
laurent@371:             altexpr = UU + class_name_ + ".prototype.__class__." + node.name
laurent@371:             print >>self.output, "    "+altexpr + " = function() {"
laurent@371:             print >>self.output, "        return " + fexpr + ".call.apply("+fexpr+", arguments);"
laurent@371:             print >>self.output, "    };"
laurent@371:             print >>self.output, "    "+altexpr+".unbound_method = true;"
laurent@371:             print >>self.output, "    "+fexpr+".instance_method = true;"
laurent@371:             print >>self.output, "    "+altexpr+".__name__ = '%s';" % node.name
laurent@371: 
laurent@371:         print >>self.output, UU + class_name_ + ".prototype.%s.__name__ = '%s';" % \
laurent@371:                 (node.name, node.name)
laurent@371: 
laurent@371:         if node.kwargs or len(node.defaults):
laurent@371:             print >>self.output, "    "+altexpr + ".parse_kwargs = " + fexpr + ".parse_kwargs;"
laurent@371: 
laurent@371:         self.method_self = None
laurent@371:         self.method_imported_globals = set()
laurent@371: 
laurent@371:     def _isNativeFunc(self, node):
laurent@371:         if isinstance(node, ast.Discard):
laurent@371:             if isinstance(node.expr, ast.CallFunc):
laurent@371:                 if isinstance(node.expr.node, ast.Name) and \
laurent@371:                        node.expr.node.name == NATIVE_JS_FUNC_NAME:
laurent@371:                     return True
laurent@371:         return False
laurent@371: 
laurent@371:     def _stmt(self, node, current_klass):
laurent@371:         debugStmt = self.debug and not self._isNativeFunc(node)
laurent@371:         if debugStmt:
laurent@371:             print >>self.output, '  try {'
laurent@371: 
laurent@371:         if isinstance(node, ast.Return):
laurent@371:             self._return(node, current_klass)
laurent@371:         elif isinstance(node, ast.Break):
laurent@371:             self._break(node, current_klass)
laurent@371:         elif isinstance(node, ast.Continue):
laurent@371:             self._continue(node, current_klass)
laurent@371:         elif isinstance(node, ast.Assign):
laurent@371:             self._assign(node, current_klass)
laurent@371:         elif isinstance(node, ast.AugAssign):
laurent@371:             self._augassign(node, current_klass)
laurent@371:         elif isinstance(node, ast.Discard):
laurent@371:             self._discard(node, current_klass)
laurent@371:         elif isinstance(node, ast.If):
laurent@371:             self._if(node, current_klass)
laurent@371:         elif isinstance(node, ast.For):
laurent@371:             self._for(node, current_klass)
laurent@371:         elif isinstance(node, ast.While):
laurent@371:             self._while(node, current_klass)
laurent@371:         elif isinstance(node, ast.Subscript):
laurent@371:             self._subscript_stmt(node, current_klass)
laurent@371:         elif isinstance(node, ast.Global):
laurent@371:             self._global(node, current_klass)
laurent@371:         elif isinstance(node, ast.Pass):
laurent@371:             pass
laurent@371:         elif isinstance(node, ast.Function):
laurent@371:             self._function(node, True)
laurent@371:         elif isinstance(node, ast.Printnl):
laurent@371:            self._print(node, current_klass)
laurent@371:         elif isinstance(node, ast.Print):
laurent@371:            self._print(node, current_klass)
laurent@371:         elif isinstance(node, ast.TryExcept):
laurent@371:             self._tryExcept(node, current_klass)
laurent@371:         elif isinstance(node, ast.Raise):
laurent@371:             self._raise(node, current_klass)
laurent@371:         else:
laurent@371:             raise TranslationError("unsupported type (in _stmt)", node)
laurent@371: 
laurent@371:         if debugStmt:
laurent@371: 
laurent@371:             lt = self.get_line_trace(node)
laurent@371: 
laurent@371:             haltException = self.module_prefix + "HaltException"
laurent@371:             isHaltFunction = self.module_prefix + "IsHaltException"
laurent@371: 
laurent@371:             print >>self.output, '  } catch (__err) {'
laurent@371:             print >>self.output, '      if (' + isHaltFunction + '(__err.name)) {'
laurent@371:             print >>self.output, '          throw __err;'
laurent@371:             print >>self.output, '      } else {'
laurent@371:             print >>self.output, "          st = sys.printstack() + "\
laurent@371:                                                 + '"%s"' % lt + "+ '\\n' ;"
laurent@371:             print >>self.output, '          alert("' + "Error in " \
laurent@371:                                                 + lt + '"' \
laurent@371:                                                 + '+"\\n"+__err.name+": "+__err.message'\
laurent@371:                                                 + '+"\\n\\nStack trace:\\n"' \
laurent@371:                                                 + '+st' \
laurent@371:                                                 + ');'
laurent@371:             print >>self.output, '          debugger;'
laurent@371: 
laurent@371:             print >>self.output, '          throw new ' + self.module_prefix + "HaltException();"
laurent@371:             print >>self.output, '      }'
laurent@371:             print >>self.output, '  }'
laurent@371: 
laurent@371: 
laurent@371:     def get_line_trace(self, node):
laurent@371:         lineNum = "Unknown"
laurent@371:         srcLine = ""
laurent@371:         if hasattr(node, "lineno"):
laurent@371:             if node.lineno != None:
laurent@371:                 lineNum = node.lineno
laurent@371:                 srcLine = self.src[min(lineNum, len(self.src))-1]
laurent@371:                 srcLine = srcLine.replace('\\', '\\\\')
laurent@371:                 srcLine = srcLine.replace('"', '\\"')
laurent@371:                 srcLine = srcLine.replace("'", "\\'")
laurent@371: 
laurent@371:         return self.raw_module_name + ".py, line " \
laurent@371:                + str(lineNum) + ":"\
laurent@371:                + "\\n" \
laurent@371:                + "    " + srcLine
laurent@371: 
laurent@371:     def _augassign(self, node, current_klass):
laurent@371:         v = node.node
laurent@371:         if isinstance(v, ast.Getattr):
laurent@371:             # XXX HACK!  don't allow += on return result of getattr.
laurent@371:             # TODO: create a temporary variable or something.
laurent@371:             lhs = self._getattr(v, current_klass, False)
laurent@371:         else:
laurent@371:             lhs = self._name(node.node, current_klass)
laurent@371:         op = node.op
laurent@371:         rhs = self.expr(node.expr, current_klass)
laurent@371:         print >>self.output, "    " + lhs + " " + op + " " + rhs + ";"
laurent@371: 
laurent@371: 
laurent@371:     def _assign(self, node, current_klass, top_level = False):
laurent@371:         if len(node.nodes) != 1:
laurent@371:             tempvar = '__temp'+str(node.lineno)
laurent@371:             tnode = ast.Assign([ast.AssName(tempvar, "OP_ASSIGN", node.lineno)], node.expr, node.lineno)
laurent@371:             self._assign(tnode, current_klass, top_level)
laurent@371:             for v in node.nodes:
laurent@371:                tnode2 = ast.Assign([v], ast.Name(tempvar, node.lineno), node.lineno)
laurent@371:                self._assign(tnode2, current_klass, top_level)
laurent@371:             return
laurent@371: 
laurent@371:         local_var_names = None
laurent@371:         if len(self.local_arg_stack) > 0:
laurent@371:             local_var_names = self.local_arg_stack[-1]
laurent@371: 
laurent@371:         def _lhsFromAttr(v, current_klass):
laurent@371:             attr_name = v.attrname
laurent@371:             if isinstance(v.expr, ast.Name):
laurent@371:                 obj = v.expr.name
laurent@371:                 lhs = self._name(v.expr, current_klass) + "." + attr_name
laurent@371:             elif isinstance(v.expr, ast.Getattr):
laurent@371:                 lhs = self._getattr(v, current_klass)
laurent@371:             elif isinstance(v.expr, ast.Subscript):
laurent@371:                 lhs = self._subscript(v.expr, current_klass) + "." + attr_name
laurent@371:             else:
laurent@371:                 raise TranslationError("unsupported type (in _assign)", v.expr)
laurent@371:             return lhs
laurent@371: 
laurent@371:         def _lhsFromName(v, top_level, current_klass):
laurent@371:             if top_level:
laurent@371:                 if current_klass:
laurent@371:                     lhs = UU+current_klass.name_ + ".prototype.__class__." \
laurent@371:                                + v.name
laurent@371:                 else:
laurent@371:                     self.top_level_vars.add(v.name)
laurent@371:                     vname = self.modpfx() + v.name
laurent@371:                     if not self.modpfx() and v.name not in\
laurent@371:                            self.method_imported_globals:
laurent@371:                         lhs = "var " + vname
laurent@371:                     else:
laurent@371:                         lhs = UU + vname
laurent@371:                     self.add_local_arg(v.name)
laurent@371:             else:
laurent@371:                 if v.name in local_var_names:
laurent@371:                     lhs = v.name
laurent@371:                 elif v.name in self.method_imported_globals:
laurent@371:                     lhs = self.modpfx() + v.name
laurent@371:                 else:
laurent@371:                     lhs = "var " + v.name
laurent@371:                     self.add_local_arg(v.name)
laurent@371:             return lhs
laurent@371: 
laurent@371:         dbg = 0
laurent@371:         v = node.nodes[0]
laurent@371:         if isinstance(v, ast.AssAttr):
laurent@371:             lhs = _lhsFromAttr(v, current_klass)
laurent@371:             if v.flags == "OP_ASSIGN":
laurent@371:                 op = "="
laurent@371:             else:
laurent@371:                 raise TranslationError("unsupported flag (in _assign)", v)
laurent@371: 
laurent@371:         elif isinstance(v, ast.AssName):
laurent@371:             lhs = _lhsFromName(v, top_level, current_klass)
laurent@371:             if v.flags == "OP_ASSIGN":
laurent@371:                 op = "="
laurent@371:             else:
laurent@371:                 raise TranslationError("unsupported flag (in _assign)", v)
laurent@371:         elif isinstance(v, ast.Subscript):
laurent@371:             if v.flags == "OP_ASSIGN":
laurent@371:                 obj = self.expr(v.expr, current_klass)
laurent@371:                 if len(v.subs) != 1:
laurent@371:                     raise TranslationError("must have one sub (in _assign)", v)
laurent@371:                 idx = self.expr(v.subs[0], current_klass)
laurent@371:                 value = self.expr(node.expr, current_klass)
laurent@371:                 print >>self.output, "    " + obj + ".__setitem__(" + idx + ", " + value + ");"
laurent@371:                 return
laurent@371:             else:
laurent@371:                 raise TranslationError("unsupported flag (in _assign)", v)
laurent@371:         elif isinstance(v, (ast.AssList, ast.AssTuple)):
laurent@371:             uniqueID = self.nextTupleAssignID
laurent@371:             self.nextTupleAssignID += 1
laurent@371:             tempName = "__tupleassign" + str(uniqueID) + "__"
laurent@371:             print >>self.output, "    var " + tempName + " = " + \
laurent@371:                                  self.expr(node.expr, current_klass) + ";"
laurent@371:             for index,child in enumerate(v.getChildNodes()):
laurent@371:                 rhs = tempName + ".__getitem__(" + str(index) + ")"
laurent@371: 
laurent@371:                 if isinstance(child, ast.AssAttr):
laurent@371:                     lhs = _lhsFromAttr(child, current_klass)
laurent@371:                 elif isinstance(child, ast.AssName):
laurent@371:                     lhs = _lhsFromName(child, top_level, current_klass)
laurent@371:                 elif isinstance(child, ast.Subscript):
laurent@371:                     if child.flags == "OP_ASSIGN":
laurent@371:                         obj = self.expr(child.expr, current_klass)
laurent@371:                         if len(child.subs) != 1:
laurent@371:                             raise TranslationError("must have one sub " +
laurent@371:                                                    "(in _assign)", child)
laurent@371:                         idx = self.expr(child.subs[0], current_klass)
laurent@371:                         value = self.expr(node.expr, current_klass)
laurent@371:                         print >>self.output, "    " + obj + ".__setitem__(" \
laurent@371:                                            + idx + ", " + rhs + ");"
laurent@371:                         continue
laurent@371:                 print >>self.output, "    " + lhs + " = " + rhs + ";"
laurent@371:             return
laurent@371:         else:
laurent@371:             raise TranslationError("unsupported type (in _assign)", v)
laurent@371: 
laurent@371:         rhs = self.expr(node.expr, current_klass)
laurent@371:         if dbg:
laurent@371:             print "b", repr(node.expr), rhs
laurent@371:         print >>self.output, "    " + lhs + " " + op + " " + rhs + ";"
laurent@371: 
laurent@371: 
laurent@371:     def _discard(self, node, current_klass):
laurent@371:         
laurent@371:         if isinstance(node.expr, ast.CallFunc):
laurent@371:             debugStmt = self.debug and not self._isNativeFunc(node)
laurent@371:             if debugStmt and isinstance(node.expr.node, ast.Name) and \
laurent@371:                node.expr.node.name == 'import_wait':
laurent@371:                debugStmt = False
laurent@371:             if debugStmt:
laurent@371:                 st = self.get_line_trace(node)
laurent@371:                 print >>self.output, "sys.addstack('%s');\n" % st
laurent@371:             if isinstance(node.expr.node, ast.Name) and node.expr.node.name == NATIVE_JS_FUNC_NAME:
laurent@371:                 if len(node.expr.args) != 1:
laurent@371:                     raise TranslationError("native javascript function %s must have one arg" % NATIVE_JS_FUNC_NAME, node.expr)
laurent@371:                 if not isinstance(node.expr.args[0], ast.Const):
laurent@371:                     raise TranslationError("native javascript function %s must have constant arg" % NATIVE_JS_FUNC_NAME, node.expr)
laurent@371:                 raw_js = node.expr.args[0].value
laurent@371:                 print >>self.output, raw_js
laurent@371:             else:
laurent@371:                 expr = self._callfunc(node.expr, current_klass)
laurent@371:                 print >>self.output, "    " + expr + ";"
laurent@371: 
laurent@371:             if debugStmt:
laurent@371:                 print >>self.output, "sys.popstack();\n"
laurent@371: 
laurent@371:         elif isinstance(node.expr, ast.Const):
laurent@371:             if node.expr.value is not None: # Empty statements generate ignore None
laurent@371:                 print >>self.output, self._const(node.expr)
laurent@371:         else:
laurent@371:             raise TranslationError("unsupported type (in _discard)", node.expr)
laurent@371: 
laurent@371: 
laurent@371:     def _if(self, node, current_klass):
laurent@371:         for i in range(len(node.tests)):
laurent@371:             test, consequence = node.tests[i]
laurent@371:             if i == 0:
laurent@371:                 keyword = "if"
laurent@371:             else:
laurent@371:                 keyword = "else if"
laurent@371: 
laurent@371:             self._if_test(keyword, test, consequence, current_klass)
laurent@371: 
laurent@371:         if node.else_:
laurent@371:             keyword = "else"
laurent@371:             test = None
laurent@371:             consequence = node.else_
laurent@371: 
laurent@371:             self._if_test(keyword, test, consequence, current_klass)
laurent@371: 
laurent@371: 
laurent@371:     def _if_test(self, keyword, test, consequence, current_klass):
laurent@371:         if test:
laurent@371:             expr = self.expr(test, current_klass)
laurent@371: 
laurent@371:             print >>self.output, "    " + keyword + " (pyjslib.bool(" + expr + ")) {"
laurent@371:         else:
laurent@371:             print >>self.output, "    " + keyword + " {"
laurent@371: 
laurent@371:         if isinstance(consequence, ast.Stmt):
laurent@371:             for child in consequence.nodes:
laurent@371:                 self._stmt(child, current_klass)
laurent@371:         else:
laurent@371:             raise TranslationError("unsupported type (in _if_test)", consequence)
laurent@371: 
laurent@371:         print >>self.output, "    }"
laurent@371: 
laurent@371: 
laurent@371:     def _from(self, node):
laurent@371:         for name in node.names:
laurent@371:             # look up "hack" in AppTranslator as to how findFile gets here
laurent@371:             module_name = node.modname + "." + name[0]
laurent@371:             try:
laurent@371:                 ff = self.findFile(module_name + ".py")
laurent@371:             except Exception:
laurent@371:                 ff = None
laurent@371:             if ff:
laurent@371:                 self.add_imported_module(module_name)
laurent@371:             else:
laurent@371:                 self.imported_classes[name[0]] = node.modname
laurent@371: 
laurent@371: 
laurent@371:     def _compare(self, node, current_klass):
laurent@371:         lhs = self.expr(node.expr, current_klass)
laurent@371: 
laurent@371:         if len(node.ops) != 1:
laurent@371:             raise TranslationError("only one ops supported (in _compare)", node)
laurent@371: 
laurent@371:         op = node.ops[0][0]
laurent@371:         rhs_node = node.ops[0][1]
laurent@371:         rhs = self.expr(rhs_node, current_klass)
laurent@371: 
laurent@371:         if op == "==":
laurent@371:             return "pyjslib.eq(%s, %s)" % (lhs, rhs)
laurent@371:         if op == "in":
laurent@371:             return rhs + ".__contains__(" + lhs + ")"
laurent@371:         elif op == "not in":
laurent@371:             return "!" + rhs + ".__contains__(" + lhs + ")"
laurent@371:         elif op == "is":
laurent@371:             op = "==="
laurent@371:         elif op == "is not":
laurent@371:             op = "!=="
laurent@371: 
laurent@371:         return "(" + lhs + " " + op + " " + rhs + ")"
laurent@371: 
laurent@371: 
laurent@371:     def _not(self, node, current_klass):
laurent@371:         expr = self.expr(node.expr, current_klass)
laurent@371: 
laurent@371:         return "!(" + expr + ")"
laurent@371: 
laurent@371:     def _or(self, node, current_klass):
laurent@371:         expr = "("+(") || (".join([self.expr(child, current_klass) for child in node.nodes]))+')'
laurent@371:         return expr
laurent@371: 
laurent@371:     def _and(self, node, current_klass):
laurent@371:         expr = "("+(") && (".join([self.expr(child, current_klass) for child in node.nodes]))+")"
laurent@371:         return expr
laurent@371: 
laurent@371:     def _for(self, node, current_klass):
laurent@371:         assign_name = ""
laurent@371:         assign_tuple = ""
laurent@371: 
laurent@371:         # based on Bob Ippolito's Iteration in Javascript code
laurent@371:         if isinstance(node.assign, ast.AssName):
laurent@371:             assign_name = node.assign.name
laurent@371:             self.add_local_arg(assign_name)
laurent@371:             if node.assign.flags == "OP_ASSIGN":
laurent@371:                 op = "="
laurent@371:         elif isinstance(node.assign, ast.AssTuple):
laurent@371:             op = "="
laurent@371:             i = 0
laurent@371:             for child in node.assign:
laurent@371:                 child_name = child.name
laurent@371:                 if assign_name == "":
laurent@371:                     assign_name = "temp_" + child_name
laurent@371:                 self.add_local_arg(child_name)
laurent@371:                 assign_tuple += """
laurent@371:                 var %(child_name)s %(op)s %(assign_name)s.__getitem__(%(i)i);
laurent@371:                 """ % locals()
laurent@371:                 i += 1
laurent@371:         else:
laurent@371:             raise TranslationError("unsupported type (in _for)", node.assign)
laurent@371: 
laurent@371:         if isinstance(node.list, ast.Name):
laurent@371:             list_expr = self._name(node.list, current_klass)
laurent@371:         elif isinstance(node.list, ast.Getattr):
laurent@371:             list_expr = self._getattr(node.list, current_klass)
laurent@371:         elif isinstance(node.list, ast.CallFunc):
laurent@371:             list_expr = self._callfunc(node.list, current_klass)
laurent@371:         else:
laurent@371:             raise TranslationError("unsupported type (in _for)", node.list)
laurent@371: 
laurent@371:         lhs = "var " + assign_name
laurent@371:         iterator_name = "__" + assign_name
laurent@371: 
laurent@371:         print >>self.output, """
laurent@371:         var %(iterator_name)s = %(list_expr)s.__iter__();
laurent@371:         try {
laurent@371:             while (true) {
laurent@371:                 %(lhs)s %(op)s %(iterator_name)s.next();
laurent@371:                 %(assign_tuple)s
laurent@371:         """ % locals()
laurent@371:         for node in node.body.nodes:
laurent@371:             self._stmt(node, current_klass)
laurent@371:         print >>self.output, """
laurent@371:             }
laurent@371:         } catch (e) {
laurent@371:             if (e.__name__ != pyjslib.StopIteration.__name__) {
laurent@371:                 throw e;
laurent@371:             }
laurent@371:         }
laurent@371:         """ % locals()
laurent@371: 
laurent@371: 
laurent@371:     def _while(self, node, current_klass):
laurent@371:         test = self.expr(node.test, current_klass)
laurent@371:         print >>self.output, "    while (pyjslib.bool(" + test + ")) {"
laurent@371:         if isinstance(node.body, ast.Stmt):
laurent@371:             for child in node.body.nodes:
laurent@371:                 self._stmt(child, current_klass)
laurent@371:         else:
laurent@371:             raise TranslationError("unsupported type (in _while)", node.body)
laurent@371:         print >>self.output, "    }"
laurent@371: 
laurent@371: 
laurent@371:     def _const(self, node):
laurent@371:         if isinstance(node.value, int):
laurent@371:             return str(node.value)
laurent@371:         elif isinstance(node.value, float):
laurent@371:             return str(node.value)
laurent@371:         elif isinstance(node.value, basestring):
laurent@371:             v = node.value
laurent@371:             if isinstance(node.value, unicode):
laurent@371:                 v = v.encode('utf-8')
laurent@371:             return  "String('%s')" % escapejs(v)
laurent@371:         elif node.value is None:
laurent@371:             return "null"
laurent@371:         else:
laurent@371:             raise TranslationError("unsupported type (in _const)", node)
laurent@371: 
laurent@371:     def _unaryadd(self, node, current_klass):
laurent@371:         return self.expr(node.expr, current_klass)
laurent@371: 
laurent@371:     def _unarysub(self, node, current_klass):
laurent@371:         return "-" + self.expr(node.expr, current_klass)
laurent@371: 
laurent@371:     def _add(self, node, current_klass):
laurent@371:         return self.expr(node.left, current_klass) + " + " + self.expr(node.right, current_klass)
laurent@371: 
laurent@371:     def _sub(self, node, current_klass):
laurent@371:         return self.expr(node.left, current_klass) + " - " + self.expr(node.right, current_klass)
laurent@371: 
laurent@371:     def _div(self, node, current_klass):
laurent@371:         return self.expr(node.left, current_klass) + " / " + self.expr(node.right, current_klass)
laurent@371: 
laurent@371:     def _mul(self, node, current_klass):
laurent@371:         return self.expr(node.left, current_klass) + " * " + self.expr(node.right, current_klass)
laurent@371: 
laurent@371:     def _mod(self, node, current_klass):
laurent@371:         if isinstance(node.left, ast.Const) and isinstance(node.left.value, StringType):
laurent@371:            self.imported_js.add("sprintf.js") # Include the sprintf functionality if it is used
laurent@371:            return "sprintf("+self.expr(node.left, current_klass) + ", " + self.expr(node.right, current_klass)+")"
laurent@371:         return self.expr(node.left, current_klass) + " % " + self.expr(node.right, current_klass)
laurent@371: 
laurent@371:     def _invert(self, node, current_klass):
laurent@371:         return "~" + self.expr(node.expr, current_klass)
laurent@371: 
laurent@371:     def _bitand(self, node, current_klass):
laurent@371:         return " & ".join([self.expr(child, current_klass) for child in node.nodes])
laurent@371: 
laurent@371:     def _bitshiftleft(self, node, current_klass):
laurent@371:         return self.expr(node.left, current_klass) + " << " + self.expr(node.right, current_klass)
laurent@371: 
laurent@371:     def _bitshiftright(self, node, current_klass):
laurent@371:         return self.expr(node.left, current_klass) + " >>> " + self.expr(node.right, current_klass)
laurent@371: 
laurent@371:     def _bitxor(self,node, current_klass):
laurent@371:         return " ^ ".join([self.expr(child, current_klass) for child in node.nodes])
laurent@371: 
laurent@371:     def _bitor(self, node, current_klass):
laurent@371:         return " | ".join([self.expr(child, current_klass) for child in node.nodes])
laurent@371: 
laurent@371:     def _subscript(self, node, current_klass):
laurent@371:         if node.flags == "OP_APPLY":
laurent@371:             if len(node.subs) == 1:
laurent@371:                 return self.expr(node.expr, current_klass) + ".__getitem__(" + self.expr(node.subs[0], current_klass) + ")"
laurent@371:             else:
laurent@371:                 raise TranslationError("must have one sub (in _subscript)", node)
laurent@371:         else:
laurent@371:             raise TranslationError("unsupported flag (in _subscript)", node)
laurent@371: 
laurent@371:     def _subscript_stmt(self, node, current_klass):
laurent@371:         if node.flags == "OP_DELETE":
laurent@371:             print >>self.output, "    " + self.expr(node.expr, current_klass) + ".__delitem__(" + self.expr(node.subs[0], current_klass) + ");"
laurent@371:         else:
laurent@371:             raise TranslationError("unsupported flag (in _subscript)", node)
laurent@371: 
laurent@371:     def _list(self, node, current_klass):
laurent@371:         return "new pyjslib.List([" + ", ".join([self.expr(x, current_klass) for x in node.nodes]) + "])"
laurent@371: 
laurent@371:     def _dict(self, node, current_klass):
laurent@371:         items = []
laurent@371:         for x in node.items:
laurent@371:             key = self.expr(x[0], current_klass)
laurent@371:             value = self.expr(x[1], current_klass)
laurent@371:             items.append("[" + key + ", " + value + "]")
laurent@371:         return "new pyjslib.Dict([" + ", ".join(items) + "])"
laurent@371: 
laurent@371:     def _tuple(self, node, current_klass):
laurent@371:         return "new pyjslib.Tuple([" + ", ".join([self.expr(x, current_klass) for x in node.nodes]) + "])"
laurent@371: 
laurent@371:     def _lambda(self, node, current_klass):
laurent@371:         if node.varargs:
laurent@371:             raise TranslationError("varargs are not supported in Lambdas", node)
laurent@371:         if node.kwargs:
laurent@371:             raise TranslationError("kwargs are not supported in Lambdas", node)
laurent@371:         res = cStringIO.StringIO()
laurent@371:         arg_names = list(node.argnames)
laurent@371:         function_args = ", ".join(arg_names)
laurent@371:         for child in node.getChildNodes():
laurent@371:             expr = self.expr(child, None)
laurent@371:         print >> res, "function (%s){" % function_args
laurent@371:         self._default_args_handler(node, arg_names, None,
laurent@371:                                    output=res)
laurent@371:         print >> res, 'return %s;}' % expr
laurent@371:         return res.getvalue()
laurent@371: 
laurent@371:     def _slice(self, node, current_klass):
laurent@371:         if node.flags == "OP_APPLY":
laurent@371:             lower = "null"
laurent@371:             upper = "null"
laurent@371:             if node.lower != None:
laurent@371:                 lower = self.expr(node.lower, current_klass)
laurent@371:             if node.upper != None:
laurent@371:                 upper = self.expr(node.upper, current_klass)
laurent@371:             return  "pyjslib.slice(" + self.expr(node.expr, current_klass) + ", " + lower + ", " + upper + ")"
laurent@371:         else:
laurent@371:             raise TranslationError("unsupported flag (in _slice)", node)
laurent@371: 
laurent@371:     def _global(self, node, current_klass):
laurent@371:         for name in node.names:
laurent@371:             self.method_imported_globals.add(name)
laurent@371: 
laurent@371:     def expr(self, node, current_klass):
laurent@371:         if isinstance(node, ast.Const):
laurent@371:             return self._const(node)
laurent@371:         # @@@ not sure if the parentheses should be here or in individual operator functions - JKT
laurent@371:         elif isinstance(node, ast.Mul):
laurent@371:             return " ( " + self._mul(node, current_klass) + " ) "
laurent@371:         elif isinstance(node, ast.Add):
laurent@371:             return " ( " + self._add(node, current_klass) + " ) "
laurent@371:         elif isinstance(node, ast.Sub):
laurent@371:             return " ( " + self._sub(node, current_klass) + " ) "
laurent@371:         elif isinstance(node, ast.Div):
laurent@371:             return " ( " + self._div(node, current_klass) + " ) "
laurent@371:         elif isinstance(node, ast.Mod):
laurent@371:             return self._mod(node, current_klass)
laurent@371:         elif isinstance(node, ast.UnaryAdd):
laurent@371:             return self._unaryadd(node, current_klass)
laurent@371:         elif isinstance(node, ast.UnarySub):
laurent@371:             return self._unarysub(node, current_klass)
laurent@371:         elif isinstance(node, ast.Not):
laurent@371:             return self._not(node, current_klass)
laurent@371:         elif isinstance(node, ast.Or):
laurent@371:             return self._or(node, current_klass)
laurent@371:         elif isinstance(node, ast.And):
laurent@371:             return self._and(node, current_klass)
laurent@371:         elif isinstance(node, ast.Invert):
laurent@371:             return self._invert(node, current_klass)
laurent@371:         elif isinstance(node, ast.Bitand):
laurent@371:             return "("+self._bitand(node, current_klass)+")"
laurent@371:         elif isinstance(node,ast.LeftShift):
laurent@371:             return self._bitshiftleft(node, current_klass)
laurent@371:         elif isinstance(node, ast.RightShift):
laurent@371:             return self._bitshiftright(node, current_klass)
laurent@371:         elif isinstance(node, ast.Bitxor):
laurent@371:             return "("+self._bitxor(node, current_klass)+")"
laurent@371:         elif isinstance(node, ast.Bitor):
laurent@371:             return "("+self._bitor(node, current_klass)+")"
laurent@371:         elif isinstance(node, ast.Compare):
laurent@371:             return self._compare(node, current_klass)
laurent@371:         elif isinstance(node, ast.CallFunc):
laurent@371:             return self._callfunc(node, current_klass)
laurent@371:         elif isinstance(node, ast.Name):
laurent@371:             return self._name(node, current_klass)
laurent@371:         elif isinstance(node, ast.Subscript):
laurent@371:             return self._subscript(node, current_klass)
laurent@371:         elif isinstance(node, ast.Getattr):
laurent@371:             return self._getattr(node, current_klass)
laurent@371:         elif isinstance(node, ast.List):
laurent@371:             return self._list(node, current_klass)
laurent@371:         elif isinstance(node, ast.Dict):
laurent@371:             return self._dict(node, current_klass)
laurent@371:         elif isinstance(node, ast.Tuple):
laurent@371:             return self._tuple(node, current_klass)
laurent@371:         elif isinstance(node, ast.Slice):
laurent@371:             return self._slice(node, current_klass)
laurent@371:         elif isinstance(node, ast.Lambda):
laurent@371:             return self._lambda(node, current_klass)
laurent@371:         else:
laurent@371:             raise TranslationError("unsupported type (in expr)", node)
laurent@371: 
laurent@371: 
laurent@371: 
laurent@371: import cStringIO
laurent@371: 
laurent@371: def translate(file_name, module_name, debug=False):
laurent@371:     f = file(file_name, "r")
laurent@371:     src = f.read()
laurent@371:     f.close()
laurent@371:     output = cStringIO.StringIO()
laurent@371:     mod = compiler.parseFile(file_name)
laurent@371:     t = Translator(module_name, module_name, module_name, src, debug, mod, output)
laurent@371:     return output.getvalue()
laurent@371: 
laurent@371: 
laurent@371: class PlatformParser:
laurent@371:     def __init__(self, platform_dir = "", verbose=True):
laurent@371:         self.platform_dir = platform_dir
laurent@371:         self.parse_cache = {}
laurent@371:         self.platform = ""
laurent@371:         self.verbose = verbose
laurent@371: 
laurent@371:     def setPlatform(self, platform):
laurent@371:         self.platform = platform
laurent@371: 
laurent@371:     def parseModule(self, module_name, file_name):
laurent@371: 
laurent@371:         importing = False
laurent@371:         if not self.parse_cache.has_key(file_name):
laurent@371:             importing = True
laurent@371:             mod = compiler.parseFile(file_name)
laurent@371:             self.parse_cache[file_name] = mod
laurent@371:         else:
laurent@371:             mod = self.parse_cache[file_name]
laurent@371: 
laurent@371:         override = False
laurent@371:         platform_file_name = self.generatePlatformFilename(file_name)
laurent@371:         if self.platform and os.path.isfile(platform_file_name):
laurent@371:             mod = copy.deepcopy(mod)
laurent@371:             mod_override = compiler.parseFile(platform_file_name)
laurent@371:             self.merge(mod, mod_override)
laurent@371:             override = True
laurent@371: 
laurent@371:         if self.verbose:
laurent@371:             if override:
laurent@371:                 print "Importing %s (Platform %s)" % (module_name, self.platform)
laurent@371:             elif importing:
laurent@371:                 print "Importing %s" % (module_name)
laurent@371: 
laurent@371:         return mod, override
laurent@371: 
laurent@371:     def generatePlatformFilename(self, file_name):
laurent@371:         (module_name, extension) = os.path.splitext(os.path.basename(file_name))
laurent@371:         platform_file_name = module_name + self.platform + extension
laurent@371: 
laurent@371:         return os.path.join(os.path.dirname(file_name), self.platform_dir, platform_file_name)
laurent@371: 
laurent@371:     def merge(self, tree1, tree2):
laurent@371:         for child in tree2.node:
laurent@371:             if isinstance(child, ast.Function):
laurent@371:                 self.replaceFunction(tree1, child.name, child)
laurent@371:             elif isinstance(child, ast.Class):
laurent@371:                 self.replaceClassMethods(tree1, child.name, child)
laurent@371: 
laurent@371:         return tree1
laurent@371: 
laurent@371:     def replaceFunction(self, tree, function_name, function_node):
laurent@371:         # find function to replace
laurent@371:         for child in tree.node:
laurent@371:             if isinstance(child, ast.Function) and child.name == function_name:
laurent@371:                 self.copyFunction(child, function_node)
laurent@371:                 return
laurent@371:         raise TranslationError("function not found: " + function_name, function_node)
laurent@371: 
laurent@371:     def replaceClassMethods(self, tree, class_name, class_node):
laurent@371:         # find class to replace
laurent@371:         old_class_node = None
laurent@371:         for child in tree.node:
laurent@371:             if isinstance(child, ast.Class) and child.name == class_name:
laurent@371:                 old_class_node = child
laurent@371:                 break
laurent@371: 
laurent@371:         if not old_class_node:
laurent@371:             raise TranslationError("class not found: " + class_name, class_node)
laurent@371: 
laurent@371:         # replace methods
laurent@371:         for function_node in class_node.code:
laurent@371:             if isinstance(function_node, ast.Function):
laurent@371:                 found = False
laurent@371:                 for child in old_class_node.code:
laurent@371:                     if isinstance(child, ast.Function) and child.name == function_node.name:
laurent@371:                         found = True
laurent@371:                         self.copyFunction(child, function_node)
laurent@371:                         break
laurent@371: 
laurent@371:                 if not found:
laurent@371:                     raise TranslationError("class method not found: " + class_name + "." + function_node.name, function_node)
laurent@371: 
laurent@371:     def copyFunction(self, target, source):
laurent@371:         target.code = source.code
laurent@371:         target.argnames = source.argnames
laurent@371:         target.defaults = source.defaults
laurent@371:         target.doc = source.doc # @@@ not sure we need to do this any more
laurent@371: 
laurent@371: def dotreplace(fname):
laurent@371:     path, ext = os.path.splitext(fname)
laurent@371:     return path.replace(".", "/") + ext
laurent@371: 
laurent@371: class AppTranslator:
laurent@371: 
laurent@371:     def __init__(self, library_dirs=[], parser=None, dynamic=False,
laurent@371:                  optimize=False, verbose=True):
laurent@371:         self.extension = ".py"
laurent@371:         self.optimize = optimize
laurent@371:         self.library_modules = []
laurent@371:         self.overrides = {}
laurent@371:         self.library_dirs = path + library_dirs
laurent@371:         self.dynamic = dynamic
laurent@371:         self.verbose = verbose
laurent@371: 
laurent@371:         if not parser:
laurent@371:             self.parser = PlatformParser()
laurent@371:         else:
laurent@371:             self.parser = parser
laurent@371: 
laurent@371:         self.parser.dynamic = dynamic
laurent@371: 
laurent@371:     def findFile(self, file_name):
laurent@371:         if os.path.isfile(file_name):
laurent@371:             return file_name
laurent@371: 
laurent@371:         for library_dir in self.library_dirs:
laurent@371:             file_name = dotreplace(file_name)
laurent@371:             full_file_name = os.path.join(
laurent@371:                 os.path.abspath(os.path.dirname(__file__)), library_dir, file_name)
laurent@371:             if os.path.isfile(full_file_name):
laurent@371:                 return full_file_name
laurent@371: 
laurent@371:             fnameinit, ext = os.path.splitext(file_name)
laurent@371:             fnameinit = fnameinit + "/__init__.py"
laurent@371: 
laurent@371:             full_file_name = os.path.join(
laurent@371:                 os.path.abspath(os.path.dirname(__file__)), library_dir, fnameinit)
laurent@371:             if os.path.isfile(full_file_name):
laurent@371:                 return full_file_name
laurent@371: 
laurent@371:         raise Exception("file not found: " + file_name)
laurent@371: 
laurent@371:     def _translate(self, module_name, is_app=True, debug=False,
laurent@371:                    imported_js=set()):
laurent@371:         if module_name not in self.library_modules:
laurent@371:             self.library_modules.append(module_name)
laurent@371: 
laurent@371:         file_name = self.findFile(module_name + self.extension)
laurent@371: 
laurent@371:         output = cStringIO.StringIO()
laurent@371: 
laurent@371:         f = file(file_name, "r")
laurent@371:         src = f.read()
laurent@371:         f.close()
laurent@371: 
laurent@371:         mod, override = self.parser.parseModule(module_name, file_name)
laurent@371:         if override:
laurent@371:             override_name = "%s.%s" % (self.parser.platform.lower(),
laurent@371:                                            module_name)
laurent@371:             self.overrides[override_name] = override_name
laurent@371:         if is_app:
laurent@371:             mn = '__main__'
laurent@371:         else:
laurent@371:             mn = module_name
laurent@371:         t = Translator(mn, module_name, module_name,
laurent@371:                        src, debug, mod, output, self.dynamic, self.optimize,
laurent@371:                        self.findFile)
laurent@371: 
laurent@371:         module_str = output.getvalue()
laurent@371:         imported_js.update(set(t.imported_js))
laurent@371:         imported_modules_str = ""
laurent@371:         for module in t.imported_modules:
laurent@371:             if module not in self.library_modules:
laurent@371:                 self.library_modules.append(module)
laurent@371:                 #imported_js.update(set(t.imported_js))
laurent@371:                 #imported_modules_str += self._translate(
laurent@371:                 #    module, False, debug=debug, imported_js=imported_js)
laurent@371: 
laurent@371:         return imported_modules_str + module_str
laurent@371: 
laurent@371: 
laurent@371:     def translate(self, module_name, is_app=True, debug=False,
laurent@371:                   library_modules=[]):
laurent@371:         app_code = cStringIO.StringIO()
laurent@371:         lib_code = cStringIO.StringIO()
laurent@371:         imported_js = set()
laurent@371:         self.library_modules = []
laurent@371:         self.overrides = {}
laurent@371:         for library in library_modules:
laurent@371:             if library.endswith(".js"):
laurent@371:                 imported_js.add(library)
laurent@371:                 continue
laurent@371:             self.library_modules.append(library)
laurent@371:             if self.verbose:
laurent@371:                 print 'Including LIB', library
laurent@371:             print >> lib_code, '\n//\n// BEGIN LIB '+library+'\n//\n'
laurent@371:             print >> lib_code, self._translate(
laurent@371:                 library, False, debug=debug, imported_js=imported_js)
laurent@371: 
laurent@371:             print >> lib_code, "/* initialize static library */"
laurent@371:             print >> lib_code, "%s%s();\n" % (UU, library)
laurent@371: 
laurent@371:             print >> lib_code, '\n//\n// END LIB '+library+'\n//\n'
laurent@371:         if module_name:
laurent@371:             print >> app_code, self._translate(
laurent@371:                 module_name, is_app, debug=debug, imported_js=imported_js)
laurent@371:         for js in imported_js:
laurent@371:            path = self.findFile(js)
laurent@371:            if os.path.isfile(path):
laurent@371:               if self.verbose:
laurent@371:                   print 'Including JS', js
laurent@371:               print >> lib_code,  '\n//\n// BEGIN JS '+js+'\n//\n'
laurent@371:               print >> lib_code, file(path).read()
laurent@371:               print >> lib_code,  '\n//\n// END JS '+js+'\n//\n'
laurent@371:            else:
laurent@371:               print >>sys.stderr, 'Warning: Unable to find imported javascript:', js
laurent@371:         return lib_code.getvalue(), app_code.getvalue()
laurent@371: 
laurent@371: usage = """
laurent@371:   usage: %s file_name [module_name]
laurent@371: """
laurent@371: 
laurent@371: def main():
laurent@371:     import sys
laurent@371:     if len(sys.argv)<2:
laurent@371:         print >> sys.stderr, usage % sys.argv[0]
laurent@371:         sys.exit(1)
laurent@371:     file_name = os.path.abspath(sys.argv[1])
laurent@371:     if not os.path.isfile(file_name):
laurent@371:         print >> sys.stderr, "File not found %s" % file_name
laurent@371:         sys.exit(1)
laurent@371:     if len(sys.argv) > 2:
laurent@371:         module_name = sys.argv[2]
laurent@371:     else:
laurent@371:         module_name = None
laurent@371:     print translate(file_name, module_name),
laurent@371: 
laurent@371: if __name__ == "__main__":
laurent@371:     main()
laurent@371: