Mercurial > repos > tabletprog
comparison 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 |
comparison
equal
deleted
inserted
replaced
49:f2cda2e6f70e | 50:3e8d2a91102c |
---|---|
1 var module = {exports: {}}; | 1 var module = {exports: {}}; |
2 var PEG; | 2 var PEG; |
3 | 3 |
4 if (arguments.length < 1) { | 4 var file = null; |
5 print('usage: d8 tpc.js -- filename'); | 5 var argtype = 'normal'; |
6 os.exit(); | 6 var includes = []; |
7 } else { | 7 var basedir = ''; |
8 compileFile(arguments[0]); | 8 for (var i = 0; i < arguments.length; i++) { |
9 switch (argtype) { | |
10 case 'normal': | |
11 switch (arguments[i]) { | |
12 case '-basedir': | |
13 case '-i': | |
14 argtype = arguments[i]; | |
15 break; | |
16 default: | |
17 if (arguments[i].charAt(0) == '-') { | |
18 print("unrecognized switch", arguments[i]); | |
19 quit(1); | |
20 } | |
21 file = arguments[i]; | |
22 } | |
23 break; | |
24 case '-basedir': | |
25 if (basedir == '') { | |
26 basedir = arguments[i]; | |
27 argtype = 'normal'; | |
28 } else { | |
29 print("only one -basedir option allowed"); | |
30 quit(1); | |
31 } | |
32 break; | |
33 case '-i': | |
34 includes.push(arguments[i]); | |
35 argtype = 'normal'; | |
36 break; | |
37 } | |
38 } | |
39 if (argtype != 'normal') { | |
40 print("switch", argtype, "expects a parameter"); | |
41 quit(1); | |
9 } | 42 } |
10 | 43 |
11 function compileFile(filename) | 44 if (!file) { |
45 print('usage: d8 tpc.js -- filename'); | |
46 quit(1); | |
47 } | |
48 | |
49 compileFile(file, basedir, includes); | |
50 | |
51 | |
52 function compileFile(filename, basedir, includes) | |
12 { | 53 { |
13 var text = read(filename); | 54 var text = read(filename); |
14 load('peg.js'); | 55 load(basedir + 'peg.js'); |
15 PEG = module.exports; | 56 PEG = module.exports; |
16 load('parser.js'); | 57 load(basedir + 'parser.js'); |
17 load('compiler.js'); | 58 load(basedir + 'compiler.js'); |
18 load('cbackend.js'); | 59 load(basedir + 'cbackend.js'); |
19 try { | 60 try { |
20 var parsed = parser.parse(text); | 61 var parsed = parser.parse(text); |
21 } catch (e) { | 62 } catch (error) { |
22 print('SyntaxError on at', error.line, ',', error.column, ':', error.message); | 63 print('SyntaxError on at', error.line, ',', error.column, ':', error.message); |
23 var lines = text.split('\n'); | 64 var lines = text.split('\n'); |
24 print(lines[error.line-1]); | 65 print(lines[error.line-1]); |
25 var spacer = ''; | 66 var spacer = ''; |
26 for (var i = 1; i < error.column; i++) { | 67 for (var i = 1; i < error.column; i++) { |
29 } else { | 70 } else { |
30 spacer += ' '; | 71 spacer += ' '; |
31 } | 72 } |
32 } | 73 } |
33 print(spacer + '^'); | 74 print(spacer + '^'); |
34 exit(1); | 75 quit(1); |
35 } | 76 } |
36 var c = parsed.toCModule(); | 77 var c = parsed.toCModule(); |
37 print(c); | 78 print(c); |
38 } | 79 } |