diff options
author | Rich Hickey <richhickey@gmail.com> | 2007-10-18 15:25:37 +0000 |
---|---|---|
committer | Rich Hickey <richhickey@gmail.com> | 2007-10-18 15:25:37 +0000 |
commit | 87fe051f497dde0a8641689cf18928bffcf329dd (patch) | |
tree | b900b59912a32badca3e5d5dbe11f1774b267f51 /src | |
parent | b9ccb2c49232b90766c12d104364b51f15ad596b (diff) |
added Repl and Script
Diffstat (limited to 'src')
-rw-r--r-- | src/jvm/clojure/lang/Compiler.java | 27 | ||||
-rw-r--r-- | src/jvm/clojure/lang/Repl.java | 83 | ||||
-rw-r--r-- | src/jvm/clojure/lang/Script.java | 42 |
3 files changed, 141 insertions, 11 deletions
diff --git a/src/jvm/clojure/lang/Compiler.java b/src/jvm/clojure/lang/Compiler.java index 85179fb7..903cf467 100644 --- a/src/jvm/clojure/lang/Compiler.java +++ b/src/jvm/clojure/lang/Compiler.java @@ -552,7 +552,7 @@ static abstract class HostExpr implements Expr{ //at this point className will be non-null if static Expr instance = null; if(className == null) - instance = analyze(C.EXPRESSION, RT.second(form)); + instance = analyze(context == C.EVAL ? context : C.EXPRESSION, RT.second(form)); if(RT.third(form) instanceof Symbol) //field { @@ -567,7 +567,7 @@ static abstract class HostExpr implements Expr{ Symbol sym = (Symbol) RT.first(RT.third(form)); PersistentVector args = PersistentVector.EMPTY; for(ISeq s = RT.rest(RT.third(form)); s != null; s = s.rest()) - args = args.cons(analyze(C.EXPRESSION, s.first())); + args = args.cons(analyze(context == C.EVAL ? context : C.EXPRESSION, s.first())); if(className != null) return new StaticMethodExpr(line, className, sym.name, args); else @@ -1475,7 +1475,8 @@ static class IfExpr implements Expr{ throw new Exception("Too many arguments to if"); else if(form.count() < 3) throw new Exception("Too few arguments to if"); - return new IfExpr((Integer) LINE.get(), analyze(C.EXPRESSION, RT.second(form)), + return new IfExpr((Integer) LINE.get(), + analyze(context == C.EVAL ? context : C.EXPRESSION, RT.second(form)), analyze(context, RT.third(form)), analyze(context, RT.fourth(form))); } @@ -1635,12 +1636,13 @@ static class MapExpr implements Expr{ for(ISeq s = RT.seq(form); s != null; s = s.rest()) { IMapEntry e = (IMapEntry) s.first(); - keyvals = (IPersistentVector) keyvals.cons(analyze(C.EXPRESSION, e.key())); - keyvals = (IPersistentVector) keyvals.cons(analyze(C.EXPRESSION, e.val())); + keyvals = (IPersistentVector) keyvals.cons(analyze(context == C.EVAL ? context : C.EXPRESSION, e.key())); + keyvals = (IPersistentVector) keyvals.cons(analyze(context == C.EVAL ? context : C.EXPRESSION, e.val())); } Expr ret = new MapExpr(keyvals); if(form instanceof IObj && ((IObj) form).meta() != null) - return new MetaExpr(ret, (MapExpr) MapExpr.parse(C.EXPRESSION, ((IObj) form).meta())); + return new MetaExpr(ret, (MapExpr) MapExpr + .parse(context == C.EVAL ? context : C.EXPRESSION, ((IObj) form).meta())); else return ret; } @@ -1680,10 +1682,11 @@ static class VectorExpr implements Expr{ static public Expr parse(C context, IPersistentVector form) throws Exception{ IPersistentVector args = PersistentVector.EMPTY; for(int i = 0; i < form.count(); i++) - args = (IPersistentVector) args.cons(analyze(C.EXPRESSION, form.nth(i))); + args = (IPersistentVector) args.cons(analyze(context == C.EVAL ? context : C.EXPRESSION, form.nth(i))); Expr ret = new VectorExpr(args); if(form instanceof IObj && ((IObj) form).meta() != null) - return new MetaExpr(ret, (MapExpr) MapExpr.parse(C.EXPRESSION, ((IObj) form).meta())); + return new MetaExpr(ret, (MapExpr) MapExpr + .parse(context == C.EVAL ? context : C.EXPRESSION, ((IObj) form).meta())); else return ret; } @@ -1744,11 +1747,13 @@ static class InvokeExpr implements Expr{ } static public Expr parse(C context, ISeq form) throws Exception{ - Expr fexpr = analyze(C.EXPRESSION, form.first()); + if(context != C.EVAL) + context = C.EXPRESSION; + Expr fexpr = analyze(context, form.first()); PersistentVector args = PersistentVector.EMPTY; for(ISeq s = RT.seq(form.rest()); s != null; s = s.rest()) { - args = args.cons(analyze(C.EXPRESSION, s.first())); + args = args.cons(analyze(context, s.first())); } // if(args.count() > MAX_POSITIONAL_ARITY) // throw new IllegalArgumentException( @@ -2610,7 +2615,7 @@ private static void registerVar(Var var) throws Exception{ VARS.set(RT.assoc(varsMap, var, var)); } -private static Symbol currentNS(){ +static Symbol currentNS(){ return (Symbol) RT.CURRENT_NS_SYM.get(); } diff --git a/src/jvm/clojure/lang/Repl.java b/src/jvm/clojure/lang/Repl.java new file mode 100644 index 00000000..e3de82d5 --- /dev/null +++ b/src/jvm/clojure/lang/Repl.java @@ -0,0 +1,83 @@ +/** + * Copyright (c) Rich Hickey. All rights reserved. + * The use and distribution terms for this software are covered by the + * Common Public License 1.0 (http://opensource.org/licenses/cpl.php) + * which can be found in the file CPL.TXT at the root of this distribution. + * By using this software in any fashion, you are agreeing to be bound by + * the terms of this license. + * You must not remove this notice, or any other, from this software. + **/ + +/* rich Oct 18, 2007 */ + +package clojure.lang; + +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; + +public class Repl{ +public static void main(String[] args){ + + for(String file : args) + try + { + Compiler.loadFile(file); + } + catch(Exception e) + { + e.printStackTrace(); + } + + //repl + LineNumberingPushbackReader rdr = new LineNumberingPushbackReader(new InputStreamReader(System.in)); + OutputStreamWriter w = (OutputStreamWriter) RT.OUT.get();//new OutputStreamWriter(System.out); + + Object EOF = new Object(); + try + { + Var.pushThreadBindings( + RT.map(RT.NS_REFERS, RT.NS_REFERS.get(), + RT.NS_IMPORTS, RT.NS_IMPORTS.get(), + RT.CURRENT_NS_SYM, RT.CURRENT_NS_SYM.get(), + Compiler.SOURCE, "REPL" + )); + w.write("Clojure\n"); + RT.inNamespace.invoke(Symbol.create("user")); + + for(; ;) + { + try + { + Var.pushThreadBindings( + RT.map(Compiler.LOADER, new DynamicClassLoader())); + w.write(Compiler.currentNS().name + "=> "); + w.flush(); + Object r = LispReader.read(rdr, false, EOF, false); + if(r == EOF) + break; + Object ret = Compiler.eval(r); + RT.print(ret, w); + w.write('\n'); + //w.flush(); + } + catch(Throwable e) + { + e.printStackTrace(); + } + finally + { + Var.popThreadBindings(); + } + } + } + catch(Exception e) + { + e.printStackTrace(); + } + finally + { + Var.popThreadBindings(); + } +} + +} diff --git a/src/jvm/clojure/lang/Script.java b/src/jvm/clojure/lang/Script.java new file mode 100644 index 00000000..781d41d3 --- /dev/null +++ b/src/jvm/clojure/lang/Script.java @@ -0,0 +1,42 @@ +/** + * Copyright (c) Rich Hickey. All rights reserved. + * The use and distribution terms for this software are covered by the + * Common Public License 1.0 (http://opensource.org/licenses/cpl.php) + * which can be found in the file CPL.TXT at the root of this distribution. + * By using this software in any fashion, you are agreeing to be bound by + * the terms of this license. + * You must not remove this notice, or any other, from this software. + **/ + +/* rich Oct 18, 2007 */ + +package clojure.lang; + +import java.io.OutputStreamWriter; +import java.io.IOException; + +public class Script{ +public static void main(String[] args){ + + for(String file : args) + try + { + Compiler.loadFile(file); + } + catch(Exception e) + { + e.printStackTrace(); + } + OutputStreamWriter w = (OutputStreamWriter) RT.OUT.get(); + try + { + w.flush(); + w.close(); + } + catch(IOException e) + { + e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + } +} +} + |