Mercurial > repos > tabletprog
view tpc.js @ 50:3e8d2a91102c
Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Fri, 13 Jul 2012 17:03:59 -0700 |
parents | 2a9c6eed0c70 |
children | 976a0924e1d4 |
line wrap: on
line source
var module = {exports: {}}; var PEG; var file = null; var argtype = 'normal'; var includes = []; var basedir = ''; for (var i = 0; i < arguments.length; i++) { switch (argtype) { case 'normal': switch (arguments[i]) { case '-basedir': case '-i': argtype = arguments[i]; 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; } } if (argtype != 'normal') { print("switch", argtype, "expects a parameter"); quit(1); } if (!file) { print('usage: d8 tpc.js -- filename'); quit(1); } compileFile(file, basedir, includes); function compileFile(filename, basedir, includes) { var text = read(filename); load(basedir + 'peg.js'); PEG = module.exports; load(basedir + 'parser.js'); load(basedir + 'compiler.js'); load(basedir + 'cbackend.js'); 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); } var c = parsed.toCModule(); print(c); }