All projects

Slang compiler and VM

A small statically typed language with a full pipeline — ANTLR parser, AST, type checker, bytecode generator and a stack-based virtual machine.

Layer
Language
Context
Programming Translators course, RAF
Team
Team of 2
Status
Complete
Built with
Java, ANTLR

We designed a small language and wrote everything needed to run it, from the grammar to a virtual machine that executes the generated bytecode.

The language

  • Static typing: every type is known before the program runs.
  • Integers, booleans and arrays.
  • Assignment, arithmetic, relational and logical operators, with short-circuit evaluation for && and ||.
  • if/else, two kinds of loops, and functions with typed parameters and return types.

The pipeline

  • Parsing with an ANTLR grammar.
  • AST construction: a converter walks ANTLR’s concrete syntax tree and builds our own typed AST, so later stages don’t depend on generated classes.
  • Type checking of declarations, expressions and function calls before anything runs.
  • Code generation from the AST into bytecode, one container per function.
  • A stack-based VM that executes the bytecode, including function values and upvalues for variables captured from enclosing scopes.