Mercurial > repos > tabletprog
annotate tpc.js @ 76:c801dccdb4fb
Add shell script for driving tpc.js
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Sun, 15 Jul 2012 13:06:29 -0700 |
parents | 3a169ebb3224 |
children | b5152f5ac138 |
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'; |
66
25b697c91629
Finish implementation of external module access
Mike Pavone <pavone@retrodev.com>
parents:
54
diff
changeset
|
6 var includes = ['.']; |
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
|
7 var basedir = ''; |
54 | 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 | 17 case '-compilerdebug': |
18 debugmode = true; | |
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 } |
68
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
66
diff
changeset
|
52 includes.push(basedir + 'modules'); |
54 | 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 |
66
25b697c91629
Finish implementation of external module access
Mike Pavone <pavone@retrodev.com>
parents:
54
diff
changeset
|
56 function parseFile(filename) |
36
3b0503a67165
Add scripts for building programs via C using d8 rather than a browser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
57 { |
68
3a169ebb3224
Change strategy for handling true and false to avoid some initialization order problems and improve performance. Add support for negative integer literals. Update samples to reflect true/false change.
Mike Pavone <pavone@retrodev.com>
parents:
66
diff
changeset
|
58 debugprint('//parsing', filename); |
36
3b0503a67165
Add scripts for building programs via C using d8 rather than a browser
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
59 var text = read(filename); |
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 } |
66
25b697c91629
Finish implementation of external module access
Mike Pavone <pavone@retrodev.com>
parents:
54
diff
changeset
|
77 return parsed; |
25b697c91629
Finish implementation of external module access
Mike Pavone <pavone@retrodev.com>
parents:
54
diff
changeset
|
78 } |
25b697c91629
Finish implementation of external module access
Mike Pavone <pavone@retrodev.com>
parents:
54
diff
changeset
|
79 |
25b697c91629
Finish implementation of external module access
Mike Pavone <pavone@retrodev.com>
parents:
54
diff
changeset
|
80 |
25b697c91629
Finish implementation of external module access
Mike Pavone <pavone@retrodev.com>
parents:
54
diff
changeset
|
81 function compileFile(filename, basedir, includes, debugmode) |
25b697c91629
Finish implementation of external module access
Mike Pavone <pavone@retrodev.com>
parents:
54
diff
changeset
|
82 { |
25b697c91629
Finish implementation of external module access
Mike Pavone <pavone@retrodev.com>
parents:
54
diff
changeset
|
83 |
25b697c91629
Finish implementation of external module access
Mike Pavone <pavone@retrodev.com>
parents:
54
diff
changeset
|
84 load(basedir + 'peg.js'); |
25b697c91629
Finish implementation of external module access
Mike Pavone <pavone@retrodev.com>
parents:
54
diff
changeset
|
85 PEG = module.exports; |
25b697c91629
Finish implementation of external module access
Mike Pavone <pavone@retrodev.com>
parents:
54
diff
changeset
|
86 load(basedir + 'parser.js'); |
25b697c91629
Finish implementation of external module access
Mike Pavone <pavone@retrodev.com>
parents:
54
diff
changeset
|
87 load(basedir + 'compiler.js'); |
25b697c91629
Finish implementation of external module access
Mike Pavone <pavone@retrodev.com>
parents:
54
diff
changeset
|
88 load(basedir + 'cbackend.js'); |
25b697c91629
Finish implementation of external module access
Mike Pavone <pavone@retrodev.com>
parents:
54
diff
changeset
|
89 var parsed = parseFile(filename); |
54 | 90 if (debugmode) { |
91 debugprint = print; | |
92 } | |
66
25b697c91629
Finish implementation of external module access
Mike Pavone <pavone@retrodev.com>
parents:
54
diff
changeset
|
93 toplevel = new topsymbols(includes); |
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
|
94 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
|
95 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
|
96 } |