File tree Expand file tree Collapse file tree 2 files changed +17
-10
lines changed Expand file tree Collapse file tree 2 files changed +17
-10
lines changed Original file line number Diff line number Diff line change @@ -58,18 +58,23 @@ func (n *UnaryNode) String() string {
58
58
}
59
59
60
60
func (n * BinaryNode ) String () string {
61
- var left , right string
62
- if b , ok := n .Left .(* BinaryNode ); ok && operator .Less (b .Operator , n .Operator ) {
63
- left = fmt .Sprintf ("(%s)" , n .Left .String ())
61
+ var lhs , rhs string
62
+
63
+ lb , ok := n .Left .(* BinaryNode )
64
+ if ok && (operator .Less (lb .Operator , n .Operator ) || lb .Operator == "??" ) {
65
+ lhs = fmt .Sprintf ("(%s)" , n .Left .String ())
64
66
} else {
65
- left = n .Left .String ()
67
+ lhs = n .Left .String ()
66
68
}
67
- if b , ok := n .Right .(* BinaryNode ); ok && operator .Less (b .Operator , n .Operator ) {
68
- right = fmt .Sprintf ("(%s)" , n .Right .String ())
69
+
70
+ rb , ok := n .Right .(* BinaryNode )
71
+ if ok && operator .Less (rb .Operator , n .Operator ) {
72
+ rhs = fmt .Sprintf ("(%s)" , n .Right .String ())
69
73
} else {
70
- right = n .Right .String ()
74
+ rhs = n .Right .String ()
71
75
}
72
- return fmt .Sprintf ("%s %s %s" , left , n .Operator , right )
76
+
77
+ return fmt .Sprintf ("%s %s %s" , lhs , n .Operator , rhs )
73
78
}
74
79
75
80
func (n * ChainNode ) String () string {
Original file line number Diff line number Diff line change @@ -3,10 +3,11 @@ package ast_test
3
3
import (
4
4
"testing"
5
5
6
- "github.com/antonmedv/expr/ast"
7
- "github.com/antonmedv/expr/parser"
8
6
"github.com/stretchr/testify/assert"
9
7
"github.com/stretchr/testify/require"
8
+
9
+ "github.com/antonmedv/expr/ast"
10
+ "github.com/antonmedv/expr/parser"
10
11
)
11
12
12
13
func TestPrint (t * testing.T ) {
@@ -67,6 +68,7 @@ func TestPrint(t *testing.T) {
67
68
{`a[1:]` , `a[1:]` },
68
69
{`a[1:]` , `a[1:]` },
69
70
{`a[:]` , `a[:]` },
71
+ {`(nil ?? 1) > 0` , `(nil ?? 1) > 0` },
70
72
}
71
73
72
74
for _ , tt := range tests {
You can’t perform that action at this time.
0 commit comments