Mercurial > repos > tabletprog
comparison interp.js @ 255:08081b0a9382
Actual working implementation of isconstant check
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Sat, 31 May 2014 22:51:00 -0700 |
parents | 0ee70ac20a02 |
children | 0dc7322590da |
comparison
equal
deleted
inserted
replaced
254:0ee70ac20a02 | 255:08081b0a9382 |
---|---|
341 return this.cleanName(); | 341 return this.cleanName(); |
342 }; | 342 }; |
343 symbol.prototype['tpmeth_name!'] = function(val) { | 343 symbol.prototype['tpmeth_name!'] = function(val) { |
344 this.name = val; | 344 this.name = val; |
345 return this; | 345 return this; |
346 }; | |
347 | |
348 intlit.prototype.isconstant = | |
349 floatlit.prototype.isconstant = | |
350 strlit.prototype.isconstant = | |
351 lambda.prototype.isconstant = function() { | |
352 return true; | |
353 }; | |
354 | |
355 object.prototype.isconstant = | |
356 symbol.prototype.isconstant = | |
357 funcall.prototype.isconstant = | |
358 op.prototype.isconstant = | |
359 assignment.prototype.isconstant = function() { | |
360 return false; | |
361 } | |
362 | |
363 arraylit.prototype.isconstant = | |
364 listlit.prototype.isconstant = function() { | |
365 for (var idx = 0; idx < this.val.length; ++idx) { | |
366 if (!this.val[idx].isconstant()) { | |
367 return false; | |
368 } | |
369 } | |
370 return true; | |
346 }; | 371 }; |
347 | 372 |
348 intlit.prototype.eval = | 373 intlit.prototype.eval = |
349 floatlit.prototype.eval = | 374 floatlit.prototype.eval = |
350 strlit.prototype.eval = | 375 strlit.prototype.eval = |
738 if (expr.expression instanceof funcall && expr.expression.name == 'macro:') { | 763 if (expr.expression instanceof funcall && expr.expression.name == 'macro:') { |
739 env.defMacro(expr.symbol.name, exp.expression.args[0].eval(env)); | 764 env.defMacro(expr.symbol.name, exp.expression.args[0].eval(env)); |
740 } else { | 765 } else { |
741 env.syms[expr.symbol.cleanName()] = {}; | 766 env.syms[expr.symbol.cleanName()] = {}; |
742 this.expressions[i] = expr.macroexpand(env); | 767 this.expressions[i] = expr.macroexpand(env); |
743 if (this.expressions[i].expression instanceof lambda || 'val' in this.expressions[i].expression) { | 768 try { |
744 env.syms[expr.symbol.cleanName()] = this.expressions[i].expression.eval(env); | 769 if (this.expressions[i].expression.isconstant()) { |
745 } else { | 770 env.syms[expr.symbol.cleanName()] = this.expressions[i].expression.eval(env); |
746 env.syms[expr.symbol.cleanName()] = null; | 771 } else { |
772 env.syms[expr.symbol.cleanName()] = null; | |
773 } | |
774 } catch(e) { | |
775 var msg = 'Error, \n\t' + e.message.split('\n').join('\n\t') + '\nevaling node ' + this.expressions[i].expression | |
776 if (typeof this.expressions[i].expression == 'object') { | |
777 msg += ' with keys ' + JSON.stringify(Object.keys(this.expressions[i].expression) + ' and constructor ' + this.expressions[i].expression.constructor.name); | |
778 } | |
779 throw new Error(msg); | |
747 } | 780 } |
748 } | 781 } |
749 } else { | 782 } else { |
750 this.expressions[i] = expr.macroexpand(env); | 783 this.expressions[i] = expr.macroexpand(env); |
751 } | 784 } |