Skip to content
Ilia Maslakov edited this page Oct 17, 2026 · 1 revision

calc

A calculator of expressions in reverse Polish notation: the operands come first, then the operator.

Input Output Note
2 3 + 5 sum
10 4 - 6 difference
6 7 * 42 the answer
1 0 / 0 division by zero

How it works

flowchart LR
    A[line] --> B(tokenize)
    B --> C{number?}
    C -->|yes| D[push]
    C -->|no| E[apply]
    E --> D
Loading

The stack holds at most $N = 64$ values, and each operator pops two of them:

if (stack_pop (s, &b) != 0 || stack_pop (s, &a) != 0)
    return -1;

Build

  1. meson setup build
  2. meson compile -C build
  3. ./build/calc

An empty line quits.

Clone this wiki locally