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 buildconstforthic=@import("forthic");
pubfnmain() !void {
vargpa=std.heap.GeneralPurposeAllocator(.{}){};
defer_=gpa.deinit();
constallocator=gpa.allocator();
varinterp=tryforthic.StandardInterpreter.init(allocator);
deferinterp.deinit();
tryinterp.run("[1 2 3] \"2 *\" MAP");
constresult=tryinterp.stackPop();
deferresult.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:
constwords= .{
.{ .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 registrationpubfnregisterWords(module: *Module) void {
inlinefor (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 Zigconstresult=tryinterp.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