# HG changeset patch # User Michael Pavone # Date 1401601860 25200 # Node ID 08081b0a938213599a4f4d07b10521ab91f60479 # Parent 0ee70ac20a021d2206cdbc49bc149daf4cfc9c5e Actual working implementation of isconstant check diff -r 0ee70ac20a02 -r 08081b0a9382 interp.js --- a/interp.js Sat May 31 21:30:18 2014 -0700 +++ b/interp.js Sat May 31 22:51:00 2014 -0700 @@ -345,6 +345,31 @@ return this; }; +intlit.prototype.isconstant = + floatlit.prototype.isconstant = + strlit.prototype.isconstant = + lambda.prototype.isconstant = function() { + return true; +}; + +object.prototype.isconstant = + symbol.prototype.isconstant = + funcall.prototype.isconstant = + op.prototype.isconstant = + assignment.prototype.isconstant = function() { + return false; +} + +arraylit.prototype.isconstant = + listlit.prototype.isconstant = function() { + for (var idx = 0; idx < this.val.length; ++idx) { + if (!this.val[idx].isconstant()) { + return false; + } + } + return true; +}; + intlit.prototype.eval = floatlit.prototype.eval = strlit.prototype.eval = @@ -740,10 +765,18 @@ } else { env.syms[expr.symbol.cleanName()] = {}; this.expressions[i] = expr.macroexpand(env); - if (this.expressions[i].expression instanceof lambda || 'val' in this.expressions[i].expression) { - env.syms[expr.symbol.cleanName()] = this.expressions[i].expression.eval(env); - } else { - env.syms[expr.symbol.cleanName()] = null; + try { + if (this.expressions[i].expression.isconstant()) { + env.syms[expr.symbol.cleanName()] = this.expressions[i].expression.eval(env); + } else { + env.syms[expr.symbol.cleanName()] = null; + } + } catch(e) { + var msg = 'Error, \n\t' + e.message.split('\n').join('\n\t') + '\nevaling node ' + this.expressions[i].expression + if (typeof this.expressions[i].expression == 'object') { + msg += ' with keys ' + JSON.stringify(Object.keys(this.expressions[i].expression) + ' and constructor ' + this.expressions[i].expression.constructor.name); + } + throw new Error(msg); } } } else {