Fix printing on conditional/binary nodes

Fixes #613
This commit is contained in:
Anton Medvedev
2024-04-16 23:23:12 +02:00
parent c8f7d22462
commit 3ceff1cdb3
3 changed files with 28 additions and 9 deletions
+18 -9
View File
@@ -65,8 +65,7 @@ func (n *BinaryNode) String() string {
var lhs, rhs string
var lwrap, rwrap bool
lb, ok := n.Left.(*BinaryNode)
if ok {
if lb, ok := n.Left.(*BinaryNode); ok {
if operator.Less(lb.Operator, n.Operator) {
lwrap = true
}
@@ -77,9 +76,7 @@ func (n *BinaryNode) String() string {
lwrap = true
}
}
rb, ok := n.Right.(*BinaryNode)
if ok {
if rb, ok := n.Right.(*BinaryNode); ok {
if operator.Less(rb.Operator, n.Operator) {
rwrap = true
}
@@ -88,6 +85,13 @@ func (n *BinaryNode) String() string {
}
}
if _, ok := n.Left.(*ConditionalNode); ok {
lwrap = true
}
if _, ok := n.Right.(*ConditionalNode); ok {
rwrap = true
}
if lwrap {
lhs = fmt.Sprintf("(%s)", n.Left.String())
} else {
@@ -108,20 +112,25 @@ func (n *ChainNode) String() string {
}
func (n *MemberNode) String() string {
node := n.Node.String()
if _, ok := n.Node.(*BinaryNode); ok {
node = fmt.Sprintf("(%s)", node)
}
if n.Optional {
if str, ok := n.Property.(*StringNode); ok && utils.IsValidIdentifier(str.Value) {
return fmt.Sprintf("%s?.%s", n.Node.String(), str.Value)
return fmt.Sprintf("%s?.%s", node, str.Value)
} else {
return fmt.Sprintf("%s?.[%s]", n.Node.String(), n.Property.String())
return fmt.Sprintf("%s?.[%s]", node, n.Property.String())
}
}
if str, ok := n.Property.(*StringNode); ok && utils.IsValidIdentifier(str.Value) {
if _, ok := n.Node.(*PointerNode); ok {
return fmt.Sprintf(".%s", str.Value)
}
return fmt.Sprintf("%s.%s", n.Node.String(), str.Value)
return fmt.Sprintf("%s.%s", node, str.Value)
}
return fmt.Sprintf("%s[%s]", n.Node.String(), n.Property.String())
return fmt.Sprintf("%s[%s]", node, n.Property.String())
}
func (n *SliceNode) String() string {
+1
View File
@@ -72,6 +72,7 @@ func TestPrint(t *testing.T) {
{`a[:]`, `a[:]`},
{`(nil ?? 1) > 0`, `(nil ?? 1) > 0`},
{`{("a" + "b"): 42}`, `{("a" + "b"): 42}`},
{`(One == 1 ? true : false) && Two == 2`, `(One == 1 ? true : false) && Two == 2`},
}
for _, tt := range tests {
+9
View File
@@ -1298,6 +1298,15 @@ func TestExpr(t *testing.T) {
require.NoError(t, err, "eval")
assert.Equal(t, tt.want, got, "eval")
}
{
program, err := expr.Compile(tt.code, expr.Env(mock.Env{}), expr.Optimize(false))
require.NoError(t, err)
code := program.Node().String()
got, err := expr.Eval(code, env)
require.NoError(t, err, code)
assert.Equal(t, tt.want, got, code)
}
})
}
}