From: Chris Barker Date: Sun, 26 Sep 2010 21:59:33 +0000 (-0400) Subject: Merge branch 'master' of ssh://server.philosophy.fas.nyu.edu/Users/lambda/lambda X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=commitdiff_plain;h=bd9306cd8ed960db37717c40573e8230a9dd65bf;hp=a8f9da95c22c7bf21df81404cd9c3f5b2e73e6a7 Merge branch 'master' of ssh://server.philosophy.fas.nyu.edu/Users/lambda/lambda --- diff --git a/code/lambda.js b/code/lambda.js index c0dbb0f0..609cce03 100644 --- a/code/lambda.js +++ b/code/lambda.js @@ -115,14 +115,19 @@ function Variable(name, tag) { this.tag = tag || 0; this.to_string = function () { // append count copies of str to accum - function markup(accum, count) { - if (count === 0) { - return accum; - } else { - return markup(accum + "'", count - 1); - } - } - return markup(this.name, this.tag); +// function markup(accum, count) { +// if (count === 0) { +// return accum; +// } else { +// return markup(accum + "'", count - 1); +// } +// } +// return markup(this.name, this.tag); + var s = this.name; + for (var count = 0; count < this.tag; count += 1) { + s += "'"; + } + return s; }; this.equal = function (other) { return (this.tag === other.tag) && (this.name === other.name); @@ -192,13 +197,19 @@ function Lambda_var(variable) { }; this.eval_loop = function (stack, eta) { function unwind(left, stack) { - if (stack.length === 0) { - return left; - } else { - var x = stack[0]; - var xs = stack.slice(1); - return unwind(new Lambda_app(left, x.eval_loop([], eta)), xs); - } +// if (stack.length === 0) { +// return left; +// } else { +// var x = stack[0]; +// var xs = stack.slice(1); +// return unwind(new Lambda_app(left, x.eval_loop([], eta)), xs); +// } + var res = left, x; + while (stack.length) { + x = stack.shift(); + res = new Lambda_app(res, x.eval_loop([], eta)); + } + return res; } return unwind(this, stack); }; @@ -364,16 +375,31 @@ function Lambda_lam(variable, body) { } }; this.to_int = function (sofar) { - if (this.body.body && this.body.body.variable && this.body.bound.equal(this.body.body.variable)) { - return 0 + sofar; - } else if (this.body.variable && this.bound.equal(this.body.variable)) { - return 1 + sofar; - } else if (this.body.body && this.body.body.left && this.body.body.left.variable && this.bound.equal(this.body.body.left.variable)) { - var new_int = new Lambda_lam(this.bound, new Lambda_lam(this.body.bound, this.body.body.right)); - return new_int.to_int(1 + sofar); - } else { - return "not a church numeral"; - } +// if (this.body.body && this.body.body.variable && this.body.bound.equal(this.body.body.variable)) { +// return 0 + sofar; +// } else if (this.body.variable && this.bound.equal(this.body.variable)) { +// return 1 + sofar; +// } else if (this.body.body && this.body.body.left && this.body.body.left.variable && this.bound.equal(this.body.body.left.variable)) { +// var new_int = new Lambda_lam(this.bound, new Lambda_lam(this.body.bound, this.body.body.right)); +// return new_int.to_int(1 + sofar); +// } else { +// return "not a church numeral"; +// } + var res = 0, s = this.bound, z, cursor; + if (this.body.variable && s.equal(this.body.variable)) { + return 1; + } else if (this.body.bound) { + z = this.body.bound; + cursor = this.body.body; + while (cursor.left && cursor.left.variable && s.equal(cursor.left.variable)) { + res += 1; + cursor = cursor.right; + } + if (cursor.variable && z.equal(cursor.variable)) { + return res; + } + } + return "not a church numeral"; }; } diff --git a/code/tokens.js b/code/tokens.js index 559c4370..53639a8b 100644 --- a/code/tokens.js +++ b/code/tokens.js @@ -84,7 +84,18 @@ String.prototype.tokens = function (prefix, suffix) { (c >= '0' && c <= '9') || c === '_' || c === '-') { str += c; i += 1; - } else { + } else if (c === '?' || c === '!') { + // should only be terminal + str += c; + i += 1; + // make sure next character is not an identifier + if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || + (c >= '0' && c <= '9') || c === '_' || c === '-' || c === '?' || c === '!') { + str += c; + i += 1; + make('name', str).error("Bad identifier"); + } + } else { break; } }