wl

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

commit 0927fd391a7bb06fa67a2aeb977ddd0cf0175ad6
parent 8c579a5c63b1a9116835b31288f499cde9204115
Author: tomas <tomas@logand.com>
Date:   Wed,  7 Oct 2009 23:19:53 +0200

Sym uses val and prop instead of car and cdr

Diffstat:
Mwl.java | 133+++++++++++++++++++++++++++++++++++++++++++------------------------------------
1 file changed, 72 insertions(+), 61 deletions(-)

diff --git a/wl.java b/wl.java @@ -61,9 +61,13 @@ class wl implements Runnable { public String nm(); public Any car(); public Any cdr(); + public Any val(); + public Any prop(); public Object cxr(); 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 boolean isCons(); public boolean isSym(); @@ -80,10 +84,14 @@ class wl implements Runnable { public String nm() {err("No Cons.nm"); return null;} public Any car() {return car;} 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 Any car(Any a) {car = a; return car;} public Any cdr(Any d) {cdr = d; return cdr;} - public Object cxr(Object x) {err("No Cons.obj"); return null;} + 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 boolean isCons() {return true;}; public boolean isSym() {return false;}; public boolean isIsym() {return false;}; @@ -96,15 +104,19 @@ class wl implements Runnable { static interface Sym extends Any {} static class Isym implements Sym { public String nm; - public Any car, cdr; - public Isym(String n, Any a, Any d) {nm = n; car = a; cdr = d;} + public Any val, prop; + public Isym(String n, Any v, Any p) {nm = n; val = v; prop = p;} public String nm() {return nm;} - public Any car() {return car;} - public Any cdr() {return cdr;} + public Any car() {if(NIL != this) err("No Isym.car"); return NIL;} + 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 Any car(Any a) {car = a; return car;} - public Any cdr(Any d) {cdr = d; return cdr;} - public Object cxr(Object x) {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 boolean isCons() {return false;}; public boolean isSym() {return true;}; public boolean isIsym() {return true;}; @@ -120,9 +132,13 @@ class wl implements Runnable { 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 Any car(Any a) {err("No Obj.set"); return null;} public Any cdr(Any d) {err("No Obj.con"); 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 boolean isCons() {return false;}; public boolean isSym() {return false;}; @@ -135,28 +151,27 @@ class wl implements Runnable { } static Any mkCons(Any a, Any d) {return new Cons(a, d);} - static Any mkIsym(String n, Any a, Any d) {return new Isym(n, a, d);} + static Any mkIsym(String n, Any v) {return new Isym(n, v, NIL);} static Any mkObj(Object x) {return new Obj(x);} static Any mkOint(String x) {return mkObj(new BigInteger(x));} static Any mkOfix(String x) {err("mkOfix not implemented"); return null;} - final static Any NIL = mkIsym("NIL", null, null); - final static Any T = mkIsym("T", NIL, null); + final static Any NIL = mkIsym("NIL", null); + final static Any T = mkIsym("T", null); static { - NIL.car(NIL); - NIL.cdr(NIL); - T.cdr(T); + NIL.val(NIL); + T.val(T); } // reader final static Any Lp = mkObj(null); final static Any Rp = mkObj(null); - final Any Qte = mkIsym("quote", NIL, NIL); - final Any Dot = mkIsym(".", NIL, NIL); - final Any At = mkIsym("@", NIL, NIL); - final Any Args = mkIsym("*Args", NIL, NIL); + final Any Qte = mkIsym("quote", NIL); + final Any Dot = mkIsym(".", NIL); + final Any At = mkIsym("@", NIL); + final Any Args = mkIsym("*Args", NIL); class In { Character c; @@ -164,11 +179,11 @@ class wl implements Runnable { public In(InputStream S) {c = null; s = S;} } - final Any In = mkIsym("*In", NIL, mkObj(new In(System.in))); - final Any Out = mkIsym("*Out", NIL, mkObj(System.out)); + final Any In = mkIsym("*In", mkObj(new In(System.in))); + final Any Out = mkIsym("*Out", mkObj(System.out)); Character peek() { - In I = (In) In.cdr().cxr(); + In I = (In) In.val().cxr(); try { if(null == I.c) I.c = (char) I.s.read(); } catch(Exception e) {} // TODO eof vs error? @@ -176,7 +191,7 @@ class wl implements Runnable { } Character xchar() { peek(); - In I = (In) In.cdr().cxr(); + In I = (In) In.val().cxr(); Character Z = I.c; I.c = null; return Z; @@ -262,7 +277,7 @@ class wl implements Runnable { HashMap<String, Any> Sd = new HashMap<String, Any>(); Any intern(String Nm) { - if(!Sd.containsKey(Nm)) Sd.put(Nm, mkIsym(Nm, NIL, NIL)); + if(!Sd.containsKey(Nm)) Sd.put(Nm, mkIsym(Nm, NIL)); return Sd.get(Nm); } @@ -277,7 +292,7 @@ class wl implements Runnable { Any eval(Any E) { Any Z = NIL; if(E.isCons()) Z = apply(E); - else if(E.isIsym()) Z = E.cdr(); + else if(E.isIsym()) Z = E.val(); else if(E.isObj()) Z = E; else err(E, "Don't know how to eval"); return Z; @@ -286,9 +301,9 @@ class wl implements Runnable { Any Z = NIL; Any F = eval(E.car()); if(F.isCons()) Z = applyC(E, F); - else if(F.isSym()) 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.isObj()) Z = japplyC(E, F); + else if(F.isObj()) Z = applyO(E, F); else err(E, "Don't know how to apply"); return Z; } @@ -320,8 +335,8 @@ class wl implements Runnable { O = mkCons(Args.cdr(), NIL); Args.cdr(mkCons(NIL, mapcarEval(A))); } else { - O = mkCons(Fa.cdr(), NIL); - Fa.cdr(A); + O = mkCons(Fa.car(), NIL); + Fa.car(A); } } } else if(Fa.isCons()) { @@ -330,20 +345,18 @@ class wl implements Runnable { while(Fa.isCons()) { Any X = Fa.car(); Fa = Fa.cdr(); - if(!X.isIsym()) err(X, "Isym expected in saveV"); - Z.cdr(mkCons(X.cdr(), NIL)); + Z.cdr(mkCons(X.val(), NIL)); Z = Z.cdr(); - X.cdr(eval(A.car())); + X.val(eval(A.car())); A = A.cdr(); } if(NIL != Fa) { - if(!Fa.isIsym()) err(Fa, "Isym expected in saveV"); if(At == Fa) { - Z.cdr(mkCons(Args.cdr(), NIL)); - Args.cdr(mkCons(NIL, mapcarEval(A))); + Z.cdr(mkCons(Args.val(), NIL)); + Args.val(mkCons(NIL, mapcarEval(A))); } else { - Z.cdr(mkCons(Fa.cdr(), NIL)); - Fa.cdr(A); + Z.cdr(mkCons(Fa.val(), NIL)); + Fa.val(A); } } O = B.cdr(); @@ -353,25 +366,23 @@ class wl implements Runnable { void restoreV(Any Fa, Any O) { if(Fa.isIsym()) { if(NIL != Fa) { - if(At == Fa) Args.cdr(O.car()); - else Fa.cdr(O.car()); + if(At == Fa) Args.car(O.car()); + else Fa.car(O.car()); } } else if(Fa.isCons()) { while(Fa.isCons()) { Any X = Fa.car(); Fa = Fa.cdr(); - if(!X.isIsym()) err(X, "Isym expected in restoreV"); - X.cdr(O.car()); + X.val(O.car()); O = O.cdr(); } if(NIL != Fa) { - if(!Fa.isIsym()) err(Fa, "Isym expected in saveV"); - if(At == Fa) Args.cdr(O.car()); - else Fa.cdr(O.car()); + if(At == Fa) Args.val(O.car()); + else Fa.val(O.car()); } } else err(Fa, "Don't know how to restoreV"); } - Any japplyC(Any E, Any O) { // 'obj 'meth [arg ...] + Any applyO(Any E, Any O) { // 'obj 'meth [arg ...] Any I = E.cdr(); Any F = eval(I.car()); Any A = I.cdr(); @@ -379,17 +390,18 @@ class wl implements Runnable { ArrayList<Object> a = new ArrayList(); ArrayList<Class> t = new ArrayList(); for(Any X = A; NIL != X; X = X.cdr()) { - // TODO handle non-native? Any Y = eval(X.car()); - a.add(Y.cxr()); - t.add(Y.cxr().getClass()); + Object y = Y.isIsym() ? Y.nm() : Y.cxr(); + 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(); Class c = o instanceof Class ? (Class) o : o.getClass(); - Method m = c.getMethod(F.nm(), ta); + String nm = F.isOstr() ? (String) F.cxr() : F.nm(); + Method m = c.getMethod(nm, ta); Object r = m.invoke(o, aa); Z = mkObj(r); } catch(NoSuchMethodException e) { @@ -404,8 +416,8 @@ class wl implements Runnable { void fn(String Nm, Fn F) { Any Z = Sd.get(Nm); - if(null != Z) Z.cdr(mkObj(F)); - else Sd.put(Nm, mkIsym(Nm, NIL, mkObj(F))); + if(null != Z) Z.val(mkObj(F)); + else Sd.put(Nm, mkIsym(Nm, mkObj(F))); } public wl() { @@ -417,7 +429,7 @@ class wl implements Runnable { Sd.put("*Args", Args); Sd.put("*In", In); Sd.put("*Out", Out); - Sd.put("java.lang.Class", mkObj(java.lang.Class.class)); + Sd.put("java.lang.Class", mkIsym("java.lang.Class", mkObj(Class.class))); fn("run", new Fn() {public Any fn(Any E) {return xrun(E.cdr().car());}}); fn("eval", new Fn() {public Any fn(Any E) {return eval(eval(E.cdr().car()));}}); @@ -576,17 +588,16 @@ class wl implements Runnable { fn("def", new Fn() {public Any fn(Any E) { Any X = E.cdr(); Any N = eval(X.car()); - if(!N.isSym()) err(E, "Symbol expected"); if(!Sd.containsKey(N.nm())) err(E, "Symbol not interned"); Any V = eval(X.cdr().car()); - Sd.get(N.nm()).cdr(V); + Sd.get(N.nm()).val(V); return N; }}); fn("val", new Fn() {public Any fn(Any E) { Any Z = NIL; Any X = eval(E.cdr().car()); if(X.isCons()) Z = X.car(); - else if(X.isSym()) Z = X.cdr(); + else if(X.isSym()) Z = X.val(); else if(X.isObj()) Z = X; else err(E, "Don't know how to val"); return Z; @@ -607,10 +618,10 @@ class wl implements Runnable { ArrayList<Object> a = new ArrayList(); ArrayList<Class> t = new ArrayList(); for(Any X = A; NIL != X; X = X.cdr()) { - // TODO handle non-native? Any Y = eval(X.car()); - a.add(Y.cxr()); - t.add(Y.cxr().getClass()); + Object y = Y.isIsym() ? Y.nm() : Y.cxr(); + a.add(y); + t.add(y.getClass()); } Object[] aa = a.toArray(); Class[] ta = (Class[]) t.toArray(new Class[aa.length]); @@ -661,7 +672,7 @@ class wl implements Runnable { } void print(Any E) { - PrintStream S = (PrintStream) Out.cdr().cxr(); + PrintStream S = (PrintStream) Out.val().cxr(); if(E.isCons()) { Any X = E; if(Qte == X.car()) { @@ -706,10 +717,10 @@ class wl implements Runnable { String str(Any E) { ByteArrayOutputStream B = new ByteArrayOutputStream(); PrintStream S = new PrintStream(B); - Any O = Out.cdr(); - Out.cdr(mkObj(S)); + Any O = Out.val(); + Out.val(mkObj(S)); print(E); - Out.cdr(O); + Out.val(O); String Z = null; try { Z = B.toString(Enc);