Пример #1
0
def intern(ns, sym):
    from clojure.lang.var import Var

    if sym.ns is not None:
        raise InvalidArgumentException("Can't intern namespace-qualified symbol")

    ns = find(ns)
    v = Var(ns, sym)
    setattr(ns, sym.name, v)
    return v
Пример #2
0
    def __new__(cls, name):
        """Returns a namespace with a given name, creating it if needed.

        Adds standard imports to a module without clojure.core.
        clojure.core is special-cased: being the first created module, some
        specific Vars must be added "by hand" (currently: *ns*,
        *command-line-args*).
        """
        if isinstance(name, Symbol):
            name = name.name
        if not sys.modules.get(name):
            mod = ModuleType.__new__(cls)
            ModuleType.__init__(mod, name)
            mod.__file__ = "<interactive namespace>"
            for i in dir(stdimps):
                if i.startswith("_"):
                    continue
                setattr(mod, i, getattr(stdimps, i))
            if mod.__name__ == "clojure.core":
                setattr(mod, "*ns*", Var(mod, "*ns*", mod).setDynamic())
                setattr(mod, "*command-line-args*",
                        Var(mod, "*command-line-args*", None).setDynamic())
            sys.modules[name] = mod
        return sys.modules[name]
Пример #3
0
def intern(ns, sym):
    """Interns a non-ns-qualified Symbol in a namespace.
    """
    sym = Symbol(sym)
    if sym.ns is not None:
        raise InvalidArgumentException(
            "Can't intern namespace-qualified symbol")
    if not isinstance(ns, ModuleType):
        raise InvalidArgumentException
    v = getattr(ns, str(sym), None)
    if v is not None:
        if not isinstance(v, Var):
            raise Exception("Can't redefine {0} as {1}: is not Var".format(
                v, sym))
        if ns.__name__ == v.ns.__name__:
            return v
    v = Var(ns, sym)
    setattr(ns, sym.name, v)
    return v
Пример #4
0
def intern(ns, sym):
    from clojure.lang.var import Var

    if isinstance(sym, str):
        sym = symbol(str)

    if sym.ns is not None:
        raise InvalidArgumentException(
            "Can't intern namespace-qualified symbol")

    ns = find(ns)
    if hasattr(ns, str(sym)):
        v = getattr(ns, str(sym))
        if not isinstance(v, Var):
            raise Exception("can't redefine " + str(v) + " as " + str(sym) +
                            ": is not Var")
        if ns.__name__ == v.ns.__name__:
            return v
    v = Var(ns, sym)
    setattr(ns, sym.name, v)
    return v
Пример #5
0
_AMP_ = Symbol("&")
_FN_ = Symbol("fn")
_VAR_ = Symbol("var")
_APPLY_ = Symbol("apply")
_DEREF_ = Symbol("deref")
_HASHMAP_ = Symbol("clojure.core", "hashmap")
_CONCAT_ = Symbol("clojure.core", "concat")
_LIST_ = Symbol("clojure.core", "list")
_SEQ_ = Symbol("clojure.core", "seq")
_VECTOR_ = Symbol("clojure.core", "vector")
_QUOTE_ = Symbol("quote")
_SYNTAX_QUOTE_ = Symbol("`")
_UNQUOTE_ = Symbol("~")
_UNQUOTE_SPLICING_ = Symbol("~@")

ARG_ENV = Var(None).setDynamic()
GENSYM_ENV = Var(None).setDynamic()

symbolPat = re.compile("[:]?([\\D^/].*/)?([\\D^/][^/]*)")

intPat = re.compile(r"""
(?P<sign>[+-])?
  (:?
    # radix: 12rAA
    (?P<radix>(?P<base>[1-9]\d?)[rR](?P<value>[0-9a-zA-Z]+))    |
    # decima1: 0, 23, 234, 3453455
    (?P<decInt>0|[1-9]\d*)                                      |
    # octal: 0777
    0(?P<octInt>[0-7]+)                                         |
    # hex: 0xff
    0[xX](?P<hexInt>[0-9a-fA-F]+))
Пример #6
0
from clojure.lang.var import Var

currentCompiler = Var()
currentCompiler.setDynamic()