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
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)
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)
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)
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()
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
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
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
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
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
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}}
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}}
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
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
def expr_ast(s): m = parse_module(expand_string(format_pycket_mod(s, extra="(define x 0)"))) return m.body[-1]
def run_mod(m, stdlib=False, srcloc=True): assert (not stdlib) mod = interpret_module(parse_module(expand_string(m, srcloc=srcloc))) return mod
def interp_w(): loader = JsonLoader(False) ast = parse_module(_json) env.globalconfig.load(ast) env.commandline_arguments = [] interpret_module(ast, env)
def run_mod(m, stdlib=False, srcloc=True): assert (not stdlib) ast = parse_module(expand_string(m, srcloc=srcloc)) return run_ast(ast)