annotate tpi.js @ 246:8c81afd6d2d3

Refactor some of the AST node object constructors into a separate AST module
author Michael Pavone <pavone@retrodev.com>
date Mon, 06 Jan 2014 19:41:35 -0800
parents 60eff5f81d9a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
206
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1 var module = {exports: {}};
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
2 var console = {log: function(text){}};
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
3 var PEG;
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
4
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
5 var file = null;
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
6 var argtype = 'normal';
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
7 var includes = ['.'];
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
8 var basedir = '';
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
9 var debugmode = false;
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
10 for (var i = 0; i < arguments.length; i++) {
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
11 switch (argtype) {
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
12 case 'normal':
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
13 switch (arguments[i]) {
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
14 case '-basedir':
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
15 case '-i':
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
16 case '-interpdebug':
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
17 debugmode = true;
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
18 break;
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
19 default:
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
20 if (arguments[i].charAt(0) == '-') {
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
21 print("unrecognized switch", arguments[i]);
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
22 quit(1);
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
23 }
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
24 file = arguments[i];
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
25 }
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
26 break;
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
27 case '-basedir':
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
28 if (basedir == '') {
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
29 basedir = arguments[i];
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
30 argtype = 'normal';
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
31 } else {
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
32 print("only one -basedir option allowed");
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
33 quit(1);
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
34 }
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
35 break;
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
36 case '-i':
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
37 includes.push(arguments[i]);
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
38 argtype = 'normal';
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
39 break;
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
40 case '-backend':
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
41 backend = arguments[i];
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
42 argtype = 'normal';
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
43 break;
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
44 }
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
45 }
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
46 if (argtype != 'normal') {
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
47 print("switch", argtype, "expects a parameter");
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
48 quit(1);
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
49 }
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
50
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
51 if (!file) {
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
52 print('usage: d8 tpi.js -- filename');
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
53 quit(1);
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
54 }
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
55 includes.push(basedir + 'modules');
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
56 interpFile(file, basedir, includes, debugmode);
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
57
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
58
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
59 function parseFile(filename)
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
60 {
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
61 debugprint('//parsing', filename);
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
62 var text = read(filename);
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
63 try {
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
64 var parsed = parser.parse(text);
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
65 } catch (error) {
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
66 print('SyntaxError on at', error.line, ',', error.column, ':', error.message);
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
67 var lines = text.split('\n');
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
68 print(lines[error.line-1]);
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
69 var spacer = '';
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
70 for (var i = 1; i < error.column; i++) {
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
71 if (lines[error.line-1].charAt(i-1) == '\t') {
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
72 spacer += ' ';
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
73 } else {
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
74 spacer += ' ';
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
75 }
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
76 }
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
77 print(spacer + '^');
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
78 quit(1);
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
79 }
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
80 return parsed;
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
81 }
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
82
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
83
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
84 function interpFile(filename, basedir, includes, debugmode)
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
85 {
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
86 load(basedir + 'mquery.js');
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
87 load(basedir + 'peg.js');
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
88 PEG = module.exports;
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
89 load(basedir + 'parser.js');
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
90 load(basedir + 'interp.js');
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
91
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
92 if (debugmode) {
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
93 debugprint = print;
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
94 } else {
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
95 debugprint = function() {};
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
96 }
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
97 var parsed = parseFile(filename);
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
98
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
99 toplevel = new topenv(includes);
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
100
207
60eff5f81d9a Basic implementation of macros is now working
Mike Pavone <pavone@retrodev.com>
parents: 206
diff changeset
101 parsed = parsed.macroexpand(toplevel);
206
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
102 var mainModule = parsed.eval(toplevel);
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
103 return mainModule.tpmeth_main();
b4a9d4e405c5 Implemented a simple interpreter to be used for macro expansion and a driver for testing it
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
104 }