Tiny Interpreter Part 2 of 3

Tiny Interpreter:
Building the Tree

The parser reads tokens and builds a syntax tree. Mermaid diagrams are drawn in the browser; the button under each one opens it full screen.

The pipeline

flowchart LR
  A[Source text] --> B(Scanner)
  B -->|tokens| C(Parser)
  C -->|tree| D(Evaluator)
  D --> E[Value]
From source text to a value.

Precedence

Each level of precedence is one function:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
func (p *Parser) expression() Node { return p.term() }

func (p *Parser) term() Node {
	left := p.factor()
	for p.match("+", "-") {
		op := p.previous()
		left = Binary{op, left, p.factor()}
	}
	return left
}