From 2cdddf221b3da00ac2a83937dc4924113ff1fb20 Mon Sep 17 00:00:00 2001 From: benceruleanlu Date: Wed, 30 Apr 2025 16:29:32 -0400 Subject: [PATCH] Support comparison operators --- src/utils/expressionParserUtil.ts | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/utils/expressionParserUtil.ts b/src/utils/expressionParserUtil.ts index d497f4921..0de3649aa 100644 --- a/src/utils/expressionParserUtil.ts +++ b/src/utils/expressionParserUtil.ts @@ -12,7 +12,7 @@ interface UnaryNode { } interface BinaryNode { type: 'Binary' - op: '&&' | '||' | '==' | '!=' + op: '&&' | '||' | '==' | '!=' | '<' | '>' | '<=' | '>=' left: ASTNode right: ASTNode } @@ -27,12 +27,16 @@ const OP_PRECEDENCE: Record = { '||': 1, '&&': 2, '==': 3, - '!=': 3 + '!=': 3, + '<': 3, + '>': 3, + '<=': 3, + '>=': 3 } // hoist and reuse the regex, avoid re‑allocating literal each call const TOKEN_REGEX = - /\s*("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|==|!=|&&|\|\||[A-Za-z0-9_.]+|!|\(|\))\s*/g + /\s*("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|==|!=|<=|>=|&&|\|\||<|>|[A-Za-z0-9_.]+|!|\(|\))\s*/g // cache parsed ASTs per expression const astCache = new Map() @@ -192,7 +196,22 @@ export function evalAst( } const lRaw = getRawValue(left, getContextKey) const rRaw = getRawValue(right, getContextKey) - return op === '==' ? lRaw === rRaw : lRaw !== rRaw + switch (op) { + case '==': + return lRaw === rRaw + case '!=': + return lRaw !== rRaw + case '<': + return (lRaw as any) < (rRaw as any) + case '>': + return (lRaw as any) > (rRaw as any) + case '<=': + return (lRaw as any) <= (rRaw as any) + case '>=': + return (lRaw as any) >= (rRaw as any) + default: + throw new Error(`Unsupported operator: ${op}`) + } } default: throw new Error(`Unknown AST node type: ${(node as ASTNode).type}`)