I am currently implementing an expression evaluator (single line expressions, like formulas) based on the following:
- the entered expression is tokenized to separate literal booleans, integers, decimals, strings, functions, identifiers (variables)
- I implemented the Shunting-yard algorithm (lightly modified to handle functions with variable number of arguments) to get rid of parenthesis and order the operators with a decent precedence in a postfixed order
- my shunting-yard simply produces a (simulated) queue of tokens (by means of an array, my Powerbuilder Classic language can define objects, but only have dynamic arrays as native storage - not true list, no dictionary) that I evaluate sequentially with a simple stack machine
My evaluator is working nicely, but I am still missing an if()
and I am wondering how to proceed.
With my shunting-yard postfixed and stack based evaluation, if I add if()
as another function with a true and false parts, a single if(true, msgbox("ok"), msgbox("not ok")
will show both messages while I would like to show only one. This is because when I need to evaluate a function, all of its arguments has already been evaluated and placed on the stack.
Could you give me some way to implement if()
in a lazy way?
I though about processing these as a kind of macro, but at early time I have not yet the condition evaluation. Perhaps that I need to use an other kind of structure than a queue to keep separately the condition and the true / false expressions? For now the expression is parsed before evaluation, but I also plan to store the intermediate representation as kind of precompiled expression for future evaluation.
Edit: after some though on the problem, I think I could build a tree representation of my expression (instead of a linear token stream), from which I could easily ignore one or another branch of my if()
.