wl

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

commit 1fd98e0abcb9c888d5a013561cce5e99aecffa8b
parent 3ccab2c059bebf5c26480765298385d3251adf17
Author: tomas <tomas@logand.com>
Date:   Sat, 10 Oct 2009 23:56:49 +0200

java run repl fix, unframe unbinds, up impl, xrun + eval accept cnt+lst TODO

Diffstat:
Mwl.java | 86+++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------
1 file changed, 62 insertions(+), 24 deletions(-)

diff --git a/wl.java b/wl.java @@ -281,8 +281,9 @@ class wl implements Runnable { Any Z = null; System.out.print(": "); while(null != (Z = read1(true))) { + Any X = eval(Z); System.out.print("-> "); - print(eval(Z)); + print(X); System.out.println(); System.out.print(": "); } @@ -296,22 +297,26 @@ class wl implements Runnable { return Sd.get(Nm); } - Any xrun(Any E) { + Any xrun(Any P, int n, Any L) { Any Z = NIL; - while(NIL != E) { - Z = eval(E.car()); - E = E.cdr(); - } + if(P.isCons()) + while(NIL != P) { + Z = eval(P.car()); + P = P.cdr(); + } + else eval(P); return Z; } - Any eval(Any E) { + Any xrun(Any P) {return xrun(P, 0, NIL);} + Any eval(Any X, int n, Any L) { Any Z = NIL; - if(E.isCons()) Z = apply(E); - else if(E.isIsym()) Z = E.val(); - else if(E.isObj()) Z = E; - else err(E, "Don't know how to eval"); + if(X.isCons()) Z = apply(X); + else if(X.isIsym()) Z = X.val(); + else if(X.isObj()) Z = X; + else err(X, "Don't know how to eval"); return Z; } + Any eval(Any X) {return eval(X, 0, NIL);} Any apply(Any E) { Any Z = NIL; Any F = eval(E.car()); @@ -328,26 +333,23 @@ class wl implements Runnable { Any Fa = F.car(); Any Fb = F.cdr(); frame(); - int n = 0; if(Fa.isIsym()) { // (@ . P) | (L . P) | (NIL . P) if(NIL != Fa) { - if(At == Fa) {bind(Args, mkCons(NIL, mapcarEval(A))); n++;} - else {bind(Fa, A); n++;} + if(At == Fa) bind(Args, mkCons(NIL, mapcarEval(A))); + else bind(Fa, A); } } else if(Fa.isCons()) { // ((L ...) . P) while(Fa.isCons()) { bind(Fa.car(), eval(A.car())); - n++; Fa = Fa.cdr(); A = A.cdr(); } if(NIL != Fa) { - if(At == Fa) {bind(Args, mkCons(NIL, mapcarEval(A))); n++;} - else {bind(Fa, A); n++;} + if(At == Fa) bind(Args, mkCons(NIL, mapcarEval(A))); + else bind(Fa, A); } } else err(Fa, "Don't know how to bind"); Z = xrun(Fb); - unbind(n); unframe(); return Z; } @@ -402,7 +404,7 @@ class wl implements Runnable { Env.val(E.cdr()); } void unbind(int n) {for(int i = 0; i < n; i++) unbind();} - void unframe() {Env.val(Env.val().cdr());} + void unframe() {while(T != Env.val().car()) unbind(); Env.val(Env.val().cdr());} void fn(String Nm, Fn F) { Any Z = Sd.get(Nm); @@ -423,10 +425,28 @@ class wl implements Runnable { Sd.put("java.lang.Class", mkIsym("java.lang.Class", mkObj(Class.class))); fn("run", new Fn() {public Any fn(Any E) { - return xrun(eval(E.cdr().car())); + Any I = E.cdr(); + Any P = eval(I.car()); + int n = 0; + Any L = NIL; + if(I.cdr().isCons()) { + I = I.cdr(); + n = ((BigInteger) I.car().val()).intValue(); + if(I.cdr().isCons()) L = I.cdr(); + } + return xrun(P, n, L); }}); fn("eval", new Fn() {public Any fn(Any E) { - return eval(eval(E.cdr().car())); + Any I = E.cdr(); + Any X = eval(I.car()); + int n = 0; + Any L = NIL; + if(I.cdr().isCons()) { + I = I.cdr(); + n = ((BigInteger) I.car().val()).intValue(); + if(I.cdr().isCons()) L = I.cdr(); + } + return eval(X, n, L); }}); 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();}}); @@ -709,9 +729,27 @@ class wl implements Runnable { } else err(E, "Don't know how to let"); return Z; }}); - fn("up", new Fn() {public Any fn(Any E) { - // TODO - return NIL; + fn("up", new Fn() {public Any fn(Any E) { // TODO cnt frame up + Any Z; + Any I = E.cdr(); + Any K = I.car(); + I = I.cdr(); + if(I.isCons()) { // (up K 'Z) + Z = eval(I.car()); + boolean done = false; + for(Any J = Env.val(); J.isCons() && T != J.car(); J = J.cdr()) { + Any C = J.car(); + if(K == C.car()) {C.cdr(Z); done = true; break;} + } + if(!done) Env.val(mkCons(mkCons(K, Z), Env.val())); + } else { // (up K) + Z = K.val(); + for(Any J = Env.val(); J.isCons() && T != J.car(); J = J.cdr()) { + Any C = J.car(); + if(K == C.car()) {Z = C.cdr(); break;} + } + } + return Z; }}); fn("jnew", new Fn() {public Any fn(Any E) { // jnew 'cls [arg ...]