def load_const(stream): tag = stream.read(1) if tag == K_SYMBOL: return make_symbol(load_string(stream)) elif tag == K_FIXNUM: return W_Fixnum(load_number(stream)) elif tag == K_PAIR: # XXX: recursive call -- stack overflow? car = load_const(stream) cdr = load_const(stream) return W_Pair(car, cdr) elif tag == K_NULL: return w_nil elif tag == K_TRUE: return make_bool(True) elif tag == K_FALSE: return make_bool(False) elif tag == K_UNSPEC: return w_unspecified else: raise ValueError('unknown tag -- %s' % tag)
import __pypy_path__ from pypy.rlib.parsing.makepackrat import PackratParser, BacktrackException, Status from sanya.objectmodel import make_symbol, W_Pair, W_Fixnum, w_nil, make_bool def parse_string(code_string): """ Parse a string and return a sexpression. """ p = MyParser(code_string) return p.prog() # ____________________________________________________________________________ # implementation details. quote_symbol = make_symbol("quote") def make_quote(sexpr): return W_Pair(quote_symbol, W_Pair(sexpr, w_nil)) class MyParser(PackratParser): r""" SYMBOL: c = `[\+\-\*\^\?a-zA-Z!<=>_~/$%&:][\+\-\*\^\?a-zA-Z0-9!<=>_~/$%&:]*` IGNORE* return {make_symbol(c)}; BOOLEAN: c = `#(t|f)`
def make_cont_hole(self): w_cont = self.make_cont_closure() w_retsym = make_symbol(arg_counter.get()) return ContHole(w_cont, w_retsym)
if not prev_cont: outer_cont = prev_cont = walker.visit(expr).make_cont_hole() else: curr_cont = walker.visit(expr).make_cont_hole() prev_cont.set_hole(curr_cont.make_cont_closure()) prev_cont = curr_cont prev_cont.set_hole(cont_s) return outer_cont.make_cont_closure() # ________________________________________________________________________ # implementation details class CPSTransformError(Exception): pass lambda_s = make_symbol('lambda') cont_s = make_symbol('cont') display_s = make_symbol('display') class Counter(object): def __init__(self, prefix=""): self.prefix = prefix self.count = 0 def get(self): res = self.prefix + str(self.count) self.count += 1 return res arg_counter = Counter('cps-t-')