@@ -91,7 +91,7 @@ export class Parser {
9191 const firstToken = instruction . tokens [ 0 ] ;
9292 const secondToken = instruction . tokens . length > 1 ? instruction . tokens [ 1 ] : null ;
9393 this . _currentToken = firstToken ;
94-
94+
9595 const logicOpIndexes : number [ ] = [ ] ;
9696 const comparisonOpIndexs : number [ ] = [ ] ;
9797 const assignTokenIndexes : number [ ] = [ ] ;
@@ -220,7 +220,7 @@ export class Parser {
220220 const source = this . createExpressionNode ( assignTokens [ 1 ] ) ;
221221 ast . body . push ( new AssignNode ( target , source , getTokenLoc ( assignTokens [ 0 ] [ 0 ] ) ) ) ;
222222 } else if ( findIndexes ( instruction . tokens , OperationTypes . Logical , logicOpIndexes ) ) {
223- ast . body . push ( this . groupComparisonOperations ( logicOpIndexes , instruction ) ) ;
223+ ast . body . push ( this . groupLogicalOperations ( logicOpIndexes , instruction ) ) ;
224224 } else if ( findIndexes ( instruction . tokens , OperationTypes . Comparison , comparisonOpIndexs ) ) {
225225 ast . body . push ( this . groupComparisonOperations ( comparisonOpIndexs , instruction ) ) ;
226226 } else {
@@ -230,25 +230,26 @@ export class Parser {
230230 }
231231 }
232232
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+
233243 private groupComparisonOperations ( indexes : number [ ] , instruction : InstructionLine ) : AstNode {
234244 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- }
244245
245246 let leftNode : AstNode | null = null ;
246247 for ( let i = 0 ; i < indexes . length ; i ++ ) {
247248 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 ] ) )
249250
250251 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 ) )
252253
253254 leftNode = new BinOpNode ( leftNode , opToken , rightNode , getTokenLoc ( instruction . tokens [ 0 ] ) ) ;
254255 }
@@ -261,7 +262,7 @@ export class Parser {
261262 const logicItems : LogicalNodeItem [ ] = [ ] ;
262263 for ( let i = 0 ; i < logicOp . length ; i ++ ) {
263264 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 ] ) ;
265266 logicItems . push ( {
266267 node : this . createExpressionNode ( logicalSlice ) ,
267268 op : getTokenValue ( opToken ) as LogicalOperators
@@ -271,7 +272,7 @@ export class Parser {
271272 }
272273
273274 logicItems . push ( {
274- node : this . createExpressionNode ( instruction . tokens . slice ( start ) )
275+ node : this . createExpressionNode ( this . sliceWithBrackets ( instruction . tokens , start , instruction . tokens . length ) )
275276 } as LogicalNodeItem ) ;
276277
277278 const lop = new LogicalOpNode ( logicItems , getTokenLoc ( instruction . tokens [ 0 ] ) ) ;
@@ -370,16 +371,6 @@ export class Parser {
370371 // create arithmetic expression
371372 const ops = findOperators ( tokens ) ;
372373 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- }
383374
384375 var prevNode : AstNode | null ;
385376 for ( let i = 0 ; i < ops . length ; i ++ ) {
@@ -394,8 +385,8 @@ export class Parser {
394385 do {
395386 const nextOpIndex2 = i + 2 < ops . length ? ops [ i + 2 ] : null ;
396387
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 ) ;
399390
400391 const left2 = this . createExpressionNode ( leftSlice2 ) ;
401392 const right2 = this . createExpressionNode ( rightSlice2 ) ;
@@ -409,14 +400,14 @@ export class Parser {
409400
410401 // add up result
411402 if ( prevNode === null ) {
412- const leftSlice = slice ( tokens , 0 , opIndex ) ;
403+ const leftSlice = this . sliceWithBrackets ( tokens , 0 , opIndex ) ;
413404 prevNode = this . createExpressionNode ( leftSlice ) ;
414405 }
415406 prevNode = new BinOpNode ( prevNode , op as ExpressionOperators , rightNode , getTokenLoc ( tokens [ 0 ] ) )
416407
417408 } 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 ) ;
420411 const left = prevNode || this . createExpressionNode ( leftSlice , prevNode ) ;
421412 const right = this . createExpressionNode ( rightSlice ) ;
422413 prevNode = new BinOpNode ( left , op as ExpressionOperators , right , getTokenLoc ( tokens [ 0 ] ) ) ;
0 commit comments