0Pricing
C Academy · Lesson

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

  1. Tokenizing Input
  2. Parsing Expressions
  3. Evaluating the Tree
  4. Adding Variables
← Back to C Academy