liveBook · Manning

Stack machine

As explained in Refactoring to Rust

A stack machine is a computational model that evaluates instructions by operating on values stored in a last-in-first-out stack.

A stack machine evaluates instructions by storing values in a last-in-first-out stack. In Reverse Polish Notation, numbers are pushed onto the stack and each operator consumes the previous two values, then places its result back on the stack. section 3.2

This model makes arithmetic evaluation direct:

  • Process tokens strictly from left to right, without infix precedence rules. section 3.2
  • Represent nested arithmetic by placing each operation in the order it should execute. section 3.2
  • Parse a calculator more simply because operators always act on the two preceding stack values. section 3.2

RPN evaluation

The infix expression “(3 + 4) * 12” becomes “3 4 + 12 *” and evaluates to 84: push 3 and 4, add them, push 12, then multiply. section 3.2

Figure 3.2 RPN stack used to calculate 3 4 + 12 *
figure

A stack diagram traces the intermediate values while evaluating a nested RPN expression. section 3.2

How Refactoring to Rust covers it

How deep it goes
Defines RPN and demonstrates left-to-right stack evaluation, stopping short of a general virtual-machine implementation.
Examples
Uses arithmetic expressions such as “3 4 + 12 *” and a C calculator extended for nested operations.
What it assumes
Assumes basic arithmetic notation while teaching RPN ordering and stack-based evaluation through concrete expressions.
What you can do after
Evaluate/understand arithmetic expressions by translating infix operations into left-to-right stack instructions.
How it connects
Connects stack machines with Reverse Polish Notation, infix notation, arithmetic parsing, and C–Rust foreign-function interfaces.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Refactoring to Rust. The section links point to the parts of the book it draws on.