posted assignment4
[lambda.git] / code / parse.js
index 2718218..08ac5f0 100644 (file)
@@ -4,6 +4,9 @@
 //      http://javascript.crockford.com/tdop/index.html
 //      Douglas Crockford 2010-06-26
 
+//      See also http://effbot.org/zone/simple-top-down-parsing.htm
+
+
 /*jslint onevar: false
  */
 
@@ -40,8 +43,8 @@ var make_parse = function () {
                 a = o.arity || "keyword";
             }
         } else if (a ===  "number") {
-            o = symbol_table["(literal)"];
-            a = "literal";
+            o = symbol_table["(number)"];
+                       a = "literal";
         } else if (a === "operator") {
             o = symbol_table[v];
             if (!o) {
@@ -65,6 +68,7 @@ var make_parse = function () {
         }
     };
 
+       /*
        try {
                if (console && console.debug) {
                        function print() {
@@ -72,6 +76,7 @@ var make_parse = function () {
                        }
                }
        } catch (e) {}
+       */
 
     var symbol = function (id) {
         var s = symbol_table[id];
@@ -126,14 +131,8 @@ var make_parse = function () {
         return body;
     };
 
-
-    var itself = function () {
-        return this;
-    };
-
     symbol("(end)");
     symbol("(name)").handler = name_handler;
-    symbol("(literal)").handler = itself;
     symbol("let").handler = lambda_handler;
     symbol("=").handler = branch_handler;
     symbol("in");
@@ -145,17 +144,6 @@ var make_parse = function () {
 
        function make_constants() {
 
-               var constant = function (s, v) {
-                       var x = symbol(s);
-                       x.handler = function () {
-                               this.value = symbol_table[this.id].value;
-                               return this;
-                       };
-                       x.value = v;
-                       x.arity = "literal";
-                       return x;
-               };
-
                function make_lam2(a, b, aa) {
                        return make_lam(a, make_lam(b, aa));
                }
@@ -177,14 +165,60 @@ var make_parse = function () {
                var zz = new Lambda_var(z);
                var_table = { u: u, v: v, x: x, s: s, z: z};
                name_table = {u: uu, v: vv, x: xx, s: ss, z: zz};
+               number_table = {};
+
+               // constants have their own id and arity = literal
+               // numbers have id = "(number)" and arity = literal
+               symbol("(number)").handler = function () {
+                       var n = this.value;
+                       var res = number_table[n];
+                       if (!res) {
+                               res = zz;
+                               while (n > 0) {
+                                       n -= 1;
+                                       res = make_app(ss, res);
+                               }
+                               res = make_lam2(s, z, res);
+                               number_table[this.value] = res;
+                       }
+                       if (this.first) {
+                               return make_app(this.first.handler(), res);
+                       } else {
+                               return res;
+                       }
+               }
+
+               var constant = function (s, v) {
+                       var x = symbol(s);
+                       x.handler = function () {
+                               this.value = symbol_table[this.id].value;
+                               if (this.first) {
+                                       return make_app(this.first.handler(), this.value);
+                               } else {
+                                       return this.value;
+                               }
+                       };
+                       x.arity = "literal";
+                       x.value = v;
+                       return x;
+               };
 
                constant("S", make_lam3(u, v, x, make_app3(uu, xx, make_app(vv, xx))));
                constant("K", make_lam2(u, v, uu));
                constant("I", make_lam(x, xx));
                constant("B", make_lam3(u, v, x, make_app(uu, make_app(vv, xx))));
                constant("C", make_lam3(u, v, x, make_app3(uu, xx, vv)));
-               constant("W", make_lam2(u, v, make_app3(uu, vv, vv)));
+
+               // trush \uv.vu = CI
                constant("T", make_lam2(u, v, make_app(vv, uu)));
+               // mockingbird \u.uu = SII
+               constant("M", make_lam(u, make_app(uu, uu)));
+               // warbler \uv.uvv = C(BM(BBT) = C(BS(C(BBI)I))I
+               constant("W", make_lam2(u, v, make_app3(uu, vv, vv)));
+               // lark \uv.u(vv) = CBM = BWB
+               constant("L", make_lam2(u, v, make_app(uu, make_app(vv, vv))));
+               // Y is SLL
+
        }
        make_constants();
 
@@ -207,9 +241,11 @@ var make_parse = function () {
                 return t;
             } else {
                 t.first = [];
-                while (token.arity === "name") {
-                    t.first.push(n);
-                    n = token;
+                while (token.arity === "name" || token.id === "\\") {
+                   if (token.id !== "\\") {
+                      t.first.push(n);
+                      n = token;
+                   }
                     advance();
                 }
                                if (token.arity === "literal" && t.first.length === 0) {