I would like to do something that is similar to constant folding using Python.
Python has a convenient built function, eval(), so that constant only equations can be easily folded in by applying eval().
Example:
s = '4 + (5) * 2'
reduced_s = str(eval(s)) # '14'
However, constant folding I want to implement should handle non-constant labels.
Examples:
s = '_tbl + (2) + (2) * 4'
should be folded to '_tbl + 10'.
If constant parts are separated by a label, for example (s = '2 + _tbl + 4'), it should still produce '_tbl + 6' (or '6 + _tbl).
I have written a constant folding routine using "C" years ago. It was not a small routine as I needed to construct a tree and evaluate the precedence of operators.
As Python is much more powerful langauge than "C", before doing the same thing using Python, I would like to seek other people's wisdom.
Your insights to this challenge are greatly appreciated.