From 52975c1697da119c8a9746106d2035b6fd9dbf90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Fri, 28 Jun 2024 00:10:18 +0200 Subject: [PATCH] feat: add string comparison support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché --- pkg/interpreter/interpreter.go | 43 +++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/pkg/interpreter/interpreter.go b/pkg/interpreter/interpreter.go index 0cd0c19..fdaa9ee 100644 --- a/pkg/interpreter/interpreter.go +++ b/pkg/interpreter/interpreter.go @@ -121,24 +121,35 @@ func (intr *treeInterpreter) execute(node parsing.ASTNode, value interface{}, fu case parsing.TOKNE: return !util.ObjsEqual(left, right), nil } - leftNum, ok := util.ToNumber(left) - if !ok { - return nil, nil - } - rightNum, ok := util.ToNumber(right) - if !ok { - return nil, nil + if leftStr, ok := left.(string); ok { + if rightStr, ok := right.(string); ok { + switch node.Value { + case parsing.TOKGT: + return leftStr > rightStr, nil + case parsing.TOKGTE: + return leftStr >= rightStr, nil + case parsing.TOKLT: + return leftStr < rightStr, nil + case parsing.TOKLTE: + return leftStr <= rightStr, nil + } + } } - switch node.Value { - case parsing.TOKGT: - return leftNum > rightNum, nil - case parsing.TOKGTE: - return leftNum >= rightNum, nil - case parsing.TOKLT: - return leftNum < rightNum, nil - case parsing.TOKLTE: - return leftNum <= rightNum, nil + if leftNum, ok := util.ToNumber(left); ok { + if rightNum, ok := util.ToNumber(right); ok { + switch node.Value { + case parsing.TOKGT: + return leftNum > rightNum, nil + case parsing.TOKGTE: + return leftNum >= rightNum, nil + case parsing.TOKLT: + return leftNum < rightNum, nil + case parsing.TOKLTE: + return leftNum <= rightNum, nil + } + } } + return nil, nil case parsing.ASTExpRef: return func(data interface{}) (interface{}, error) { return intr.execute(node.Children[0], data, functionCaller)