def getAccessCode(self, sym): if (sym.ns is not None and sym.ns == self.getNS().__name__) \ or sym.ns is None: if self.getNS() is None: raise CompilerException("no namespace has been defined", None) if not hasattr(self.getNS(), RT.name(sym)): raise CompilerException("could not resolve '" + str(sym) + "', '" \ + RT.name(sym) + "' not found in " + self.getNS().__name__ + " reference " + str(self.getNamesString(False)), None) var = getattr(self.getNS(), RT.name(sym)) return [GlobalPtr(self.getNS(), RT.name(sym))] if hasattr(self.getNS(), "__aliases__") and \ symbol(sym.ns) in self.getNS().__aliases__: sym = symbol(self.getNS().__aliases__[symbol(sym.ns)].__name__, RT.name(sym)) splt = [] if sym.ns is not None: module = findNamespace(sym.ns) if not hasattr(module, RT.name(sym)): raise CompilerException( str(module) + " does not define " + RT.name(sym), None) return [GlobalPtr(module, RT.name(sym))] code = LOAD_ATTR if sym.ns else LOAD_GLOBAL #if not sym.ns and RT.name(sym).find(".") != -1 and RT.name(sym) != "..": raise CompilerException( "unqualified dotted forms not supported: " + str(sym), sym) if len(RT.name(sym).replace(".", "")): splt.extend((code, attr) for attr in RT.name(sym).split(".")) else: splt.append((code, RT.name(sym))) return splt
def getAccessCode(self, sym): if (sym.ns is not None and sym.ns == self.getNS().__name__) \ or sym.ns is None: if self.getNS() is None: raise CompilerException("no namespace has been defined", None) if not hasattr(self.getNS(), sym.name): raise CompilerException("could not resolve '" + str(sym) + "', '" \ + sym.name + "' not found in " + self.getNS().__name__ + " reference " + str(self.getNamesString(False)), None) var = getattr(self.getNS(), sym.name) return [GlobalPtr(self.getNS(), sym.name)] if hasattr(self.getNS(), "__aliases__") and \ symbol(sym.ns) in self.getNS().__aliases__: sym = symbol(self.getNS().__aliases__[symbol(sym.ns)].__name__, sym.name) splt = [] if sym.ns is not None: module = findNamespace(sym.ns) if not hasattr(module, sym.name): raise CompilerException(str(module) + " does not define " + sym.name, None) return [GlobalPtr(module, sym.name)] code = LOAD_ATTR if sym.ns else LOAD_GLOBAL #if not sym.ns and sym.name.find(".") != -1 and sym.name != "..": raise CompilerException("unqualified dotted forms not supported: " + str(sym), sym) if len(sym.name.replace(".", "")): splt.extend((code, attr) for attr in sym.name.split(".")) else: splt.append((code, sym.name)) return splt
def find(sym): from clojure.lang.namespace import find as findNamespace if sym.ns is None: raise InvalidArgumentException("Symbol must be namespace-qualified") ns = findNamespace(symbol(sym.ns)) if ns is None: raise InvalidArgumentException("No such namespace " + str(sym.ns)) return getattr(ns, sym.name)
def registerFns(ns, fns): ns = findNamespace(ns) protofns = {} for fn in fns: fname = ns.__name__ + fn if hasattr(ns, fn): proto = getattr(ns, fn) else: proto = ProtocolFn(fname) setattr(ns, fn, proto) proto.__name__ = fn protofns[fn] = proto return protofns
def protocolFromType(ns, tp): """Considers the input type to be a prototype for a protocol. Useful for turning abstract classes into protocols""" fns = [] for x in dir(tp): if not x.startswith("_"): fns.append(x) thens = findNamespace(ns) proto = Protocol(ns, tp.__name__, fns) if not hasattr(tp, "__protocols__"): tp.__protocols__ = [] tp.__protocols__.append(proto) if not hasattr(thens, tp.__name__): setattr(thens, tp.__name__, proto) return proto
def protocolFromType(ns, tp): """Considers the input type to be a prototype for a protocol. Useful for turning abstract classes into protocols""" fns = [] for x in dir(tp): if not x.startswith("_"): fns.append(x) thens = findNamespace(ns) proto = Protocol(ns, tp.__name__, fns) tp.__exactprotocol__ = proto tp.__exactprotocolclass__ = tp if not hasattr(tp, "__protocols__"): tp.__protocols__ = [] tp.__protocols__.append(proto) if not hasattr(thens, tp.__name__): setattr(thens, tp.__name__, proto) return proto
def getAccessCode(self, sym): print sym if (sym.ns is not None and sym.ns == self.nsString) \ or sym.ns is None: if self.getNS() is None: raise CompilerException("no namespace has been defined", None) if not hasattr(self.getNS(), RT.name(sym)): raise CompilerException( "could not resolve '{0}', '{1}' not found in {2} reference {3}". format(sym, RT.name(sym), self.getNS().__name__, self.getNamesString(False)), None) var = getattr(self.getNS(), RT.name(sym)) return maybeDeref(self.ns, self.nsString, RT.name(sym), self.nsString) if symbol(sym.ns) in getattr(self.getNS(), "__aliases__", {}): sym = symbol(self.getNS().__aliases__[symbol(sym.ns)].__name__, RT.name(sym)) splt = [] if sym.ns is not None: module = findNamespace(sym.ns) if not hasattr(module, RT.name(sym)): raise CompilerException( "{0} does not define {1}".format(module, RT.name(sym)), None) return getAttrChain(sym.getNamespace() + "." + sym.getName()) code = LOAD_ATTR if sym.ns else LOAD_GLOBAL #if not sym.ns and RT.name(sym).find(".") != -1 and RT.name(sym) != "..": raise CompilerException( "unqualified dotted forms not supported: {0}".format(sym), sym) if len(RT.name(sym).replace(".", "")): splt.extend((code, attr) for attr in RT.name(sym).split(".")) else: splt.append((code, RT.name(sym))) return splt
def getAccessCode(self, sym): if (sym.ns is not None and sym.ns == self.getNS().__name__) \ or sym.ns is None: if not hasattr(self.getNS(), sym.name): raise CompilerException("could not resolve " + str(sym) + " " \ + sym.name + " not found in " + self.getNS().__name__, sym) var = getattr(self.getNS(), sym.name) if isinstance(var, Var): if (var.isDynamic()): return [(LOAD_CONST, var.deref), (CALL_FUNCTION, 0)] else: return [(LOAD_CONST, var.deref())] return [(LOAD_GLOBAL, sym.name)] splt = [] if sym.ns is not None: module = findNamespace(sym.ns) if not hasattr(module, sym.name): raise CompilerException( str(module) + " does not define " + sym.name, None) attr = getattr(module, sym.name) if isinstance(attr, Var): splt.append((LOAD_CONST, attr.deref)) splt.append((CALL_FUNCTION, 0)) else: splt.append((LOAD_CONST, module)) splt.append((LOAD_ATTR, sym.name)) return splt code = LOAD_ATTR if sym.ns else LOAD_GLOBAL if not sym.ns and sym.name.find(".") != -1 and sym.name != "..": raise CompilerException( "unqualified dotted forms not supported: " + str(sym), sym) if len(sym.name.replace(".", "")): splt.extend((code, attr) for attr in sym.name.split(".")) else: splt.append((code, sym.name)) return splt
def getAccessCode(self, sym): if (sym.ns is not None and sym.ns == self.getNS().__name__) \ or sym.ns is None: if not hasattr(self.getNS(), sym.name): raise CompilerException("could not resolve " + str(sym) + " " \ + sym.name + " not found in " + self.getNS().__name__, sym) var = getattr(self.getNS(), sym.name) if isinstance(var, Var): if (var.isDynamic()): return [(LOAD_CONST, var.deref), (CALL_FUNCTION, 0)] else: return [(LOAD_CONST, var.deref())] return [(LOAD_GLOBAL, sym.name)] splt = [] if sym.ns is not None: module = findNamespace(sym.ns) if not hasattr(module, sym.name): raise CompilerException(str(module) + " does not define " + sym.name, None) attr = getattr(module, sym.name) if isinstance(attr, Var): splt.append((LOAD_CONST, attr.deref)) splt.append((CALL_FUNCTION, 0)) else: splt.append((LOAD_CONST, module)) splt.append((LOAD_ATTR, sym.name)) return splt code = LOAD_ATTR if sym.ns else LOAD_GLOBAL if not sym.ns and sym.name.find(".") != -1 and sym.name != "..": raise CompilerException("unqualified dotted forms not supported: " + str(sym), sym) if len(sym.name.replace(".", "")): splt.extend((code, attr) for attr in sym.name.split(".")) else: splt.append((code, sym.name)) return splt