3
3
* */
4
4
5
5
import { StmtNS , ExprNS } from "./ast-types" ;
6
+
6
7
type Expr = ExprNS . Expr ;
7
8
type Stmt = StmtNS . Stmt ;
8
9
import { Token } from "./tokenizer" ;
@@ -52,6 +53,7 @@ export interface EstreeLocation {
52
53
53
54
export class Translator implements StmtNS . Visitor < BaseNode > , ExprNS . Visitor < BaseNode > {
54
55
private readonly source : string
56
+
55
57
constructor ( source : string ) {
56
58
this . source = source ;
57
59
}
@@ -70,6 +72,7 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
70
72
const source : string = token . lexeme ;
71
73
return { source, start, end} ;
72
74
}
75
+
73
76
private toEstreeLocation ( stmt : Stmt | Expr ) : EstreeLocation {
74
77
const start : EstreePosition = {
75
78
// Convert zero-based to one-based.
@@ -94,13 +97,15 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
94
97
resolveStmt ( stmt : Stmt ) {
95
98
return stmt . accept ( this ) ;
96
99
}
97
- resolveManyStmt ( stmts : Stmt [ ] ) : Statement [ ] {
100
+
101
+ resolveManyStmt ( stmts : Stmt [ ] ) : Statement [ ] {
98
102
const res = [ ] ;
99
103
for ( const stmt of stmts ) {
100
104
res . push ( this . resolveStmt ( stmt ) )
101
105
}
102
106
return res ;
103
107
}
108
+
104
109
resolveExpr ( expr : Expr ) {
105
110
return expr . accept ( this ) ;
106
111
}
@@ -122,6 +127,7 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
122
127
loc : this . toEstreeLocation ( stmtOrExpr ) ,
123
128
} ;
124
129
}
130
+
125
131
// Token to estree identifier.
126
132
private convertToIdentifier ( name : Token ) : Identifier {
127
133
return {
@@ -270,25 +276,29 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
270
276
loc : this . toEstreeLocation ( stmt ) ,
271
277
} ;
272
278
}
279
+
273
280
visitGlobalStmt ( stmt : StmtNS . Global ) : EmptyStatement {
274
281
return {
275
282
type : 'EmptyStatement' ,
276
283
loc : this . toEstreeLocation ( stmt ) ,
277
284
} ;
278
285
}
286
+
279
287
visitNonLocalStmt ( stmt : StmtNS . NonLocal ) : EmptyStatement {
280
288
return {
281
289
type : 'EmptyStatement' ,
282
290
loc : this . toEstreeLocation ( stmt ) ,
283
291
} ;
284
292
}
293
+
285
294
visitReturnStmt ( stmt : StmtNS . Return ) : ReturnStatement {
286
295
return {
287
296
type : 'ReturnStatement' ,
288
297
argument : stmt . value == null ? null : this . resolveExpr ( stmt . value ) ,
289
298
loc : this . toEstreeLocation ( stmt ) ,
290
299
} ;
291
300
}
301
+
292
302
visitWhileStmt ( stmt : StmtNS . While ) : WhileStatement {
293
303
return {
294
304
type : 'WhileStatement' ,
@@ -297,13 +307,15 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
297
307
loc : this . toEstreeLocation ( stmt ) ,
298
308
}
299
309
}
310
+
300
311
visitSimpleExprStmt ( stmt : StmtNS . SimpleExpr ) : ExpressionStatement {
301
312
return {
302
313
type : 'ExpressionStatement' ,
303
314
expression : this . resolveExpr ( stmt . expression ) ,
304
315
loc : this . toEstreeLocation ( stmt ) ,
305
316
}
306
317
}
318
+
307
319
// @TODO
308
320
visitFromImportStmt ( stmt : StmtNS . FromImport ) : ImportDeclaration {
309
321
const specifiers : ImportSpecifier [ ] = stmt . names . map ( name => {
@@ -324,12 +336,14 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
324
336
}
325
337
}
326
338
}
339
+
327
340
visitContinueStmt ( stmt : StmtNS . Continue ) : ContinueStatement {
328
341
return {
329
342
type : 'ContinueStatement' ,
330
343
loc : this . toEstreeLocation ( stmt ) ,
331
344
}
332
345
}
346
+
333
347
visitBreakStmt ( stmt : StmtNS . Break ) : BreakStatement {
334
348
return {
335
349
type : 'BreakStatement' ,
@@ -359,17 +373,19 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
359
373
loc : this . toEstreeLocation ( expr ) ,
360
374
}
361
375
}
376
+
362
377
// disabled for now
363
378
visitMultiLambdaExpr ( expr : ExprNS . MultiLambda ) : EmptyStatement {
364
379
return {
365
380
type : 'EmptyStatement' ,
366
381
loc : this . toEstreeLocation ( expr ) ,
367
382
}
368
383
}
384
+
369
385
visitUnaryExpr ( expr : ExprNS . Unary ) : UnaryExpression {
370
386
const op = expr . operator . type ;
371
387
let res : UnaryOperator = '-' ;
372
- switch ( op ) {
388
+ switch ( op ) {
373
389
case TokenType . NOT :
374
390
res = '!'
375
391
break ;
@@ -391,14 +407,16 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
391
407
loc : this . toEstreeLocation ( expr ) ,
392
408
}
393
409
}
410
+
394
411
visitGroupingExpr ( expr : ExprNS . Grouping ) : Expression {
395
412
return this . resolveExpr ( expr . expression ) ;
396
413
}
414
+
397
415
visitBinaryExpr ( expr : ExprNS . Binary ) : BinaryExpression {
398
416
const op = expr . operator . type ;
399
417
let res : BinaryOperator = '+' ;
400
418
// To make the type checker happy.
401
- switch ( op ) {
419
+ switch ( op ) {
402
420
case TokenType . PLUS :
403
421
res = '+' ;
404
422
break ;
@@ -429,11 +447,12 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
429
447
loc : this . toEstreeLocation ( expr ) ,
430
448
}
431
449
}
450
+
432
451
visitCompareExpr ( expr : ExprNS . Compare ) : BinaryExpression {
433
452
const op = expr . operator . type ;
434
453
let res : BinaryOperator = '+' ;
435
454
// To make the type checker happy.
436
- switch ( op ) {
455
+ switch ( op ) {
437
456
case TokenType . LESS :
438
457
res = '<' ;
439
458
break ;
@@ -469,11 +488,12 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
469
488
loc : this . toEstreeLocation ( expr ) ,
470
489
}
471
490
}
491
+
472
492
visitBoolOpExpr ( expr : ExprNS . BoolOp ) : LogicalExpression {
473
493
const op = expr . operator . type ;
474
494
let res : LogicalOperator = '||' ;
475
495
// To make the type checker happy.
476
- switch ( op ) {
496
+ switch ( op ) {
477
497
case TokenType . AND :
478
498
res = '&&' ;
479
499
break ;
0 commit comments