Skip to content

Commit b80b3a3

Browse files
committed
fix(expr): fix priority
Change-Id: I5e7601b9a7ed623021282f14ff512e92247223be
1 parent 78680a1 commit b80b3a3

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

expr.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,21 @@ func (p *Expr) checkSyntax() error {
189189
**/
190190

191191
func sortPriority(e ExprNode) {
192+
for subSortPriority(e) {
193+
}
194+
}
195+
196+
func subSortPriority(e ExprNode) bool {
192197
if e == nil {
193-
return
198+
return false
194199
}
195-
sortPriority(e.LeftOperand())
196-
sortPriority(e.RightOperand())
200+
leftChanged := subSortPriority(e.LeftOperand())
201+
rightChanged := subSortPriority(e.RightOperand())
197202
if getPriority(e) > getPriority(e.LeftOperand()) {
198203
leftOperandToParent(e)
204+
return true
199205
}
206+
return leftChanged || rightChanged
200207
}
201208

202209
func getPriority(e ExprNode) (i int) {

expr_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ func TestPriority(t *testing.T) {
136136
expr string
137137
val interface{}
138138
}{
139+
{expr: "false||true&&8==8", val: true},
139140
{expr: "1+2>5-4", val: true},
140141
{expr: "1+2*4/2", val: 5.0},
141142
{expr: "(true||false)&&false||false", val: false},

spec_operator.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
package tagexpr
1616

17-
import "math"
17+
import (
18+
"math"
19+
)
1820

1921
// --------------------------- Operator ---------------------------
2022

@@ -212,7 +214,7 @@ type andExprNode struct{ exprBackground }
212214
func newAndExprNode() ExprNode { return &andExprNode{} }
213215

214216
func (ae *andExprNode) Run(currField string, tagExpr *TagExpr) interface{} {
215-
for _, e := range []ExprNode{ae.leftOperand, ae.rightOperand} {
217+
for _, e := range [2]ExprNode{ae.leftOperand, ae.rightOperand} {
216218
switch r := e.Run(currField, tagExpr).(type) {
217219
case float64:
218220
if r == 0 {
@@ -240,7 +242,7 @@ type orExprNode struct{ exprBackground }
240242
func newOrExprNode() ExprNode { return &orExprNode{} }
241243

242244
func (oe *orExprNode) Run(currField string, tagExpr *TagExpr) interface{} {
243-
for _, e := range []ExprNode{oe.leftOperand, oe.rightOperand} {
245+
for _, e := range [2]ExprNode{oe.leftOperand, oe.rightOperand} {
244246
switch r := e.Run(currField, tagExpr).(type) {
245247
case float64:
246248
if r != 0 {

0 commit comments

Comments
 (0)