view jsbackend.js @ 23:068d63627b16

Populate in scope symbol buttons when clicking on a symbol in the source
author Mike Pavone <pavone@retrodev.com>
date Mon, 26 Mar 2012 21:29:03 -0700
parents 6c8ae6b47ab5
children 4d87c38404d6
line wrap: on
line source

var mainModule;

function toobj(val)
{
	switch(typeof val)
	{
	case 'boolean':
		if(val) {
			return mainModule.strue;
		} else {
			return mainModule.sfalse;
		}
	case 'number':
		return mainModule.snumber(val);
	}
	throw new Error("can't make val into object");
}

op.prototype.toJS = function(isReceiver) {
	var ret = '(' + this.left.toJS() +' '+ (this.op == '=' ? '==' : this.op) +' '+ this.right.toJS() + ')';
	if (isReceiver) {
		ret = 'toobj' + ret;
	}
	return ret;
};

symbol.prototype.toJS = function() {
	var name = this.cleanName();
	if (name == 'self') {
		return this.symbols.selfVar();
	}
	name = name.replace("_", "UN_").replace(":", "CN_").replace("!", "EX_").replace('?', 'QS_').replace('@', 'AT_');
	var reserved = {'true': true, 'false': true, 'this': true, 'if': true, 'else': true, 'NaN': true};
	if (name in reserved) {
		name = 's' + name;
	}
	return name;
}

intlit.prototype.toJS = function() {
	return this.val.toString();
}

floatlit.prototype.toJS = function() {
	return this.val.toString();
}

strlit.prototype.toJS = function() {
	return '"' + this.val.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n').replace('\r', '\\r') + '"';
}

funcall.prototype.toJS = function() {
	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);
	}
	var funinfo = this.symbols.find(name);
	if (!funinfo || funinfo.def instanceof setter) {
		var receiver = args[0];
		args.splice(0, 1);
		for (var i in args) {
			args[i] = args[i].toJS();
		}
		var rJS = receiver.toJS(true);
		if ((name[name.length-1] == '!' && args.length == 1) || (funinfo && funinfo.def instanceof setter)) {
			console.log(name.substr(0, name.length-1));
			return  '(' + rJS + '.' + (new symbol(name.substr(0, name.length-1), this.symbols)).toJS()  + ' = ' + args[0] + ', ' + rJS + ')'
		} else {
			var callee = rJS + '.' + (new symbol(name, this.symbols)).toJS();
			var callCode = callee + '(' + args.join(', ') + ')';
			if (args.length == 0) {
				return '(' + callee + ' instanceof Function ? ' + callCode + ' : ' + callee + ')';
			} else {
				return callCode;
			}
		}
	}
	var ret = '';
	switch(funinfo.type)
	{
	case 'self':
		if (args.length < funinfo.def.args.length || funinfo.def.args[0].name != 'self') {
			var receiver = new symbol('self', this.symbols);
		} else {
			var receiver = args[0];
			args.splice(0, 1);
		}
		ret = receiver.toJS(true) + '.';
		break;
	case 'parent':
		ret = 'this';
		for (var i = 0; i < funinfo.depth; ++i) {
			ret += '.parent';
		}
		break;
	}
	for (var i in args) {
		args[i] = args[i].toJS();
	}
	return ret + (new symbol(name, this.symbols)).toJS() + '(' + args.join(', ') + ')';
}

object.prototype.toJS = function() {
	var messages = this.messages;
	var compiled = []
	for (var i in messages) {
		var js = messages[i].toJSObject();
		if (js) {
			compiled.push(indent(js));
		}
	}
	return '{\n\tparent: ' + this.symbols.parentObject() + ',\n\t' + compiled.join(',\n\t') + '\n}';
}

object.prototype.toJSModule = function() {
	this.populateSymbols(null);
	return '(function () {\n\tvar module = ' + indent(this.toJS()) + ';\n\treturn module;\n})'
}

lambda.prototype.toJS = function() {
	var args = this.args ? this.args.slice(0, this.args.length) : [];
	if (args.length && args[0].cleanName() == 'self') {
		args.splice(0, 1);
	}
	var exprs = this.expressions;
	for (var i in args) {
		args[i] = args[i].toJS();
	}
	var compiled = []
	for (var i in exprs) {
		var js = exprs[i].toJS();
		if (js) {
			compiled.push(indent(js));
		}
	}
	exprs = compiled;
	if (exprs.length) {
		exprs[exprs.length-1] = 'return ' + exprs[exprs.length-1] + ';';
	}
	return 'function (' + args.join(', ') + ') {\n\t' + (this.symbols.needsSelfVar ? 'var self = this;\n\t' : '') + exprs.join(';\n\t') + '\n}'
};
lambda.prototype.toJSModule = function() {
	this.populateSymbols(null);
	return this.toJS();
}

assignment.prototype.toJS = function() {
	var existing = this.symbols.find(this.symbol.name);
	var prefix = '';
	if (!existing) {
		prefix =  'var ';
	} else {
		switch (existing.type)
		{
		case 'self':
			prefix = 'this.';
			break;
		case 'parent':
			prefix = 'this.';
			for (var i = 0; i < existing.depth; ++i) {
				prefix += 'parent.';
			}
			break;
		}
	}
	var val = this.expression.toJS();
	if (val === null) {
		return null;
	}
	return prefix + this.symbol.toJS() + ' = ' + val;
};
assignment.prototype.toJSObject = function() {
	var val = this.expression.toJS();
	if (val === null) {
		return null;
	}
	return this.symbol.toJS() + ': ' + val;
};