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 {
5858}
5959
6060func (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 ())
6466 } else {
65- left = n .Left .String ()
67+ lhs = n .Left .String ()
6668 }
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 ())
6973 } else {
70- right = n .Right .String ()
74+ rhs = n .Right .String ()
7175 }
72- return fmt .Sprintf ("%s %s %s" , left , n .Operator , right )
76+
77+ return fmt .Sprintf ("%s %s %s" , lhs , n .Operator , rhs )
7378}
7479
7580func (n * ChainNode ) String () string {
Original file line number Diff line number Diff line change @@ -3,10 +3,11 @@ package ast_test
33import (
44 "testing"
55
6- "github.com/antonmedv/expr/ast"
7- "github.com/antonmedv/expr/parser"
86 "github.com/stretchr/testify/assert"
97 "github.com/stretchr/testify/require"
8+
9+ "github.com/antonmedv/expr/ast"
10+ "github.com/antonmedv/expr/parser"
1011)
1112
1213func TestPrint (t * testing.T ) {
@@ -67,6 +68,7 @@ func TestPrint(t *testing.T) {
6768 {`a[1:]` , `a[1:]` },
6869 {`a[1:]` , `a[1:]` },
6970 {`a[:]` , `a[:]` },
71+ {`(nil ?? 1) > 0` , `(nil ?? 1) > 0` },
7072 }
7173
7274 for _ , tt := range tests {
You can’t perform that action at this time.
0 commit comments