def _classdef(node, scope): imports = [] dct = {} dct['left'] = do_left(ast.Name(node.name, {}), scope) dct['name'] = node.name; dct['rname'] = resolve(node.name, scope); dct['bases'] = ', '.join(resolve(name.id, scope) for name in node.bases) dct['dec_front'] = '' dct['dec_back'] = '' for dec in node.decorator_list: js = convert_node(dec, scope) dct['dec_front'] += js+'(' dct['dec_back'] += ')' scope = new_scope(scope) scope['exp locals'] = True text = convert_block(node.body, scope) prefix = local_prefix(scope) for name in scope['locals']: text = re.sub('{:undef:' + name + ':[^:]*:}', prefix + name, text) dct['contents'] = text dct['lnum'] = len(scope['parent locals']) text = TEMPLATES['class'] % dct 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 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(node, scope): if node.module == '__future__': return '' template = '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 += '.' prefix = local_prefix(scope) for alias in node.names: if alias.name == '*': template += TEMPLATES['import *'] % (prefix, prefix) break asname = alias.asname or alias.name # scope['locals'].append(asname) left = do_left(ast.Name(asname, []), scope) template += '%s = __pjs_tmp_module.%s;\n' % (left, subs_name + alias.name) to_import.append(node.module) return template
def convert_module(mod, filename): dct = {'scope':'_', 'filename':os.path.abspath(filename)} dct['doc'] = multiline(ast.get_docstring(mod)) _globs = ['__name__','__doc__','__file__'] scope = { 'globals':[], 'locals':[], 'exp globals':[], 'parent locals':(), 'exp locals':False, 'num iters':0, 'in atomic':0, } scope['globals'] = scope['locals'] = _globs contents = convert_block(mod.body, scope) dct['contents'] = contents text = TEMPLATES['module'] % dct prefix = local_prefix(scope) for name in scope['locals']: text = re.sub('{:undef:' + name + ':[^:]*:}', prefix + name, text) text = re.sub('{:undef:(\w+):([^:]*):}', '$b.assertdefined(\\2\\1)', text) text = text.replace('&coln;', ':').replace('&', '&') return text
dct['args'] = ', '.join(args) dct['dec_front'] = '' dct['dec_back'] = '' for dec in node.decorator_list: js = convert_node(dec, scope) dct['dec_front'] += js+'(' dct['dec_back'] += ')' scope = new_scope(scope) scope['exp locals'] = False for n in args: scope['locals'].append(n) text = convert_block(node.body, scope) prefix = local_prefix(scope) for name in scope['locals']: text = re.sub('{:undef:' + name + ':[^:]*:}', prefix + name, text) #if isinstance(text, Chunks): # text.resolve(local_prefix(scope), scope['locals']) # text.levelUp() dct['contents'] = text text = TEMPLATES['function'] % dct return text #TODO: genexp (a for a in b if a!=c) #TODO: ifexp e.g. 3 if True else 5 #TODO: lambda? #TODO: listcomp [a for a in b if a!=c]