No description
- Zig 97.1%
- Nix 2.9%
| src | ||
| .envrc | ||
| .gitattributes | ||
| .gitignore | ||
| build.zig | ||
| build.zig.zon | ||
| flake.lock | ||
| flake.nix | ||
| LICENSE | ||
| README.md | ||
monkey — Monkey Language Interpreter in Zig
A lexer, parser, and REPL for the Monkey programming language, implemented in Zig following Thorsten Ball's "Writing An Interpreter In Go".
Status
| Component | Milestone |
|---|---|
| Tokenizer / Lexer | Full Monkey token set, keyword lookup, two-char operators (==, !=) |
| Parser | let statements, structured error reporting (no heap allocs per error) |
| AST | Tagged unions (Statement / Expression) — no vtables, no interface casting |
| REPL | Reads from stdin, tokenizes and prints tokens (zig build repl) |
Quick start
# Run all tests
zig test src/Interpreter/Parser.zig
zig test src/Interpreter/Lexer.zig
zig test src/Interpreter/Ast.zig
zig test src/Interpreter/Token.zig
# Or use the build system
zig build test
# Run the REPL
zig build repl
# Check compilation (all packages)
zig build check
Project structure
src/Interpreter/
Token.zig — Token type, Type enum, keyword lookup (StaticStringMap)
Lexer.zig — Character-by-character tokenizer
Ast.zig — AST types: Program, Statement (tagged union), Identifier, LetStatement
Parser.zig — Recursive-descent parser, structured error collection
Repl.zig — Interactive read-eval-print loop
src/main.zig — Stub entry point (not wired to interpreter)
build.zig — Data-driven package registration, one loop for test + check steps
Design notes
- Tagged unions for AST —
Statement/Expressionareunion(enum), not Go-style interfaces or Zig vtables. The fixed set of node types means no runtime dispatch. - Allocation-free parse errors — parser errors store
Token.Typevalues directly, noallocPrintper error. TheErrorFmtstruct uses the{f}format specifier for custom display. - Borrowed literals —
Token.literalslices into the original source input. No string copies during lexing. - Build loop —
build.zigusesstd.meta.fieldsover aPackagesnamed struct to register every module, its test artifact, and its check executable in a single comptime loop. - Zig 0.16 conventions —
ArrayListis unmanaged by default (allocator passed per-operation),toOwnedSlicereturns right-sized allocations,pub fn formatwith two args responds to the{f}specifier.
The above was AI generated