simplified

Virtual Machine

A stack machine with twenty-three opcodes, executing the bytecode your program compiles to — one instruction at a time, with the operand stack, the locals and the call stack visible.

simplifiedA real VM, a small one

The instruction set, the compiler that targets it and the interpreter that runs it are all in src/compilers/sim/vm.ts. Integer arithmetic is JavaScript arithmetic truncated toward zero, so it cannot demonstrate wrapping overflow. There is a step budget: a non-terminating program stops and says so rather than hanging or reporting a number it does not have.

AtlasLang source
Examples
No diagnostics. The program lexes, parses and type-checks.
main — 10 slots: n, total, %0, %1, %2, %3, %4, %5, %6, %7
  0  PUSH 0
  1  STORE 0 ; n
  2  PUSH 0
  3  STORE 1 ; total
  4  JUMP 5
  5  LOAD 0 ; n
  6  STORE 2 ; %0
  7  LOAD 2 ; %0
  8  PUSH 5
  9  LT
 10  STORE 3 ; %1
 11  LOAD 3 ; %1
 12  JUMPF 33
 13  JUMP 14
 14  LOAD 1 ; total
 15  STORE 4 ; %2
 16  LOAD 0 ; n
 17  STORE 5 ; %3
 18  LOAD 4 ; %2
 19  LOAD 5 ; %3
 20  ADD
 21  STORE 6 ; %4
 22  LOAD 6 ; %4
 23  STORE 1 ; total
 24  LOAD 0 ; n
 25  STORE 7 ; %5
 26  LOAD 7 ; %5
 27  PUSH 1
 28  ADD
 29  STORE 8 ; %6
 30  LOAD 8 ; %6
 31  STORE 0 ; n
 32  JUMP 5
 33  LOAD 1 ; total
 34  STORE 9 ; %7
 35  LOAD 9 ; %7
 36  PRINT
 37  PUSH 0
 38  RETURN
 39  HALT
halted

The program ran to completion.

159 instructions executed · first 158 traced

Output
10
1 / 158main:0 PUSH 0
Operand stack (top last)
0
Locals
    Call stack
    • main