def test_compiled_code(self): with open('code/same-partiy.scm', 'r') as f: data = f.read() code = schemepy.scompile(schemepy.split_code_tree(data.decode('utf-8'))) self.assertEqual( str(code), '''((define (same-partiy a . l) (define (same-even a l) (cond ((null? l) '()) ((= a (remainder (car l) 2)) (cons (car l) (same-even a (cdr l)))) (else (same-even a (cdr l))))) (same-even (remainder a 2) l)) (same-partiy 1 3 4 5 6 7))''')
def main(): ''' -c: compile file -d: debug mode -h: help -i: indent file -n: no coredump -p: print source file after parse cdp: coredump file scc: scheme compiled ''' optlist, argv = getopt.getopt(sys.argv[1:], 'cdhinp') optdict = dict(optlist) if '-h' in optdict: print main.__doc__ return if '-c' in optdict: return compile_src(argv[0]) if '-p' in optdict: return print_src(argv[0]) if '-i' in optdict: return indent_src(argv[0], sys.stdout) if len(argv) == 0: return REPL().cmdloop() fname, extname = path.splitext(argv[0]) if extname == '.cdp': with open(argv[0], 'rb') as fi: stack, _ = schemepy.Stack.load(fi.read(), schemepy.builtin) elif extname == '.scc': with open(argv[0], 'rb') as fi: code = cPickle.load(fi) stack = schemepy.init(code, schemepy.builtin) else: with open(argv[0], 'r') as f: data = f.read() code = schemepy.scompile(schemepy.split_code_tree(data.decode('utf-8'))) stack = schemepy.init(code, schemepy.builtin) dbg = schemepy.Debuger() if '-d' in optdict else None def coredump(data): with open(fname+'.cdp', 'wb') as fo: fo.write(data) if '-n' in optdict: coredump = None print stack.trampoline(debug=dbg, coredump=coredump)
def test_break(self): builtin_ = schemepy.builtin.copy() sym_pause.evaled = True builtin_['pause'] = sym_pause r = None code = schemepy.scompile(schemepy.split_code_tree(self.src)) stack = schemepy.init(code, builtin_) breakcode = [] with self.assertRaises(schemepy.BreakException) as be: stack.trampoline(r, coredump=breakcode.append) self.assertEqual(be.exception.args[0][0], 'abc') stack, r = schemepy.Stack.load(''.join(breakcode), builtin_) breakcode = [] with self.assertRaises(schemepy.BreakException) as be: stack.trampoline(r, coredump=breakcode.append) self.assertEqual(be.exception.args[0][0], 'resume1') stack, r = schemepy.Stack.load(''.join(breakcode), builtin_) breakcode = [] r = stack.trampoline(r, coredump=breakcode.append) self.assertEqual(r, 3)
def default(self, line): code = schemepy.scompile(schemepy.split_code_tree(line)) stack = schemepy.Stack() stack.append((schemepy.PrognStatus(code), self.env)) print stack.trampoline()
def indent_src(filename, stream): with open(filename, 'r') as f: data = f.read() code = schemepy.scompile(schemepy.split_code_tree(data.decode('utf-8'))) for i in code: stream.write(unicode(i)+u'\n')
def compile_src(filename): with open(filename, 'r') as f: data = f.read() code = schemepy.scompile(schemepy.split_code_tree(data.decode('utf-8'))) __import__('pprint').pprint(code)
def run_scheme(filepath): with open(filepath, 'r') as f: data = f.read() code = schemepy.scompile(schemepy.split_code_tree(data.decode('utf-8'))) stack = schemepy.init(code, schemepy.builtin) return stack.trampoline()