Beispiel #1
0
    def file_input(self, nodelist):
        # Add a "from IO_MODULE import IO_CLASS" statement to the
        # beginning of the module.
        doc = None  # self.get_docstring(nodelist, symbol.file_input)

        if sys.hexversion >= 0x02050000:
            io_imp = ast.From(IO_MODULE, [(IO_CLASS, None)], 0)
            markup_imp = ast.From(MARKUP_MODULE, [(MARKUP_CLASS, None)], 0)
        else:
            io_imp = ast.From(IO_MODULE, [(IO_CLASS, None)])
            markup_imp = ast.From(MARKUP_MODULE, [(MARKUP_CLASS, None)])

        markup_assign = ast.Assign(
            [ast.AssName(MARKUP_MANGLED_CLASS, OP_ASSIGN)],
            ast.Name(MARKUP_CLASS))

        # Add an IO_INSTANCE binding for module level expressions (like
        # doc strings).  This instance will not be returned.
        io_instance = ast.CallFunc(ast.Name(IO_CLASS), [])
        io_assign_name = ast.AssName(IO_INSTANCE, OP_ASSIGN)
        io_assign = ast.Assign([io_assign_name], io_instance)

        stmts = [io_imp, io_assign, markup_imp, markup_assign]

        for node in nodelist:
            if node[0] != token.ENDMARKER and node[0] != token.NEWLINE:
                self.com_append_stmt(stmts, node)

        return ast.Module(doc, ast.Stmt(stmts))
Beispiel #2
0
def compileCodeObjects(filename, codeobjs):
    if len(codeobjs) == 0:
        stmts = []
    else:
        stmts = [ast.For(ast.AssName('[--codeobj--]', 'OP_ASSIGN'),
                         ast.Const(codeobjs),
                         ast.Stmt([ast.Exec(ast.Name('[--codeobj--]'),
                                            None, None)]),
                         None),
                 ast.AssName('[--codeobj--]', 'OP_DELETE')]

    module = ast.Module(None, ast.Stmt(stmts))
    compiler.misc.set_filename(filename, module)
    return pycodegen.ModuleCodeGenerator(module).getCode()
Beispiel #3
0
def Assign(left, right):
    names = []
    if isinstance(left, ast.Name):
        # Single assignment on left
        return ast.Assign([ast.AssName(left.name, 'OP_ASSIGN')], right)
    elif isinstance(left, ast.Tuple):
        # List of things - make sure they are Name nodes
        names = []
        for child in left.getChildren():
            if not isinstance(child, ast.Name):
                raise SyntaxError("that assignment not supported")
            names.append(child.name)
        ass_list = [ast.AssName(name, 'OP_ASSIGN') for name in names]
        return ast.Assign([ast.AssTuple(ass_list)], right)
    else:
        raise SyntaxError("Can't do that yet")
Beispiel #4
0
def compileDeflang(self, name, parentx, body):
    assert isinstance(name, Symbol)

    if parentx:
        assertResult(parentx, "derive language from")
        parent = topy(parentx)
    else:
        parent = logixglobal('langlang')

    funcname = "#lang:%s" % name

    body = len(body) > 0 and block(body, False).nodes or []

    funcbody = ast.Stmt(
        body
        + [ast.CallFunc(ast.Getattr(ast.Getattr(ast.Name(str(name)),
                                                '__impl__'),
                                    'addDeflangLocals'),
                        [ast.CallFunc(ast.Name('locals'), [])])])
    
    res = ast.Stmt([
        ast.Assign([compilePlace(Symbol(name))],
                   ast.CallFunc(logixglobal('defLanguage'),
                                [ast.Const(str(name)),
                                 parent,
                                 ast.CallFunc(GlobalName("globals"), [])])),
        astFunction(funcname, tuple(), tuple(), 0, None, funcbody),
        ast.CallFunc(ast.Name(funcname), []),
        ast.AssName(funcname, 'OP_DELETE')])
    
    lineno = getmeta(self, 'lineno')
    for n in res.nodes:
        n.lineno = lineno
    res.lineno = lineno
    return res
Beispiel #5
0
    def function(self, name, expr):
        """
        Create a function of one argument with the given name returning the
        given expr.

        @param name: The function name.
        @param expr: The AST to insert into the function.
        """

        fexpr = ast.Stmt([ast.Assign([ast.AssName('__locals', 'OP_ASSIGN')],
                                     ast.Dict([(ast.Const('self'),
                                                ast.Name('self'))])),
                          ast.Assign([ast.Subscript(ast.Getattr(
                                                  ast.Name('self'), 'locals'),
                                                    'OP_ASSIGN',
                                                    [ast.Const(
                                                     name.split('_',1)[1])])],
                                     ast.Name('__locals')),
                          expr])
        f = ast.Lambda(['self'], [], 0, fexpr)
        f.filename = self.name
        return f
Beispiel #6
0
def compilePlace(place, opname='OP_ASSIGN'):
    if isinstance(place, Symbol):
        return ast.AssName(str(place), opname)

    elif isinstance(place, rootops.PyOp):
        token = place.__syntax__.token

        if token == ".":
            # {{{ assign to field
            expr = topy(place[0])
            field = place[1]
            if isinstance(field, Symbol):
                return ast.AssAttr(expr, str(field), opname)
            else:
                raise CompileError("Cannot assign to %s" % place)
            # }}}

        elif token == "__continue__":
            # {{{ assign to slice or subscript
            last = place[1][-1]

            # expr = the continuationOp not including the last part
            if len(place[1]) == 1:
                expr = place[0]
            else:
                expr = basel.getContinuationOp()(place[0], place[1][:-1])
            
            kind = last[-1]
            if kind == 'subscript':
                # {{{ assign to subscript
                assertResult(expr, "subscript")

                keys = last[:-1]
                for key in keys:
                    assertResult(key, "subscript with")

                return ast.Subscript(topy(expr), opname, map(topy, keys))
                # }}}

            elif kind ==  "slice":
                # {{{ assign to slice
                assertResult(expr, "slice")

                start,end,step = last[:-1]
                assertResult(start, "slice from ")
                assertResult(end, "slice to")
                assertResult(step, "slice with step")

                return ast.Subscript(topy(expr), opname,
                                     [ast.Sliceobj(map(topy, (start,end,step)))])
                # }}}
                
            else:
                raise CompileError("Cannot asign to %s " % place)
            # }}}

        elif token == ",":
            return ast.AssTuple([compilePlace(p, opname) for p in list(place)])

        elif token == "[" and place[1] == 'list':
            return ast.AssList([compilePlace(p, opname) for p in place.elems])

        elif token == "(":
            return compilePlace(place[1], opname)

        else:
            raise CompileError("Cannot asign to %s " % place)

    else:
        raise CompileError("Cannot assign to %s" % place)
Beispiel #7
0
def compilePlace(place, opname='OP_ASSIGN'):

    # {{{ def compileSubscriptPlace(objx, keyxs):
    def compileSubscriptPlace(objx, keyxs):
         assertResult(objx, "subscript")

         for key in keyxs:
             assertResult(key, "subscript with")

         return ast.Subscript(topy(objx), opname, map(topy, keyxs))
    # }}}

    # {{{ def compileSlicePlace(objx, fromx, tox, stepx):
    def compileSlicePlace(objx, fromx, tox, stepx):
         assertResult(objx, "slice")
         
         assertResult(fromx, "slice from ")
         assertResult(tox, "slice to")
         assertResult(stepx, "slice with step")
         
         return ast.Subscript(topy(objx), opname,
                              [ast.Sliceobj(map(topy, (fromx, tox, stepx)))])
    # }}}
    
    if isinstance(place, Symbol):
        if place.namespace != "":
            raise CompileError, "cannot assign to symbol with namespace: %s" % place
        return ast.AssName(place.name, opname)

    elif isDoc(place, rootops.sliceOp):
        return compileSlicePlace(place[0], place[1], place[2], place[3])

    elif isDoc(place, rootops.subscriptOp):
        return compileSubscriptPlace(place[0], place[1:])

    elif isPyOp(place):
        token = place.tag.name

        if token == ".":
            # {{{ assign to field
            expr = topy(place[0])
            field = place[1]
            if isinstance(field, Symbol) and field.namespace == "":
                return ast.AssAttr(expr, field.name, opname)
            else:
                raise CompileError("Cannot assign to %s" % place)
            # }}}

        elif token == "": # continuation op
            # {{{ assign to slice or subscript
            last = place[-1]

            # expr = the continuationOp not including the last part
            if len(place) == 2:
                expr = place[0]
            else:
                expr = Doc(Symbol(rootops.base_ns, ""),
                           place[:-1])
            
            kind = last[-1]
            if kind == 'subscript':
                return compileSubscriptPlace(expr, last[:-1])

            elif kind ==  "slice":
                start,end,step = last[:-1]
                return compileSlicePlace(expr, start, end, step)
                
            else:
                raise CompileError("Cannot asign to %s " % place)
            # }}}

        elif token == ",":
            return ast.AssTuple([compilePlace(p, opname) for p in list(place)])

        elif token == "[" and place[1] == 'list':
            return ast.AssList([compilePlace(p, opname) for p in place])

        elif token == "(":
            return compilePlace(place[1], opname)

        else:
            raise CompileError("Cannot asign to %s " % place)

    else:
        raise CompileError("Cannot assign to %s" % place)
Beispiel #8
0
    def funcdef(self, nodelist):
        if len(nodelist) == 6:
            assert nodelist[0][0] == symbol.decorators
            decorators = self.decorators(nodelist[0][1:])
        else:
            assert len(nodelist) == 5
            decorators = None

        lineno = nodelist[-4][2]
        name = nodelist[-4][1]
        args = nodelist[-3][2]

        if not re.match('_q_((html|plain)_)?template_', name):
            # just a normal function, let base class handle it
            self.__template_type.append(None)
            n = transformer.Transformer.funcdef(self, nodelist)

        else:
            if name.startswith(PLAIN_TEMPLATE_PREFIX):
                name = name[len(PLAIN_TEMPLATE_PREFIX):]
                template_type = "plain"
            elif name.startswith(HTML_TEMPLATE_PREFIX):
                name = name[len(HTML_TEMPLATE_PREFIX):]
                template_type = "html"
            elif name.startswith(TEMPLATE_PREFIX):
                name = name[len(TEMPLATE_PREFIX):]
                template_type = "plain"
            else:
                raise RuntimeError, 'unknown prefix on %s' % name

            self.__template_type.append(template_type)

            # Add "IO_INSTANCE = IO_CLASS()" statement at the beginning of
            # the function and a "return IO_INSTANCE" at the end.
            if args[0] == symbol.varargslist:
                names, defaults, flags = self.com_arglist(args[1:])
            else:
                names = defaults = ()
                flags = 0
            doc = None  # self.get_docstring(nodelist[-1])

            # code for function
            code = self.com_node(nodelist[-1])

            # create an instance, assign to IO_INSTANCE
            klass = ast.Name(IO_CLASS)
            args = [ast.Const(template_type == "html")]
            instance = ast.CallFunc(klass, args)
            assign_name = ast.AssName(IO_INSTANCE, OP_ASSIGN)
            assign = ast.Assign([assign_name], instance)

            # return the IO_INSTANCE.getvalue(...)
            func = ast.Getattr(ast.Name(IO_INSTANCE), "getvalue")
            ret = ast.Return(ast.CallFunc(func, []))

            # wrap original function code
            code = ast.Stmt([assign, code, ret])

            if sys.hexversion >= 0x20400a2:
                n = ast.Function(decorators, name, names, defaults, flags, doc,
                                 code)
            else:
                n = ast.Function(name, names, defaults, flags, doc, code)
            n.lineno = lineno

        self.__template_type.pop()
        return n
Beispiel #9
0
 def _do_NameExpression(self, node, context):
     name = str(node.Name)
     if context == OP_APPLY:
         return ast.Name(name)
     else:
         return ast.AssName(name, context)
Beispiel #10
0
 def STORE_FAST(decompiler, varname):
     if varname.startswith('_['):
         throw(InvalidQuery('Use generator expression (... for ... in ...) instead of list comprehension [... for ... in ...] inside query'))
     decompiler.assnames.add(varname)
     decompiler.store(ast.AssName(varname, 'OP_ASSIGN'))
Beispiel #11
0
 def STORE_DEREF(decompiler, freevar):
     decompiler.assnames.add(freevar)
     decompiler.store(ast.AssName(freevar, 'OP_ASSIGN'))