view tpc.js @ 251:2557ce4e671f

Fix a couple of compiler bugs. topenv was getting initialized in multiple places. This resulted in multiple copies of modules getting created which caused problems for macro expansion. Additionally, arguments were not being marked as declared during code generation so assigning to an argument that was not closed over generated invalid C code.
author Michael Pavone <pavone@retrodev.com>
date Fri, 11 Apr 2014 22:29:32 -0700
parents 2dac67e9d18b
children
line wrap: on
line source

var module = {exports: {}};
var console = {log: function(text){}};
var PEG;

var file = null;
var argtype = 'normal';
var includes = ['.'];
var basedir = '';
var debugmode = false;
var backend = 'C';
var macroonly = false;
for (var i = 0; i < arguments.length; i++) {
	switch (argtype) {
	case 'normal':
		switch (arguments[i]) {
		case '-basedir':
		case '-i':
		case '-backend':
			argtype = arguments[i];
			break;
		case '-compilerdebug':
			debugmode = true;
			break;
		case '-m':
			macroonly = true;
			break;
		default:
			if (arguments[i].charAt(0) == '-') {
				print("unrecognized switch", arguments[i]);
				quit(1);
			}
			file = arguments[i];
		}
		break;
	case '-basedir':
		if (basedir == '') {
			basedir = arguments[i];
			argtype = 'normal';
		} else {
			print("only one -basedir option allowed");
			quit(1);
		}
		break;
	case '-i':
		includes.push(arguments[i]);
		argtype = 'normal';
		break;
	case '-backend':
		backend = arguments[i];
		argtype = 'normal';
		break;
	}
}
if (argtype != 'normal') {
	print("switch", argtype, "expects a parameter");
	quit(1);
}

if (!file) {
	print('usage: d8 tpc.js -- filename');
	quit(1);
}
includes.push(basedir + 'modules');
compileFile(file, basedir, includes, debugmode, backend);


function parseFile(filename)
{
	debugprint('//parsing', filename);
	var text = read(filename);
	try {
		var parsed = parser.parse(text);
	} catch (error) {
		print('SyntaxError on at', error.line, ',', error.column, ':', error.message);
		var lines = text.split('\n');
		print(lines[error.line-1]);
		var spacer = '';
		for (var i = 1; i < error.column; i++) {
			if (lines[error.line-1].charAt(i-1) == '\t') {
				spacer += '    ';
			} else {
				spacer += ' ';
			}
		}
		print(spacer + '^');
		quit(1);
	}
	return parsed;
}


function compileFile(filename, basedir, includes, debugmode, backend)
{
	load(basedir + 'mquery.js');
	load(basedir + 'peg.js');
	PEG = module.exports;
	load(basedir + 'parser.js');
	load(basedir + 'compiler.js');
  load(basedir + 'interp.js');
	if (backend == 'C') {
		load(basedir + 'cbackend.js');
	} else if (backend == 'IL') {
		load(basedir + 'ilbackend.js');
	} else {
		load(basedir + 'jsbackend.js');
	}

	var parsed = parseFile(filename);
	if (debugmode) {
		debugprint = print;
	}
	var top = new topenv(includes);
  parsed = parsed.macroexpand(top);
	if (macroonly) {
		print(''+parsed);
	} else {
		toplevel = new topsymbols(includes, top);
		switch(backend)
		{
		case 'C':
			var c = parsed.toCModule();
			break;
		case 'IL':
			var c = parsed.toILModule();
		case 'JS':
			var c = makeJSProg(parsed);
			break;
		default:
			print('Backend', backend, ' not recognized');
			quit(1);
		}
		print(c);
	}
}