Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

The stack

Rune runs on a stack-based virtual machine. Values live in two places — the stack and the heap — and there are no registers.

Instructions operate directly on the stack. Let’s inspect a simple addition with --trace and --dump-stack:

1 + 3
$> cargo run -- run scripts/book/the_stack/add.rn --trace --dump-stack
fn main() (0xe7fc1d6083100dcd):
  0000 = integer 1
    0+0 = 1
  0001 = integer 3
    0+0 = 1
    0+1 = 3
  0002 = add
    0+0 = 4
  0003 = return
    *empty*
== 4 (7.7691ms)
# stack dump after halting
frame #0 (+0)
    *empty*

Let’s examine the stack after each instruction.

  0000 = integer 1
    0+0 = 1

We evaluate the integer 1 instruction, which pushes an integer with the value 1 onto the stack.

  0001 = integer 3
    0+0 = 1
    0+1 = 3

We evaluate the integer 3 instruction, which pushes an integer with the value 3 onto the stack.

  0002 = add
    0+0 = 4

We evaluate the add instruction, which pops two values from the stack and adds them together. Two integers use built-in accelerated instructions to perform the addition.

  0003 = return
== 4 (7.7691ms)

We return from the virtual machine. The last value of the stack will be popped as the return value.

# stack dump
frame #0 (+0)

This is the stack dump after the virtual machine exits. It tells us that at call frame #0 (+0), the final call frame at stack position +0, nothing remains on the stack.