view tpc.js @ 44:9dd370530f69

Fix escape codes in string literals. Don't print out the return value of main method. Fixup fib example to use print: method. Cleanup error handling in compiler slightly
author Mike Pavone <pavone@retrodev.com>
date Thu, 12 Jul 2012 22:49:08 -0700
parents 27a2167663dd
children 2a9c6eed0c70
line wrap: on
line source

var module = {exports: {}};
var PEG;

if (arguments.length < 1) {
	print('usage: d8 tpc.js -- filename');
	os.exit();
} else {
	compileFile(arguments[0]);
}

function compileFile(filename)
{
	var text = read(filename);
	load('peg.js');
	PEG = module.exports;
	load('parser.js');
	load('compiler.js');
	load('cbackend.js');
	try {
		var parsed = parser.parse(text);
		var c = parsed.toCModule();
		print(c);
	} catch(error if error.name == 'SyntaxError') {
		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 + '^');

	}
}