X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=code%2Fparse.js;h=8856b164aa86b96acf69000016995b3f039779fe;hp=cb412f7fe523c3d0b7ab1fd98876c14fa11a8955;hb=934f19eef90e5474be0af07e7400867538068bc9;hpb=df6a6fe6807edabf1b3c3e904eb2ead20eedf0ca diff --git a/code/parse.js b/code/parse.js index cb412f7f..8856b164 100644 --- a/code/parse.js +++ b/code/parse.js @@ -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 */ @@ -34,14 +37,14 @@ var make_parse = function () { a = t.type; if (a === "name") { o = symbol_table[v]; - if (o && typeof o !== 'function') { - a = "keyword"; - } else { + if (!o || typeof o === 'function') { o = symbol_table["(name)"]; + } else { + 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]; @@ -79,7 +84,6 @@ var make_parse = function () { s = Object.create(original_symbol); s.id = s.value = id; symbol_table[id] = s; - print(s, s.arity); } return s; }; @@ -127,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"); @@ -146,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; - this.arity = "literal"; - return this; - }; - x.value = v; - return x; - }; - function make_lam2(a, b, aa) { return make_lam(a, make_lam(b, aa)); } @@ -178,6 +165,43 @@ 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)); @@ -186,6 +210,7 @@ var make_parse = function () { constant("C", make_lam3(u, v, x, make_app3(uu, xx, vv))); constant("W", make_lam2(u, v, make_app3(uu, vv, vv))); constant("T", make_lam2(u, v, make_app(vv, uu))); + } make_constants(); @@ -208,12 +233,16 @@ 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 === "\\") { + t.first.push(n); + n = token; advance(); } - if (token.id === ".") { + if (token.arity === "literal" && t.first.length === 0) { + t.first.push(n); + t.second = token; + advance(); + } else if (token.id === ".") { t.first.push(n); advance(); t.second = expression(in_let); @@ -249,8 +278,8 @@ var make_parse = function () { n = token; advance(")"); } else { - if (token.arity !== "name") { - token.error("Expected a variable name."); + if (token.arity !== "name" && token.arity !== "literal") { + token.error("Expected a variable name or literal."); } token.first = n; n = token;