Esempio n. 1
0
 def test_name_collision(self):
     sym0 = 1
     sym1 = 2
     func1 = f(_ + sym0)
     assert func1(10) == 11
     func2 = f(_ + sym0 + _ + sym1)
     assert func2(10, 10) == 23
Esempio n. 2
0
File: pinq.py Progetto: jjpe/macropy
def _recurse(tree, **kw):
    if type(tree) is Compare and type(tree.ops[0]) is In:
        return q((ast(tree.left)).in_(ast(tree.comparators[0])))

    elif type(tree) is GeneratorExp:

        aliases = map(f(_.target), tree.generators)
        tables = map(f(_.iter), tree.generators)

        aliased_tables = map(lambda x: q((ast(x)).alias().c), tables)

        elt = tree.elt
        if type(elt) is Tuple:
            sel = q(ast_list(elt.elts))
        else:
            sel = q([ast(elt)])

        out = q(select(ast(sel)))

        for gen in tree.generators:
            for cond in gen.ifs:
                out = q(ast(out).where(ast(cond)))


        out = q((lambda x: ast(out))())
        out.func.args.args = aliases
        out.args = aliased_tables
        return out
Esempio n. 3
0
File: peg.py Progetto: jjpe/macropy
 def reduce_chain(chain):
     chain = list(reversed(chain))
     o_dict = {
         "+": f(_+_),
         "-": f(_-_),
         "*": f(_*_),
         "/": f(_/_),
     }
     while len(chain) > 1:
         a, [o, b] = chain.pop(), chain.pop()
         chain.append(o_dict[o](a, b))
     return chain[0]
Esempio n. 4
0
File: peg.py Progetto: jjpe/macropy
    def test_block(self):
        with peg:
            parse1 = ("Hello World", "!".rep1) // f(_[1])
            parse2 = parse1 // len

        with require:
            parse1.parse_string("Hello World!!!").output == ['!', '!', '!']
            parse1.parse_string("Hello World").index == 11
            parse2.parse_string("Hello World!!!").output == 3
Esempio n. 5
0
File: peg.py Progetto: jjpe/macropy
    def test_conversion(self):
        parse1 = peg((("Hello World", "!".rep1) // f(_[1])))

        with require:
            parse1.parse_string("Hello World!!!").output == ['!', '!', '!']
            parse1.parse_string("Hello World").index == 11

        parse2 = parse1 // len

        with require:
            parse2.parse_string("Hello World!!!").output == 3
Esempio n. 6
0
File: peg.py Progetto: jjpe/macropy
    def test_arithmetic(self):
        """
        PEG grammar from Wikipedia

        Op      <- "+" / "-" / "*" / "/"
        Value   <- [0-9]+ / '(' Expr ')'
        Expr <- Value (Op Value)*

        simplified it to remove operator precedence
        """

        def reduce_chain(chain):
            chain = list(reversed(chain))
            o_dict = {
                "+": f(_+_),
                "-": f(_-_),
                "*": f(_*_),
                "/": f(_/_),
            }
            while len(chain) > 1:
                a, [o, b] = chain.pop(), chain.pop()
                chain.append(o_dict[o](a, b))
            return chain[0]

        with peg:
            value = '[0-9]+'.r // int | ('(', expr, ')') // (f(_[1]))
            op = '+' | '-' | '*' | '/'
            expr = (value is first, (op, value).rep is rest) >> reduce_chain([first] + rest)

        with require:
            expr.parse_string("123").output == 123
            expr.parse_string("((123))").output == 123
            expr.parse_string("(123+456+789)").output == 1368
            expr.parse_string("(6/2)").output == 3
            expr.parse_string("(1+2+3)+2").output == 8
            expr.parse_string("(((((((11)))))+22+33)*(4+5+((6))))/12*(17+5)").output == 1804
Esempio n. 7
0
File: peg.py Progetto: jjpe/macropy
def _PegWalker(tree, ctx, stop, collect, **kw):
    if type(tree) is Str:
        stop()
        return q(Parser.Raw(ast(tree)))

    if type(tree) is BinOp and type(tree.op) is RShift:
        tree.left, b_left = _PegWalker.recurse_real(tree.left)
        tree.right = q(lambda bindings: ast(tree.right))
        tree.right.args.args = map(f(Name(id = _)), flatten(b_left))
        stop()
        return tree

    if type(tree) is BinOp and type(tree.op) is FloorDiv:
        tree.left, b_left = _PegWalker.recurse_real(tree.left)
        stop()
        collect(b_left)
        return tree

    if type(tree) is Tuple:
        result = q(Parser.Seq([]))

        result.args[0].elts = tree.elts
        all_bindings = []
        for i, elt in enumerate(tree.elts):
            result.args[0].elts[i], bindings = _PegWalker.recurse_real(tree.elts[i])
            all_bindings.append(bindings)
        stop()
        collect(all_bindings)
        return result

    if type(tree) is Compare and type(tree.ops[0]) is Is:
        left_tree, bindings = _PegWalker.recurse_real(tree.left)
        new_tree = q(ast(left_tree).bind_to(u(tree.comparators[0].id)))
        stop()
        collect(bindings + [tree.comparators[0].id])
        return new_tree
Esempio n. 8
0
 def test_partial(self):
     basetwo = f(int(_, base=2))
     assert basetwo('10010') == 18
Esempio n. 9
0
 def test_basic(self):
     assert map(f(_ - 1), [1, 2, 3]) == [0, 1, 2]
     assert reduce(f(_ + _), [1, 2, 3]) == 6
Esempio n. 10
0
 def test_no_args(self):
     from random import random
     thunk = f(random())
     assert thunk() != thunk()
Esempio n. 11
0
 def test_attribute(self):
     assert map(f(_.split(' ')[0]), ["i am cow", "hear me moo"]) == ["i", "hear"]
Esempio n. 12
0
 def call(*args):
     if args not in cache:
         f.cache[args] = f(*args)
     return f.cache[args]