Skip to content

Commit

Permalink
Fix unary node print for conditional expr (#676)
Browse files Browse the repository at this point in the history
* feat: extract code for compiling equal operator

* fix: fix unary node inside condition node print
  • Loading branch information
zhuliquan authored Jun 23, 2024
1 parent b24c7f3 commit 9713880
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
11 changes: 7 additions & 4 deletions ast/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,16 @@ func (n *ConstantNode) String() string {
}

func (n *UnaryNode) String() string {
op := ""
op := n.Operator
if n.Operator == "not" {
op = fmt.Sprintf("%s ", n.Operator)
} else {
op = fmt.Sprintf("%s", n.Operator)
}
if _, ok := n.Node.(*BinaryNode); ok {
wrap := false
switch n.Node.(type) {
case *BinaryNode, *ConditionalNode:
wrap = true
}
if wrap {
return fmt.Sprintf("%s(%s)", op, n.Node.String())
}
return fmt.Sprintf("%s%s", op, n.Node.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 @@ -77,6 +77,7 @@ func TestPrint(t *testing.T) {
{`(nil ?? 1) > 0`, `(nil ?? 1) > 0`},
{`{("a" + "b"): 42}`, `{("a" + "b"): 42}`},
{`(One == 1 ? true : false) && Two == 2`, `(One == 1 ? true : false) && Two == 2`},
{`not (a == 1 ? b > 1 : b < 2)`, `not (a == 1 ? b > 1 : b < 2)`},
}

for _, tt := range tests {
Expand Down

0 comments on commit 9713880

Please sign in to comment.