Skip to the content ForoHackers
Log inJoin

Interpreters and virtual machines: start here

Interpreters and virtual machines
#3039 · · Quote · Report

An interpreter runs a program directly instead of translating it ahead of time, and a virtual machine is the abstract computer many languages run on, defined by its own instruction set. Together they power Python, JavaScript, Java and many more. Building one is the quickest way to understand what a language really does.

Key concepts

  • Tree-walking: evaluate the syntax tree directly; simple and slow.
  • Bytecode: compile to compact instructions for a virtual machine, then run them in a dispatch loop.
  • Stack or register machine: operands live on a stack or in numbered registers; each design shapes the bytecode.
  • Environments and closures: how variables are stored, captured and looked up.
  • Memory management: reference counting or tracing garbage collection, and where objects live.
  • Just-in-time compilation: turning hot code into machine code while the program runs.

Vocabulary

  • Bytecode: the instruction set of a virtual machine.
  • REPL: read-eval-print loop, an interactive prompt.
  • Dispatch: choosing and running the next instruction.
  • Garbage collector: the part of a runtime that reclaims memory nobody can reach.

What to study, in order

  1. Write a calculator that parses and evaluates expressions with a tree-walking evaluator.
  2. Add variables, conditionals, loops, functions and closures to it.
  3. Rewrite it as a compiler to bytecode plus a stack machine, and compare speed.
  4. Add a garbage collector, first a simple mark and sweep.
  5. Profile your machine, then try one optimisation such as caching variable lookups.
  6. Read the documentation of a real virtual machine and its bytecode listing.

Related tags

#compilers #runtimes #memory-management #language-design

You are not logged in, so this goes up anonymously: no account, no name and no address is stored with it, and it may carry no link and no image. Log in or join to post under your name. Our web server's logs keep the address and time of every request for up to 14 days; to keep yours out of them, use Tor. To reach the staff in confidence, use the drop. Sign as a returning source (a key kept only in this browser)

Sending takes a few seconds here: your browser first works out a proof of work against spam.

Reply