wl

Unnamed repository; edit this file 'description' to name the repository.
git clone https://logand.com/git/wl.git/
Log | Files | Refs | LICENSE

commit ece72e5ae3a5298045be26bff225d3d6ae28b10a
parent f52a20b38a71df5f713542e6a8ad47ac6dab7bd8
Author: tomas <tomas@logand.com>
Date:   Sun, 11 Oct 2009 11:54:59 +0200

cxr renamed to obj, number reader, clean up

Diffstat:
Mwl.java | 217+++++++++++++++++++------------------------------------------------------------
1 file changed, 52 insertions(+), 165 deletions(-)

diff --git a/wl.java b/wl.java @@ -65,12 +65,12 @@ class wl implements Runnable { public Any cdr(); public Any val(); public Any prop(); - public Object cxr(); + public Object obj(); public Any car(Any a); public Any cdr(Any d); public Any val(Any v); public Any prop(Any p); - public Object cxr(Object x); + public Object obj(Object x); public boolean isCons(); public boolean isSym(); public boolean isIsym(); @@ -88,12 +88,12 @@ class wl implements Runnable { public Any cdr() {return cdr;} public Any val() {err("No Cons.val"); return null;} public Any prop() {err("No Cons.prop"); return null;} - public Object cxr() {err("No Cons.cxr"); return null;} + public Object obj() {err("No Cons.obj"); return null;} public Any car(Any a) {car = a; return car;} public Any cdr(Any d) {cdr = d; return cdr;} public Any val(Any v) {err("No Cons.val"); return null;} public Any prop(Any p) {err("No Cons.prop"); return null;} - public Object cxr(Object x) {err("No Cons.cxr"); return null;} + public Object obj(Object x) {err("No Cons.obj"); return null;} public boolean isCons() {return true;}; public boolean isSym() {return false;}; public boolean isIsym() {return false;}; @@ -113,12 +113,12 @@ class wl implements Runnable { public Any cdr() {if(NIL != this) err("No Isym.cdr"); return NIL;} public Any val() {return val;} public Any prop() {return prop;} - public Object cxr() {err("No Isym.cxr"); return null;} + public Object obj() {err("No Isym.obj"); return null;} public Any car(Any a) {err("No Isym.car"); return null;} public Any cdr(Any d) {err("No Isym.cdr"); return null;} public Any val(Any v) {val = v; return val;} public Any prop(Any p) {prop = p; return prop;} - public Object cxr(Object x) {err("No Isym.cxr"); return null;} + public Object obj(Object x) {err("No Isym.obj"); return null;} public boolean isCons() {return false;}; public boolean isSym() {return true;}; public boolean isIsym() {return true;}; @@ -129,26 +129,26 @@ class wl implements Runnable { public boolean isOobj() {return false;}; } static class Obj implements Any { - public Object cxr; - public Obj(Object x) {cxr = x;} + public Object obj; + public Obj(Object x) {obj = x;} public String nm() {err("No Obj.nm"); return null;} public Any car() {err("No Obj.car"); return null;} public Any cdr() {err("No Obj.cdr"); return null;} public Any val() {err("No Obj.val"); return null;} public Any prop() {err("No Obj.prop"); return null;} - public Object cxr() {return cxr;} + public Object obj() {return obj;} public Any car(Any a) {err("No Obj.car"); return null;} public Any cdr(Any d) {err("No Obj.cdr"); return null;} public Any val(Any v) {err("No Obj.val"); return null;} public Any prop(Any p) {err("No Obj.prop"); return null;} - public Object cxr(Object x) {cxr = x; return cxr;} + public Object obj(Object x) {obj = x; return obj;} public boolean isCons() {return false;}; public boolean isSym() {return false;}; public boolean isIsym() {return false;}; public boolean isObj() {return true;}; - public boolean isOfn() {return cxr instanceof Fn;}; - public boolean isOstr() {return cxr instanceof String;}; - public boolean isOnum() {return cxr instanceof BigInteger;}; + public boolean isOfn() {return obj instanceof Fn;}; + public boolean isOstr() {return obj instanceof String;}; + public boolean isOnum() {return obj instanceof BigInteger;}; public boolean isOobj() {return !isOfn() && !isOstr() && !isOnum();}; } @@ -206,10 +206,10 @@ class wl implements Runnable { final Any Env = mkIsym("*Env", NIL); final Any Stk = mkIsym("*Stk", NIL); - Character peek() {return ((In) In.val().cxr()).peek();} - Character xchar() {return ((In) In.val().cxr()).xchar();} - boolean eof() {return ((In) In.val().cxr()).eof();} - void eof(Any X) {((In) In.val().cxr()).eof(X);} + Character peek() {return ((In) In.val().obj()).peek();} + Character xchar() {return ((In) In.val().obj()).xchar();} + boolean eof() {return ((In) In.val().obj()).eof();} + void eof(Any X) {((In) In.val().obj()).eof(X);} boolean charIn(Character C, String L) {return 0 <= L.indexOf(C);} void skip1() { @@ -227,12 +227,21 @@ class wl implements Runnable { Any symbol() { Character C = xchar(); if(charIn(C, "#()\" \t\n\r")) err(C, "Symbol expected"); - StringBuffer L = new StringBuffer(); - L.append(C); - while((null != (C = peek())) && !charIn(C, "#()\" \t\n\r")) - L.append(xchar()); - String M = L.toString(); - return intern(M); + boolean N = charIn(C, "+-0123456789."); + boolean F = '.' == C; + StringBuffer b = new StringBuffer(); + b.append(C); + while((null != (C = peek())) && !charIn(C, "#()\" \t\n\r")) { + C = xchar(); + b.append(C); + if(N && !charIn(C, "0123456789")) { + if(!F && '.' == C) F = true; + else N = false; + } + } + String M = b.toString(); + if(1 == M.length() && charIn(M.charAt(0), "+-.")) N = false; + return N ? (F ? mkOfix(M) : mkOint(M)) : intern(M); } Any text() { StringBuffer L = new StringBuffer(); @@ -259,7 +268,6 @@ class wl implements Runnable { Character X = peek(); if(null != X) { switch(X) { - // case "#": return comment(); case '(': xchar(); Z = readL(); break; case ')': xchar(); if(Top) err("Reader overflow"); Z = Rp; break; case '"': xchar(); Z = text(); break; @@ -336,7 +344,7 @@ class wl implements Runnable { Stk.val(mkCons(E.car(), Stk.val())); if(F.isCons()) Z = applyC(E, F); //else if(F.isSym()) Z = applyS(E, F); // TODO ? - else if(F.isOfn()) Z = ((Fn) F.cxr()).fn(E); + else if(F.isOfn()) Z = ((Fn) F.obj()).fn(E); else if(F.isObj()) Z = applyO(E, F); else err(E, "Don't know how to apply"); Stk.val(Stk.val().cdr()); @@ -387,16 +395,16 @@ class wl implements Runnable { ArrayList<Class> t = new ArrayList(); for(Any X = A; NIL != X; X = X.cdr()) { Any Y = eval(X.car()); - Object y = Y.isIsym() ? Y.nm() : Y.cxr(); + Object y = Y.isIsym() ? Y.nm() : Y.obj(); a.add(y); t.add(y.getClass()); } Object[] aa = a.toArray(); Class[] ta = (Class[]) t.toArray(new Class[aa.length]); try { - Object o = O.cxr(); + Object o = O.obj(); Class c = o instanceof Class ? (Class) o : o.getClass(); - String nm = F.isOstr() ? (String) F.cxr() : F.nm(); + String nm = F.isOstr() ? (String) F.obj() : F.nm(); Method m = c.getMethod(nm, ta); Object r = m.invoke(o, aa); Z = mkObj(r); @@ -427,6 +435,7 @@ class wl implements Runnable { else Sd.put(Nm, mkIsym(Nm, mkObj(F))); } Any undo(int n, Any L) { + if(n != 1) err("TODO undo n!=1"); Any Z = NIL; Any E = Env.val(); while(E.isCons() && T != E.car()) { @@ -492,7 +501,7 @@ class wl implements Runnable { Any L = NIL; if(I.cdr().isCons()) { I = I.cdr(); - n = 1; // TODO ((BigInteger) I.car().val()).intValue(); + n = ((BigInteger) I.car().obj()).intValue(); if(I.cdr().isCons()) L = I.cdr(); } return xrun(P, n, L); @@ -504,7 +513,7 @@ class wl implements Runnable { Any L = NIL; if(I.cdr().isCons()) { I = I.cdr(); - n = 1; // TODO ((BigInteger) I.car().val()).intValue(); + n = ((BigInteger) I.car().obj()).intValue(); if(I.cdr().isCons()) L = I.cdr(); } return eval(X, n, L); @@ -512,56 +521,6 @@ class wl implements Runnable { fn("quote", new Fn() {public Any fn(Any E) {return E.cdr();}}); fn("car", new Fn() {public Any fn(Any E) {return eval(E.cdr().car()).car();}}); fn("cdr", new Fn() {public Any fn(Any E) {return eval(E.cdr().car()).cdr();}}); - // fn("-", new Fn() {public Any fn(Any E) { - // Any X = E.cdr; - // Any Z = eval(X.car); - // if(NIL == Z) return NIL; - // if(NIL == X.cdr) Z.cxr = -((Integer) Z.cxr); - // else - // while(NIL != X.cdr) { - // X = X.cdr; - // Any Y = eval(X.car); - // if(NIL == Y) return NIL; - // Z.cxr = (Integer) Z.cxr - (Integer) Y.cxr; - // } - // return Z; - // }}); - // fn("*", new Fn() {public Any fn(Any E) { - // Any X = E.cdr; - // Any Z = eval(X.car); - // if(NIL == Z) return NIL; - // while(NIL != X.cdr) { - // X = X.cdr; - // Any Y = eval(X.car); - // if(NIL == Y) return NIL; - // Z.cxr = (Integer) Z.cxr * (Integer) Y.cxr; - // } - // return Z; - // }}); - // fn("/", new Fn() {public Any fn(Any E) { - // Any X = E.cdr; - // Any Z = eval(X.car); - // if(NIL == Z) return NIL; - // while(NIL != X.cdr) { - // X = X.cdr; - // Any Y = eval(X.car); - // if(NIL == Y) return NIL; - // Z.cxr = (Integer) Z.cxr / (Integer) Y.cxr; - // } - // return Z; - // }}); - // fn("%", new Fn() {public Any fn(Any E) { - // Any X = E.cdr; - // Any Z = eval(X.car); - // if(NIL == Z) return NIL; - // while(NIL != X.cdr) { - // X = X.cdr; - // Any Y = eval(X.car); - // if(NIL == Y) return NIL; - // Z.cxr = (Integer) Z.cxr % (Integer) Y.cxr; - // } - // return Z; - // }}); fn("loop", new Fn() {public Any fn(Any E) { while(true) { for(Any X = E.cdr(); X.isCons(); X = X.cdr()) { @@ -587,7 +546,6 @@ class wl implements Runnable { Any X = E.cdr(); return eval(X.car()) == eval(X.cdr().car()) ? T : NIL; }}); - fn("peek", new Fn() {public Any fn(Any E) { Character X = peek(); return null == X ? NIL : mkObj(X.toString()); @@ -597,7 +555,7 @@ class wl implements Runnable { return null == X ? NIL : mkObj(X.toString()); }}); fn("print", new Fn() {public Any fn(Any E) { - PrintStream S = (PrintStream) Out.val().cxr(); + PrintStream S = (PrintStream) Out.val().obj(); Any Z = NIL; int I = 0; for(Any X = E.cdr(); NIL != X; X = X.cdr()) { @@ -608,7 +566,7 @@ class wl implements Runnable { return Z; }}); fn("prin", new Fn() {public Any fn(Any E) { - PrintStream S = (PrintStream) Out.val().cxr(); + PrintStream S = (PrintStream) Out.val().obj(); Any Z = NIL; int I = 0; for(Any X = E.cdr(); NIL != X; X = X.cdr()) { @@ -635,66 +593,6 @@ class wl implements Runnable { } return mkObj(Z); }}); - - // fn("in", new Fn() {public Any fn(Any E) { - // Any X = E.cdr; - // String F = evStr(X.car); - // In S = null; - // Any Z = NIL; - // try { - // S = new In(new FileInputStream(F)); - // In I = (In) In.cxr; - // In.cxr = S; - // Z = xrun(X.cdr); - // In.cxr = I; - // S.s.close(); - // } catch(FileNotFoundException e) { - // err(E, "File not found"); - // } catch(IOException e) { - // err(E, "Error closing input"); - // } - // return Z; - // }}); - // fn("out", new Fn() {public Any fn(Any E) { - // Any X = E.cdr; - // String F = evStr(X.car); - // Any Z = NIL; - // try { - // FileOutputStream B = new FileOutputStream(F); - // PrintStream S = new PrintStream(B); - // OutputStream O = (OutputStream) Out.cxr; - // Out.cxr = S; - // Z = xrun(X.cdr); - // Out.cxr = O; - // S.close(); - // } catch(FileNotFoundException e) { - // err(E, "File not found"); - // } - // return Z; - // }}); - // fn("load", new Fn() {public Any fn(Any E) { - // Any Z = NIL; - // for(Any X = E.cdr; NIL != X; X = X.cdr) { - // String F = evStr(X.car); - // In S = null; - // try { - // S = new In(new FileInputStream(F)); - // In I = (In) In.cxr; - // In.cxr = S; - // do Z = eval(read()); - // while(null != peek()); - // In.cxr = I; - // S.s.close(); - // return Z; - // } catch(FileNotFoundException e) { - // err(E, "File not found"); - // } catch(IOException e) { - // err(E, "Error closing input"); - // } - // } - // return Z; - // }}); - fn("def", new Fn() {public Any fn(Any E) { Any X = E.cdr(); Any A = eval(X.car()); @@ -717,7 +615,7 @@ class wl implements Runnable { return mkCons(eval(X.car()), eval(X.cdr().car())); }}); fn("intern", new Fn() {public Any fn(Any E) { - return intern((String) eval(E.cdr().car()).cxr()); + return intern((String) eval(E.cdr().car()).obj()); }}); fn("pop", new Fn() {public Any fn(Any E) { Any Z = NIL; @@ -851,7 +749,6 @@ class wl implements Runnable { } return Z; }}); - fn("jnew", new Fn() {public Any fn(Any E) { // jnew 'cls [arg ...] Any I = E.cdr(); Any C = eval(I.car()); @@ -861,14 +758,14 @@ class wl implements Runnable { ArrayList<Class> t = new ArrayList(); for(Any X = A; NIL != X; X = X.cdr()) { Any Y = eval(X.car()); - Object y = Y.isIsym() ? Y.nm() : Y.cxr(); + Object y = Y.isIsym() ? Y.nm() : Y.obj(); a.add(y); t.add(y.getClass()); } Object[] aa = a.toArray(); Class[] ta = (Class[]) t.toArray(new Class[aa.length]); try { - Constructor c = ((Class) C.cxr()).getConstructor(ta); + Constructor c = ((Class) C.obj()).getConstructor(ta); Object r = c.newInstance(aa); Z = mkObj(r); } catch(NoSuchMethodException e) { @@ -890,7 +787,7 @@ class wl implements Runnable { Any F = eval(X.car()); Any Z = NIL; try { - Object o = O.cxr(); + Object o = O.obj(); Class c = o instanceof Class ? (Class) o : o.getClass(); Field f = c.getField(F.nm()); Object r = f.get(o); @@ -904,17 +801,8 @@ class wl implements Runnable { }}); } - String evStr(Any E) { - Any X = eval(E); - String S = null; - if(X.isSym()) S = X.nm(); - else if(X.isOstr()) S = (String) X.cxr(); - else err(E, "Don't know how to evStr"); - return S; - } - void print(Any E) { - PrintStream S = (PrintStream) Out.val().cxr(); + PrintStream S = (PrintStream) Out.val().obj(); if(E.isCons()) { Any X = E; if(Qte == X.car()) { @@ -938,10 +826,10 @@ class wl implements Runnable { S.print(')'); } } else if(E.isIsym()) S.print(E.nm()); - else if(E.isOnum()) S.print(E.cxr()); + else if(E.isOnum()) S.print(E.obj()); else if(E.isOstr()) { S.print('"'); - String X = (String) E.cxr(); + String X = (String) E.obj(); for(int I = 0; I < X.length(); I++) { Character C = X.charAt(I); if('\\' == C) S.print("\\\\"); @@ -951,12 +839,12 @@ class wl implements Runnable { S.print('"'); } else if(E.isObj()) { S.print('['); - S.print(E.cxr()); + S.print(E.obj()); S.print(']'); } else err(E, "Don't know how to print"); } Any prin(Any E) { - PrintStream S = (PrintStream) Out.val().cxr(); + PrintStream S = (PrintStream) Out.val().obj(); if(NIL == E); else if(E.isCons()) { Any X = E; @@ -966,7 +854,7 @@ class wl implements Runnable { } prin(X); } else if(E.isIsym()) S.print(E.nm()); - else if(E.isObj()) S.print(E.cxr()); + else if(E.isObj()) S.print(E.obj()); else err(E, "Don't know how to print"); return E; } @@ -993,8 +881,7 @@ class wl implements Runnable { try { In.val(mkObj(new In(new FileInputStream(F)))); Any Z; - while(null != (Z = read1(true))) - eval(Z); + while(null != (Z = read1(true))) eval(Z); } catch(FileNotFoundException e) { err(F, "File not found"); }