X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=code%2Fparse.js;h=6882d79f6d7a170a9dc5b71d74767a7dccd3f334;hp=8a4761b50be13482cdf30a8be23527b9198aae9d;hb=fee1f7831003a7edee103dd7999b390abc7583c8;hpb=847feba6f0cf9ca175f3786dbcefacdc3f1cac5a diff --git a/code/parse.js b/code/parse.js index 8a4761b5..6882d79f 100644 --- a/code/parse.js +++ b/code/parse.js @@ -126,13 +126,8 @@ var make_parse = function () { return body; }; - var number_handler = function () { - - }; - symbol("(end)"); symbol("(name)").handler = name_handler; - symbol("(number)").handler = number_handler; symbol("let").handler = lambda_handler; symbol("=").handler = branch_handler; symbol("in"); @@ -144,23 +139,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; - if (this.first) { - return make_app(this.first.handler(), this.value); - } else { - return this.value; - } - }; - // constants have their own id and arity = literal - // numbers have id = "(number)" and arity = literal - x.arity = "literal"; - x.value = v; - return x; - }; - function make_lam2(a, b, aa) { return make_lam(a, make_lam(b, aa)); } @@ -182,6 +160,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)); @@ -190,6 +205,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();