commit 60da3e04316b17c98586378ad483100d3712c6f5
parent b283196594a44ce044922e301cfc0927d7b75add
Author: tomas <tomas@logand.com>
Date: Sat, 12 Sep 2009 00:28:13 +0200
many initial modifications
Diffstat:
M | wl.js | | | 188 | ++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------- |
1 file changed, 119 insertions(+), 69 deletions(-)
diff --git a/wl.js b/wl.js
@@ -65,8 +65,8 @@ xset(T, T);
// TODO set props for NIL and T
function intern(Sym) {
- if(!(Sym in Syms)) Syms[Sym] = mkSym(Sym, NIL, NIL);
- return Syms[Sym];
+ //if(!(Sym in Syms)) Syms[Sym] = mkSym(Sym, NIL, NIL);
+ return Syms[Sym] || (Syms[Sym] = mkSym(Sym, NIL, NIL));
}
function isNil(X) {
@@ -77,37 +77,16 @@ function isT(X) {
return X === T;
}
-function car(L) {
- return isNil(L) ? NIL : L.car;
-}
-
-function cdr(L) {
- return isNil(L) ? NIL : L.cdr;
-}
-
-function caar(L) {
- return car(car(L));
-}
-
-function cadr(L) {
- return car(cdr(L));
-}
-
-function cdar(L) {
- return cdr(car(L));
-}
-
-function cddr(L) {
- return cdr(cdr(L));
-}
-
-function caddr(L) {
- return car(cdr(cdr(L)));
-}
-
-function cadddr(L) {
- return car(cdr(cdr(cdr(L))));
-}
+function car(L) {return isNil(L) ? NIL : L.car;}
+function cdr(L) {return isNil(L) ? NIL : L.cdr;}
+function caar(L) {return car(car(L));}
+function cadr(L) {return car(cdr(L));}
+function cdar(L) {return cdr(car(L));}
+function cddr(L) {return cdr(cdr(L));}
+function caddr(L) {return car(cdr(cdr(L)));}
+function cdddr(L) {return cdr(cdr(cdr(L)));}
+function cadddr(L) {return car(cdr(cdr(cdr(L))));}
+function cddddr(L) {return cdr(cdr(cdr(cdr(L))));}
function array2list(A) {
var L = NIL;
@@ -247,13 +226,72 @@ function parse(S) { // TODO cons . notation
var X;
while(!isNil(peek()) && peek() != ")") {
var O = one();
- if(O) {
- X = cons(O, X ? X : NIL);
- }
+ if(O || typeof O == "number") X = cons(O, X ? X : NIL);
}
if(X) X = reverse(X);
return X;
}
+ function tok() {
+ var Tok = [];
+ var N = true;
+ var F = false;
+ var S;
+ if(peek() == "+" || peek() == "-") S = xchar();
+ while(!isNil(peek()) && peek() != ")" && !isWhite(peek())) {
+ var C = xchar();
+ if(N && C == ".") break;
+ if(!(0 <= "0123456789".indexOf(C))) N = false;
+ Tok.push(C);
+ }
+ if(N && C == ".") {
+ var Tok2 = [];
+ while(!isNil(peek()) && peek() != ")" && !isWhite(peek())) {
+ var C = xchar();
+ if(!(0 <= "0123456789".indexOf(C))) N = false;
+ Tok2.push(C);
+ }
+ if(isNil(Scl || NIL)) {
+ Tok2.unshift(".");
+ Tok = Tok.concat(Tok2);
+ if(N) F = true;
+ } else {
+ if(N) {
+ if(Tok.length == 0 && Tok2.length == 0) Tok = ["."];
+ else for(var I = 0; I < Scl; I++)
+ Tok.push(Tok2.shift() || "0");
+ } else {
+ Tok2.unshift(".");
+ Tok = Tok.concat(Tok2);
+ }
+ }
+ }
+ if(0 < Tok.length) {
+ var X = Tok.join("");
+ if(X == ".") return X;
+ if(N) {
+ X = F ? parseFloat(X) : parseInt(X);
+ if(S == "-") X = -X;
+ } else X = intern(S ? S + X : X);
+ return X;
+ }
+ }
+ function str() {
+ var Tok = [];
+ while(peek() != '"') {
+ var C = xchar();
+ if(C == "\\") C = xchar();
+ else if(C == "^") {
+ C = xchar();
+ if(C == "I") C = "\t";
+ else if(C == "J") C = "\n";
+ else if(C == "M") C = "\r";
+ else C = String.fromCharCode(C == "?" ? 127 : C & 0x1f);
+ }
+ Tok.push(C);
+ }
+ if(xchar() != '"') throw "Unbalanced double quote";
+ if(0 < Tok.length) return Tok.join(""); //.replace(/\r\n/g, "\n");
+ }
function one() {
skip();
var X;
@@ -263,15 +301,10 @@ function parse(S) { // TODO cons . notation
xchar();
X = many() || NIL;
if(xchar() != ")") throw "Unbalanced parenthesis";
- } else {
- var Tok = [];
- while(!isNil(peek()) && peek() != ")" && !isWhite(peek())) {
- Tok.push(xchar());
- }
- if(0 < Tok.length) {
- X = Tok.join("");
- }
- }
+ } else if(C == '"') {
+ xchar();
+ X = str();
+ } else X = tok();
}
return X;
}
@@ -282,12 +315,8 @@ function parse(S) { // TODO cons . notation
function unparse(X) {
var A = [];
-// if(isNil(X)) {
-// A.push("NIL");
-// } else if(isT(X)) {
-// A.push("T");
if(isSym(X)) {
- A.push(X._nm); // TODO sym()
+ A.push(X._nm);
} else if(isCons(X)) {
A.push("(");
while(isCons(X)) {
@@ -301,7 +330,17 @@ function unparse(X) {
}
A.push(")");
} else {
- A.push("\"" + X + "\"");
+ if(typeof X == "number") A.push(X);
+ else {
+ var Y = X.split("");
+ for(var I = 0; I < Y.length; I++) {
+ if(Y[I] == "\\") Y[I] = "\\\\";
+ else if(Y[I] == "\"") Y[I] = "\\\"";
+ }
+ var S = Y.join("");
+ //var S = X.replace(/\"/g, "\\\"").replace(/\\/g, "\\\\");
+ A.push("\"" + S + "\"");
+ }
}
return A;
}
@@ -338,23 +377,34 @@ function xalert() {
alert(X.join(" "));
}
-function format(N, S) {
- var X = "" + N;
- var L = X.length;
- return X.slice(0, L - S) + "." + X.slice(L - S, L);
+function xmsg(A) {
+ var B = parse(A);
+ var X = flatten(unparse(B)).join("");
+ mk(w("test"), "pre", {}, {}, "'" + A + "' => " + X);
}
-// function initLisp() {
-// // xalert(parse(""));
-// // xalert(parse(" "));
-// // xalert(parse("1"));
-// // xalert(parse("NIL"));
-// // xalert(parse("T"));
-// // xalert(parse("()"));
-// // xalert(parse("(cons 1 NIL)"));
-// // xalert(parse("(cons T T)"));
-// // xalert(parse("(list)"));
-// // xalert(parse("(list 1 2)"));
-// xalert(xeval(NIL), xeval(T), xeval(12), xeval(12.3), xeval({x: "hi"}));
-// xalert(xeval(cons(intern("if"), NIL));
-// }
+function initLisp() {
+ xmsg("");
+ xmsg(" ");
+ xmsg("1");
+ xmsg("1 \"string\" a 2.3");
+ xmsg("NIL");
+ xmsg("T");
+ xmsg("()");
+ xmsg("1 ()");
+ xmsg("1 () (())");
+ xmsg("1 ( ) (( )) ((( ))) (( )) ( ) 2");
+ xmsg("(-213cons 1 NIL)");
+ xmsg("(cons 1 NIL) 2 (-3 b c ) \"st\\\"r^Ii\\\"ng^M^J2\" () 4.5678 9");
+ xmsg("(cons T T)");
+ xmsg("(list)");
+ xmsg("(list 1 -2 -3.4 5 6.7 9. .23 -0 -0.0)");
+ xmsg("1 (NIL (T 2 3.5 ) if)");
+ xmsg("1 (NIL (T 2 . 3.5) if)");
+ xmsg("1 (NIL . (T 2 . 3.5 6) if)");
+ xmsg("1 (NIL (T 2 (. 3.5 . 6)) if)");
+ //xalert(xeval(NIL), xeval(T), xeval(12), xeval(12.3), xeval({x: "hi"}));
+ //xalert(xeval(cons(intern("if"), NIL)));
+ //xalert(cons(NIL, "if"));
+ xmsg("(595 842) (20 12 0 0 0 NIL NIL) (NIL NIL NIL NIL NIL NIL NIL) ("hallo second page^M^Jsecond line" 35 24 NIL 65 154 205 50 NIL NIL) ("hello^M^Jtomas" 340 260 NIL NIL NIL NIL NIL NIL NIL)");
+}