# HG changeset patch # User Mike Pavone # Date 1342224239 25200 # Node ID 3e8d2a91102cedf857172a9d0021710095d7e47a # Parent f2cda2e6f70ebcf1f95c6f095515ecafab19e7fe Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting. diff -r f2cda2e6f70e -r 3e8d2a91102c tpc.js --- a/tpc.js Fri Jul 13 14:16:51 2012 -0700 +++ b/tpc.js Fri Jul 13 17:03:59 2012 -0700 @@ -1,24 +1,65 @@ var module = {exports: {}}; var PEG; -if (arguments.length < 1) { - print('usage: d8 tpc.js -- filename'); - os.exit(); -} else { - compileFile(arguments[0]); +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); } -function compileFile(filename) +if (!file) { + print('usage: d8 tpc.js -- filename'); + quit(1); +} + +compileFile(file, basedir, includes); + + +function compileFile(filename, basedir, includes) { var text = read(filename); - load('peg.js'); + load(basedir + 'peg.js'); PEG = module.exports; - load('parser.js'); - load('compiler.js'); - load('cbackend.js'); + load(basedir + 'parser.js'); + load(basedir + 'compiler.js'); + load(basedir + 'cbackend.js'); try { var parsed = parser.parse(text); - } catch (e) { + } catch (error) { print('SyntaxError on at', error.line, ',', error.column, ':', error.message); var lines = text.split('\n'); print(lines[error.line-1]); @@ -31,7 +72,7 @@ } } print(spacer + '^'); - exit(1); + quit(1); } var c = parsed.toCModule(); print(c);