annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
36
3b0503a67165 Add scripts for building programs via C using d8 rather than a browser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1 var module = {exports: {}};
3b0503a67165 Add scripts for building programs via C using d8 rather than a browser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
2 var PEG;
3b0503a67165 Add scripts for building programs via C using d8 rather than a browser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
3
50
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
4 var file = null;
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
5 var argtype = 'normal';
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
6 var includes = [];
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
7 var basedir = '';
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
8 for (var i = 0; i < arguments.length; i++) {
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
9 switch (argtype) {
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
10 case 'normal':
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
11 switch (arguments[i]) {
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
12 case '-basedir':
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
13 case '-i':
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
14 argtype = arguments[i];
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
15 break;
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
16 default:
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
17 if (arguments[i].charAt(0) == '-') {
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
18 print("unrecognized switch", arguments[i]);
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
19 quit(1);
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
20 }
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
21 file = arguments[i];
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
22 }
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
23 break;
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
24 case '-basedir':
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
25 if (basedir == '') {
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
26 basedir = arguments[i];
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
27 argtype = 'normal';
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
28 } else {
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
29 print("only one -basedir option allowed");
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
30 quit(1);
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
31 }
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
32 break;
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
33 case '-i':
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
34 includes.push(arguments[i]);
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
35 argtype = 'normal';
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
36 break;
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
37 }
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
38 }
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
39 if (argtype != 'normal') {
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
40 print("switch", argtype, "expects a parameter");
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
41 quit(1);
36
3b0503a67165 Add scripts for building programs via C using d8 rather than a browser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
42 }
3b0503a67165 Add scripts for building programs via C using d8 rather than a browser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
43
50
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
44 if (!file) {
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
45 print('usage: d8 tpc.js -- filename');
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
46 quit(1);
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
47 }
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
48
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
49 compileFile(file, basedir, includes);
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
50
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
51
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
52 function compileFile(filename, basedir, includes)
36
3b0503a67165 Add scripts for building programs via C using d8 rather than a browser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
53 {
3b0503a67165 Add scripts for building programs via C using d8 rather than a browser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
54 var text = read(filename);
50
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
55 load(basedir + 'peg.js');
36
3b0503a67165 Add scripts for building programs via C using d8 rather than a browser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
56 PEG = module.exports;
50
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
57 load(basedir + 'parser.js');
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
58 load(basedir + 'compiler.js');
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
59 load(basedir + 'cbackend.js');
43
27a2167663dd Improve compiler error reporting. Fix operator associativity. Add some more string operations and a string ops sample
Mike Pavone <pavone@retrodev.com>
parents: 36
diff changeset
60 try {
45
2a9c6eed0c70 Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents: 44
diff changeset
61 var parsed = parser.parse(text);
50
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
62 } catch (error) {
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
Mike Pavone <pavone@retrodev.com>
parents: 43
diff changeset
63 print('SyntaxError on at', error.line, ',', error.column, ':', error.message);
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
Mike Pavone <pavone@retrodev.com>
parents: 43
diff changeset
64 var lines = text.split('\n');
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
Mike Pavone <pavone@retrodev.com>
parents: 43
diff changeset
65 print(lines[error.line-1]);
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
Mike Pavone <pavone@retrodev.com>
parents: 43
diff changeset
66 var spacer = '';
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
Mike Pavone <pavone@retrodev.com>
parents: 43
diff changeset
67 for (var i = 1; i < error.column; i++) {
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
Mike Pavone <pavone@retrodev.com>
parents: 43
diff changeset
68 if (lines[error.line-1].charAt(i-1) == '\t') {
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
Mike Pavone <pavone@retrodev.com>
parents: 43
diff changeset
69 spacer += ' ';
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
Mike Pavone <pavone@retrodev.com>
parents: 43
diff changeset
70 } else {
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
Mike Pavone <pavone@retrodev.com>
parents: 43
diff changeset
71 spacer += ' ';
43
27a2167663dd Improve compiler error reporting. Fix operator associativity. Add some more string operations and a string ops sample
Mike Pavone <pavone@retrodev.com>
parents: 36
diff changeset
72 }
27a2167663dd Improve compiler error reporting. Fix operator associativity. Add some more string operations and a string ops sample
Mike Pavone <pavone@retrodev.com>
parents: 36
diff changeset
73 }
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
Mike Pavone <pavone@retrodev.com>
parents: 43
diff changeset
74 print(spacer + '^');
50
3e8d2a91102c Improve tpc.js compiler driver with some nice swithc handling and fix a small bug in syntax error reporting.
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
75 quit(1);
43
27a2167663dd Improve compiler error reporting. Fix operator associativity. Add some more string operations and a string ops sample
Mike Pavone <pavone@retrodev.com>
parents: 36
diff changeset
76 }
45
2a9c6eed0c70 Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents: 44
diff changeset
77 var c = parsed.toCModule();
2a9c6eed0c70 Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents: 44
diff changeset
78 print(c);
36
3b0503a67165 Add scripts for building programs via C using d8 rather than a browser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
79 }