33* */
44
55import { StmtNS , ExprNS } from "./ast-types" ;
6+
67type Expr = ExprNS . Expr ;
78type Stmt = StmtNS . Stmt ;
89import { Token } from "./tokenizer" ;
@@ -52,6 +53,7 @@ export interface EstreeLocation {
5253
5354export class Translator implements StmtNS . Visitor < BaseNode > , ExprNS . Visitor < BaseNode > {
5455 private readonly source : string
56+
5557 constructor ( source : string ) {
5658 this . source = source ;
5759 }
@@ -70,6 +72,7 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
7072 const source : string = token . lexeme ;
7173 return { source, start, end} ;
7274 }
75+
7376 private toEstreeLocation ( stmt : Stmt | Expr ) : EstreeLocation {
7477 const start : EstreePosition = {
7578 // Convert zero-based to one-based.
@@ -94,13 +97,15 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
9497 resolveStmt ( stmt : Stmt ) {
9598 return stmt . accept ( this ) ;
9699 }
97- resolveManyStmt ( stmts : Stmt [ ] ) : Statement [ ] {
100+
101+ resolveManyStmt ( stmts : Stmt [ ] ) : Statement [ ] {
98102 const res = [ ] ;
99103 for ( const stmt of stmts ) {
100104 res . push ( this . resolveStmt ( stmt ) )
101105 }
102106 return res ;
103107 }
108+
104109 resolveExpr ( expr : Expr ) {
105110 return expr . accept ( this ) ;
106111 }
@@ -122,6 +127,7 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
122127 loc : this . toEstreeLocation ( stmtOrExpr ) ,
123128 } ;
124129 }
130+
125131 // Token to estree identifier.
126132 private convertToIdentifier ( name : Token ) : Identifier {
127133 return {
@@ -270,25 +276,29 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
270276 loc : this . toEstreeLocation ( stmt ) ,
271277 } ;
272278 }
279+
273280 visitGlobalStmt ( stmt : StmtNS . Global ) : EmptyStatement {
274281 return {
275282 type : 'EmptyStatement' ,
276283 loc : this . toEstreeLocation ( stmt ) ,
277284 } ;
278285 }
286+
279287 visitNonLocalStmt ( stmt : StmtNS . NonLocal ) : EmptyStatement {
280288 return {
281289 type : 'EmptyStatement' ,
282290 loc : this . toEstreeLocation ( stmt ) ,
283291 } ;
284292 }
293+
285294 visitReturnStmt ( stmt : StmtNS . Return ) : ReturnStatement {
286295 return {
287296 type : 'ReturnStatement' ,
288297 argument : stmt . value == null ? null : this . resolveExpr ( stmt . value ) ,
289298 loc : this . toEstreeLocation ( stmt ) ,
290299 } ;
291300 }
301+
292302 visitWhileStmt ( stmt : StmtNS . While ) : WhileStatement {
293303 return {
294304 type : 'WhileStatement' ,
@@ -297,13 +307,15 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
297307 loc : this . toEstreeLocation ( stmt ) ,
298308 }
299309 }
310+
300311 visitSimpleExprStmt ( stmt : StmtNS . SimpleExpr ) : ExpressionStatement {
301312 return {
302313 type : 'ExpressionStatement' ,
303314 expression : this . resolveExpr ( stmt . expression ) ,
304315 loc : this . toEstreeLocation ( stmt ) ,
305316 }
306317 }
318+
307319 // @TODO
308320 visitFromImportStmt ( stmt : StmtNS . FromImport ) : ImportDeclaration {
309321 const specifiers : ImportSpecifier [ ] = stmt . names . map ( name => {
@@ -324,12 +336,14 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
324336 }
325337 }
326338 }
339+
327340 visitContinueStmt ( stmt : StmtNS . Continue ) : ContinueStatement {
328341 return {
329342 type : 'ContinueStatement' ,
330343 loc : this . toEstreeLocation ( stmt ) ,
331344 }
332345 }
346+
333347 visitBreakStmt ( stmt : StmtNS . Break ) : BreakStatement {
334348 return {
335349 type : 'BreakStatement' ,
@@ -359,17 +373,19 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
359373 loc : this . toEstreeLocation ( expr ) ,
360374 }
361375 }
376+
362377 // disabled for now
363378 visitMultiLambdaExpr ( expr : ExprNS . MultiLambda ) : EmptyStatement {
364379 return {
365380 type : 'EmptyStatement' ,
366381 loc : this . toEstreeLocation ( expr ) ,
367382 }
368383 }
384+
369385 visitUnaryExpr ( expr : ExprNS . Unary ) : UnaryExpression {
370386 const op = expr . operator . type ;
371387 let res : UnaryOperator = '-' ;
372- switch ( op ) {
388+ switch ( op ) {
373389 case TokenType . NOT :
374390 res = '!'
375391 break ;
@@ -391,14 +407,16 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
391407 loc : this . toEstreeLocation ( expr ) ,
392408 }
393409 }
410+
394411 visitGroupingExpr ( expr : ExprNS . Grouping ) : Expression {
395412 return this . resolveExpr ( expr . expression ) ;
396413 }
414+
397415 visitBinaryExpr ( expr : ExprNS . Binary ) : BinaryExpression {
398416 const op = expr . operator . type ;
399417 let res : BinaryOperator = '+' ;
400418 // To make the type checker happy.
401- switch ( op ) {
419+ switch ( op ) {
402420 case TokenType . PLUS :
403421 res = '+' ;
404422 break ;
@@ -429,11 +447,12 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
429447 loc : this . toEstreeLocation ( expr ) ,
430448 }
431449 }
450+
432451 visitCompareExpr ( expr : ExprNS . Compare ) : BinaryExpression {
433452 const op = expr . operator . type ;
434453 let res : BinaryOperator = '+' ;
435454 // To make the type checker happy.
436- switch ( op ) {
455+ switch ( op ) {
437456 case TokenType . LESS :
438457 res = '<' ;
439458 break ;
@@ -469,11 +488,12 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
469488 loc : this . toEstreeLocation ( expr ) ,
470489 }
471490 }
491+
472492 visitBoolOpExpr ( expr : ExprNS . BoolOp ) : LogicalExpression {
473493 const op = expr . operator . type ;
474494 let res : LogicalOperator = '||' ;
475495 // To make the type checker happy.
476- switch ( op ) {
496+ switch ( op ) {
477497 case TokenType . AND :
478498 res = '&&' ;
479499 break ;
0 commit comments