changeset 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 03a07e540b9f
files interp.js
diffstat 1 files changed, 37 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- 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 {