コード例 #1
0
 def visit_For(self, node, is_async=False):
     set_precedence(node, node.target)
     prefix = 'async ' if is_async else ''
     self.statement(node, '%sfor ' % prefix, node.target, ' in ',
                    node.iter, '{')
     self.body_or_else(node)
     self.newline()
     self.write("}")
コード例 #2
0
ファイル: bulbul2.py プロジェクト: mdornseif/bulbul
 def visit_UnaryOp(self, node):
     with self.delimit(node, node.op) as delimiters:
         set_precedence(delimiters.p, node.operand)
         # In Python 2.x, a unary negative of a literal
         # number is merged into the number itself.  This
         # bit of ugliness means it is useful to know
         # what the parent operation was...
         node.operand._p_op = node.op
         sym = get_op_symbol(node.op)
         self.write(sym, ' ' if sym.isalpha() else '', node.operand)
コード例 #3
0
ファイル: bulbul2.py プロジェクト: mdornseif/bulbul
 def visit_Assign(self, node):
     set_precedence(node, node.value, *node.targets)
     self.newline(node)
     if not hasattr(node, '_scope'):
         node._scope = {'variables': []}
     for target in node.targets:
         if hasattr(target, 'id') and target.id not in node._scope['variables']:
             self.write('var ', target, ' = ')
             node._scope['variables'].append(target.id)
         else:
             self.write(target, ' = ')
     self.visit(node.value)
コード例 #4
0
 def visit_If(self, node):
     set_precedence(node, node.test)
     self.statement(node, 'if ', node.test, ':')
     self.body(node.body)
     while True:
         else_ = node.orelse
         if len(else_) == 1 and isinstance(else_[0], ast.If):
             node = else_[0]
             set_precedence(node, node.test)
             self.write(self.newline, 'elif ', node.test, '{')
             self.body(node.body)
         else:
             self.else_body(else_)
             break
コード例 #5
0
ファイル: bulbul2.py プロジェクト: mdornseif/bulbul
 def visit_If(self, node):
     set_precedence(node, node.test)
     self.statement(node, 'if (', node.test, ') {', '\n')
     self.body(node.body)
     self.statement(node, '\n', '}')
     while True:
         else_ = node.orelse
         if len(else_) == 1 and isinstance(else_[0], ast.If):
             node = else_[0]
             set_precedence(node, node.test)
             self.write('\n', 'else if (', node.test, ') {', '\n')
             self.body(node.body)
             self.statement(node, '\n', '}')
         else:
             self.else_body(else_)
             break
コード例 #6
0
 def visit_While(self, node):
     set_precedence(node, node.test)
     self.statement(node, 'while ', node.test, '{')
     self.body_or_else(node)
     self.newline()
     self.write("}")
コード例 #7
0
ファイル: bulbul2.py プロジェクト: mdornseif/bulbul
 def visit_comprehension(self, node):
     set_precedence(node, node.iter, *node.ifs)
     set_precedence(Precedence.comprehension_target, node.target)
     self.write(' for (', node.target, ' of ', node.iter, ')')
     for if_ in node.ifs:
         self.write(' if ', if_)
コード例 #8
0
ファイル: bulbul2.py プロジェクト: mdornseif/bulbul
 def visit_IfExp(self, node):
     with self.delimit(node) as delimiters:
         set_precedence(delimiters.p + 1, node.body, node.test)
         set_precedence(delimiters.p, node.orelse)
         self.write(node.test, ' ? ', node.body, ' : ', node.orelse)
コード例 #9
0
ファイル: bulbul2.py プロジェクト: mdornseif/bulbul
 def visit_Lambda(self, node):
     with self.delimit(node) as delimiters:
         set_precedence(delimiters.p, node.body)
         self.write('lambda ')
         self.visit_arguments(node.args)
         self.write(': ', node.body)
コード例 #10
0
ファイル: bulbul2.py プロジェクト: mdornseif/bulbul
 def visit_Compare(self, node):
     with self.delimit(node, node.ops[0]) as delimiters:
         set_precedence(delimiters.p + 1, node.left, *node.comparators)
         self.visit(node.left)
         for op, right in zip(node.ops, node.comparators):
             self.write(get_op_symbol(op, ' %s '), right)
コード例 #11
0
ファイル: bulbul2.py プロジェクト: mdornseif/bulbul
 def visit_BoolOp(self, node):
     with self.delimit(node, node.op) as delimiters:
         op = get_op_symbol(node.op, ' %s ')
         set_precedence(delimiters.p + 1, *node.values)
         for idx, value in enumerate(node.values):
             self.write(idx and op or '', value)
コード例 #12
0
ファイル: bulbul2.py プロジェクト: mdornseif/bulbul
    def visit_Call(self, node):
        want_comma = []

        def write_comma():
            if want_comma:
                self.write(', ')
            else:
                want_comma.append(True)

        # special cases for build-ins
        if isinstance(node.func, ast.Attribute):
            name = node.func.attr
            if node.func.attr == 'append':
                node.func.attr = 'push'  # rewrite Method name
            if node.func.attr == 'values':
                node.func.attr = 'entries'  # rewrite Method name
        elif isinstance(node.func, ast.Name):
            name = node.func.id
            if node.func.id == 'int':
                node.func.id = "Number"
            if node.func.id == 'object':
                node.func.id = "Object"
            # include shim
            if node.func.id in set(('len', 'sum')):
                shims.add(node.func.id)
            # rewrite to literal
            if node.func.id == 'list':
                with self.delimit('[]'):
                    self.comma_list(node.args)
                return
            if node.func.id == 'dict':
                with self.delimit('{}'):
                    for keyword in node.keywords:
                        # a keyword.arg of None indicates dictionary unpacking
                        # (Python >= 3.5)
                        self.write(write_comma, keyword.arg, ':', keyword.value)
                return

        args = node.args
        keywords = node.keywords
        starargs = self.get_starargs(node)
        kwargs = self.get_kwargs(node)
        numargs = len(args) + len(keywords)
        numargs += starargs is not None
        numargs += kwargs is not None
        p = Precedence.Comma if numargs > 1 else Precedence.call_one_arg
        set_precedence(p, *args)
        if name[0].isupper():
            # we assume this is a class Constructor
            self.write('new ')
        self.visit(node.func)
        self.write('(')
        for arg in args:
            self.write(write_comma, arg)

        set_precedence(Precedence.Comma, *(x.value for x in keywords))
        for keyword in keywords:
            # a keyword.arg of None indicates dictionary unpacking
            # (Python >= 3.5)
            arg = keyword.arg or ''
            self.write(write_comma, arg, '=' if arg else '**', keyword.value)
        # 3.5 no longer has these
        self.conditional_write(write_comma, '*', starargs)
        self.conditional_write(write_comma, '**', kwargs)
        self.write(')')
コード例 #13
0
ファイル: bulbul2.py プロジェクト: mdornseif/bulbul
 def visit_Return(self, node):
     set_precedence(node, node.value)
     self.statement(node, 'return')
     with self.delimit('()'):
         self.conditional_write(node.value)
コード例 #14
0
ファイル: bulbul2.py プロジェクト: mdornseif/bulbul
            self.write(': ', node.body)

    def visit_ListComp(self, node):
        with self.delimit('[]'):
            self.write(*node.generators)
            self.write(node.elt)


    def visit_IfExp(self, node):
        with self.delimit(node) as delimiters:
            set_precedence(delimiters.p + 1, node.body, node.test)
            set_precedence(delimiters.p, node.orelse)
            self.write(node.test, ' ? ', node.body, ' : ', node.orelse)

    def visit_For(self, node, async=False):
        set_precedence(node, node.target)
        prefix = 'async ' if async else ''
        self.statement(node, '%sfor (var ' % prefix,
                       node.target, ' of ', node.iter, ')', '\n')
        with self.delimit('{}'):
            self.body_or_else(node)

    def visit_Module(self, node):
        scope = {'variables': [], 'functions': []}
        for b in node.body:
            b._scope = scope
        self.write(*node.body)
        self.write('\n', '// exports for commonJS')
        for funcname in scope.get('functions', []):
            if not funcname.startswith('_'):
                self.write('\n', 'exports.%s' % funcname, ' = ', funcname)