예제 #1
0
 def test_pair_from_list(self):
     s1 = symbol("s1")
     s2 = symbol("s2")
     p = list_to_pair([s1, s2])
     self.assertEquals(p.car_w(), s1)
     self.assertEquals(p.cdr_w().car_w(), s2)
     self.assertEquals(p.cdr_w().cdr_w(), w_nil)
예제 #2
0
 def test_pair_from_list(self):
     s1 = symbol('s1')
     s2 = symbol('s2')
     p = list_to_pair([s1, s2])
     self.assertEquals(p.car_w(), s1)
     self.assertEquals(p.cdr_w().car_w(), s2)
     self.assertEquals(p.cdr_w().cdr_w(), w_nil)
예제 #3
0
 def test_load_const(self):
     code = makecode([Op.LOADCONST, 0, Op.DUP, Op.PRINT, Op.HALT])
     w_proto = W_Proto(code, 0, 0, [], [symbol("x")])
     w_cont = W_Cont(w_proto, [])
     frame = Frame(w_cont, [w_proto])
     w_ret = frame.run()
     self.assertEquals(w_ret.sval, "x")
예제 #4
0
 def test_pair(self):
     s1 = symbol("s1")
     s2 = symbol("s2")
     p = W_Pair(s1, s2)
     self.assertEquals(p.car_w(), s1)
     self.assertEquals(p.cdr_w(), s2)
예제 #5
0
 def test_symbol_ctor(self):
     s1 = symbol("s")
     s2 = symbol("s")
     self.assertIs(s1, s2)
     self.assertEquals(s1.to_string(), ":s")
     self.assertRaises(OperationError, s1.to_int)
예제 #6
0
def w_tag(s, w_x):
    return W_Pair(symbol(s), W_Pair(w_x, w_nil))
예제 #7
0
 def test_pair(self):
     s1 = symbol('s1')
     s2 = symbol('s2')
     p = W_Pair(s1, s2)
     self.assertEquals(p.car_w(), s1)
     self.assertEquals(p.cdr_w(), s2)
예제 #8
0
 def test_symbol_ctor(self):
     s1 = symbol('s')
     s2 = symbol('s')
     self.assertIs(s1, s2)
     self.assertEquals(s1.to_string(), ':s')
     self.assertRaises(OperationError, s1.to_int)
예제 #9
0
    def test_recur(self):
        maincode = makecode(
            [
                Op.INT,
                10,
                0,
                Op.GETGLOBAL,
                1,  # 'display-and-halt
                Op.GETGLOBAL,
                0,  # 'fibo
                Op.CONT,  # (fibo 10 display-and-halt)
            ]
        )
        print_and_halt = makecode([Op.LOAD, 0, Op.DUP, Op.PRINT, Op.NEWLINE, Op.HALT])
        fibo_entry = makecode(
            [
                Op.LOAD,
                0,
                Op.DUP,
                Op.INT,
                2,
                0,
                Op.LT,
                Op.BRANCHIFNOT,
                3,
                0,
                # base case
                Op.LOAD,
                1,
                Op.CONT,
                # recur case
                Op.INT,
                1,
                0,
                Op.ISUB,
                Op.BUILDCONT,
                1,
                0,  # 'fibo-k0, with upval[0] = n, upval[1] = k
                Op.GETGLOBAL,
                0,  # 'fibo
                Op.CONT,
            ]
        )
        fibo_k0 = makecode(
            [
                Op.GETUPVAL,
                0,
                Op.INT,
                2,
                0,
                Op.ISUB,
                Op.BUILDCONT,
                2,
                0,  # 'fibo-k1, with upval[0] = $Rv_0, upval[1] = k
                Op.GETGLOBAL,
                0,  # 'fibo
                Op.CONT,
            ]
        )
        fibo_k1 = makecode([Op.GETUPVAL, 0, Op.LOAD, 0, Op.IADD, Op.GETUPVAL, 1, Op.CONT])  # $Rv_0  # $Rv_1  # k
        fibo_sym = symbol("fibo")
        dah_sym = symbol("display-and-halt")
        const_w0 = [fibo_sym, dah_sym]
        const_w1 = [fibo_sym]
        proto_w = [None, None, None, None, None]
        w_module = ModuleDict()
        proto_w[0] = W_Proto(maincode, nb_args=0, nb_locals=0, upval_descr=[], const_w=const_w0, w_module=w_module)
        w_maincont = W_Cont(proto_w[0], upval_w=[])
        proto_w[1] = W_Proto(
            fibo_k0, nb_args=1, nb_locals=1, upval_descr=[chr(0), chr(1)], const_w=const_w1, w_module=w_module
        )
        proto_w[2] = W_Proto(
            fibo_k1, nb_args=1, nb_locals=1, upval_descr=[chr(0), chr(2)], const_w=const_w1, w_module=w_module
        )
        proto_w[3] = W_Proto(fibo_entry, nb_args=2, nb_locals=2, upval_descr=[], const_w=const_w1, w_module=w_module)
        w_fibocont = W_Cont(proto_w[3], upval_w=[])
        proto_w[4] = W_Proto(print_and_halt, nb_args=1, nb_locals=1, upval_descr=[], const_w=[], w_module=w_module)
        w_printcont = W_Cont(proto_w[4], upval_w=[])

        w_module.setitem(fibo_sym, w_fibocont)
        w_module.setitem(dah_sym, w_printcont)

        frame = Frame(w_maincont, proto_w)

        w_ret = frame.run()
        self.assertEquals(w_ret.to_int(), 55)
예제 #10
0
def regimpl(w_cont):
    prelude_impl[symbol(w_cont.w_proto.name)] = w_cont
예제 #11
0
def regimpl(w_cont):
    prelude_impl[symbol(w_cont.w_proto.name)] = w_cont
예제 #12
0
파일: cps.py 프로젝트: overminder/jitplay
 def __init__(self, nodelist, lastcont=None, toplevel=False):
     self.nodelist = nodelist
     self.lastcont = lastcont or Var(symbol('halt'))
     self.cpsform = None
     self.toplevel = toplevel
예제 #13
0
def w_tag(s, w_x):
    return W_Pair(symbol(s), W_Pair(w_x, w_nil))
예제 #14
0
파일: cps.py 프로젝트: overminder/jitplay
 def __init__(self, nodelist, lastcont=None, toplevel=False):
     self.nodelist = nodelist
     self.lastcont = lastcont or Var(symbol('halt'))
     self.cpsform = None
     self.toplevel = toplevel