Example #1
0
def parse_file(fname, *replacements, **kwargs):
    abspath = kwargs.get("abspath", False)
    if not abspath:
        fname = os.path.join(os.path.dirname(__file__), fname)

    if kwargs.get("inplace", False):
        assert not replacements
        if not pytest.config.byte_option:
            reader = JsonLoader(bytecode_expand=False)
        else:
            reader = JsonLoader(bytecode_expand=True)
        ast = reader.expand_to_ast(fname)
        return ast

    with file(fname) as f:
        s = f.read()
    for replace, with_ in replacements:
        assert s.count(replace) == 1
        s = s.replace(replace, with_)
    s = s.decode("utf-8")

    if not pytest.config.byte_option:
        s = expand_string(s)
        ast = parse_module(s)
    else:
        s = expand_from_bytecode(s, True)
        ast = parse_module(s, bytecode_expand=True)

    return ast
Example #2
0
def parse_file(fname, *replacements, **kwargs):
    abspath = kwargs.get("abspath", False)
    if not abspath:
        fname = os.path.join(os.path.dirname(__file__), fname)

    if kwargs.get("inplace", False):
        assert not replacements
        if not pytest.config.byte_option:
            reader = JsonLoader(bytecode_expand=False)
        else:
            reader = JsonLoader(bytecode_expand=True)
        ast = reader.expand_to_ast(fname)
        return ast

    with file(fname) as f:
        s = f.read()
    for replace, with_ in replacements:
        assert s.count(replace) == 1
        s = s.replace(replace, with_)
    s = s.decode("utf-8")

    if not pytest.config.byte_option:
        s = expand_string(s)
        ast = parse_module(s)
    else:
        s = expand_from_bytecode(s, True)
        ast = parse_module(s, bytecode_expand=True)

    return ast
Example #3
0
def run_mod(m, stdlib=False, srcloc=True):
    assert not stdlib
    if not pytest.config.byte_option:
        ast = parse_module(expand_string(m, srcloc=srcloc))
    else:
        ast = parse_module(expand_from_bytecode(m, srcloc), bytecode_expand=True)

    return run_ast(ast)
Example #4
0
def run_mod(m, stdlib=False, srcloc=True):
    assert not stdlib
    if not pytest.config.byte_option:
        ast = parse_module(expand_string(m, srcloc=srcloc))
    else:
        ast = parse_module(expand_from_bytecode(m, srcloc),
                           bytecode_expand=True)

    return run_ast(ast)
Example #5
0
    def run_string(self, str):
        ast = parse_module(expand_string(str))
        env = ToplevelEnv()

        def interp_w():
            interpret_module(ast, env)

        interp_w() # check that it runs

        ast = parse_module(expand_string(str))
        self.meta_interp(interp_w, [], listcomp=True, listops=True, backendopt=True)
Example #6
0
def run_mod(m, stdlib=False, srcloc=True):
    assert (not stdlib)
    
    if not pytest.config.byte_option:
        ast = parse_module(expand_string(m, srcloc=srcloc))
    elif pytest.config.byte_option == "recursive" or pytest.config.byte_option == "non-recursive":
        # currently we don't care about the recursive/non-recursive call,
        # eventually the all transformation's going to be recursive anyway
        ast = parse_module(expand_from_bytecode(m, srcloc), lib="-l pycket/zoTransform --")
    else:
        print "Check byteOption configuration!"
        
    return run_ast(ast)
Example #7
0
def test_callgraph_reconstruction():
    from pycket.expand import expand_string, parse_module
    from pycket import config
    from pycket.callgraph import LOOP_PARTICIPANT, LOOP_HEADER
    from pycket.env import w_global_config as conf
    str = """
        #lang pycket
        (define (f x) (g (+ x 1)))
        (define (g x) (if (= x 0) (g 5) (h x)))
        (define (h x) x)
        (f 5)
        (f -1)
        """
    conf.reset_callgraph()
    ast = parse_module(expand_string(str))
    env = ToplevelEnv(config.get_testing_config(**{"pycket.callgraph": True}))
    m = interpret_module(ast, env)
    f = m.defs[W_Symbol.make("f")].closure.caselam.lams[0]
    g = m.defs[W_Symbol.make("g")].closure.caselam.lams[0]
    h = m.defs[W_Symbol.make("h")].closure.caselam.lams[0]

    assert conf.callgraph.calls == {f: {g: None}, g: {h: None, g: None}}
    assert g.body[0].should_enter

    str = """
        #lang pycket
        (define (f x) (g (+ x 1)))
        (define (g x) (if (= x 0) (f 5) (h x)))
        (define (h x) x)
        (g 0)
        """

    conf.reset_callgraph()

    ast = parse_module(expand_string(str))
    env = ToplevelEnv(config.get_testing_config(**{"pycket.callgraph": True}))
    m = interpret_module(ast, env)
    f = m.defs[W_Symbol.make("f")].closure.caselam.lams[0]
    g = m.defs[W_Symbol.make("g")].closure.caselam.lams[0]
    h = m.defs[W_Symbol.make("h")].closure.caselam.lams[0]

    assert conf.callgraph.calls == {f: {g: None}, g: {h: None, f: None}}
    assert (conf.callgraph.recursive == {
        f: LOOP_HEADER,
        g: LOOP_PARTICIPANT
    } or conf.callgraph.recursive == {
        f: LOOP_PARTICIPANT,
        g: LOOP_HEADER
    })
    assert g.body[0].should_enter or f.body[0].should_enter
    conf.reset_callgraph()
Example #8
0
    def run_string(self, str):
        ast = parse_module(expand_string(str))
        env = ToplevelEnv()

        def interp_w():
            interpret_module(ast, env)

        interp_w()  # check that it runs

        ast = parse_module(expand_string(str))
        self.meta_interp(interp_w, [],
                         listcomp=True,
                         listops=True,
                         backendopt=True)
Example #9
0
def parse_file(fname, *replacements):
    fname = os.path.join(os.path.dirname(__file__), fname)
    with file(fname) as f:
        s = f.read()
    for replace, with_ in replacements:
        assert s.count(replace) == 1
        s = s.replace(replace, with_)
    s = expand_string(s)
    ast = parse_module(s)
    return ast
Example #10
0
def test_callgraph_reconstruction():
    from pycket.expand    import expand_string, parse_module
    from pycket           import config
    from pycket.callgraph import LOOP_PARTICIPANT, LOOP_HEADER
    str = """
        #lang pycket
        (define (f x) (g (+ x 1)))
        (define (g x) (if (= x 0) (g 5) (h x)))
        (define (h x) x)
        (f 5)
        (f -1)
        """

    ast = parse_module(expand_string(str))
    env = ToplevelEnv(config.get_testing_config(**{"pycket.callgraph":True}))
    m = interpret_module(ast, env)
    f = m.defs[W_Symbol.make("f")].closure.caselam.lams[0]
    g = m.defs[W_Symbol.make("g")].closure.caselam.lams[0]
    h = m.defs[W_Symbol.make("h")].closure.caselam.lams[0]

    assert env.callgraph.calls == {f: {g: None}, g: {h: None, g: None}}
    assert g.body[0].should_enter

    str = """
        #lang pycket
        (define (f x) (g (+ x 1)))
        (define (g x) (if (= x 0) (f 5) (h x)))
        (define (h x) x)
        (g 0)
        """

    ast = parse_module(expand_string(str))
    env = ToplevelEnv(config.get_testing_config(**{"pycket.callgraph":True}))
    m = interpret_module(ast, env)
    f = m.defs[W_Symbol.make("f")].closure.caselam.lams[0]
    g = m.defs[W_Symbol.make("g")].closure.caselam.lams[0]
    h = m.defs[W_Symbol.make("h")].closure.caselam.lams[0]

    assert env.callgraph.calls == {f: {g: None}, g: {h: None, f: None}}
    assert (env.callgraph.recursive == {f: LOOP_HEADER, g: LOOP_PARTICIPANT} or
            env.callgraph.recursive == {f: LOOP_PARTICIPANT, g: LOOP_HEADER})
    assert g.body[0].should_enter or f.body[0].should_enter
Example #11
0
def parse_file(fname, *replacements):
    fname = os.path.join(os.path.dirname(__file__), fname)
    with file(fname) as f:
        s = f.read()
    for replace, with_ in replacements:
        assert s.count(replace) == 1
        s = s.replace(replace, with_)
    s = s.decode("utf-8")
    s = expand_string(s)
    ast = parse_module(s)
    return ast
Example #12
0
def test_callgraph_reconstruction():
    from pycket.expand import expand_string, parse_module
    from pycket import config
    str = """
        #lang pycket
        (define (f x) (g (+ x 1)))
        (define (g x) (if (= x 0) (g 5) (h x)))
        (define (h x) x)
        (f 5)
        (f -1)
        """

    ast = parse_module(expand_string(str))
    env = ToplevelEnv(config.get_testing_config(**{"pycket.callgraph": True}))
    m = interpret_module(ast, env)
    f = m.defs[W_Symbol.make("f")].closure.caselam.lams[0]
    g = m.defs[W_Symbol.make("g")].closure.caselam.lams[0]
    h = m.defs[W_Symbol.make("h")].closure.caselam.lams[0]

    assert env.callgraph.calls == {f: {g: None}, g: {h: None, g: None}}
    assert g.body[0].should_enter

    str = """
        #lang pycket
        (define (f x) (g (+ x 1)))
        (define (g x) (if (= x 0) (f 5) (h x)))
        (define (h x) x)
        (g 0)
        """

    ast = parse_module(expand_string(str))
    env = ToplevelEnv(config.get_testing_config(**{"pycket.callgraph": True}))
    m = interpret_module(ast, env)
    f = m.defs[W_Symbol.make("f")].closure.caselam.lams[0]
    g = m.defs[W_Symbol.make("g")].closure.caselam.lams[0]
    h = m.defs[W_Symbol.make("h")].closure.caselam.lams[0]

    assert env.callgraph.calls == {f: {g: None}, g: {h: None, f: None}}
    assert env.callgraph.recursive == {f: None, g: None}
    assert g.body[0].should_enter
Example #13
0
def parse_file(fname, *replacements):
    fname = os.path.join(os.path.dirname(__file__), fname)
    with file(fname) as f:
        s = f.read()
    for replace, with_ in replacements:
        assert s.count(replace) == 1
        s = s.replace(replace, with_)
    s = s.decode("utf-8")
    
    if not pytest.config.byte_option:
        s = expand_string(s)

        ast = parse_module(s)
    elif pytest.config.byte_option == "recursive" or pytest.config.byte_option == "non-recursive":
        s = expand_from_bytecode(s, True)

        ast = parse_module(s, lib="-l pycket/zoTransform --")
    else:
        print "Check byteOption configuration!"
        

    return ast
Example #14
0
def test_callgraph_reconstruction_through_primitives():
    from pycket.expand import expand_string, parse_module
    from pycket import config
    str = """
        #lang pycket
        (define (f k) (k (apply h '(5))))
        (define (g x) (+ (call-with-current-continuation f) 7))
        (define (h x) x)
        (g 5)
        """

    ast = parse_module(expand_string(str))
    env = ToplevelEnv(config.get_testing_config(**{"pycket.callgraph": True}))
    m = interpret_module(ast, env)
    f = m.defs[W_Symbol.make("f")].closure.caselam.lams[0]
    g = m.defs[W_Symbol.make("g")].closure.caselam.lams[0]
    h = m.defs[W_Symbol.make("h")].closure.caselam.lams[0]

    assert env.callgraph.calls == {g: {f: None}, f: {h: None}}
Example #15
0
def test_callgraph_reconstruction_through_primitives():
    from pycket.expand import expand_string, parse_module
    from pycket        import config
    str = """
        #lang pycket
        (define (f k) (k (apply h '(5))))
        (define (g x) (+ (call/cc f) 7))
        (define (h x) x)
        (g 5)
        """

    ast = parse_module(expand_string(str))
    env = ToplevelEnv(config.get_testing_config(**{"pycket.callgraph":True}))
    m = interpret_module(ast, env)
    f = m.defs[W_Symbol.make("f")].closure.caselam.lams[0]
    g = m.defs[W_Symbol.make("g")].closure.caselam.lams[0]
    h = m.defs[W_Symbol.make("h")].closure.caselam.lams[0]

    assert env.callgraph.calls == {g: {f: None}, f: {h: None}}
Example #16
0
def test_should_enter_downrecursion():
    from pycket.expand import expand_string, parse_module
    from pycket        import config
    str = """
        #lang pycket

        (define (append a b)
          (if (null? a)
              b
              (cons (car a) (append (cdr a) b))))
        (append (list 1 2 3 5 6 6 7 7 8 3 4 5 3 5 4 3 5 3 5 3 3 5 4 3) (list 4 5 6))

        (define (n->f n)
          (cond
           [(zero? n) (lambda (f) (lambda (x) x))]
           [else
            (define n-1 (n->f (- n 1)))
            (lambda (f)
               (define fn-1 (n-1 f))
               (lambda (x) (f (fn-1 x))))]))
        (n->f 10)


    """

    ast = parse_module(expand_string(str))
    env = ToplevelEnv(config.get_testing_config(**{"pycket.callgraph":True}))
    m = interpret_module(ast, env)
    append = m.defs[W_Symbol.make("append")].closure.caselam.lams[0]
    f = m.defs[W_Symbol.make("n->f")].closure.caselam.lams[0]

    assert env.callgraph.calls == {append: {append: None}, f: {f: None}}

    assert append.body[0].should_enter
    # This is long to account for let conversion
    assert append.body[0].els.body[0].should_enter

    assert f.body[0].should_enter
    assert f.body[0].els.body[0].should_enter
Example #17
0
def test_should_enter_downrecursion():
    from pycket.expand import expand_string, parse_module
    from pycket import config
    str = """
        #lang pycket

        (define (append a b)
          (if (null? a)
              b
              (cons (car a) (append (cdr a) b))))
        (append (list 1 2 3 5 6 6 7 7 8 3 4 5 3 5 4 3 5 3 5 3 3 5 4 3) (list 4 5 6))

        (define (n->f n)
          (cond
           [(zero? n) (lambda (f) (lambda (x) x))]
           [else
            (define n-1 (n->f (- n 1)))
            (lambda (f)
               (define fn-1 (n-1 f))
               (lambda (x) (f (fn-1 x))))]))
        (n->f 10)


    """

    ast = parse_module(expand_string(str))
    env = ToplevelEnv(config.get_testing_config(**{"pycket.callgraph": True}))
    m = interpret_module(ast, env)
    append = m.defs[W_Symbol.make("append")].closure.caselam.lams[0]
    f = m.defs[W_Symbol.make("n->f")].closure.caselam.lams[0]

    assert env.callgraph.calls == {append: {append: None}, f: {f: None}}

    assert append.body[0].should_enter
    # This is long to account for let conversion
    assert append.body[0].els.body[0].should_enter

    assert f.body[0].should_enter
    assert f.body[0].els.body[0].should_enter
Example #18
0
def expr_ast(s):
    m = parse_module(expand_string(format_pycket_mod(s, extra="(define x 0)")))
    return m.body[-1]
Example #19
0
def run_mod(m, stdlib=False, srcloc=True):
    assert (not stdlib)
    mod = interpret_module(parse_module(expand_string(m, srcloc=srcloc)))
    return mod
Example #20
0
def expr_ast(s):
    m = parse_module(expand_string(format_pycket_mod(s, extra="(define x 0)")))
    return m.body[-1]
Example #21
0
File: jit.py Project: yws/pycket-1
 def interp_w():
     loader = JsonLoader(False)
     ast = parse_module(_json)
     env.globalconfig.load(ast)
     env.commandline_arguments = []
     interpret_module(ast, env)
Example #22
0
def run_mod(m, stdlib=False, srcloc=True):
    assert (not stdlib)
    ast = parse_module(expand_string(m, srcloc=srcloc))
    return run_ast(ast)
Example #23
0
 def interp_w():
     loader = JsonLoader(False)
     ast = parse_module(_json)
     env.globalconfig.load(ast)
     env.commandline_arguments = []
     interpret_module(ast, env)