annotate tpc.js @ 54:976a0924e1d4

Fix closure over self var
author Mike Pavone <pavone@retrodev.com>
date Fri, 13 Jul 2012 19:22:39 -0700
parents 3e8d2a91102c
children 25b697c91629
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 = '';
54
976a0924e1d4 Fix closure over self var
Mike Pavone <pavone@retrodev.com>
parents: 50
diff changeset
8 var debugmode = false;
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
9 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
10 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
11 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
12 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
13 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
14 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
15 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
16 break;
54
976a0924e1d4 Fix closure over self var
Mike Pavone <pavone@retrodev.com>
parents: 50
diff changeset
17 case '-compilerdebug':
976a0924e1d4 Fix closure over self var
Mike Pavone <pavone@retrodev.com>
parents: 50
diff changeset
18 debugmode = true;
976a0924e1d4 Fix closure over self var
Mike Pavone <pavone@retrodev.com>
parents: 50
diff changeset
19 break;
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
20 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
21 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
22 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
23 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
24 }
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 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
26 }
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 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
28 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
29 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
30 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
31 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
32 } 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
33 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
34 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
35 }
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 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
38 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
39 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 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
41 }
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
42 }
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
43 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
44 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
45 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
46 }
3b0503a67165 Add scripts for building programs via C using d8 rather than a browser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
47
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
48 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
49 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
50 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
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
54
976a0924e1d4 Fix closure over self var
Mike Pavone <pavone@retrodev.com>
parents: 50
diff changeset
53 compileFile(file, basedir, includes, debugmode);
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
54
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
54
976a0924e1d4 Fix closure over self var
Mike Pavone <pavone@retrodev.com>
parents: 50
diff changeset
56 function compileFile(filename, basedir, includes, debugmode)
36
3b0503a67165 Add scripts for building programs via C using d8 rather than a browser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
57 {
3b0503a67165 Add scripts for building programs via C using d8 rather than a browser
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
58 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
59 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
60 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
61 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
62 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
63 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
64 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
65 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
66 } 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
67 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
68 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
69 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
70 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
71 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
72 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
73 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
74 } 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
75 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
76 }
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
77 }
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
78 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
79 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
80 }
54
976a0924e1d4 Fix closure over self var
Mike Pavone <pavone@retrodev.com>
parents: 50
diff changeset
81 if (debugmode) {
976a0924e1d4 Fix closure over self var
Mike Pavone <pavone@retrodev.com>
parents: 50
diff changeset
82 debugprint = print;
976a0924e1d4 Fix closure over self var
Mike Pavone <pavone@retrodev.com>
parents: 50
diff changeset
83 }
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
84 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
85 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
86 }