English | 简体中文
Haifa Python is a teaching-oriented compiler and virtual machine playground built in Python. It uses one core bytecode VM to support two learning tracks:
- a jq-style JSON query/runtime tool (
pyjq) - a Lua subset with runtime, coroutines, and tracing (
pylua)
The project is designed for people who want to study how source code moves through the full execution pipeline: lexer, parser, AST, semantic analysis, bytecode generation, VM execution, and debugging/visualization.
Haifa Python exists to make language implementation easier to inspect, modify, and teach. Instead of treating compilers and interpreters as black boxes, it exposes each layer as readable Python modules with examples, tests, and visualizers.
This makes the repository useful for:
- compiler and interpreter courses
- programming languages self-study
- experiments with bytecode design and runtime behavior
- demonstrations of coroutines, closures, tables, and multi-return semantics
- jq-style data transformation on top of a reusable VM core
The codebase maps directly to core CS and PL concepts:
- lexical analysis and parsing
- abstract syntax trees and source mapping
- semantic analysis for scopes, closures, and upvalues
- bytecode instruction design
- stack/register-style VM execution tradeoffs
- call stacks, environments, and runtime error reporting
- coroutines and event-driven execution tracing
- CLI tooling, debugging views, and execution visualization
If you are teaching or learning compiler construction, language runtimes, or virtual machines, this repository is meant to be studied as much as it is meant to be run.
compiler/: core bytecode VM, jq frontend/runtime, instruction set, and visualizershaifa_lua/: Lua lexer, parser, compiler, runtime, stdlib, coroutines, and CLIdocs/: user guides, sprint notes, and reference materialknowledge/: architecture and deep-dive notesexamples/: runnable Lua and coroutine examplesbenchmark/: benchmark scripts, runner, and stored results
- Python 3.11+
- Optional:
pygamefor the GUI visualizer
python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install --upgrade pip setuptools wheel
python3 -m pip install .To enable the GUI visualizer:
python3 -m pip install ".[gui]"pytestRun a script:
python3 -m haifa_lua.cli examples/hello.lua --print-outputEvaluate inline code:
python3 -m haifa_lua.cli -e 'x = 1; y = 2; return x + y' --print-outputStart the REPL:
python3 -m haifa_lua.cli --replTrace or visualize execution:
python3 -m haifa_lua.cli examples/coroutines.lua --trace coroutine
python3 -m haifa_lua.cli examples/coroutines.lua --visualize cursesQuery a JSON file:
python3 -m haifa_jq.jq_cli '.items[] | .name' --input compiler/sample.jsonPipe JSON from stdin:
cat compiler/sample.json | python3 -m haifa_jq.jq_cli '.items[] | .price'Use the terminal visualizer:
python3 -m haifa_jq.jq_cli '.items[] | .name' --input compiler/sample.json --visualize cursesAfter installation, the same tools are also available as:
pylua --help
pyjq --helpFor a guided tour, a practical sequence is:
- Run a Lua example from
examples/. - Read
haifa_lua/lexer.py,haifa_lua/parser.py, andhaifa_lua/compiler.py. - Follow execution into
compiler/bytecode.pyandcompiler/bytecode_vm.py. - Compare the Lua flow with the jq flow in
haifa_jq/jq_parser.py,haifa_jq/jq_compiler.py, andhaifa_jq/jq_vm.py. - Open the visualizer or trace output to inspect runtime state changes.
docs/lua_guide.md: practical guide to the Lua runtimedocs/guide.md: jq CLI guide and examplesdocs/reference.md: command referencedocs/lua_sprint.md: implementation milestones and roadmapknowledge/03-bytecode-and-vm.md: bytecode/VM backgroundknowledge/06-lua-execution-pipeline.md: end-to-end Lua execution pipeline
- The GUI visualizer depends on
pygame. In non-GUI environments, use--visualize curses. - The Lua implementation is intentionally a subset/runtime experiment, not a full Lua compatibility target.
- The repository favors readability and inspectability over aggressive optimization.