A Python 3.10 interpreter written in Zig, designed to execute Python bytecode with performance in mind.
Recently migrated to Zig 0.16.0 - Bytecode parsing, marshal, and VM execution working.
- Python 3.10 bytecode compilation (PYC generation via CPython)
- Complete PYC format deserialization (Marshal)
- Bytecode instruction decoding (19 opcodes)
- CodeObject parsing with constants, names, and varnames
- Object type system (Int, String, Tuple, CodeObject, None, True, False)
- Reference-based object sharing (TYPE_REF)
- Hex dump and bytecode inspection
- Stack-based instruction execution
- Builtin functions -
print(),abs(),bool(),int(),input() - User defined functions and calls
- Member functions -
list.append() - All 9 test cases in
tests/
- Garbage collection integration
- GC module missing - currently using ArenaAllocator
- Graph and RefMask evaluation needs Zig 0.16.0 updates
- Some marshal edge cases (filename/name field TYPE_REF handling)
Osmium consists of three main components:
-
Frontend (
src/frontend/Python.zig)- Parses Python source files
- Generates Python bytecode (PYC format)
- Handles Python 3.10 bytecode specification
-
Compiler/Marshal (
src/compiler/Marshal.zig)- Deserializes PYC bytecode
- Converts bytecode into internal CodeObject representation
- Manages object references and constants pool
-
Virtual Machine (
src/vm/Vm.zig)- Executes Python bytecode instructions
- Manages runtime state (stack, variables, scopes)
- Handles Python object lifecycle
- Implements Python's execution model
- Zig 0.16.0
- macOS or Linux (Windows support planned)
# Install Zig 0.16.0
brew install zig # macOS
# or use zigup: brew install zigup && zigup 0.16.0
# Build Osmium
zig build
# Run with a Python file
./zig-out/osmium hello.pyThe project includes a Justfile for common tasks:
# Build and run
just dev
# Run tests
just test# hello.py
a = 1
b = 2
c = a + b
def add(x, y):
return x + y
result = add(5, 3)$ zig build && ./zig-out/osmium hello.py