@@ -37,7 +37,7 @@ export class Parser {
3737 * @param tokens tokens
3838 * @param options parsing options. By default it will exclude comments and include LOC (Line of code)
3939 */
40- parse ( tokens : Token [ ] , name = 'main.jspy' , type : string = 'module' ) : AstBlock {
40+ parse ( tokens : Token [ ] , name = 'main.jspy' , type = 'module' ) : AstBlock {
4141 this . _moduleName = name ;
4242 const ast = { name, type, funcs : [ ] , body : [ ] } as AstBlock ;
4343
@@ -214,7 +214,6 @@ export class Parser {
214214 excepts . push ( except ) ;
215215 }
216216
217-
218217 i ++ ;
219218 }
220219
@@ -229,7 +228,10 @@ export class Parser {
229228 } else if ( getTokenValue ( firstToken ) === 'break' ) {
230229 ast . body . push ( new BreakNode ( ) ) ;
231230 } else if ( getTokenValue ( firstToken ) === 'return' ) {
232- ast . body . push ( new ReturnNode ( this . createExpressionNode ( instruction . tokens . slice ( 1 ) ) , getTokenLoc ( firstToken ) ) ) ;
231+ ast . body . push ( new ReturnNode (
232+ instruction . tokens . length > 1 ? this . createExpressionNode ( instruction . tokens . slice ( 1 ) ) : undefined ,
233+ getTokenLoc ( firstToken ) )
234+ ) ;
233235 } else if ( getTokenValue ( firstToken ) === 'raise' ) {
234236
235237 if ( instruction . tokens . length === 1 ) {
@@ -291,7 +293,7 @@ export class Parser {
291293 const body = { } as AstBlock ; // empty for now
292294 ast . body . push ( new ImportNode ( module , body , undefined , getTokenLoc ( firstToken ) ) )
293295 } else if ( getTokenValue ( firstToken ) === 'from' ) {
294- let importIndex = findTokenValueIndex ( instruction . tokens , v => v === 'import' ) ;
296+ const importIndex = findTokenValueIndex ( instruction . tokens , v => v === 'import' ) ;
295297 if ( importIndex < 0 ) {
296298 throw Error ( `'import' must follow 'from'` ) ;
297299 }
@@ -336,7 +338,7 @@ export class Parser {
336338 }
337339
338340 private groupComparisonOperations ( indexes : number [ ] , tokens : Token [ ] ) : AstNode {
339- let start = 0 ;
341+ const start = 0 ;
340342
341343 let leftNode : AstNode | null = null ;
342344 for ( let i = 0 ; i < indexes . length ; i ++ ) {
@@ -352,7 +354,7 @@ export class Parser {
352354 return leftNode as AstNode ;
353355 }
354356
355- private groupLogicalOperations ( logicOp : number [ ] , tokens : Token [ ] ) {
357+ private groupLogicalOperations ( logicOp : number [ ] , tokens : Token [ ] ) : LogicalOpNode {
356358 let start = 0 ;
357359 const logicItems : LogicalNodeItem [ ] = [ ] ;
358360 for ( let i = 0 ; i < logicOp . length ; i ++ ) {
@@ -477,15 +479,15 @@ export class Parser {
477479 const ops = findOperators ( tokens ) ;
478480 if ( ops . length ) {
479481
480- var prevNode : AstNode | null ;
482+ let prevNode : AstNode | null = null ;
481483 for ( let i = 0 ; i < ops . length ; i ++ ) {
482484 const opIndex = ops [ i ] ;
483485 const op = getTokenValue ( tokens [ opIndex ] ) as Operators ;
484486
485487 let nextOpIndex = i + 1 < ops . length ? ops [ i + 1 ] : null ;
486488 let nextOp = nextOpIndex !== null ? getTokenValue ( tokens [ nextOpIndex ] ) : null ;
487489 if ( nextOpIndex !== null && ( nextOp === '*' || nextOp === '/' ) ) {
488- var rightNode : AstNode | null = null ;
490+ let rightNode : AstNode | null = null ;
489491 // iterate through all continuous '*', '/' operations
490492 do {
491493 const nextOpIndex2 = i + 2 < ops . length ? ops [ i + 2 ] : null ;
@@ -513,7 +515,7 @@ export class Parser {
513515 } else {
514516 const leftSlice = prevNode ? [ ] : this . sliceWithBrackets ( tokens , 0 , opIndex ) ;
515517 const rightSlice = this . sliceWithBrackets ( tokens , opIndex + 1 , nextOpIndex || tokens . length ) ;
516- const left = prevNode || this . createExpressionNode ( leftSlice , prevNode ) ;
518+ const left : AstNode = prevNode || this . createExpressionNode ( leftSlice , prevNode ) ;
517519 const right = this . createExpressionNode ( rightSlice ) ;
518520 prevNode = new BinOpNode ( left , op as ExpressionOperators , right , getTokenLoc ( tokens [ 0 ] ) ) ;
519521 }
0 commit comments