forked from ucsd-cse231-w21/chocopy-wasm-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.ts
528 lines (494 loc) · 14.6 KB
/
parser.ts
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
import {parser} from "lezer-python";
import { TreeCursor} from "lezer-tree";
import { Program, Expr, Stmt, UniOp, BinOp, Parameter, Type, FunDef, VarInit, Class, Literal } from "./ast";
import { NUM, BOOL, NONE, CLASS } from "./utils";
export function traverseLiteral(c : TreeCursor, s : string) : Literal {
switch(c.type.name) {
case "Number":
return {
tag: "num",
value: Number(s.substring(c.from, c.to))
}
case "Boolean":
return {
tag: "bool",
value: s.substring(c.from, c.to) === "True"
}
case "None":
return {
tag: "none"
}
default:
throw new Error("Not literal")
}
}
export function traverseExpr(c : TreeCursor, s : string) : Expr<null> {
switch(c.type.name) {
case "Number":
case "Boolean":
case "None":
return {
tag: "literal",
value: traverseLiteral(c, s)
}
case "VariableName":
return {
tag: "id",
name: s.substring(c.from, c.to)
}
case "CallExpression":
c.firstChild();
const callExpr = traverseExpr(c, s);
c.nextSibling(); // go to arglist
let args = traverseArguments(c, s);
c.parent(); // pop CallExpression
if (callExpr.tag === "lookup") {
return {
tag: "method-call",
obj: callExpr.obj,
method: callExpr.field,
arguments: args
}
} else if (callExpr.tag === "id") {
const callName = callExpr.name;
var expr : Expr<null>;
if (callName === "print" || callName === "abs") {
expr = {
tag: "builtin1",
name: callName,
arg: args[0]
};
} else if (callName === "max" || callName === "min" || callName === "pow") {
expr = {
tag: "builtin2",
name: callName,
left: args[0],
right: args[1]
}
}
else {
expr = { tag: "call", name: callName, arguments: args};
}
return expr;
} else {
throw new Error("Unknown target while parsing assignment");
}
case "BinaryExpression":
c.firstChild(); // go to lhs
const lhsExpr = traverseExpr(c, s);
c.nextSibling(); // go to op
var opStr = s.substring(c.from, c.to);
var op;
switch(opStr) {
case "+":
op = BinOp.Plus;
break;
case "-":
op = BinOp.Minus;
break;
case "*":
op = BinOp.Mul;
break;
case "//":
op = BinOp.IDiv;
break;
case "%":
op = BinOp.Mod;
break
case "==":
op = BinOp.Eq;
break;
case "!=":
op = BinOp.Neq;
break;
case "<=":
op = BinOp.Lte;
break;
case ">=":
op = BinOp.Gte;
break;
case "<":
op = BinOp.Lt;
break;
case ">":
op = BinOp.Gt;
break;
case "is":
op = BinOp.Is;
break;
case "and":
op = BinOp.And;
break;
case "or":
op = BinOp.Or;
break;
default:
throw new Error("Could not parse op at " + c.from + " " + c.to + ": " + s.substring(c.from, c.to))
}
c.nextSibling(); // go to rhs
const rhsExpr = traverseExpr(c, s);
c.parent();
return {
tag: "binop",
op: op,
left: lhsExpr,
right: rhsExpr
}
case "ParenthesizedExpression":
c.firstChild(); // Focus on (
c.nextSibling(); // Focus on inside
var expr = traverseExpr(c, s);
c.parent();
return expr;
case "UnaryExpression":
c.firstChild(); // Focus on op
var opStr = s.substring(c.from, c.to);
var op;
switch(opStr) {
case "-":
op = UniOp.Neg;
break;
case "not":
op = UniOp.Not;
break;
default:
throw new Error("Could not parse op at " + c.from + " " + c.to + ": " + s.substring(c.from, c.to))
}
c.nextSibling(); // go to expr
var expr = traverseExpr(c, s);
c.parent();
return {
tag: "uniop",
op: op,
expr: expr
}
case "MemberExpression":
c.firstChild(); // Focus on object
var objExpr = traverseExpr(c, s);
c.nextSibling(); // Focus on .
c.nextSibling(); // Focus on property
var propName = s.substring(c.from, c.to);
c.parent();
return {
tag: "lookup",
obj: objExpr,
field: propName
}
case "self":
return {
tag: "id",
name: "self"
};
default:
throw new Error("Could not parse expr at " + c.from + " " + c.to + ": " + s.substring(c.from, c.to));
}
}
export function traverseArguments(c : TreeCursor, s : string) : Array<Expr<null>> {
c.firstChild(); // Focuses on open paren
const args = [];
c.nextSibling();
while(c.type.name !== ")") {
let expr = traverseExpr(c, s);
args.push(expr);
c.nextSibling(); // Focuses on either "," or ")"
c.nextSibling(); // Focuses on a VariableName
}
c.parent(); // Pop to ArgList
return args;
}
export function traverseStmt(c : TreeCursor, s : string) : Stmt<null> {
switch(c.node.type.name) {
case "ReturnStatement":
c.firstChild(); // Focus return keyword
var value : Expr<null>;
if (c.nextSibling()) // Focus expression
value = traverseExpr(c, s);
else
value = { tag: "literal", value: { tag: "none" } };
c.parent();
return { tag: "return", value };
case "AssignStatement":
c.firstChild(); // go to name
const target = traverseExpr(c, s);
c.nextSibling(); // go to equals
c.nextSibling(); // go to value
var value = traverseExpr(c, s);
c.parent();
if (target.tag === "lookup") {
return {
tag: "field-assign",
obj: target.obj,
field: target.field,
value: value
}
} else if (target.tag === "id") {
return {
tag: "assign",
name: target.name,
value: value
}
} else {
throw new Error("Unknown target while parsing assignment");
}
case "ExpressionStatement":
c.firstChild();
const expr = traverseExpr(c, s);
c.parent(); // pop going into stmt
return { tag: "expr", expr: expr }
// case "FunctionDefinition":
// c.firstChild(); // Focus on def
// c.nextSibling(); // Focus on name of function
// var name = s.substring(c.from, c.to);
// c.nextSibling(); // Focus on ParamList
// var parameters = traverseParameters(c, s)
// c.nextSibling(); // Focus on Body or TypeDef
// let ret : Type = NONE;
// if(c.type.name === "TypeDef") {
// c.firstChild();
// ret = traverseType(c, s);
// c.parent();
// }
// c.firstChild(); // Focus on :
// var body = [];
// while(c.nextSibling()) {
// body.push(traverseStmt(c, s));
// }
// console.log("Before pop to body: ", c.type.name);
// c.parent(); // Pop to Body
// console.log("Before pop to def: ", c.type.name);
// c.parent(); // Pop to FunctionDefinition
// return {
// tag: "fun",
// name, parameters, body, ret
// }
case "IfStatement":
c.firstChild(); // Focus on if
c.nextSibling(); // Focus on cond
var cond = traverseExpr(c, s);
// console.log("Cond:", cond);
c.nextSibling(); // Focus on : thn
c.firstChild(); // Focus on :
var thn = [];
while(c.nextSibling()) { // Focus on thn stmts
thn.push(traverseStmt(c,s));
}
// console.log("Thn:", thn);
c.parent();
c.nextSibling(); // Focus on else
c.nextSibling(); // Focus on : els
c.firstChild(); // Focus on :
var els = [];
while(c.nextSibling()) { // Focus on els stmts
els.push(traverseStmt(c, s));
}
c.parent();
c.parent();
return {
tag: "if",
cond: cond,
thn: thn,
els: els
}
case "WhileStatement":
c.firstChild(); // Focus on while
c.nextSibling(); // Focus on condition
var cond = traverseExpr(c, s);
c.nextSibling(); // Focus on body
var body = [];
c.firstChild(); // Focus on :
while(c.nextSibling()) {
body.push(traverseStmt(c, s));
}
c.parent();
c.parent();
return {
tag: "while",
cond,
body
}
case "PassStatement":
return { tag: "pass" }
default:
throw new Error("Could not parse stmt at " + c.node.from + " " + c.node.to + ": " + s.substring(c.from, c.to));
}
}
export function traverseType(c : TreeCursor, s : string) : Type {
// For now, always a VariableName
let name = s.substring(c.from, c.to);
switch(name) {
case "int": return NUM;
case "bool": return BOOL;
default: return CLASS(name);
}
}
export function traverseParameters(c : TreeCursor, s : string) : Array<Parameter<null>> {
c.firstChild(); // Focuses on open paren
const parameters = [];
c.nextSibling(); // Focuses on a VariableName
while(c.type.name !== ")") {
let name = s.substring(c.from, c.to);
c.nextSibling(); // Focuses on "TypeDef", hopefully, or "," if mistake
let nextTagName = c.type.name; // NOTE(joe): a bit of a hack so the next line doesn't if-split
if(nextTagName !== "TypeDef") { throw new Error("Missed type annotation for parameter " + name)};
c.firstChild(); // Enter TypeDef
c.nextSibling(); // Focuses on type itself
let typ = traverseType(c, s);
c.parent();
c.nextSibling(); // Move on to comma or ")"
parameters.push({name, type: typ});
c.nextSibling(); // Focuses on a VariableName
}
c.parent(); // Pop to ParamList
return parameters;
}
export function traverseVarInit(c : TreeCursor, s : string) : VarInit<null> {
c.firstChild(); // go to name
var name = s.substring(c.from, c.to);
c.nextSibling(); // go to : type
if(c.type.name !== "TypeDef") {
c.parent();
throw Error("invalid variable init");
}
c.firstChild(); // go to :
c.nextSibling(); // go to type
const type = traverseType(c, s);
c.parent();
c.nextSibling(); // go to =
c.nextSibling(); // go to value
var value = traverseLiteral(c, s);
c.parent();
return { name, type, value }
}
export function traverseFunDef(c : TreeCursor, s : string) : FunDef<null> {
c.firstChild(); // Focus on def
c.nextSibling(); // Focus on name of function
var name = s.substring(c.from, c.to);
c.nextSibling(); // Focus on ParamList
var parameters = traverseParameters(c, s)
c.nextSibling(); // Focus on Body or TypeDef
let ret : Type = NONE;
if(c.type.name === "TypeDef") {
c.firstChild();
ret = traverseType(c, s);
c.parent();
c.nextSibling();
}
c.firstChild(); // Focus on :
var inits = [];
var body = [];
var hasChild = c.nextSibling();
while(hasChild) {
if (isVarInit(c, s)) {
inits.push(traverseVarInit(c, s));
} else {
break;
}
hasChild = c.nextSibling();
}
while(hasChild) {
body.push(traverseStmt(c, s));
hasChild = c.nextSibling();
}
// console.log("Before pop to body: ", c.type.name);
c.parent(); // Pop to Body
// console.log("Before pop to def: ", c.type.name);
c.parent(); // Pop to FunctionDefinition
return { name, parameters, ret, inits, body }
}
export function traverseClass(c : TreeCursor, s : string) : Class<null> {
const fields : Array<VarInit<null>> = [];
const methods : Array<FunDef<null>> = [];
c.firstChild();
c.nextSibling(); // Focus on class name
const className = s.substring(c.from, c.to);
c.nextSibling(); // Focus on arglist/superclass
c.nextSibling(); // Focus on body
c.firstChild(); // Focus colon
while(c.nextSibling()) { // Focuses first field
if (isVarInit(c, s)) {
fields.push(traverseVarInit(c, s));
} else if (isFunDef(c, s)) {
methods.push(traverseFunDef(c, s));
} else {
throw new Error(`Could not parse the body of class: ${className}` );
}
}
c.parent();
c.parent();
if (!methods.find(method => method.name === "__init__")) {
methods.push({ name: "__init__", parameters: [{ name: "self", type: CLASS(className) }], ret: NONE, inits: [], body: [] });
}
return {
name: className,
fields,
methods
};
}
export function traverseDefs(c : TreeCursor, s : string) : [Array<VarInit<null>>, Array<FunDef<null>>, Array<Class<null>>] {
const inits : Array<VarInit<null>> = [];
const funs : Array<FunDef<null>> = [];
const classes : Array<Class<null>> = [];
while(true) {
if (isVarInit(c, s)) {
inits.push(traverseVarInit(c, s));
} else if (isFunDef(c, s)) {
funs.push(traverseFunDef(c, s));
} else if (isClassDef(c, s)) {
classes.push(traverseClass(c, s));
} else {
return [inits, funs, classes];
}
c.nextSibling();
}
}
export function isVarInit(c : TreeCursor, s : string) : Boolean {
if (c.type.name === "AssignStatement") {
c.firstChild(); // Focus on lhs
c.nextSibling(); // go to : type
const isVar = c.type.name as any === "TypeDef";
c.parent();
return isVar;
} else {
return false;
}
}
export function isFunDef(c : TreeCursor, s : string) : Boolean {
return c.type.name === "FunctionDefinition";
}
export function isClassDef(c : TreeCursor, s : string) : Boolean {
return c.type.name === "ClassDefinition";
}
export function traverse(c : TreeCursor, s : string) : Program<null> {
switch(c.node.type.name) {
case "Script":
const inits : Array<VarInit<null>> = [];
const funs : Array<FunDef<null>> = [];
const classes : Array<Class<null>> = [];
const stmts : Array<Stmt<null>> = [];
var hasChild = c.firstChild();
while(hasChild) {
if (isVarInit(c, s)) {
inits.push(traverseVarInit(c, s));
} else if (isFunDef(c, s)) {
funs.push(traverseFunDef(c, s));
} else if (isClassDef(c, s)) {
classes.push(traverseClass(c, s));
} else {
break;
}
hasChild = c.nextSibling();
}
while(hasChild) {
stmts.push(traverseStmt(c, s));
hasChild = c.nextSibling();
}
c.parent();
return { funs, inits, classes, stmts };
default:
throw new Error("Could not parse program at " + c.node.from + " " + c.node.to);
}
}
export function parse(source : string) : Program<null> {
const t = parser.parse(source);
return traverse(t.cursor(), source);
}