File tree 4 files changed +100
-0
lines changed
4 files changed +100
-0
lines changed Original file line number Diff line number Diff line change @@ -1237,6 +1237,26 @@ func TestExpr(t *testing.T) {
1237
1237
`[nil, 3, 4]?.[0]?.[1]` ,
1238
1238
nil ,
1239
1239
},
1240
+ {
1241
+ `1 > 2 < 3` ,
1242
+ false ,
1243
+ },
1244
+ {
1245
+ `1 < 2 < 3` ,
1246
+ true ,
1247
+ },
1248
+ {
1249
+ `1 < 2 < 3 > 4` ,
1250
+ false ,
1251
+ },
1252
+ {
1253
+ `1 < 2 < 3 > 2` ,
1254
+ true ,
1255
+ },
1256
+ {
1257
+ `1 < 2 < 3 == true` ,
1258
+ true ,
1259
+ },
1240
1260
}
1241
1261
1242
1262
for _ , tt := range tests {
Original file line number Diff line number Diff line change @@ -54,3 +54,7 @@ var Binary = map[string]Operator{
54
54
"^" : {100 , Right },
55
55
"??" : {500 , Left },
56
56
}
57
+
58
+ func IsComparison (op string ) bool {
59
+ return op == "<" || op == ">" || op == ">=" || op == "<="
60
+ }
Original file line number Diff line number Diff line change @@ -164,6 +164,11 @@ func (p *parser) parseExpression(precedence int) Node {
164
164
break
165
165
}
166
166
167
+ if operator .IsComparison (opToken .Value ) {
168
+ nodeLeft = p .parseComparison (nodeLeft , opToken , op .Precedence )
169
+ goto next
170
+ }
171
+
167
172
var nodeRight Node
168
173
if op .Associativity == operator .Left {
169
174
nodeRight = p .parseExpression (op .Precedence + 1 )
@@ -685,3 +690,34 @@ func (p *parser) parsePostfixExpression(node Node) Node {
685
690
}
686
691
return node
687
692
}
693
+
694
+ func (p * parser ) parseComparison (left Node , token Token , precedence int ) Node {
695
+ var rootNode Node
696
+ for {
697
+ comparator := p .parseExpression (precedence + 1 )
698
+ cmpNode := & BinaryNode {
699
+ Operator : token .Value ,
700
+ Left : left ,
701
+ Right : comparator ,
702
+ }
703
+ cmpNode .SetLocation (token .Location )
704
+ if rootNode == nil {
705
+ rootNode = cmpNode
706
+ } else {
707
+ rootNode = & BinaryNode {
708
+ Operator : "&&" ,
709
+ Left : rootNode ,
710
+ Right : cmpNode ,
711
+ }
712
+ rootNode .SetLocation (token .Location )
713
+ }
714
+
715
+ left = comparator
716
+ token = p .current
717
+ if ! (token .Is (Operator ) && operator .IsComparison (token .Value ) && p .err == nil ) {
718
+ break
719
+ }
720
+ p .next ()
721
+ }
722
+ return rootNode
723
+ }
Original file line number Diff line number Diff line change @@ -531,6 +531,46 @@ world`},
531
531
To : & IntegerNode {Value : 3 },
532
532
},
533
533
},
534
+ {
535
+ `1 < 2 > 3` ,
536
+ & BinaryNode {
537
+ Operator : "&&" ,
538
+ Left : & BinaryNode {
539
+ Operator : "<" ,
540
+ Left : & IntegerNode {Value : 1 },
541
+ Right : & IntegerNode {Value : 2 },
542
+ },
543
+ Right : & BinaryNode {
544
+ Operator : ">" ,
545
+ Left : & IntegerNode {Value : 2 },
546
+ Right : & IntegerNode {Value : 3 },
547
+ },
548
+ },
549
+ },
550
+ {
551
+ `1 < 2 < 3 < 4` ,
552
+ & BinaryNode {
553
+ Operator : "&&" ,
554
+ Left : & BinaryNode {
555
+ Operator : "&&" ,
556
+ Left : & BinaryNode {
557
+ Operator : "<" ,
558
+ Left : & IntegerNode {Value : 1 },
559
+ Right : & IntegerNode {Value : 2 },
560
+ },
561
+ Right : & BinaryNode {
562
+ Operator : "<" ,
563
+ Left : & IntegerNode {Value : 2 },
564
+ Right : & IntegerNode {Value : 3 },
565
+ },
566
+ },
567
+ Right : & BinaryNode {
568
+ Operator : "<" ,
569
+ Left : & IntegerNode {Value : 3 },
570
+ Right : & IntegerNode {Value : 4 },
571
+ },
572
+ },
573
+ },
534
574
}
535
575
for _ , test := range tests {
536
576
t .Run (test .input , func (t * testing.T ) {
You can’t perform that action at this time.
0 commit comments