A Zig implementation of the Forthic stack-based concatenative programming language.
Forthic is a stack-based, concatenative language designed for composable transformations. This is the official Zig runtime implementation, providing full compatibility with other Forthic runtimes while leveraging Zig's comptime metaprogramming and performance.
- ✅ Complete Forthic language implementation
- ✅ All 8 standard library modules
- ✅ Comptime decorators for zero-overhead abstractions
- ✅ Manual memory management for maximum performance
- ✅ gRPC support for multi-runtime execution
- ✅ CLI with REPL, script execution, and eval modes
- ✅ Comprehensive test suite
zig buildconst forthic = @import("forthic");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var interp = try forthic.StandardInterpreter.init(allocator);
defer interp.deinit();
try interp.run("[1 2 3] \"2 *\" MAP");
const result = try interp.stackPop();
defer result.deinit(allocator);
// result is [2, 4, 6]
}# REPL mode
./zig-out/bin/forthic-zig repl
# Execute a script
./zig-out/bin/forthic-zig run script.forthic
# Eval mode (one-liner)
./zig-out/bin/forthic-zig eval "[1 2 3] LENGTH"# Build
zig build
# Run tests
zig build test
# Run specific test
zig test src/forthic/interpreter.zig
# Build with optimizations
zig build -Doptimize=ReleaseFastforthic-zig/
├── src/
│ ├── main.zig # Entry point
│ └── forthic/
│ ├── interpreter.zig # Core interpreter
│ ├── tokenizer.zig # Lexical analysis
│ ├── module.zig # Module system
│ └── modules/standard/ # Standard library (8 modules)
├── tests/ # Test suites
└── build.zig # Build configuration
- core: Stack operations, variables, control flow
- array: Data transformation (MAP, SELECT, SORT, etc.)
- record: Dictionary operations
- string: Text processing
- math: Arithmetic operations
- boolean: Logical operations
- datetime: Date/time manipulation
- json: JSON serialization
Zig's comptime enables zero-overhead word registration:
const words = .{
.{ .name = "SWAP", .meta = Word.init("( a b -- b a )", "Swap"), .func = swap },
.{ .name = "DUP", .meta = Word.init("( a -- a a )", "Duplicate"), .func = dup },
};
// Compile-time registration
pub fn registerWords(module: *Module) void {
inline for (words) |word| {
module.addWord(word.name, word.meta, word.func);
}
}This runtime supports calling words from other Forthic runtimes via gRPC:
// Call a TypeScript word from Zig
const result = try interp.executeRemoteWord("typescript-runtime", "MY-WORD", args);Zig's manual memory management and comptime features provide excellent performance:
- Zero-cost abstractions
- No garbage collection pauses
- Predictable memory usage
- Optimal code generation
BSD 2-CLAUSE
- forthix.com - Learn about Forthic and Categorical Coding
- Category Theory for Coders - Understand the foundations
- Forthic Language Specification
- TypeScript Runtime (reference implementation)
- Documentation