diff jsbackend.js @ 12:6e4851a204a5

Add ability to load code into editor
author Mike Pavone <pavone@retrodev.com>
date Wed, 21 Mar 2012 23:13:51 -0700
parents 02b20292f187
children 132c7756860e
line wrap: on
line diff
--- a/jsbackend.js	Wed Mar 21 21:42:38 2012 -0700
+++ b/jsbackend.js	Wed Mar 21 23:13:51 2012 -0700
@@ -62,6 +62,15 @@
 
 funcall.prototype.toJS = function(symbols) {
 	var name = this.name[this.name.length-1] == ':' ? this.name.substr(0, this.name.length-1) : this.name;
+	if (name == 'foreign') {
+		if ((this.args[0] instanceof lambda) || (this.args[0] instanceof object)) {
+			return null;
+		} else if(this.args[0] instanceof symbol) {
+			return this.args[0].name;
+		} else {
+			throw new Error("Unexpected AST type for foreign:");
+		}
+	}
 	var args = this.args.slice(0, this.args.length);
 	if (this.receiver) {
 		args.splice(0, 0, this.receiver);
@@ -171,15 +180,17 @@
 			break;
 		}
 	}
-	if (this.expression instanceof funcall && this.expression.name == 'foreign:') {
+	var val = this.expression.toJS(symbols);
+	if (val === null) {
 		return null;
 	}
-	return prefix + this.symbol.toJS(symbols) + ' = ' + this.expression.toJS(symbols);
+	return prefix + this.symbol.toJS(symbols) + ' = ' + val;
 };
 assignment.prototype.toJSObject = function(symbols) {
 	symbols.defineMsg(this.symbol.name, this.expression);
-	if (this.expression instanceof funcall && this.expression.name == 'foreign:') {
+	var val = this.expression.toJS(symbols);
+	if (val === null) {
 		return null;
 	}
-	return this.symbol.toJS(symbols) + ': ' + this.expression.toJS(symbols);
+	return this.symbol.toJS(symbols) + ': ' + val;
 };