Skip to content
ahmedtwinePublic
forked from Rexicon226/osmium

About

A Python Interpreter written in Zig

Resources

Stars

0 stars

Watchers

0 watching

Forks

 
 

Latest commit

 

History

156 Commits

Folders and files

Repository files navigation

Osmium

A Python 3.10 interpreter written in Zig, designed to execute Python bytecode with performance in mind.

Project Status

Recently migrated to Zig 0.16.0 - Bytecode parsing, marshal, and VM execution working.

What Works

  • 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/

In Progress

  • Garbage collection integration

Blocked/Needs Work

  • 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)

Architecture

Osmium consists of three main components:

  1. Frontend (src/frontend/Python.zig)

    • Parses Python source files
    • Generates Python bytecode (PYC format)
    • Handles Python 3.10 bytecode specification
  2. Compiler/Marshal (src/compiler/Marshal.zig)

    • Deserializes PYC bytecode
    • Converts bytecode into internal CodeObject representation
    • Manages object references and constants pool
  3. 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

Building

Requirements

  • Zig 0.16.0
  • macOS or Linux (Windows support planned)

Installation

# 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.py

Using Just

The project includes a Justfile for common tasks:

# Build and run
just dev

# Run tests
just test

Example Usage

# 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

About

A Python Interpreter written in Zig

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages