diff tpi.js @ 206:b4a9d4e405c5

Implemented a simple interpreter to be used for macro expansion and a driver for testing it
author Mike Pavone <pavone@retrodev.com>
date Wed, 23 Oct 2013 19:10:03 -0700
parents
children 60eff5f81d9a
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tpi.js	Wed Oct 23 19:10:03 2013 -0700
@@ -0,0 +1,103 @@
+var module = {exports: {}};
+var console = {log: function(text){}};
+var PEG;
+
+var file = null;
+var argtype = 'normal';
+var includes = ['.'];
+var basedir = '';
+var debugmode = false;
+for (var i = 0; i < arguments.length; i++) {
+	switch (argtype) {
+	case 'normal':
+		switch (arguments[i]) {
+		case '-basedir':
+		case '-i':
+		case '-interpdebug':
+			debugmode = 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 tpi.js -- filename');
+	quit(1);
+}
+includes.push(basedir + 'modules');
+interpFile(file, basedir, includes, debugmode);
+
+
+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 interpFile(filename, basedir, includes, debugmode)
+{
+	load(basedir + 'mquery.js');
+	load(basedir + 'peg.js');
+	PEG = module.exports;
+	load(basedir + 'parser.js');
+	load(basedir + 'interp.js');
+
+	if (debugmode) {
+		debugprint = print;
+	} else {
+		debugprint = function() {};
+	}
+	var parsed = parseFile(filename);
+
+	toplevel = new topenv(includes);
+
+	var mainModule = parsed.eval(toplevel);
+	return mainModule.tpmeth_main();
+}