Esempio n. 1
0
    def visit_ImportFrom(self, imp: ast.ImportFrom):
        if imp.lineno not in self.act or imp.names[0].name == '*':
            return imp
        if imp.module is None:
            from_mod = ast.Name(".from_mod")
            @quote
            def mk_from_mod(from_mod_n, level):
                from_mod_n = '.'.join(__name__.split('.')[:-level])
            mk_from_mod = mk_from_mod(from_mod, ast.Constant(imp.level))
        else:
            mk_from_mod = []
            from_mod = ast.Constant(imp.module)

        names_var = ast.Constant(tuple((name.name, name.asname) for name in imp.names))
        name = ast.Name(".name")
        asname = ast.Name(".asname")
        @quote
        def f(lazy_module, from_mod, name, asname, names_var):
            for name, asname, in names_var:
                lazy_module(globals(), name, asname, from_mod)
        stmts = f(
            ast.Name(runtime_lazy_mod, ast.Load()),
            from_mod,
            name,
            asname,
            names_var
        )
        mk_from_mod.extend(stmts)
        return stmts
Esempio n. 2
0
        def item(expr: Expr, i):
            if i < n1:
                return self.item(expr, i)
            if i > n1:
                return self.item(expr, i - n)

            @quote
            def get_item(value, start, end):
                # noinspection PyStatementEffect
                value[start:end]

            # noinspection PyTypeChecker
            expr = get_item(expr.value, ast.Constant(n1),
                            ast.Constant(end))[0]  # type: ast.Expr
            return Expr(expr.value)
Esempio n. 3
0
    def size_is(self, n: int):
        n = ast.Constant(n)

        def then(pattern):
            if -5 < get_constant(n) < 256:
                # noinspection PyStatementEffect PyUnusedLocal
                @quote
                def quote_size_chk(ret, tag, n, stmts):
                    if len(tag) is n:
                        stmts
                    else:
                        ret = None
            else:
                # noinspection PyStatementEffect PyUnusedLocal
                @quote
                def quote_size_chk(ret, tag, n, stmts):
                    if len(tag) == n:
                        stmts
                    else:
                        ret = None

            @dyn_check
            def pat(target: Expr, remain: Stmts):
                remain = pattern.apply(target, remain)
                # noinspection PyTypeChecker
                stmts = quote_size_chk(self.ret, target.value, n,
                                       remain.suite)
                return Stmts(stmts)

            return Pattern(pat)

        return then
Esempio n. 4
0
    def item(self, expr: Expr, i):
        @quote
        def get_item(value, ith):
            # noinspection PyStatementEffect
            value[ith]

        # noinspection PyTypeChecker
        expr = get_item(expr.value, ast.Constant(i))[0]  # type: ast.Expr
        return Expr(expr.value)
Esempio n. 5
0
    def visit_Import(self, imp: ast.Import):
        if imp.lineno not in self.act:
            return imp

        @quote
        def f(lazy_module, name, asname, names_var):
            for name, asname, in names_var:
                lazy_module(globals(), name, asname)

        names_var = ast.Constant(tuple((name.name, name.asname) for name in imp.names))
        name = ast.Name(".name")
        asname = ast.Name(".asname")
        return f(ast.Name(runtime_lazy_mod, ast.Load()), name, asname, names_var)
Esempio n. 6
0
        def pat(target):
            then_code = Stmts(quote_match_failed(not_exhaustive_err_type))
            for each, body in reversed(pairs):

                suite = body.suite
                suite.reverse()
                suite.append(ast.Assign([self.ret], ast.Constant(())))
                suite.reverse()

                now_code = each.apply(target, body)  # type: Stmts
                assert isinstance(now_code, Stmts)
                stmts = quote_alt(now_code.suite, then_code.suite, self.ret)
                then_code = Stmts(stmts)
            return then_code
Esempio n. 7
0
        def pat(target: Expr, body: Stmts):
            mid = Gensym("decons").gen()
            n = len(elts)

            # noinspection PyStatementEffect PyUnusedLocal
            @quote
            def decons(mid, ret, ctor, n, tag, body):
                mid = ctor.__match__(n, tag)
                if mid is None:
                    ret = None
                else:
                    body

            inner = self.seq_n(tuple, elts)
            stmts = inner.apply(Expr(mid), body)
            # noinspection PyTypeChecker
            suite = decons(mid, self.ret, ctor.value,
                           ast.Constant(n), target.value, stmts.suite)
            return Stmts(suite)
Esempio n. 8
0
    def size_ge(self, n: int):
        n = ast.Constant(n)

        def then(pattern):
            @quote
            def quote_size_chk(ret, tag, n, stmts):
                if len(tag) >= n:
                    stmts
                else:
                    ret = None

            @dyn_check
            def pat(target: Expr, remain: Stmts):
                remain = pattern.apply(target, remain)
                # noinspection PyTypeChecker
                stmts = quote_size_chk(self.ret, target.value, n, remain.suite)
                return Stmts(stmts)

            return Pattern(pat)

        return then