@@ -91,7 +91,7 @@ export class Parser {
91
91
const firstToken = instruction . tokens [ 0 ] ;
92
92
const secondToken = instruction . tokens . length > 1 ? instruction . tokens [ 1 ] : null ;
93
93
this . _currentToken = firstToken ;
94
-
94
+
95
95
const logicOpIndexes : number [ ] = [ ] ;
96
96
const comparisonOpIndexs : number [ ] = [ ] ;
97
97
const assignTokenIndexes : number [ ] = [ ] ;
@@ -220,7 +220,7 @@ export class Parser {
220
220
const source = this . createExpressionNode ( assignTokens [ 1 ] ) ;
221
221
ast . body . push ( new AssignNode ( target , source , getTokenLoc ( assignTokens [ 0 ] [ 0 ] ) ) ) ;
222
222
} else if ( findIndexes ( instruction . tokens , OperationTypes . Logical , logicOpIndexes ) ) {
223
- ast . body . push ( this . groupComparisonOperations ( logicOpIndexes , instruction ) ) ;
223
+ ast . body . push ( this . groupLogicalOperations ( logicOpIndexes , instruction ) ) ;
224
224
} else if ( findIndexes ( instruction . tokens , OperationTypes . Comparison , comparisonOpIndexs ) ) {
225
225
ast . body . push ( this . groupComparisonOperations ( comparisonOpIndexs , instruction ) ) ;
226
226
} else {
@@ -230,25 +230,26 @@ export class Parser {
230
230
}
231
231
}
232
232
233
+ private sliceWithBrackets ( a : Token [ ] , begin : number , end : number ) : Token [ ] {
234
+ // if expression is in brackets, then we need clean brackets
235
+ if ( getTokenValue ( a [ begin ] ) === '(' ) {
236
+ begin ++ ;
237
+ end -- ;
238
+ }
239
+
240
+ return a . slice ( begin , end ) ;
241
+ }
242
+
233
243
private groupComparisonOperations ( indexes : number [ ] , instruction : InstructionLine ) : AstNode {
234
244
let start = 0 ;
235
- const slice = ( a : Token [ ] , begin : number , end : number ) : Token [ ] => {
236
- // if expression is in brackets, then we need clean brackets
237
- if ( getTokenValue ( a [ begin ] ) === '(' ) {
238
- begin ++ ;
239
- end -- ;
240
- }
241
-
242
- return a . slice ( begin , end ) ;
243
- }
244
245
245
246
let leftNode : AstNode | null = null ;
246
247
for ( let i = 0 ; i < indexes . length ; i ++ ) {
247
248
const opToken = getTokenValue ( instruction . tokens [ indexes [ i ] ] ) as ComparisonOperators ;
248
- leftNode = ( leftNode ) ? leftNode : this . createExpressionNode ( slice ( instruction . tokens , start , indexes [ i ] ) )
249
+ leftNode = ( leftNode ) ? leftNode : this . createExpressionNode ( this . sliceWithBrackets ( instruction . tokens , start , indexes [ i ] ) )
249
250
250
251
const endInd = ( i + 1 < indexes . length ) ? indexes [ i + 1 ] : instruction . tokens . length ;
251
- const rightNode = this . createExpressionNode ( slice ( instruction . tokens , indexes [ i ] + 1 , endInd ) )
252
+ const rightNode = this . createExpressionNode ( this . sliceWithBrackets ( instruction . tokens , indexes [ i ] + 1 , endInd ) )
252
253
253
254
leftNode = new BinOpNode ( leftNode , opToken , rightNode , getTokenLoc ( instruction . tokens [ 0 ] ) ) ;
254
255
}
@@ -261,7 +262,7 @@ export class Parser {
261
262
const logicItems : LogicalNodeItem [ ] = [ ] ;
262
263
for ( let i = 0 ; i < logicOp . length ; i ++ ) {
263
264
const opToken = instruction . tokens [ logicOp [ i ] ] ;
264
- const logicalSlice = instruction . tokens . slice ( start , logicOp [ i ] ) ;
265
+ const logicalSlice = this . sliceWithBrackets ( instruction . tokens , start , logicOp [ i ] ) ;
265
266
logicItems . push ( {
266
267
node : this . createExpressionNode ( logicalSlice ) ,
267
268
op : getTokenValue ( opToken ) as LogicalOperators
@@ -271,7 +272,7 @@ export class Parser {
271
272
}
272
273
273
274
logicItems . push ( {
274
- node : this . createExpressionNode ( instruction . tokens . slice ( start ) )
275
+ node : this . createExpressionNode ( this . sliceWithBrackets ( instruction . tokens , start , instruction . tokens . length ) )
275
276
} as LogicalNodeItem ) ;
276
277
277
278
const lop = new LogicalOpNode ( logicItems , getTokenLoc ( instruction . tokens [ 0 ] ) ) ;
@@ -370,16 +371,6 @@ export class Parser {
370
371
// create arithmetic expression
371
372
const ops = findOperators ( tokens ) ;
372
373
if ( ops . length ) {
373
- // create binary node here
374
- const slice = ( a : Token [ ] , begin : number , end : number ) : Token [ ] => {
375
- // if expression is in brackets, then we need clean brackets
376
- if ( getTokenValue ( a [ begin ] ) === '(' ) {
377
- begin ++ ;
378
- end -- ;
379
- }
380
-
381
- return a . slice ( begin , end ) ;
382
- }
383
374
384
375
var prevNode : AstNode | null ;
385
376
for ( let i = 0 ; i < ops . length ; i ++ ) {
@@ -394,8 +385,8 @@ export class Parser {
394
385
do {
395
386
const nextOpIndex2 = i + 2 < ops . length ? ops [ i + 2 ] : null ;
396
387
397
- const leftSlice2 = slice ( tokens , opIndex + 1 , nextOpIndex ) ;
398
- const rightSlice2 = slice ( tokens , nextOpIndex + 1 , nextOpIndex2 || tokens . length ) ;
388
+ const leftSlice2 = this . sliceWithBrackets ( tokens , opIndex + 1 , nextOpIndex ) ;
389
+ const rightSlice2 = this . sliceWithBrackets ( tokens , nextOpIndex + 1 , nextOpIndex2 || tokens . length ) ;
399
390
400
391
const left2 = this . createExpressionNode ( leftSlice2 ) ;
401
392
const right2 = this . createExpressionNode ( rightSlice2 ) ;
@@ -409,14 +400,14 @@ export class Parser {
409
400
410
401
// add up result
411
402
if ( prevNode === null ) {
412
- const leftSlice = slice ( tokens , 0 , opIndex ) ;
403
+ const leftSlice = this . sliceWithBrackets ( tokens , 0 , opIndex ) ;
413
404
prevNode = this . createExpressionNode ( leftSlice ) ;
414
405
}
415
406
prevNode = new BinOpNode ( prevNode , op as ExpressionOperators , rightNode , getTokenLoc ( tokens [ 0 ] ) )
416
407
417
408
} else {
418
- const leftSlice = prevNode ? [ ] : slice ( tokens , 0 , opIndex ) ;
419
- const rightSlice = slice ( tokens , opIndex + 1 , nextOpIndex || tokens . length ) ;
409
+ const leftSlice = prevNode ? [ ] : this . sliceWithBrackets ( tokens , 0 , opIndex ) ;
410
+ const rightSlice = this . sliceWithBrackets ( tokens , opIndex + 1 , nextOpIndex || tokens . length ) ;
420
411
const left = prevNode || this . createExpressionNode ( leftSlice , prevNode ) ;
421
412
const right = this . createExpressionNode ( rightSlice ) ;
422
413
prevNode = new BinOpNode ( left , op as ExpressionOperators , right , getTokenLoc ( tokens [ 0 ] ) ) ;
0 commit comments