94
« on: March 29, 2008, 04:59:34 PM »
Well, for those who don't know programming, you won't understand. For those who do...
The expression system is a tree data structure, where each node in the tree is a class derived from the Expression abstract base class. Leaf nodes are literals or variable names, and non-leaf nodes are operators like +, -, AND, OR, >, <, etc. So when your expression contains a +, there's a node in the tree for the + which has two expressions as children. That's the trees.
The recursion comes as a natural result of working with trees. To evaluate an operator like +, you simply evaluate its sub-expressions, add the results together, and return the answer. The recursion stops when you evaluate a literal or variable node (which just returns the value). And the polymorphism comes in with the Evaluate() function being a pure virtual function in the Expression class.