diff compiler.js @ 34:a10f1b049193

Working closures, but need to rethink method call strategy
author Mike Pavone <pavone@retrodev.com>
date Mon, 09 Jul 2012 08:57:50 -0700
parents 668f533e5284
children bf5e88f6419d
line wrap: on
line diff
--- a/compiler.js	Sun Jul 08 12:32:24 2012 -0700
+++ b/compiler.js	Mon Jul 09 08:57:50 2012 -0700
@@ -37,6 +37,9 @@
 	}
 	return null;
 }
+topsymbols.prototype.getEnvType = function() {
+	return 'void';
+}
 
 function osymbols(parent)
 {
@@ -95,14 +98,22 @@
 	}
 	return curlist;
 }
+osymbols.prototype.getEnvType = function() {
+	console.log('osymbol parent', this.parent);
+	return this.parent.getEnvType();
+}
 
 function lsymbols(parent)
 {
 	this.parent = parent;
 	this.names = {};
+	this.closedover = {};
 	this.needsSelfVar = false;
+	this.passthruenv = false;
+	this.envtype = 'void';
 }
-lsymbols.prototype.find = function(name) {
+lsymbols.prototype.find = function(name, nestedcall) {
+	console.log('find', name, nestedcall);
 	if (name in this.names) {
 		if (this.names[name] instanceof funcall && this.names[name].name == 'foreign:') {
 			return {
@@ -110,18 +121,32 @@
 				def: this.names[name]
 			};
 		}
+		if (nestedcall) {
+			console.log('closedover', name);
+			this.closedover[name] = true;
+		} 
+		if (name in this.closedover) {
+			return {
+				type: 'closedover',
+				def: this.names[name]
+			};
+		}
 		return {
 			type: 'local',
 			def: this.names[name]
 		};
 	} else if(this.parent) {
-		var ret = this.parent.find(name);
+		var ret = this.parent.find(name, true);
 		if (ret) {
-			if (ret.type == 'local') {
+			if (ret.type == 'closedover') {
 				ret.type = 'upvar';
 				ret.depth = 1;
 			} else if (ret.type == 'upvar') {
-				ret.depth++;
+				if (Object(this.closedover).keys.length) {
+					ret.depth++;
+				} else {
+					this.passthruenv = true;
+				}
 			}
 		}
 		return ret;
@@ -147,6 +172,20 @@
 	}
 };
 lsymbols.prototype.allSymbols = osymbols.prototype.allSymbols;
+lsymbols.prototype.parentEnvType = function() {
+	if (!this.parent) {
+		return 'void';
+	}
+	return this.parent.getEnvType();
+};
+lsymbols.prototype.getEnvType = function() {
+	if (this.passthruenv) {
+		console.log('lsymbol parent', this.parent);
+		return this.parent.getEnvType();
+	} else {
+		return this.envtype;
+	}
+}
 
 var mainModule;
 
@@ -173,6 +212,7 @@
 
 symbol.prototype.populateSymbols = function(symbols) {
 	this.symbols = symbols;
+	symbols.find(this.cleanName());
 }
 
 intlit.prototype.populateSymbols = function(symbols) {