plantain-00 / expression-engine

An expression tokenizer, parser and evaluator.
MIT License
8 stars 1 forks source link

Possibility to add new operand/custom evaluator function #6

Open niclaslindberg opened 1 year ago

niclaslindberg commented 1 year ago

Hi!

Thanks for a nice project! We've been elaborating a little bit with ExpressionEngine to create a evaluator for labels. I'm wondering if expression-engine is designed to be extendible for custom operands/evaluators. The reason for asking is that we would like to add function to evaluate variables as semantic versions. We have the semantic evaluator function in place

Like:

version>3 // should be evaluated by semanicEvaluator.gt(version,"3")

or version<3.3.1 // should be evaluated by semanicEvaluator.lt(version,"3.3.1"))

Thanks.

BR

Niclas

plantain-00 commented 1 year ago

For version>3, it can get AST by parseExpression(tokenizeExpression('version>3')), then can be custom evaluated to your result. For version<3.3.1, the answer is no, because '3.3.1' is not valid number.

niclaslindberg commented 1 year ago

I got it. Thanks. Are there any plans to open up for configuring/overriding the evaluation process? Psuedo like: evaluate(expression, (ast) => result )) result != undefined => overridden In my case I could then check if: return (ast.op === "<" && typeof(ast.left) === "string" && typeof(ast.right) === "string") ? semantic.lt(ast.left,ast.right) : undfined

plantain-00 commented 1 year ago

No