Fix unary node print for conditional expr (#676)

* feat: extract code for compiling equal operator

* fix: fix unary node inside condition node print
This commit is contained in:
zhuliquan
2024-06-23 21:20:41 +02:00
committed by GitHub
parent 0217bda141
commit b74ed56efa
2 changed files with 8 additions and 4 deletions
+7 -4
View File
@@ -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())
+1
View File
@@ -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 {