From 715e2b4be2d839e2bfb550b144074cf3952ecac3 Mon Sep 17 00:00:00 2001 From: zhuliquan Date: Thu, 28 Mar 2024 00:10:08 +0800 Subject: [PATCH 1/2] feat: extract code for compiling equal operator --- compiler/compiler.go | 48 ++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/compiler/compiler.go b/compiler/compiler.go index 808b53c9b..a38d977d5 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -395,34 +395,12 @@ func (c *compiler) UnaryNode(node *ast.UnaryNode) { } func (c *compiler) BinaryNode(node *ast.BinaryNode) { - l := kind(node.Left) - r := kind(node.Right) - - leftIsSimple := isSimpleType(node.Left) - rightIsSimple := isSimpleType(node.Right) - leftAndRightAreSimple := leftIsSimple && rightIsSimple - switch node.Operator { case "==": - c.compile(node.Left) - c.derefInNeeded(node.Left) - c.compile(node.Right) - c.derefInNeeded(node.Right) - - if l == r && l == reflect.Int && leftAndRightAreSimple { - c.emit(OpEqualInt) - } else if l == r && l == reflect.String && leftAndRightAreSimple { - c.emit(OpEqualString) - } else { - c.emit(OpEqual) - } + c.equalBinaryNode(node) case "!=": - c.compile(node.Left) - c.derefInNeeded(node.Left) - c.compile(node.Right) - c.derefInNeeded(node.Right) - c.emit(OpEqual) + c.equalBinaryNode(node) c.emit(OpNot) case "or", "||": @@ -580,6 +558,28 @@ func (c *compiler) BinaryNode(node *ast.BinaryNode) { } } +func (c *compiler) equalBinaryNode(node *ast.BinaryNode) { + l := kind(node.Left) + r := kind(node.Right) + + leftIsSimple := isSimpleType(node.Left) + rightIsSimple := isSimpleType(node.Right) + leftAndRightAreSimple := leftIsSimple && rightIsSimple + + c.compile(node.Left) + c.derefInNeeded(node.Left) + c.compile(node.Right) + c.derefInNeeded(node.Right) + + if l == r && l == reflect.Int && leftAndRightAreSimple { + c.emit(OpEqualInt) + } else if l == r && l == reflect.String && leftAndRightAreSimple { + c.emit(OpEqualString) + } else { + c.emit(OpEqual) + } +} + func isSimpleType(node ast.Node) bool { if node == nil { return false From 309dbf71d8018af4aa9c74622a37b5b5cebf7607 Mon Sep 17 00:00:00 2001 From: zhuliquan Date: Mon, 24 Jun 2024 00:31:25 +0800 Subject: [PATCH 2/2] fix: fix unary node inside condition node print --- ast/print.go | 11 +++++++---- ast/print_test.go | 1 + 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/ast/print.go b/ast/print.go index f59377153..b79048b2c 100644 --- a/ast/print.go +++ b/ast/print.go @@ -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()) diff --git a/ast/print_test.go b/ast/print_test.go index 51edd63f5..a4b20b0a1 100644 --- a/ast/print_test.go +++ b/ast/print_test.go @@ -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 {