A robust, memory-safe Forth-79 Standard interpreter built from scratch in Rust. This project implements a stack-based execution engine, custom word definitions, and strict error handling without relying on external dependencies or unsafe code.
Developed for Taller de Programación I (FIUBA), this interpreter replicates the core mechanics of Forth. It utilizes a stack-based architecture where operations pop arguments and push results, supporting both standard arithmetic and complex conditional logic.
- Stack Manipulation: Full support for
DUP,DROP,SWAP,OVER, andROT. - Word Definitions: Dynamic dictionary expansion using
: <name> <body> ;. - Control Flow: Conditional execution with
IF ... ELSE ... THENblocks. - Persistence: Automatic state preservation; the final stack is saved to
stack.fthafter execution. - Configurable Memory: Custom stack size allocation via CLI arguments.
This project adheres to the strictest Rust development standards:
- No Unsafe: 100% safe Rust code.
- No Panics: Zero use of
.unwrap(),.expect(),panic!(), orexit(). - Zero Dependencies: Relies exclusively on the Rust Standard Library (
std). - Standard Compliant: Formatted with
cargo fmtand verified withclippy.
cargo build --releaseExecute a Forth script by providing its path:
cargo run -- path/to/script.fthOptionally, define a custom stack size (in bytes):
cargo run -- path/to/script.fth stack-size=262144| Category | Operations |
|---|---|
| Arithmetic | +, -, *, / |
| Logic | =, <, >, AND, OR, NOT |
| Stack | DUP, DROP, SWAP, OVER, ROT |
| I/O | ., EMIT, CR, ." <message>" |
The project includes a comprehensive suite of unit and integration tests to ensure interpreter parity with the Forth-79 standard.
cargo test- Clippy: No warnings allowed.
- Docs: Fully documented using cargo doc.
rust-the-forth/
├── src/
│ ├── main.rs # Entry point & CLI parsing
│ ├── interpreter.rs # Core execution logic
│ ├── stack.rs # Stack implementation
│ └── dictionary.rs # Word definitions & lookup
├── tests/ # Integration tests with .fth files
├── Enunciado.pdf # Academic requirements
└── Cargo.toml # Project metadata
- Language: Rust (v1.85+).
- Environment: Unix / Linux.
- Standard Library:
std::env,std::fs,std::io
This project is licensed under the MIT License - see the LICENSE file for details.