Example #1
0
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)
Example #2
0
    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)
Example #3
0
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()