def syntaxQuote(self, form): 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, sym.name[:-1] + "__" + str(RT.nextID()) + "__auto__") 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.get(lambda: None) 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") sym = symbol(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 hasattr(form, "meta") and form.meta() 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
def run_repl(comp=None): """ Starts the repl. Assumes that RT.init has allready be called. """ print "clojure-py", VERSION print "Python", sys.version if comp is None: curr = currentCompiler.get(lambda: None) if curr == None: comp = Compiler() currentCompiler.set(comp) else: comp = curr comp.setNS(symbol("user")) core = sys.modules["clojure.core"] for i in dir(core): if not i.startswith("_"): setattr(comp.getNS(), i, getattr(core, i)) last3 = [None, None, None] def execute(string): r = StringReader(string) s = read(r, False, None, True) res = comp.compile(s) return comp.executeCode(res) while 1: #for i, value in enumerate(last3, 1): # v = findVar(symbol("clojure.core", "*%s" % i)) # if isinstance(value, Var): # v.bindRoot(value.deref()) # v.setMeta(value.meta()) # else: # v.bindRoot(value) try: line = raw_input(comp.getNS().__name__ + "=> ") except EOFError: print break if not line: continue invalid = False while 1: unbalance = unbalanced(line) if unbalance == -1: invalid = True break elif unbalance is False: break try: new_line = '\n' + raw_input('.' * len(comp.getNS().__name__) + '.. ') except EOFError: break if not new_line.strip().startswith(';'): line += new_line if invalid: print "Invalid input" continue # Propogate break from above loop. if unbalanced(line): break try: out = execute(line) except Exception: traceback.print_exc() else: last3.pop() last3.insert(0, out) RT.printTo(out)
def run_repl(comp=None): """ Starts the repl. Assumes that RT.init has allready be called. """ if comp is None: curr = currentCompiler.get(lambda: None) if curr == None: comp = Compiler() currentCompiler.set(comp) else: comp = curr comp.setNS(symbol("user")) last3 = [None, None, None] def execute(string): r = StringReader(string) s = read(r, False, None, True) res = comp.compile(s) return comp.executeCode(res) while 1: for i, value in enumerate(last3, 1): sym = symbol('*%s' % i) v = internVar(comp.getNS(), sym) v.setDynamic(True) if isinstance(value, Var): v.bindRoot(value.deref()) v.setMeta(value.meta()) else: v.bindRoot(value) try: line = raw_input(comp.getNS().__name__ + "=> ") except EOFError: break if not line: continue invalid = False while 1: unbalance = unbalanced(line) if unbalance == -1: invalid = True break elif unbalance is False: break try: new_line = '\n' + raw_input('.' * len(comp.getNS().__name__) + '.. ') except EOFError: break if not new_line.strip().startswith(';'): line += new_line if invalid: print "Invalid input" continue # Propogate break from above loop. if unbalanced(line): break try: out = execute(line) except Exception: traceback.print_exc() else: last3.pop() last3.insert(0, out) print out
def run_repl(opts, comp=None): """Initializes and runs the REPL. Assumes that RT.init has been called. Repeatedly reads well-formed forms from stdin (with an interactive prompt if a tty) and evaluates them (and prints the result if a tty). Exits on EOF. """ if not opts.quiet and os.isatty(0): print VERSION_MSG if comp is None: curr = currentCompiler.get(lambda: None) if curr == None: comp = Compiler() currentCompiler.set(comp) else: comp = curr comp.setNS(Symbol("user")) core = sys.modules["clojure.core"] for i in dir(core): if not i.startswith("_"): setattr(comp.getNS(), i, getattr(core, i)) line = opts.cmd last3 = [None, None, None] def firstLinePrompt(): return comp.getNS().__name__ + "=> " if os.isatty(0) else "" def continuationLinePrompt(): return "." * len(comp.getNS().__name__) + ".. " if os.isatty(0) else "" while True: # Evaluating before prompting caters for initially given forms. r = StringReader(line) while True: try: s = read(r, False, None, True) if s is None: break res = comp.compile(s) out = comp.executeCode(res) except Exception: traceback.print_exc() else: if os.isatty(0): RT.printTo(out) last3.pop() last3.insert(0, out) for i, value in enumerate(last3, 1): v = findItem(Namespace("clojure.core"), Symbol("*{0}".format(i))) if isinstance(value, Var): v.bindRoot(value.deref()) v.setMeta(value.meta()) else: v.bindRoot(value) try: line = raw_input(firstLinePrompt()) while unbalanced(line): line += "\n" + raw_input(continuationLinePrompt()) except BracketsException as exc: print exc continue except EOFError: print break