Skip to content

Commit

Permalink
Fix printing on conditional/binary nodes
Browse files Browse the repository at this point in the history
Fixes #613
  • Loading branch information
antonmedv committed Apr 16, 2024
1 parent 5804ccb commit 6157395
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
27 changes: 18 additions & 9 deletions ast/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions ast/print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
Expand Down

0 comments on commit 6157395

Please sign in to comment.