AYAZ AHMED / PORTFOLIO
← ALL PROJECTSSELECTED WORK / 03

COMPILER / GRAVITY VM

LUNA

A custom language implemented in C++: zero-copy tokenisation, arena-allocated ASTs, semantic resolution and a 36-opcode bytecode VM with explicit execution quotas.

LUNA demonstration
LUNA / GRAVITY BYTECODE VM
01 / THE PROJECT

Pratt parsing, bytecode generation and bounded stack execution.

Luna implements the complete source-to-execution path from zero-copy lexical analysis to VM bytecode dispatch. The front end scans string_view lexemes, resolves expression precedence with a Pratt parser and allocates syntax nodes in an arena. The compiler emits bytecode chunks and constant pools for Gravity, a stack VM with local slots, call frames and recursive function execution. A reference tree walker and differential tests verify runtime correctness across supported constructs.

36

Bytecode opcodes

Explicit ISA in compiler/Opcode.hpp.

10M

Default instruction ceiling

SandboxRequest also defaults to 512 frames and 16,384 stack slots.

29

Tests in supplied run

Test suite results covering unit, differential, and quota verification.

02 / THROUGH THE SYSTEM

INPUT → OUTPUT
  1. 01

    Scan

    A zero-copy lexer identifies tokens as views into the source buffer.

  2. 02

    Parse

    A Pratt parser handles expression precedence and builds arena-allocated syntax nodes.

  3. 03

    Resolve

    Name resolution connects declarations and uses before code generation.

  4. 04

    Compile

    The compiler emits bytecode chunks, constants and function instructions.

  5. 05

    Execute

    Gravity dispatches 36 opcodes using a value stack and call frames, within configured budgets.

01 / ENGINEERING

Zero-copy lexing and arena lifetimes

The lexer returns string_view slices into the input buffer, eliminating per-token string allocations. Statement parsing uses recursive descent; Pratt binding powers encode expression precedence and associativity. Arena allocation groups AST node storage into a single contiguous lifetime, binding token views directly to the source buffer.

02 / ENGINEERING

Name resolution and bytecode lowering

Resolver walks lexical scopes, tracks declarations and rejects invalid top-level returns. Compiler maintains its own local-slot mapping and lowers control flow into jumps with backpatched offsets. Chunks store bytecode, source information and constants. uint16 operands address constants and local slots, while Call carries a uint8 argument count. Disassembly exposes the emitted instruction stream for inspection.

03 / ENGINEERING

Stack frames, quotas and failure categories

Gravity dispatches a 36-opcode instruction set spanning constants, locals/globals, arithmetic, comparisons, jumps, calls and output. A frame base locates each function’s local slots in the value stack. SandboxService configures instruction, frame and stack ceilings and returns Success, CompileError, QuotaExceeded or RuntimeError with captured output, instruction count and duration_us. These are in-process VM budgets.

04 / ENGINEERING

Differential verification and stage-level benchmarks

Parser and VM tests verify language semantics; differential cases compare compiled execution with the reference tree walker on their shared subset. Quota tests exercise instruction, frame and stack exhaustion. Dedicated benchmarks isolate lexer, parser, compiler and VM stages, profiling each compilation and execution phase independently.

04 / BUILT WITH

TOOLS & THEIR PLACE
  • 01C++26Zero-copy string_view token slices
  • 02Pratt parserBinding-power grammar
  • 03Arena allocatorAST memory lifetime
  • 04Bytecode chunksConstants & jump patching
  • 05Gravity ISA36-opcode stack machine
  • 06Call framesLocals & recursion
  • 07GoogleTestDifferential & quota tests
  • 08Google BenchmarkStage-level measurements
AYAZ AHMED / PORTFOLIOBACK TO COLLECTION ↑