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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
PPrint: adt
{
ex: ref Exec;
code: ref Code;
stack: array of string;
sp: int;
};
mkpprint(ex: ref Exec, code: ref Code): ref PPrint
{
return ref PPrint(ex, code, array[4] of string, 0);
}
funcprint(ex: ref Exec, func: ref Ecmascript->Obj): string
{
params := func.call.params;
(nil, name) := str->splitr(func.val.str, ".");
s := "function " + name + "(";
sep := "";
for(i := 0; i < len params; i++){
s += sep + params[i];
sep = ", ";
}
s += "){";
if(func.host != nil)
s += "[host code]";
else
s += "\n" + pprint(ex, func.call.code, " ");
s += "}";
return s;
}
pprint(ex: ref Exec, code: ref Code, indent: string): string
{
pp := ref PPrint(ex, code, array[4] of string, 0);
#for(i:=0; i < code.npc; i++) sys->print("%d: %d\n", i, int code.ops[i]);
s := pstmt(pp, 0, code.npc, indent);
if(pp.sp != 0)
fatal(ex, "pprint stack not balanced");
return s;
}
pstmt(pp: ref PPrint, pc, epc: int, indent: string): string
{
e, e1, e2: string;
c, apc: int;
code := pp.code;
s := "";
while(pc < epc){
op := int code.ops[pc++];
while(op == Llabel){
(pc, c) = getconst(code.ops, pc);
s += code.strs[c] + ":\n";
op = int code.ops[pc++];
}
s += indent;
case op{
Lbreak or
Lcontinue or
Lreturn =>
s += tokname(op);
if(op == Lreturn){
(pc, e) = pexp(pp, pc, code.npc);
s += " " + e;
}
s += ";\n";
Lbreaklab or
Lcontinuelab =>
s += tokname(op);
(pc, c) = getconst(code.ops, pc);
s += " " + code.strs[c] + ";\n";
'{' =>
(pc, apc) = getjmp(code.ops, pc);
s += "{\n" + pstmt(pp, pc, apc, indent+" ") + indent + "}\n";
pc = apc;
Lif or
Lwith or
Lwhile =>
(pc, apc) = getjmp(code.ops, pc);
(pc, e) = pexp(pp, pc, apc);
(pc, apc) = getjmp(code.ops, pc);
s += tokname(op) + "(" + e + "){\n";
s += pstmt(pp, pc, apc, indent+" ");
if(op == Lif){
(pc, apc) = getjmp(code.ops, apc);
if(pc != apc)
s += indent + "}else{\n";
s += pstmt(pp, pc, apc, indent+" ");
}
s += indent + "}\n";
pc = apc;
Ldo =>
(pc, apc) = getjmp(code.ops, pc);
e = pstmt(pp, pc, apc, indent+" ");
(pc, apc) = getjmp(code.ops, apc);
(pc, e1) = pexp(pp, pc, apc);
s += "do{\n" + e + indent + "}(while(" + e1 + ");\n";
pc = apc;
Lfor or
Lforvar or
Lforin or
Lforvarin =>
(pc, apc) = getjmp(code.ops, pc);
(pc, e) = pexp(pp, pc, apc);
(pc, apc) = getjmp(code.ops, pc);
(pc, e1) = pexp(pp, pc, apc);
s += "for(";
if(op == Lforvar || op == Lforvarin)
s += "var ";
s += e;
if(op == Lfor || op == Lforvar){
(pc, apc) = getjmp(code.ops, pc);
(pc, e2) = pexp(pp, pc, apc);
s += "; " + e1 + "; " + e2;
}else
s += " in " + e1;
s += "){\n";
(pc, apc) = getjmp(code.ops, pc);
s += pstmt(pp, pc, apc, indent+" ");
s += indent + "}\n";
pc = apc;
';' =>
s += ";\n";
Lvar =>
(pc, apc) = getjmp(code.ops, pc);
(pc, e) = pexp(pp, pc, apc);
s += "var " + e + ";\n";
Lswitch =>
(pc, apc) = getjmp(code.ops, pc);
(pc, e) = pexp(pp, pc, apc);
s += "switch (" + e + ") {\n";
(pc, apc) = getjmp(code.ops, pc);
(pc, e) = pcaseblk(pp, pc, apc, indent);
s += e + indent + "}\n";
pc = apc;
Lthrow =>
(pc, e) = pexp(pp, pc, code.npc);
s += "throw " + e + "\n";
Ltry =>
s += "try\n";
(pc, apc) = getjmp(code.ops, pc);
s += pstmt(pp, pc, apc, indent+" ");
(pc, apc) = getjmp(code.ops, apc);
if(pc != apc){
(pc, c) = getconst(code.ops, ++pc);
s += "catch(" + code.strs[c] + ")\n";
s += pstmt(pp, pc, apc, indent+" ");
}
(pc, apc) = getjmp(code.ops, apc);
if(pc != apc){
s += "finally\n";
s += pstmt(pp, pc, apc, indent+" ");
}
pc = apc;
* =>
(pc, e) = pexp(pp, pc-1, code.npc);
s += e + ";\n";
}
}
return s;
}
pexp(pp: ref PPrint, pc, epc: int): (int, string)
{
c, apc: int;
s, f, a, a1, a2: string;
code := pp.code;
savesp := pp.sp;
out: while(pc < epc){
case op := int code.ops[pc++]{
Lthis =>
s = "this";
Lid or
Lnum or
Lstr or
Lregexp =>
(pc, c) = getconst(code.ops, pc);
if(op == Lnum)
s = string code.nums[c];
else{
s = code.strs[c];
if(op == Lstr)
s = "\""+escstr(code.strs[c])+"\"";
}
'*' or
'/' or
'%' or
'+' or
'-' or
Llsh or
Lrsh or
Lrshu or
'<' or
'>' or
Lleq or
Lgeq or
Lin or
Linstanceof or
Leq or
Lneq or
Lseq or
Lsne or
'&' or
'^' or
'|' or
'=' or
'.' or
',' or
'[' =>
a2 = ppop(pp);
a1 = ppop(pp);
s = tokname(op);
if(a1[0] == '='){
s += "=";
a1 = a1[1:];
}
if(op == '[')
s = a1 + "[" + a2 + "]";
else{
if(op != '.'){
if(op != ',')
s = " " + s;
s = s + " ";
}
s = a1 + s + a2;
}
Ltypeof or
Ldelete or
Lvoid or
Lnew or
Linc or
Ldec or
Lpreadd or
Lpresub or
'~' or
'!' or
Lpostinc or
Lpostdec =>
a = ppop(pp);
s = tokname(op);
if(op == Lpostinc || op == Lpostdec)
s = a + s;
else{
if(op == Ltypeof || op == Ldelete || op == Lvoid || op == Lnew)
s += " ";
s += a;
}
'(' =>
s = "(";
')' =>
s = ppop(pp);
if(ppop(pp) != "(")
fatal(pp.ex, "unbalanced () in pexp");
s = "(" + s + ")";
Lgetval or
Las =>
continue;
Lasop =>
s = "=" + ppop(pp);
Lcall or
Lnewcall =>
(pc, c) = getconst(code.ops, pc);
a = "";
sep := "";
for(sp := pp.sp-c; sp < pp.sp; sp++){
a += sep + pp.stack[sp];
sep = ", ";
}
pp.sp -= c;
f = ppop(pp);
if(op == Lnewcall)
f = "new " + f;
s = f + "(" + a + ")";
';' =>
break out;
Landand or
Loror or
'?' =>
s = ppop(pp);
(pc, apc) = getjmp(code.ops, pc);
(pc, a1) = pexp(pp, pc, apc);
s += " " + tokname(op) + " " + a1;
if(op == '?'){
(pc, apc) = getjmp(code.ops, pc);
(pc, a2) = pexp(pp, pc, apc);
s += " : "+ a2;
}
* =>
fatal(pp.ex, "pexp: unknown op " + tokname(op));
}
ppush(pp, s);
}
if(savesp == pp.sp)
return (pc, "");
if(savesp != pp.sp-1)
fatal(pp.ex, "unbalanced stack in pexp");
return (pc, ppop(pp));
}
pcaseblk(pp: ref PPrint, pc, epc: int, indent: string): (int, string)
{
code := pp.code;
defpc, clausepc, nextpc, apc: int;
s, a: string;
(pc, defpc) = getjmp(code.ops, pc);
clausepc = pc;
(pc, nextpc) = getjmp(code.ops, pc);
for (; pc < epc; (clausepc, (pc, nextpc)) = (nextpc, getjmp(code.ops, nextpc))) {
if (clausepc == defpc) {
s += indent + "default:\n";
} else {
(pc, apc) = getjmp(code.ops, pc);
(pc, a) = pexp(pp, pc, apc);
s += indent + "case " + a + ":\n";
}
s += pstmt(pp, pc, nextpc, indent+"\t");
}
return (epc, s);
}
ppush(pp: ref PPrint, s: string)
{
if(pp.sp >= len pp.stack){
st := array[2 * len pp.stack] of string;
st[:] = pp.stack;
pp.stack = st;
}
pp.stack[pp.sp++] = s;
}
ppop(pp: ref PPrint): string
{
if(pp.sp == 0)
fatal(pp.ex, "popping too far off the pstack");
return pp.stack[--pp.sp];
}
unescmap := array[128] of
{
'\'' => byte '\'',
'"' => byte '"',
'\\' => byte '\\',
'\b' => byte 'b',
'\u000c' => byte 'f',
'\n' => byte 'n',
'\r' => byte 'r',
'\t' => byte 't',
* => byte 0
};
escstr(s: string): string
{
n := len s;
sb := "";
for(i := 0; i < n; i++){
c := s[i];
if(c < 128 && (e := int unescmap[c])){
sb[len sb] = '\\';
sb[len sb] = e;
}else if(c > 128 || c < 32){
sb += "\\u0000";
for(j := 1; j <= 4; j++){
sb[len sb - j] = "0123456789abcdef"[c & 16rf];
c >>= 4;
}
}else
sb[len sb] = c;
}
return sb;
}
|