def assign(conv, node, scope): rest = '' target = node.targets[0] if isinstance(target, ast.Tuple): left = 'var __pjs_tmp' rest = utils.deepleft(conv, target, [], scope) elif isinstance(target, ast.Subscript): left = conv.get_converter(target)(conv, target, scope, True) if left.endswith(' '): return left + conv.convert_node(node.value, scope) + ');\n' elif isinstance(target, ast.Name): left = utils.lhand_assign(target.id, scope) else: left = conv.convert_node(target, scope) for targ in node.targets[1:]: var = left if var.startswith('var '): var = var[len('var '):] if isinstance(targ, ast.Tuple): rest += utils.deepleft(conv, targ, [], scope, var) elif isinstance(targ, ast.Name): mr = utils.lhand_assign(targ.id, scope) rest += mr + ' = ' + var + ';\n' else: rest += '%s = %s;\n' % (conv.convert_node(targ, scope), var) js = conv.convert_node(node.value, scope) line = '%s = %s;\n' % (left, js) return line + rest
def _tryexcept(conv, node, scope): single = '''%s{ %s }''' body = conv.convert_block(node.body, scope) subs = [] temp = conv.get_temp('err') for handler in node.handlers: eb = '' if handler.name is not None: name = utils.lhand_assign(handler.name.id, scope) eb = ' %s = %s;\n ' % (name, temp) eb_ = conv.convert_block(handler.body, scope) eb += eb_ if handler.type is not None: t = conv.convert_node(handler.type, scope) top = 'if (%s.__class__ && $b.isinstance(%s, %s)) ' % (temp, temp, t) else: top = '' subs.append(single % (top, eb)) text = TRY_TPL % (body, temp, ' else '.join(subs)) conv.kill_temp('err') return text
def classdef(conv, node, scope): imports = [] dct = { 'name': node.name, 'bases': ', '.join(utils.resolve(base.id, scope) for base in node.bases), 'left': utils.lhand_assign(node.name, scope), 'rname': utils.resolve(node.name, scope), } dct['dec_front'] = '' dct['dec_back'] = '' for dec in node.decorator_list: dct['dec_front'] += conv.convert_node(dec, scope) + '(' dct['dec_back'] += ')' scope = scope.copy() scope.explicit_locals = True dct['contents'] = utils.fix_undef(conv.convert_block(node.body, scope), scope) dct['lnum'] = len(scope.parent_locals) return CLASS_TEMPLATE % dct
def _import(conv, node, scope): tpl = '%s = $b.__import__("%s", _.__name__, _.__file__);\n' text = '' for name in node.names: asname = name.name.split('.')[0] if name.asname: raise PJsException('import x as y not yet supported') asname = name.asname asname = utils.lhand_assign(asname, scope) text += tpl % (asname, name.name) conv.add_import(name.name) return text
def _augassign(conv, node, scope): tpl = '%s = $b.%s(%s, %s);\n' op = node.op.__class__.__name__.lower() ljs = conv.convert_node(node.target, scope) rjs = conv.convert_node(node.value, scope) if isinstance(node.target, ast.Subscript): left = conv.get_converter(node.target)(conv, node.target, scope, True) if left.endswith(' '): return left + conv.convert_node(node.value, scope) + ');\n' elif isinstance(node.target, ast.Name): left = utils.lhand_assign(node.target.id, scope) else: left = conv.convert_node(node.target, scope) return tpl % (left, op, ljs, rjs)
def _for(conv, node, scope): ible = conv.convert_node(node.iter, scope) temp_iter = conv.get_temp('iter') if isinstance(node.target, ast.Name): targ = utils.lhand_assign(node.target.id, scope) assign = '%s = %s.value;\n' % (targ, temp_iter) else: assign = utils.deepleft(conv, node.target, [], scope, '%s.value' % temp_iter).replace( '\n', '\n ') body = conv.convert_block(node.body, scope) conv.kill_temp('iter') return FOR_TPL % (temp_iter, ible, temp_iter, assign, body)
def importfrom(conv, node, scope): text = 'var __pjs_tmp_module = $b.__import__("%s", _.__name__, _.__file__);\n' % node.module base_name = node.module.split('.')[0] subs_name = '.'.join(node.module.split('.')[1:]) if subs_name: subs_name = '.' + subs_name prefix = utils.local_prefix(scope) for alias in node.names: if alias.name == '*': text += IMPORT_TEMPLATE % {'prefix': prefix, 'subs': subs_name} break asname = alias.asname or alias.name left = utils.lhand_assign(asname, scope) text += '%s = __pjs_tmp_module%s.%s;\n' % (left, subs_name, alias.name) conv.add_import(node.module) return text
def importfrom(conv, node, scope): text = 'var __pjs_tmp_module = $b.__import__("%s", _.__name__, _.__file__);\n' % node.module base_name = node.module.split('.')[0] subs_name = '.'.join(node.module.split('.')[1:]) if subs_name: subs_name = '.' + subs_name prefix = utils.local_prefix(scope) for alias in node.names: if alias.name == '*': text += IMPORT_TEMPLATE % {'prefix': prefix, 'subs':subs_name} break asname = alias.asname or alias.name left = utils.lhand_assign(asname, scope) text += '%s = __pjs_tmp_module%s.%s;\n' % (left, subs_name, alias.name) conv.add_import(node.module) return text
def functiondef(conv, node, scope): dct = { 'name': node.name, 'lineno': node.lineno, 'special': function_special(conv, node, scope), 'left': utils.lhand_assign(node.name, scope), 'rname': utils.resolve(node.name, scope), } args = function_args(conv, node, scope) dct['args'] = ', '.join(args) dct['dec_front'] = '' dct['dec_back'] = '' for dec in node.decorator_list: dct['dec_front'] += conv.convert_node(dec, scope) + '(' dct['dec_back'] += ')' scope = scope.copy() scope.explicit_locals = False scope.locals += args dct['contents'] = utils.fix_undef(conv.convert_block(node.body, scope), scope) return FUNC_TEMPLATE % dct