diff options
author | Rich Hickey <richhickey@gmail.com> | 2007-07-02 20:33:09 +0000 |
---|---|---|
committer | Rich Hickey <richhickey@gmail.com> | 2007-07-02 20:33:09 +0000 |
commit | 829f3e3fb7908c958355ccde936c62c4ddc8099c (patch) | |
tree | 5f5a68e692b118cd0e814365b58a6f67c4089ce0 /src | |
parent | 2a55f48aef818dd78265d09c3ca741ae4d74e2fa (diff) |
interim checkin
Diffstat (limited to 'src')
-rw-r--r-- | src/jvm/clojure/lang/Compiler.java | 129 | ||||
-rw-r--r-- | src/jvm/clojure/lang/LispReader.java | 471 |
2 files changed, 296 insertions, 304 deletions
diff --git a/src/jvm/clojure/lang/Compiler.java b/src/jvm/clojure/lang/Compiler.java index 5f9bab6e..d3b9c72c 100644 --- a/src/jvm/clojure/lang/Compiler.java +++ b/src/jvm/clojure/lang/Compiler.java @@ -22,68 +22,50 @@ import java.lang.reflect.Modifier; public class Compiler{ //* -static Symbol DEF = Symbol.intern("def"); -static Symbol FN = Symbol.intern("fn"); -static Symbol DO = Symbol.intern("do"); -static Symbol IF = Symbol.intern("if"); -static Symbol OR = Symbol.intern("or"); -static Symbol AND = Symbol.intern("and"); -static Symbol LET = Symbol.intern("let"); -static Symbol LET_STAR_ = Symbol.intern("let*"); -static Symbol LETFN = Symbol.intern("letfn"); -static Symbol NOT = Symbol.intern("not"); -static Symbol NULL_QM_ = Symbol.intern("null?"); - -static Symbol IMPORT = Symbol.intern("import"); -static Symbol USE = Symbol.intern("use"); -static Symbol _AMP_KEY = Symbol.intern("&key"); -static Symbol _AMP_REST = Symbol.intern("&rest"); - -static public Var _CRT_OUT = RT.OUT; -static public Var _CRT_MODULE = RT._CT_MODULE; +static Symbol DEF = new Symbol("def"); +static Symbol FN = new Symbol("fn"); +static Symbol DO = new Symbol("do"); +static Symbol IF = new Symbol("if"); +static Symbol OR = new Symbol("or"); +static Symbol AND = new Symbol("and"); +static Symbol LET = new Symbol("let"); +static Symbol LET_STAR_ = new Symbol("let*"); +static Symbol LETFN = new Symbol("letfn"); +static Symbol NOT = new Symbol("not"); +static Symbol NULL_QM_ = new Symbol("null?"); + +static Symbol IMPORT = new Symbol("import"); +static Symbol USE = new Symbol("use"); +static Symbol _AMP_KEY = new Symbol("&key"); +static Symbol _AMP_REST = new Symbol("&rest"); + +static public TRef _CRT_OUT = RT.OUT; +static public TRef _CRT_MODULE = RT.CURRENT_MODULE; static NilExpr NIL_EXPR = new NilExpr(); //short-name-string->full-name-string -static public Var IMPORTS; - -static - { - try - { - IMPORTS = Module.intern("clojure", "^compiler-imports"); - KEYWORDS = Module.intern("clojure", "^compiler-keywords"); - VARS = Module.intern("clojure", "^compiler-vars"); - LOCAL_ENV = Module.intern("clojure", "^compiler-local-env"); - METHOD = Module.intern("clojure", "^compiler-method"); - USES = Module.intern("clojure", "^compiler-uses"); - FNS = Module.intern("clojure", "^compiler-fns"); - } - catch(Exception e) - { - e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. - } - } +static public TRef IMPORTS = new TRef(); //keyword->keywordexpr -static public Var KEYWORDS; +static public TRef KEYWORDS = new TRef(); //var->var -static public Var VARS; +static public TRef VARS = new TRef(); //symbol->localbinding -static public Var LOCAL_ENV; +static public TRef LOCAL_ENV = new TRef(); //FnFrame -static public Var METHOD; +static public TRef METHOD = new TRef(); //module->module -static public Var USES; +static public TRef USES = new TRef(); //ISeq FnExprs -static public Var FNS; +static public TRef FNS = new TRef(); static public IPersistentMap CHAR_MAP = new PersistentArrayMap('-', "_DSH_", @@ -154,14 +136,14 @@ static String compile(String ns, String className, LineNumberingPushbackReader.. //makes entries in IMPORTS for: //"ThisClass"->"org.package.ThisClass" //"ThatClass"->"org.package.ThatClass" - IPersistentMap importMap = (IPersistentMap) IMPORTS.getValue(); + IPersistentMap importMap = (IPersistentMap) IMPORTS.currentVal(); String pkg = RT.second(form).toString(); for(ISeq classes = RT.rest(RT.rest(form)); classes != null; classes = classes.rest()) { String iclassName = classes.first().toString(); importMap = (IPersistentMap) RT.assoc(iclassName, pkg + "." + iclassName, importMap); } - IMPORTS.setValue(importMap); + IMPORTS.set(importMap); } else if(op == USE) { @@ -178,12 +160,12 @@ static String compile(String ns, String className, LineNumberingPushbackReader.. } } //declare static members for keywords, vars - for(ISeq keys = RT.seq(KEYWORDS.getValue()); keys != null; keys = keys.rest()) + for(ISeq keys = RT.seq(KEYWORDS.currentVal()); keys != null; keys = keys.rest()) { KeywordExpr k = (KeywordExpr) ((IMapEntry) keys.first()).val(); format("static Keyword ~A;~%", k.emitExpressionString()); } - for(ISeq vars = RT.seq(VARS.getValue()); vars != null; vars = vars.rest()) + for(ISeq vars = RT.seq(VARS.currentVal()); vars != null; vars = vars.rest()) { Var v = (Var) ((IMapEntry) vars.first()).val(); format("static Var ~A;~%", munge(v.toString())); @@ -192,7 +174,7 @@ static String compile(String ns, String className, LineNumberingPushbackReader.. //todo declare static members for syms, quoted aggregates //emit nested static class/method declarations for nested fns - PersistentArrayList fns = (PersistentArrayList) FNS.getValue(); + PersistentArrayList fns = (PersistentArrayList) FNS.currentVal(); for(int f = 0; f < fns.count(); f++) { FnExpr fn = (FnExpr) fns.nth(f); @@ -202,12 +184,12 @@ static String compile(String ns, String className, LineNumberingPushbackReader.. //define the load function format("public void load() throws Exception{~%"); //init the keywords and vars - for(ISeq keys = RT.seq(KEYWORDS.getValue()); keys != null; keys = keys.rest()) + for(ISeq keys = RT.seq(KEYWORDS.currentVal()); keys != null; keys = keys.rest()) { KeywordExpr k = (KeywordExpr) ((IMapEntry) keys.first()).val(); format("~A = (Keyword)Symbol.intern(~S);~%", k.emitExpressionString(), k.sym.name); } - for(ISeq vars = RT.seq(VARS.getValue()); vars != null; vars = vars.rest()) + for(ISeq vars = RT.seq(VARS.currentVal()); vars != null; vars = vars.rest()) { Var v = (Var) ((IMapEntry) vars.first()).val(); format("~A = Module.intern(~S,~S);~%", munge(v.toString()), v.module.name, v.name.name); @@ -420,9 +402,10 @@ private static Expr analyzeSeq(C context, ISeq form) throws Exception{ for(ISeq s = op instanceof InstanceMemberInvoker ? RT.rrest(form) : RT.rest(form); s != null; s = s.rest()) args = args.cons(analyze(C.EXPRESSION, macroexpand(s.first()))); - if(op instanceof ClassSymbol) - return new InvokeConstructorExpr((ClassSymbol) op, args); - else if(op instanceof StaticMemberInvoker) + //if(op instanceof ClassSymbol) + // return new InvokeConstructorExpr((ClassSymbol) op, args); + //else + if(op instanceof StaticMemberInvoker) return new InvokeStaticMethodExpr((StaticMemberInvoker) op, args); else if(op instanceof InstanceMemberInvoker) return analyzeInstanceInvoke((InstanceMemberInvoker) op, @@ -480,6 +463,7 @@ static class InvokeExpr extends AnExpr{ } } +/* static class InvokeConstructorExpr extends AHostExpr{ HostClassExpr fexpr; PersistentArrayList args; @@ -504,6 +488,7 @@ static class InvokeConstructorExpr extends AHostExpr{ format("))"); } } +*/ static class InvokeStaticMethodExpr extends AHostExpr{ final StaticMemberInvoker sym; @@ -809,7 +794,7 @@ private static Expr analyzeLet(C context, ISeq form) throws Exception{ } try { - LOCAL_ENV.pushThreadBinding(LOCAL_ENV.getValue()); + LOCAL_ENV.pushThreadBinding(LOCAL_ENV.currentVal()); for(int i = 0; i < bindingInits.count(); i++) { BindingInit bi = (BindingInit) bindingInits.nth(i); @@ -835,7 +820,7 @@ private static Expr analyzeLetFn(C context, ISeq form) throws Exception{ return analyze(context, RT.list(RT.list(FN, null, form))); try { - LOCAL_ENV.pushThreadBinding(LOCAL_ENV.getValue()); + LOCAL_ENV.pushThreadBinding(LOCAL_ENV.currentVal()); ISeq bindings = (ISeq) RT.second(form); ISeq body = RT.rest(RT.rest(form)); PersistentArrayList bindingPairs = new PersistentArrayList(4); @@ -882,7 +867,7 @@ private static Expr analyzeLetStar(C context, ISeq form) throws Exception{ try { - LOCAL_ENV.pushThreadBinding(LOCAL_ENV.getValue()); + LOCAL_ENV.pushThreadBinding(LOCAL_ENV.currentVal()); PersistentArrayList bindingInits = new PersistentArrayList(4); for(ISeq bs = bindings; bs != null; bs = RT.rest(RT.rest(bs))) { @@ -1005,7 +990,7 @@ private static Expr analyzeOr(C context, ISeq form) throws Exception{ if(context != C.STATEMENT) { //we'll need a temp var - tb = new LocalBinding(Symbol.intern("OR_TEMP")); + tb = new LocalBinding(new Symbol("OR_TEMP")); registerLocal(tb); } @@ -1426,9 +1411,9 @@ private static FnMethod analyzeMethod(FnExpr fn, ISeq form) throws Exception{ ISeq body = RT.rest(form); try { - FnMethod method = new FnMethod(fn, (FnMethod) METHOD.getValue()); + FnMethod method = new FnMethod(fn, (FnMethod) METHOD.currentVal()); METHOD.pushThreadBinding(method); - LOCAL_ENV.pushThreadBinding(LOCAL_ENV.getValue()); + LOCAL_ENV.pushThreadBinding(LOCAL_ENV.currentVal()); PSTATE state = PSTATE.REQ; for(ISeq ps = parms; ps != null; ps = ps.rest()) { @@ -1504,7 +1489,7 @@ private static Expr analyzeDef(C context, ISeq form) throws Exception{ if(form.count() > 3) throw new Exception("Too many arguments to def"); Symbol sym = (Symbol) RT.second(form); - Module module = (Module) _CRT_MODULE.getValue(); + Module module = (Module) _CRT_MODULE.currentVal(); Var var = module.intern(baseSymbol(sym)); registerVar(var); VarExpr ve = new VarExpr(var, typeHint(sym)); @@ -1565,11 +1550,11 @@ private static Expr analyzeSymbol(Symbol sym, boolean inFnPosition) throws Excep } static Var lookupVar(Symbol sym) throws Exception{ - Module module = (Module) _CRT_MODULE.getValue(); + Module module = (Module) _CRT_MODULE.currentVal(); Var v = module.find(sym); if(v != null) return v; - for(ISeq seq = RT.seq(USES.getValue()); seq != null; seq = RT.rest(seq)) + for(ISeq seq = RT.seq(USES.currentVal()); seq != null; seq = RT.rest(seq)) { module = (Module) ((IMapEntry) RT.first(seq)).key(); v = module.find(sym); @@ -1584,7 +1569,7 @@ static Object macroexpand(Object x){ } private static KeywordExpr registerKeyword(Keyword keyword) throws Exception{ - IPersistentMap keywordsMap = (IPersistentMap) KEYWORDS.getValue(); + IPersistentMap keywordsMap = (IPersistentMap) KEYWORDS.currentVal(); KeywordExpr ke = (KeywordExpr) RT.get(keyword, keywordsMap); if(ke == null) KEYWORDS.setValue(RT.assoc(keyword, ke = new KeywordExpr(keyword), keywordsMap)); @@ -1592,13 +1577,13 @@ private static KeywordExpr registerKeyword(Keyword keyword) throws Exception{ } private static void registerVar(Var var) throws Exception{ - IPersistentMap varsMap = (IPersistentMap) VARS.getValue(); + IPersistentMap varsMap = (IPersistentMap) VARS.currentVal(); if(RT.get(var, varsMap) == null) VARS.setValue(RT.assoc(var, var, varsMap)); } private static void registerFn(FnExpr fn) throws Exception{ - FNS.setValue(RT.cons(fn, (IPersistentCollection) FNS.getValue())); + FNS.setValue(RT.cons(fn, (IPersistentCollection) FNS.currentVal())); } static void closeOver(LocalBinding b, FnMethod method){ @@ -1611,18 +1596,18 @@ static void closeOver(LocalBinding b, FnMethod method){ } static LocalBinding referenceLocal(Symbol sym) throws Exception{ - LocalBinding b = (LocalBinding) RT.get(sym, LOCAL_ENV.getValue()); + LocalBinding b = (LocalBinding) RT.get(sym, LOCAL_ENV.currentVal()); if(b != null) { - closeOver(b, (FnMethod) METHOD.getValue()); + closeOver(b, (FnMethod) METHOD.currentVal()); } return b; } private static void registerLocal(LocalBinding b) throws Exception{ - IPersistentMap localsMap = (IPersistentMap) LOCAL_ENV.getValue(); + IPersistentMap localsMap = (IPersistentMap) LOCAL_ENV.currentVal(); LOCAL_ENV.setValue(RT.assoc(b.sym, b, localsMap)); - FnMethod method = (FnMethod) METHOD.getValue(); + FnMethod method = (FnMethod) METHOD.currentVal(); method.locals = (IPersistentMap) RT.assoc(b, b, method.locals); } /* @@ -1645,7 +1630,7 @@ static String resolveHostClassname(String classname) throws Exception{ return classname; if(isPrimitive(classname)) return classname; - IPersistentMap importMap = (IPersistentMap) IMPORTS.getValue(); + IPersistentMap importMap = (IPersistentMap) IMPORTS.currentVal(); String fullyQualifiedName = (String) RT.get(classname, importMap); if(fullyQualifiedName == null) throw new Exception("Can't resolve type name: " + classname); @@ -1957,7 +1942,7 @@ static class VarExpr extends AnExpr{ } public void emitExpression() throws Exception{ - format("~A.getValue()", getName()); + format("~A.currentVal()", getName()); } public Class getHostType() throws Exception{ diff --git a/src/jvm/clojure/lang/LispReader.java b/src/jvm/clojure/lang/LispReader.java index c804e3d7..b6800eca 100644 --- a/src/jvm/clojure/lang/LispReader.java +++ b/src/jvm/clojure/lang/LispReader.java @@ -16,12 +16,12 @@ import java.util.regex.Matcher; import java.util.ArrayList;
import java.math.BigInteger;
-public class LispReader {
+public class LispReader{
-static Symbol QUOTE = Symbol.intern("quote");
-static Symbol BACKQUOTE = Symbol.intern("backquote");
-static Symbol UNQUOTE = Symbol.intern("unquote");
-static Symbol UNQUOTE_SPLICING = Symbol.intern("unquote-splicing");
+static Symbol QUOTE = new Symbol("quote");
+static Symbol BACKQUOTE = new Symbol("backquote");
+static Symbol UNQUOTE = new Symbol("unquote");
+static Symbol UNQUOTE_SPLICING = new Symbol("unquote-splicing");
static IFn[] macros = new IFn[256];
static Pattern symbolPat = Pattern.compile("[:]?[\\D&&[^:\\.]][^:\\.]*");
@@ -35,104 +35,106 @@ static Pattern instanceMemberPat = Pattern.compile("\\.([a-zA-Z_][\\w\\.]*)\\.([ static Pattern staticMemberPat = Pattern.compile("([a-zA-Z_][\\w\\.]*)\\.([a-zA-Z_]\\w*)");
static Pattern classNamePat = Pattern.compile("([a-zA-Z_][\\w\\.]*)\\.");
-static{
-macros['"'] = new StringReader();
-macros[';'] = new CommentReader();
-macros['\''] = new QuoteReader();
-macros['`'] = new BackquoteReader();
-macros[','] = new UnquoteReader();
-macros['('] = new ListReader();
-macros[')'] = new UnmatchedDelimiterReader();
-macros['\\'] = new CharacterReader();
-}
+static
+ {
+ macros['"'] = new StringReader();
+ macros[';'] = new CommentReader();
+ macros['\''] = new QuoteReader();
+ macros['`'] = new BackquoteReader();
+ macros[','] = new UnquoteReader();
+ macros['('] = new ListReader();
+ macros[')'] = new UnmatchedDelimiterReader();
+ macros['\\'] = new CharacterReader();
+ }
+
static public Object read(PushbackReader r, boolean eofIsError, Object eofValue, boolean isRecursive)
- throws Exception {
+ throws Exception{
- for (; ;)
- {
- int ch = r.read();
+ for(; ;)
+ {
+ int ch = r.read();
- while (Character.isWhitespace(ch))
- ch = r.read();
+ while(Character.isWhitespace(ch))
+ ch = r.read();
- if (ch == -1)
- {
- if (eofIsError)
- throw new Exception("EOF while reading");
- return eofValue;
- }
+ if(ch == -1)
+ {
+ if(eofIsError)
+ throw new Exception("EOF while reading");
+ return eofValue;
+ }
- if(Character.isDigit(ch))
- {
- Object n = readNumber(r,(char) ch);
- if(RT.suppressRead())
- return null;
- return n;
- }
+ if(Character.isDigit(ch))
+ {
+ Object n = readNumber(r, (char) ch);
+ if(RT.suppressRead())
+ return null;
+ return n;
+ }
- IFn macroFn = getMacro(ch);
- if (macroFn != null)
- {
- Object ret = macroFn.invoke(r, (char)ch);
- if(RT.suppressRead())
- return null;
- //no op macros return the reader
- if (ret == r)
- continue;
- return ret;
- }
+ IFn macroFn = getMacro(ch);
+ if(macroFn != null)
+ {
+ Object ret = macroFn.invoke(r, (char) ch);
+ if(RT.suppressRead())
+ return null;
+ //no op macros return the reader
+ if(ret == r)
+ continue;
+ return ret;
+ }
- if(ch == '+' || ch == '-')
- {
- int ch2 = r.read();
- if(Character.isDigit(ch2))
- {
- r.unread(ch2);
- Object n = readNumber(r,(char) ch);
- if(RT.suppressRead())
- return null;
- return n;
- }
- r.unread(ch2);
- }
+ if(ch == '+' || ch == '-')
+ {
+ int ch2 = r.read();
+ if(Character.isDigit(ch2))
+ {
+ r.unread(ch2);
+ Object n = readNumber(r, (char) ch);
+ if(RT.suppressRead())
+ return null;
+ return n;
+ }
+ r.unread(ch2);
+ }
- String token = readToken(r,(char)ch);
- if(RT.suppressRead())
- return null;
- return interpretToken(token);
- }
+ String token = readToken(r, (char) ch);
+ if(RT.suppressRead())
+ return null;
+ return interpretToken(token);
+ }
}
-static private String readToken(PushbackReader r, char initch) throws Exception {
- StringBuilder sb = new StringBuilder();
- sb.append(initch);
+static private String readToken(PushbackReader r, char initch) throws Exception{
+ StringBuilder sb = new StringBuilder();
+ sb.append(initch);
- for(;;)
- {
- int ch = r.read();
- if(ch == -1 || Character.isWhitespace(ch) || isMacro(ch))
- {
- r.unread(ch);
- return sb.toString();
- }
- sb.append((char)ch);
- }
+ for(; ;)
+ {
+ int ch = r.read();
+ if(ch == -1 || Character.isWhitespace(ch) || isMacro(ch))
+ {
+ r.unread(ch);
+ return sb.toString();
+ }
+ sb.append((char) ch);
+ }
}
-static private Object readNumber(PushbackReader r, char initch) throws Exception {
- StringBuilder sb = new StringBuilder();
- sb.append(initch);
+static private Object readNumber(PushbackReader r, char initch) throws Exception{
+ StringBuilder sb = new StringBuilder();
+ sb.append(initch);
- for(;;)
- {
- int ch = r.read();
- if(ch == -1 || Character.isWhitespace(ch) || isMacro(ch))
- {
- r.unread(ch);
- break;
- }
- sb.append((char)ch);
- }
+ for(; ;)
+ {
+ int ch = r.read();
+ if(ch == -1 || Character.isWhitespace(ch) || isMacro(ch))
+ {
+ r.unread(ch);
+ break;
+ }
+ sb.append((char) ch);
+ }
String s = sb.toString();
Object n = matchNumber(s);
@@ -210,15 +212,15 @@ static private Object readMember(PushbackReader r) throws Exception { */
static private Object interpretToken(String s) throws Exception{
- if (s.equals("null"))
- {
- return null;
- }
- Object ret = null;
+ if(s.equals("null"))
+ {
+ return null;
+ }
+ Object ret = null;
- ret = matchVar(s);
- if(ret != null)
- return ret;
+ ret = matchVar(s);
+ if(ret != null)
+ return ret;
return Symbol.intern(s);
}
@@ -249,215 +251,220 @@ private static Object matchSymbol(String s) { */
private static Object matchVar(String s) throws Exception{
- Matcher m = varPat.matcher(s);
- if(m.matches())
- return Module.intern(m.group(1),m.group(2));
- return null;
+ Matcher m = varPat.matcher(s);
+ if(m.matches())
+ return Module.intern(m.group(1), m.group(2));
+ return null;
}
-private static Object matchNumber(String s) {
- Matcher m = intPat.matcher(s);
- if(m.matches())
- return Num.from(new BigInteger(s));
- m = floatPat.matcher(s);
- if(m.matches())
- return Num.from(Double.parseDouble(s));
- m = ratioPat.matcher(s);
- if(m.matches())
- {
- return Num.divide(new BigInteger(m.group(1)),new BigInteger(m.group(2)));
- }
- return null;
+private static Object matchNumber(String s){
+ Matcher m = intPat.matcher(s);
+ if(m.matches())
+ return Num.from(new BigInteger(s));
+ m = floatPat.matcher(s);
+ if(m.matches())
+ return Num.from(Double.parseDouble(s));
+ m = ratioPat.matcher(s);
+ if(m.matches())
+ {
+ return Num.divide(new BigInteger(m.group(1)), new BigInteger(m.group(2)));
+ }
+ return null;
}
-static private IFn getMacro(int ch) {
- if (ch < macros.length)
- return macros[ch];
- return null;
+static private IFn getMacro(int ch){
+ if(ch < macros.length)
+ return macros[ch];
+ return null;
}
-static private boolean isMacro(int ch) {
- return (ch < macros.length && macros[ch] != null);
+static private boolean isMacro(int ch){
+ return (ch < macros.length && macros[ch] != null);
}
static class StringReader extends AFn{
- public Object invoke(Object reader, Object doublequote) throws Exception {
- StringBuilder sb = new StringBuilder();
- Reader r = (Reader) reader;
+ public Object invoke(Object reader, Object doublequote) throws Exception{
+ StringBuilder sb = new StringBuilder();
+ Reader r = (Reader) reader;
- for(int ch = r.read();ch != '"';ch = r.read())
+ for(int ch = r.read(); ch != '"'; ch = r.read())
{
if(ch == -1)
- throw new Exception("EOF while reading string");
+ throw new Exception("EOF while reading string");
if(ch == '\\') //escape
{
ch = r.read();
if(ch == -1)
- throw new Exception("EOF while reading string");
+ throw new Exception("EOF while reading string");
switch(ch)
{
- case 't':
+ case't':
ch = '\t';
break;
- case 'r':
+ case'r':
ch = '\r';
break;
- case 'n':
+ case'n':
ch = '\n';
break;
- case '\\':
+ case'\\':
break;
- case '"':
+ case'"':
break;
default:
- throw new Exception("Unsupported escape character: \\" + (char)ch);
+ throw new Exception("Unsupported escape character: \\" + (char) ch);
}
}
- sb.append((char)ch);
+ sb.append((char) ch);
}
- return sb.toString();
- }
+ return sb.toString();
+ }
}
+
static class CommentReader extends AFn{
- public Object invoke(Object reader, Object semicolon) throws Exception {
- Reader r = (Reader) reader;
- int ch;
- do
- {
- ch = r.read();
- } while (ch != -1 && ch != '\n' && ch != '\r');
- return r;
- }
+ public Object invoke(Object reader, Object semicolon) throws Exception{
+ Reader r = (Reader) reader;
+ int ch;
+ do
+ {
+ ch = r.read();
+ } while(ch != -1 && ch != '\n' && ch != '\r');
+ return r;
+ }
}
static class QuoteReader extends AFn{
- public Object invoke(Object reader, Object quote) throws Exception {
- PushbackReader r = (PushbackReader) reader;
- Object o = read(r, true, null, true);
- return RT.list(QUOTE, o);
- }
+ public Object invoke(Object reader, Object quote) throws Exception{
+ PushbackReader r = (PushbackReader) reader;
+ Object o = read(r, true, null, true);
+ return RT.list(QUOTE, o);
+ }
}
static class BackquoteReader extends AFn{
- public Object invoke(Object reader, Object backquote) throws Exception {
- PushbackReader r = (PushbackReader) reader;
- Object o = read(r, true, null, true);
- return RT.list(BACKQUOTE, o);
- }
+ public Object invoke(Object reader, Object backquote) throws Exception{
+ PushbackReader r = (PushbackReader) reader;
+ Object o = read(r, true, null, true);
+ return RT.list(BACKQUOTE, o);
+ }
}
static class UnquoteReader extends AFn{
- public Object invoke(Object reader, Object comma) throws Exception {
- PushbackReader r = (PushbackReader) reader;
- int ch = r.read();
- if(ch == -1)
- throw new Exception("EOF while reading character");
+ public Object invoke(Object reader, Object comma) throws Exception{
+ PushbackReader r = (PushbackReader) reader;
+ int ch = r.read();
+ if(ch == -1)
+ throw new Exception("EOF while reading character");
if(ch == '^')
{
- Object o = read(r, true, null, true);
- return RT.list(UNQUOTE_SPLICING, o);
+ Object o = read(r, true, null, true);
+ return RT.list(UNQUOTE_SPLICING, o);
}
- else
+ else
{
r.unread(ch);
Object o = read(r, true, null, true);
return RT.list(UNQUOTE, o);
}
- }
+ }
}
static class CharacterReader extends AFn{
- public Object invoke(Object reader, Object backslash) throws Exception {
- PushbackReader r = (PushbackReader) reader;
- int ch = r.read();
- if(ch == -1)
- throw new Exception("EOF while reading character");
- String token = readToken(r,(char)ch);
- if(token.length() == 1)
- return token.charAt(0);
- else if(token.equals("newline"))
- return '\n';
- else if(token.equals("space"))
- return ' ';
- else if(token.equals("tab"))
- return '\t';
- throw new Exception("Unsupported character: \\" + token);
- }
+ public Object invoke(Object reader, Object backslash) throws Exception{
+ PushbackReader r = (PushbackReader) reader;
+ int ch = r.read();
+ if(ch == -1)
+ throw new Exception("EOF while reading character");
+ String token = readToken(r, (char) ch);
+ if(token.length() == 1)
+ return token.charAt(0);
+ else if(token.equals("newline"))
+ return '\n';
+ else if(token.equals("space"))
+ return ' ';
+ else if(token.equals("tab"))
+ return '\t';
+ throw new Exception("Unsupported character: \\" + token);
+ }
}
+
static class ListReader extends AFn{
- public Object invoke(Object reader, Object leftparen) throws Exception {
- PushbackReader r = (PushbackReader) reader;
- return readDelimitedList(')', r, true);
- }
+ public Object invoke(Object reader, Object leftparen) throws Exception{
+ PushbackReader r = (PushbackReader) reader;
+ return readDelimitedList(')', r, true);
+ }
}
+
static class UnmatchedDelimiterReader extends AFn{
- public Object invoke(Object reader, Object rightdelim) throws Exception {
- throw new Exception("Unmatched delimiter: " + rightdelim);
- }
+ public Object invoke(Object reader, Object rightdelim) throws Exception{
+ throw new Exception("Unmatched delimiter: " + rightdelim);
+ }
}
-public static ISeq readDelimitedList(char delim, PushbackReader r, boolean isRecursive) throws Exception {
- ArrayList a = new ArrayList();
- for (; ;)
- {
- int ch = r.read();
+public static ISeq readDelimitedList(char delim, PushbackReader r, boolean isRecursive) throws Exception{
+ ArrayList a = new ArrayList();
- while (Character.isWhitespace(ch))
- ch = r.read();
+ for(; ;)
+ {
+ int ch = r.read();
- if (ch == -1)
- throw new Exception("EOF while reading");
+ while(Character.isWhitespace(ch))
+ ch = r.read();
- if(ch == delim)
- break;
+ if(ch == -1)
+ throw new Exception("EOF while reading");
- IFn macroFn = getMacro(ch);
- if (macroFn != null)
- {
- Object mret = macroFn.invoke(r, (char)ch);
- //no op macros return the reader
- if (mret != r)
- a.add(mret);
- }
- else
- {
- r.unread(ch);
+ if(ch == delim)
+ break;
- Object o = read(r, true, null, isRecursive);
- if (o != r)
- a.add(o);
- }
- }
+ IFn macroFn = getMacro(ch);
+ if(macroFn != null)
+ {
+ Object mret = macroFn.invoke(r, (char) ch);
+ //no op macros return the reader
+ if(mret != r)
+ a.add(mret);
+ }
+ else
+ {
+ r.unread(ch);
+
+ Object o = read(r, true, null, isRecursive);
+ if(o != r)
+ a.add(o);
+ }
+ }
- return RT.seq(a);
+ return RT.seq(a);
}
public static void main(String[] args){
- LineNumberingPushbackReader r = new LineNumberingPushbackReader(new InputStreamReader(System.in));
- OutputStreamWriter w = new OutputStreamWriter(System.out);
- Object ret = null;
- try{
- for(;;)
- {
- ret = LispReader.read(r, true, null, false);
- RT.print(ret, w);
- w.write('\n');
- w.write(ret.getClass().toString());
- w.write('\n');
- w.flush();
- }
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
+ LineNumberingPushbackReader r = new LineNumberingPushbackReader(new InputStreamReader(System.in));
+ OutputStreamWriter w = new OutputStreamWriter(System.out);
+ Object ret = null;
+ try
+ {
+ for(; ;)
+ {
+ ret = LispReader.read(r, true, null, false);
+ RT.print(ret, w);
+ w.write('\n');
+ w.write(ret.getClass().toString());
+ w.write('\n');
+ w.flush();
+ }
+ }
+ catch(Exception e)
+ {
+ e.printStackTrace();
+ }
}
|