This version of the parser now produces trees like this one: Expression -> Term -> Factor -> (Numeric | Variable | Subexpression in parentheses | Function). This is much higher-level than the old parser, which parsed out BinaryOperator, UnaryOperator, Variable, and Constant nodes. Now, everything is a factor, contained in a term, contained in an expression. Evaluation works like the following:
Expression.evaluate(st:SymbolTable) = {
var runningTotal = 0;
foreach (term in terms) {
runningTotal += term.evaluate(st)
}
}
Term.evaluate(st:SymbolTable) = {
var runningTotal = 1;
foreach (factor in factors) {
runningTotal *= factor.evaluate(st)
}
}
Of course, the actual evaluation is more complex because the code switches based on whether a term or factor is declared as "inverse". In that case, it subtracts or divides the term or factor from the running total.
No comments:
Post a Comment