I'm currently working on a script interpreter for a game I'm porting as a personal project - I have asked a few questions in regard to it earlier.
It's starting to work pretty well, I did however came to the point where I have to evaluate boolean expressions, for example the ones in while-loops, if-statements, etcetera.
My current code tokenizes the script, and passes the boolean expression to a function called ProcessExpression(string[] tokens)
when it encounters one. An example token-array could be:
local.i
<=
local.arrayLength
+
1
&&
(
local.i
<
local.notThis
||
local.i
>
local.notThis
)
Thinking about how to tackle the actual evaluation, I figured I'd start by replacing the variables with actual values, for example: local.i
would become 0, local.arrayLength
would become 16 and local.notThis
would become 10.
Then I thought about pulling the resulting tokens through a postfix calculator to get rid of all the math, leaving just the logical conditions.
However, I'm kind of on the fence if this is the approach I should take or not. Seeing as the postfix calculation alone would add a lot code, I was wondering if there are some tricks I didn't think off? If postfix is indeed the way to go, can I somehow also make it handle the logical conditions alongside with the math operators?
I'm making this in C#, but I am however unwilling to use external libraries or some kind of "Eval"-function. I'm also not interested in making a grammar for this using things like ANTLR, as there must be less intrusive ways for something seemingly "simple" as an expression solver (if that would be the term for it).