-
Notifications
You must be signed in to change notification settings - Fork 2
/
tdop.js
174 lines (155 loc) · 6.11 KB
/
tdop.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Source for the `tdop.html` demonstration page.
define(["html-escape", "json2", "ts!parse", "ts!bcompile", "ts!binterp", "ts!stdlib", "ts!top-level", "ts!tests"], function(html_escape, JSON, parse, bcompile, binterp, stdlib, top_level, tests) {
/*jslint evil: true */
/*members create, error, message, name, prototype, stringify, toSource,
toString, write
*/
/*global JSON, make_parse, parse, source, tree */
var CATCH_ERRORS = false;
var SHOW_SOURCE = true;
var PRINT_TREE = false;
var PRINT_JCOMPILE = false;
var PRINT_RAW_BYTECODE = false;
var PRINT_ENCODED_BYTECODE = true;
function fill(elem_id, h1, contents, no_escape) {
if (!no_escape) contents = html_escape(contents);
elem = document.getElementById(elem_id);
elem.innerHTML = "<h1>" + h1 + "</h1>\n" + contents;
}
function do_it() {
// We are going to make the compiler/interpreter compile itself.
var make_self_test = function(parse, bcompile, top_level) {
return function (source) {
var result;
source = source || '{ return 1+2; }';
//result = tokenize(source, '=<>!+-*&|/%^', '=<>&|');
var tree = parse(source, top_level);
//result = tree;
var bc = bcompile(tree);
result = bc.decompile(0);
//result = bc.encode().join();
//console.log(result);
return result;
};
};
define("self_test", ["parse", "bcompile", "top-level"], make_self_test);
var self_test_source = make_self_test.toSource ?
make_self_test.toSource() : make_self_test.toString();
self_test_source = 'define("self_test", ["parse","bcompile","top-level"], '+
self_test_source + ');\n';
// combine sources
var isource = '{ return 2+3; }'; // input to interpreted version of compiler
//isource = 'var f = function() { };';
//isource = sources[7].replace(/module/, 'library_init');
var fake_require =
"var __modules__ = {};\n"+
"define = function(name, deps, init_func) {\n"+
" var d = deps.map(function(m) { return __modules__[m]; });\n"+
" __modules__[name] = init_func.apply(this, d);\n"+
"};";
var top_level_source = 'define("top-level", [], '+
'function() { return '+JSON.stringify(top_level)+'; });';
var source = '{\n'+
stdlib.source()+'\n'+
fake_require+'\n'+
tests.lookup("tokenize")+"\n"+
tests.lookup("parse")+"\n"+
tests.lookup("bytecode-table")+"\n"+
tests.lookup("literal-map")+"\n"+
tests.lookup("bcompile")+"\n"+
tests.lookup("binterp")+"\n"+
top_level_source+"\n"+
self_test_source+"\n"+
"return __modules__['self_test'](arguments[0]); }\n";
if (0) {
// HACK to run specific test cases
test_case = tests[29].replace(/^define\([^,]+,/, "define('self_test',");
source = '{\n' + stdlib.source() + '\n' + fake_require + '\n' +
test_case + "\n"+
"return __modules__['self_test'](arguments[0]); }\n";
console.log(source);
// END HACK
}
if (1) {
// for use comparing the output of the interpreter with the
// output of the javascript code run directly.
eval("function self_test() {"+source+"}");
eresult = self_test(isource);
if (typeof(eresult) != "string") eresult = ""+JSON.stringify(eresult);
fill('expected-result', 'Expected result', eresult);
}
if (SHOW_SOURCE) {
sl = source.split(/\n/);
ci = 0;
o = "<pre>";
for (j=0; j < sl.length; j++) {
o += ci+": "+html_escape(sl[j])+"\n";
ci += sl[j].length + 1;
}
o += "</pre>";
fill('source', 'Interpreted source', o, 1/*no escape*/);
}
tree = parse(source, top_level);
if (tree) {
/* Raw compiled tree */
if (PRINT_TREE) {
fill('parse-tree', 'Parse tree',
JSON.stringify(tree, ['key', 'name', 'message',
'value', 'arity', 'first', 'second', 'third', 'fourth'], 4));
}
/* Pretty-printed to eval'able javascript */
if (PRINT_JCOMPILE) {
fill('jcompile', 'Recompiled to JavaScript', jcompile(tree));
}
/* Bytecode compiled (raw) */
bc = bcompile(tree);
if (PRINT_RAW_BYTECODE) {
fill('raw-bytecode', 'Raw Bytecode File Contents',
JSON.stringify(bc, ['functions', 'literals', 'id', 'nargs',
'bytecode', 'label'], 4));
}
if (PRINT_ENCODED_BYTECODE) {
var out_file = bc.encode();
fill('encoded-bytecode', 'Encoded Bytecode', out_file.length+" "+
JSON.stringify(out_file));
}
/* Pretty-printed bytecode */
b = "<p>Index: ";
for (i=0; i<bc.functions.length; i++) {
var f = bc.functions[i];
b+= "<a href='#bc-"+i+"'>";
b+= html_escape((f.name) ? f.name : ("<#"+i+">"));
b+= "</a> ";
}
b += "</p>";
for (i=0; i<bc.functions.length; i++) {
var f = bc.functions[i];
b += "<h2><a id='bc-"+i+"'>Function #"+f.id;
if (f.name) b += " "+html_escape(f.name);
b += "</a>";
b += " ("+f.nargs+" args; max stack depth="+f.max_stack+")</h2>\n";
b += "<pre>"+html_escape(bc.decompile(f.id))+"</pre>";
}
fill('bytecode', 'Compiled Bytecode', b, 1/*no escape*/);
/* Interpreter test! */
frame = binterp.make_top_level_frame.call(/*this: */{/*this*/} ,
/* args:*/ isource);
result = binterp.binterp(bc, 0, frame);
fill('isource', 'Input to interpreted compiler', isource);
if (typeof(result) != "string") result = ""+JSON.stringify(result);
fill('result', 'Result from interpreter', result);
}
}
if (!CATCH_ERRORS) {
do_it();
} else {
try {
do_it();
} catch (e) {
fill('errors', 'Error!',
JSON.stringify(e, ['name', 'message', 'from', 'to',
'key', 'value', 'arity', 'first',
'second', 'third', 'fourth'], 4));
}
}
});