I'm in the process of writing a small, rule-based 'math' engine. I realize this is unclear, so I'll provide a small example.
Let's say you have some variable a, that holds an integer. You also have some functions you can apply to the number, i.e.
sqr
- square the numberflp
- flip the bits of the numberdec
- decrement the numberinc
- increment the number
You can then say, do_formula(a, "2sqr+inc+flp")
. If a were 3, it'd square it twice (81), increment it (82), and flip the bits of it (~82 -- which is -83, if dealing with signed integers, I believe).
What would be the best way of parsing the formula? It's relatively simple, and I'm thinking of making all the opcodes be 3 characters... would it be overkill to use Lex? Should I just write a simple home-brewed solution or use something else entirely?
I realize the above example is silly; I'm not building a calculator that'll do that, but it illustrates what I'm trying to do well enough.