Evaluating the Tree
Compute the result.
Walking the AST
Evaluation is a post-order traversal: compute the children first, then combine them with the node's operator. A number leaf simply returns its value.
This tree-walking interpreter is the simplest backend an interpreter can have.
The eval Signature
Our evaluator takes a node pointer and returns an integer. Because the tree is recursive, the function is too.
For floating-point languages you would return a double or a tagged value instead.
int eval(Node *n); /* returns the integer value of the subtree */All lessons in this course
- Tokenizing Input
- Parsing Expressions
- Evaluating the Tree
- Adding Variables