예제 #1
0
def make_multi_arity(frame, argc):
    d = {}
    required_arity = 0
    rest_fn = None
    for i in range(argc):
        a = frame.get_inst()
        if a & 256:
            affirm(rest_fn is None, u"Can't have multiple rest_fns")
            required_arity = a & 0xFF
            rest_fn = frame.pop()
        else:
            fn = frame.pop()
            d[a] = fn

    return code.MultiArityFn(d, required_arity, rest_fn)
예제 #2
0
def gensym1():
    return gensym2(rt.wrap(u"gensym_"))


def gensym2(prefix):
    rt.reset_BANG_(gensym_id, rt._add(rt.deref(gensym_id), rt.wrap(1)))
    i = rt.deref(gensym_id)

    return rt.symbol(rt.str(prefix, i))


gensym = code.intern_var(u"pixie.stdlib", u"gensym")
gensym.set_root(
    code.MultiArityFn(u"gensym", {
        0: code.wrap_fn(gensym1),
        1: code.wrap_fn(gensym2)
    }))


class with_ns(object):
    def __init__(self, nm, include_stdlib=False):
        assert isinstance(nm, unicode)
        self._ns = nm
        self._include_stdlib = include_stdlib

    def __enter__(self):
        code._dynamic_vars.push_binding_frame()
        NS_VAR.set_value(code._ns_registry.find_or_make(self._ns))
        if self._include_stdlib:
            NS_VAR.deref().include_stdlib()