Exemplo n.º 1
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)
Exemplo n.º 2
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()
Exemplo n.º 3
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)
Exemplo n.º 4
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")

    s = expand_string(s)
    ast = parse_module(s)

    # 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
Exemplo n.º 5
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
Exemplo n.º 6
0
Arquivo: jit.py Projeto: yws/pycket-1
    def _cons_map(self):
        from pycket.expand import expand_string
        from pycket.json import loads

        ast = to_ast(
            loads(
                expand_string("""
        (letrec ([range-it (lambda (n a)
                                   (if (<= n 0)
                                       a
                                       (range-it (- n 1) (cons n a))))]
                 [range (lambda (n) (range-it n '()))])
          (foldl + 0 (range 1000)))
        """,
                              wrap=True,
                              stdlib=True)))

        def interp_w():
            val = interpret_one(ast)
            ov = check_one_val(val)
            assert isinstance(ov, W_Fixnum)
            return ov.value

        assert interp_w() == 500500

        self.meta_interp(interp_w, [],
                         listcomp=True,
                         listops=True,
                         backendopt=True)
Exemplo n.º 7
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)
Exemplo n.º 8
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)
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 13
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)
Exemplo n.º 14
0
    def run_string(self, str):
        _json = expand_string(str)
        env = ToplevelEnv()

        def interp_w():
            loader = JsonLoader(False)
            ast = parse_module(_json)
            env.globalconfig.load(ast)
            env.commandline_arguments = []
            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)
Exemplo n.º 15
0
Arquivo: jit.py Projeto: yws/pycket-1
    def run_string(self, str):
        _json = expand_string(str)
        env = ToplevelEnv()

        def interp_w():
            loader = JsonLoader(False)
            ast = parse_module(_json)
            env.globalconfig.load(ast)
            env.commandline_arguments = []
            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)
Exemplo n.º 16
0
def expand_from_bytecode(m, srcloc):
    with NamedTemporaryFile(delete=delete_temp_files) as f:
        f.write(m)
        f.seek(0)
        os.rename(f.name, f.name+'.rkt')
        s = expand_string(m, reuse=False, srcloc=srcloc, byte_option=True, tmp_file_name=f.name)
        os.rename(f.name+'.rkt', f.name) # for automatic deletion to find it

        if delete_temp_files: # removing the compiled/*.dep && *.zo as well
            subs = f.name.split("/")
            parent = subs[:-1]
            parent.append('compiled')
            file_name = subs[-1]
            byteCodesPrefix = "/".join(parent) + "/" + file_name + "_rkt"
            os.remove(byteCodesPrefix + ".zo")
            os.remove(byteCodesPrefix + ".dep")

    return s
Exemplo n.º 17
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}}
Exemplo n.º 18
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}}
Exemplo n.º 19
0
def expand_from_bytecode(m, srcloc):
    with NamedTemporaryFile(delete=delete_temp_files) as f:
        f.write(m.encode("utf-8"))
        f.seek(0)
        os.rename(f.name, f.name + '.rkt')
        s = expand_string(m.encode("utf-8"),
                          reuse=False,
                          srcloc=srcloc,
                          byte_option=True,
                          tmp_file_name=f.name)
        os.rename(f.name + '.rkt', f.name)  # for automatic deletion to find it

        if delete_temp_files:  # removing the compiled/*.dep && *.zo as well
            subs = f.name.split("/")
            parent = subs[:-1]
            parent.append('compiled')
            file_name = subs[-1]
            byteCodesPrefix = "/".join(parent) + "/" + file_name + "_rkt"
            os.remove(byteCodesPrefix + ".zo")
            os.remove(byteCodesPrefix + ".dep")

    return s
Exemplo n.º 20
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
Exemplo n.º 21
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
Exemplo n.º 22
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
Exemplo n.º 23
0
    def _cons_map(self):
        from pycket.expand import expand_string
        from pycket.json import loads

        ast = to_ast(loads(expand_string("""
        (letrec ([range-it (lambda (n a)
                                   (if (<= n 0)
                                       a
                                       (range-it (- n 1) (cons n a))))]
                 [range (lambda (n) (range-it n '()))])
          (foldl + 0 (range 1000)))
        """, wrap=True, stdlib=True)))

        def interp_w():
            val = interpret_one(ast)
            ov = check_one_val(val)
            assert isinstance(ov, W_Fixnum)
            return ov.value
        assert interp_w() == 500500

        self.meta_interp(interp_w, [],
                         listcomp=True, listops=True, backendopt=True)
Exemplo n.º 24
0
def test_read_err ():
    with pytest.raises(Exception):
        expand_string("(", result=False)
Exemplo n.º 25
0
def expr_ast(s):
    m = parse_module(expand_string(format_pycket_mod(s, extra="(define x 0)")))
    return m.body[-1]
Exemplo n.º 26
0
def load_expanded(fname):
    stdlib = readfile("stdlib.sch")
    s = readfile(fname)
    code = "(let () \n%s\n%s\n)" % (stdlib, s)
    return expand_string(code)
Exemplo n.º 27
0
def run_mod(m, stdlib=False, srcloc=True):
    assert (not stdlib)
    mod = interpret_module(parse_module(expand_string(m, srcloc=srcloc)))
    return mod
Exemplo n.º 28
0
def test_read_err():
    with pytest.raises(Exception):
        expand_string("(", result=False)
Exemplo n.º 29
0
def run_mod(m, stdlib=False, srcloc=True):
    assert (not stdlib)
    ast = parse_module(expand_string(m, srcloc=srcloc))
    return run_ast(ast)
Exemplo n.º 30
0
def expr_ast(s):
    m = parse_module(expand_string(format_pycket_mod(s, extra="(define x 0)")))
    return m.body[-1]
Exemplo n.º 31
0
def load_expanded(fname):
    stdlib = readfile("stdlib.sch")
    s = readfile(fname)
    code = "(let () \n%s\n%s\n)" % (stdlib, s)
    return expand_string(code)