Exemple #1
0
def currentNSName():
    comp = currentCompiler.deref()
    if comp is None:
        raise IllegalStateException("No Compiler found in syntax quote!")
    ns = comp.getNS()
    if ns is None:
        raise IllegalStateException("No ns in reader")
    return ns.__name__
Exemple #2
0
    def set(self, val):
        self.validate(self.getValidator(), val)
        b = self.getThreadBinding()
        if b is not None:
            if currentThread() != b.thread:
                raise IllegalStateException("Can't set!: " + str(self.sym) +
                                            " from non-binding thread")
            b.val = val
            return self

        raise IllegalStateException("Can't change/establish root binding "
                                    "of: %s with set" % str(self.sym))
Exemple #3
0
def fnReader(rdr, lparen):
    """Read an anonymous function #() from reader

    rdr -- a read/unread-able object
    lparen -- ignored

    Return an IPersistentList"""
    if ARG_ENV.deref() is not None:
        raise IllegalStateException("Nested #()s are not allowed")
    with threadBindings(RT.map(ARG_ENV, EMPTY_MAP)):
        rdr.back()
        form = read(rdr, True, None, True)
        drefed = ARG_ENV.deref()
        sargs = sorted(list(filter(lambda x: x != -1, drefed)))
        args = []
        if len(sargs):
            for x in range(1, int(str(sargs[-1])) + 1):
                if x in drefed:
                    args.append(drefed[x])
                else:
                    args.append(garg(x))
            retsym = drefed[-1]
            if retsym is not None:
                args.append(_AMP_)
                args.append(retsym)
        vargs = RT.vector(*args)
    return RT.list(_FN_, vargs, form)
Exemple #4
0
    def pop(self):
        """Return a new PersistentVector.

        The returned vector will contain all the items it this vector except
        the *last* item. It will have this vector's meta data attached.

        Will raise IllegalStateException if this vector is empty."""
        if not self._cnt:
            raise IllegalStateException("Can't pop empty vector")
        if self._cnt == 1:
            return EMPTY.withMeta(self.meta())
        # pop from the _tail, done
        if self._cnt - self._tailoff() > 1:
            newTail = self._tail[:]
            newTail.pop()
            return PersistentVector(self.meta(), self._cnt - 1, self._shift,
                                    self._root, newTail)
        # the last sublist, post-pop
        newtail = self._arrayFor(self._cnt - 2)
        # pop from the last Node, done
        newroot = self._popTail(self._shift, self._root)
        newshift = self._shift
        if newroot is None:
            newroot = EMPTY_NODE
        if self._shift > 5 and newroot._array[1] is None:
            newroot = newroot._array[0]
            newshift -= 5
        return PersistentVector(self.meta(), self._cnt - 1, newshift, newroot,
                                newtail)
Exemple #5
0
def registerArg(arg):
    argsyms = ARG_ENV.deref()
    if argsyms is None:
        raise IllegalStateException("arg literal not in #()")
    ret = argsyms[arg]
    if ret is None:
        ret = garg(arg)
        ARG_ENV.set(argsyms.assoc(arg, ret))
    return ret
Exemple #6
0
def pushThreadBindings(bindings):
    f = dvals.get(lambda: Frame())
    bmap = f.bindings
    for v in bindings:
        value = bindings[v]
        if not v.dynamic:
            raise IllegalStateException("Can't dynamically bind non-dynamic "
                                        "var: " + str(v.ns) + "/" + str(v.sym))
        v.validate(v.getValidator(), value)
        v.threadBound = True
        bmap = bmap.assoc(v, TBox(currentThread(), value))
    dvals.set(Frame(bmap, f))
Exemple #7
0
    def validate(self, *args):
        if len(args) == 1:
            val = args[0]
            vf = self.validator
        elif len(args) == 2:
            vf = args[0]
            val = args[1]
        else:
            raise ArityException()

        if vf is not None \
           and not RT.booleanCast(vf(val)):
            raise IllegalStateException("Invalid reference state")
Exemple #8
0
def pushThreadBindings(bindings):
    f = dvals.get(lambda: Frame())
    bmap = f.bindings
    bs = bindings.seq()
    while bs is not None:
        e = bs.first()
        v = e.getKey()
        if not v.dynamic:
            raise IllegalStateException("Can't dynamically bind non-dynamic "
                                        "var: " + str(v.ns) + "/"
                                        + str(v.sym))
        v.validate(v.getValidator(), e.getValue())
        v.threadBound = True
        bmap = bmap.assoc(v, TBox(currentThread(), e.getValue()))
        bs = bs.next()
    dvals.set(Frame(bmap, f))
    def pop(self):
        if not self.cnt:
            raise IllegalStateException("Can't pop empty vector")
        if self.cnt == 1:
            return EMPTY.withMeta(self.meta())
        if self.cnt - self.tailoff() > 1:
            newTail = self.tail[:]
            newTail.pop()
            return PersistentVector(self.meta(), self.cnt - 1, self.shift,
                                    self.root, newTail)

        newtail = self.arrayFor(self.cnt - 2)

        newroot = self.popTail(self.shift, self.root)
        newshift = self.shift
        if newroot is None:
            newroot = EMPTY_NODE
        if self.shift > 5 and newroot.array[1] is None:
            newroot = newroot.array[0]
            newshift -= 5
        if newroot is None:
            pass
        return PersistentVector(self.meta(), self.cnt - 1, newshift, newroot,
                                newtail)
Exemple #10
0
    def syntaxQuote(self, form):
        # compiler uses this module, so import it lazily
        from clojure.lang.compiler import builtins as compilerbuiltins

        if form in compilerbuiltins:
            ret = RT.list(_QUOTE_, form)
        elif isinstance(form, Symbol):
            sym = form
            if sym.ns is None and sym.name.endswith("#"):
                gmap = GENSYM_ENV.deref()
                if gmap == None:
                    raise ReaderException("Gensym literal not in syntax-quote, before", self.rdr)
                gs = gmap[sym]
                if gs is None:
                    gs = Symbol(None, "{0}__{1}__auto__".format(sym.name[:-1], RT.nextID()))
                    GENSYM_ENV.set(gmap.assoc(sym, gs))
                sym = gs
            elif sym.ns is None and sym.name.endswith("."):
                ret = sym
            elif sym.ns is None and sym.name.startswith("."):
                ret = sym
            elif sym.ns is not None:
                ret = sym

            else:
                comp = currentCompiler.deref()
                if comp is None:
                    raise IllegalStateException("No Compiler found in syntax quote!")
                ns = comp.getNS()
                if ns is None:
                    raise IllegalStateException("No ns in reader")
                
                item = namespace.findItem(ns, sym)
                if item is None:
                    sym = Symbol(ns.__name__, sym.name)
                else:
                    sym = Symbol(item.ns.__name__, sym.name)
            ret = RT.list(_QUOTE_, sym)
        else:
            if isUnquote(form):
                return form.next().first()
            elif isUnquoteSplicing(form):
                raise IllegalStateException("splice not in list")
            elif isinstance(form, IPersistentCollection):
                if isinstance(form, IPersistentMap):
                    keyvals = self.flattenMap(form)
                    ret = RT.list(_APPLY_, _HASHMAP_, RT.list(RT.cons(_CONCAT_, self.sqExpandList(keyvals.seq()))))
                elif isinstance(form, (IPersistentVector, IPersistentSet)):
                    ret = RT.list(_APPLY_, _VECTOR_, RT.list(_SEQ_, RT.cons(_CONCAT_, self.sqExpandList(form.seq()))))
                elif isinstance(form, (ISeq, IPersistentList)):
                    seq = form.seq()
                    if seq is None:
                        ret = RT.cons(_LIST_, None)
                    else:
                        ret = RT.list(_SEQ_, RT.cons(_CONCAT_, self.sqExpandList(seq)))
                else:
                    raise IllegalStateException("Unknown collection type")
            elif isinstance(form, (int, float, str, Keyword)):
                ret = form
            else:
                ret = RT.list(_QUOTE_, form)
        if getattr(form, "meta", lambda: None)() is not None:
            newMeta = form.meta().without(LINE_KEY)
            if len(newMeta) > 0:
                return RT.list(_WITH_META_, ret, self.syntaxQuote(form.meta()))#FIXME: _WITH_META_ undefined
        return ret
Exemple #11
0
def popThreadBindings():
    f = dvals.get(Frame)
    if f.prev is None:
        raise IllegalStateException("Pop without matching push")
    dvals.set(f.prev)
Exemple #12
0
 def pop(self):
     """Raise IllegalStateException."""
     raise IllegalStateException("Can't pop an empty list")