Ejemplo n.º 1
0
def test_rewrite_simple3():
    a = aparse('A(1,2)')
    b = aparse('B(2,1)')

    mod = module(complex_rr)

    rule = mod['foo']

    assert rule(a) == b
Ejemplo n.º 2
0
def test_rule3():
    rule = dslparse('b: @a(x,y) -> F(a,y,x)')
    l = rule[0].lhs
    r = rule[0].rhs

    rr = build_rule(l,r)

    sub = aparse('f(1,2)')
    res = aparse('F(f,2,1)')
    assert rr(sub) == res
Ejemplo n.º 3
0
def test_rule2():
    rule = dslparse('b: a@f(x,y) -> g(a,a(a))')
    l = rule[0].lhs
    r = rule[0].rhs

    rr = build_rule(l,r)

    sub = aparse('f(1,2)')
    res = aparse('g(f(1, 2), a(f(1, 2)))')
    assert rr(sub) == res
Ejemplo n.º 4
0
def test_rule1():
    rule = dslparse('b: f(x,y) -> f(x,x,y,x,f(x,y))')
    l = rule[0].lhs
    r = rule[0].rhs

    rr = build_rule(l,r)

    sub = aparse('f(1,2)')
    res = aparse('f(1,1,2,1,f(1,2))')
    assert rr(sub) == res
Ejemplo n.º 5
0
def test_rule5():
    rule = dslparse('b: f(x,g(y,z)) -> (x,y,z)')
    l = rule[0].lhs
    r = rule[0].rhs

    rr = build_rule(l,r)

    sub = aparse('f(1,g(2,3))')
    res = aparse('(1,2,3)')
    assert rr(sub) == res
Ejemplo n.º 6
0
def test_rewrite_simple2():
    a = aparse('A()')
    b = aparse('B()')
    c = aparse('C()')

    mod = module(simple_rr)

    rule = mod['bar']

    assert rule(a) == c
Ejemplo n.º 7
0
def test_linear():
    x = aparse('x')
    y = aparse('y')

    a0 = aparse('f(x,y)')
    v = free(a0)

    assert list(v) == [
        (x, 'x', ast.aterm),
        (y, 'y', ast.aterm)
    ]
Ejemplo n.º 8
0
def test_rewrite_simple():
    a = aparse('A()')
    b = aparse('B()')
    c = aparse('C()')

    mod = module(simple_rr)

    rule = mod['foo']

    assert rule(a) == b
    assert rule(b) == c
    assert rule(rule(a)) == c
Ejemplo n.º 9
0
def test_aspattern2():
    a0 = dslparse('b: @f(x,y) -> a')
    v = free(a0[0].lhs)

    f = aparse('f')
    x = aparse('x')
    y = aparse('y')

    assert list(v) == [
        (f, 'f', ast.aterm),
        (x, 'x', ast.aterm),
        (y, 'y', ast.aterm)
    ]
Ejemplo n.º 10
0
def test_rule4():
    rule = dslparse('b: f(x,x) -> x')
    l = rule[0].lhs
    r = rule[0].rhs

    rr = build_rule(l,r)

    sub = aparse('f(1,1)')
    res = aparse('1')
    assert rr(sub) == res

    with assert_raises(NoMatch):
        sub = aparse('f(1,2)')
        assert rr(sub) == res
Ejemplo n.º 11
0
def test_aspattern3():
    a0 = dslparse('b: a@f(x,y) -> a')
    v = free(a0[0].lhs)

    f = aparse('f(x,y)')

    assert list(v) == [
        (f, 'a', ast.aappl)
    ]
Ejemplo n.º 12
0
def test_aspattern1():
    x = aparse('x')

    a0 = dslparse('b: f(a@x) -> a')
    v = free(a0[0].lhs)

    assert list(v) == [
        (x, 'a', ast.aterm)
    ]
Ejemplo n.º 13
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('module', nargs='?', help='Module')
    parser.add_argument('--noprelude', action='store_true', help='Include prelude')
    args = parser.parse_args()

    # State
    mod = {}
    last = None
    bindings = {}

    if args.module:
        with open(args.module) as fd:
            mod = module(fd.read())

    if not args.noprelude:
        mod.update(prelude)

    print banner
    readline.parse_and_bind("tab: complete")
    readline.set_completer(partial(completer, mod))

    while True:
        try:
            line = raw_input('>> ').strip()
        except EOFError:
            break


        #-----------------------------------------------
        if line.startswith('?'):
            at = aparse(line[1:])
            matcher = partial(match, freev(at))
            matched, localbind = matcher(last)

            # TODO: Use dict
            #bindings.update(localbind)

            #if matched:
            #    print bindings
            #else:
            #    print 'failed'

        #-----------------------------------------------
        elif line.startswith('!'):
            try:
                rr = mod[line[1:].strip()]
                last = rr.rewrite(last)
                print last
            except KeyError:
                print "No such rule or strategy '%s'" % line[1:]
            except NoMatch:
                print 'failed'

        #-----------------------------------------------
        elif line.startswith(':show') or line.startswith(':s'):
            try:
                rr = mod[line[1:].strip()]
                print rr
            except KeyError:
                print "No such rule or strategy '%s'" % line[1:]

        #-----------------------------------------------
        elif line.startswith(':type') or line.startswith(':t'):
            try:
                at = aparse(line[2:])
                print type(at).__name__
            except Exception as e:
                print e

        #-----------------------------------------------
        elif line.startswith(':bindings'):
            if bindings:
                pprint.pprint(bindings)
            continue

        #-----------------------------------------------
        elif line.startswith(':let'):
            env = module(line[4:], _env=mod)
            mod.update(env)

        #-----------------------------------------------
        elif line.startswith(':load'):
            fname = line[5:].strip()
            try:
                contents = open(fname).read()
                mod.update(module(contents))
            except IOError:
                print "No such module", fname

        #-----------------------------------------------
        elif line.startswith(':browse'):
            pprint.pprint(mod)

        #-----------------------------------------------
        elif line.startswith(':help'):
            print help
            pass

        #-----------------------------------------------
        else:
            bindings = {}
            try:
                last = aparse(line)
                print last
            except EOFError:
                pass
            except Exception as e:
                print traceback.format_exc()
Ejemplo n.º 14
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('module', nargs='?', help='Module')
    parser.add_argument('--noprelude',
                        action='store_true',
                        help='Include prelude')
    args = parser.parse_args()

    # State
    mod = {}
    last = None
    bindings = {}

    if args.module:
        with open(args.module) as fd:
            mod = module(fd.read())

    if not args.noprelude:
        mod.update(prelude)

    print banner
    readline.parse_and_bind("tab: complete")
    readline.set_completer(partial(completer, mod))

    while True:
        try:
            line = raw_input('>> ').strip()
        except EOFError:
            break

        #-----------------------------------------------
        if line.startswith('?'):
            at = aparse(line[1:])
            matcher = partial(match, freev(at))
            matched, localbind = matcher(last)

            # TODO: Use dict
            #bindings.update(localbind)

            #if matched:
            #    print bindings
            #else:
            #    print 'failed'

        #-----------------------------------------------
        elif line.startswith('!'):
            try:
                rr = mod[line[1:].strip()]
                last = rr.rewrite(last)
                print last
            except KeyError:
                print "No such rule or strategy '%s'" % line[1:]
            except NoMatch:
                print 'failed'

        #-----------------------------------------------
        elif line.startswith(':show') or line.startswith(':s'):
            try:
                rr = mod[line[1:].strip()]
                print rr
            except KeyError:
                print "No such rule or strategy '%s'" % line[1:]

        #-----------------------------------------------
        elif line.startswith(':type') or line.startswith(':t'):
            try:
                at = aparse(line[2:])
                print type(at).__name__
            except Exception as e:
                print e

        #-----------------------------------------------
        elif line.startswith(':bindings'):
            if bindings:
                pprint.pprint(bindings)
            continue

        #-----------------------------------------------
        elif line.startswith(':let'):
            env = module(line[4:], _env=mod)
            mod.update(env)

        #-----------------------------------------------
        elif line.startswith(':load'):
            fname = line[5:].strip()
            try:
                contents = open(fname).read()
                mod.update(module(contents))
            except IOError:
                print "No such module", fname

        #-----------------------------------------------
        elif line.startswith(':browse'):
            pprint.pprint(mod)

        #-----------------------------------------------
        elif line.startswith(':help'):
            print help
            pass

        #-----------------------------------------------
        else:
            bindings = {}
            try:
                last = aparse(line)
                print last
            except EOFError:
                pass
            except Exception as e:
                print traceback.format_exc()