A tutorial-oriented scripting language implementation project, based on "Implementing a Script Language in Two Weeks"
Stone is a concise dynamic scripting language with the following features:
- First-class functions: Functions can be passed, returned, and stored as values
- Closure support: Anonymous functions can capture their defining environment
- Dynamic arrays: Mutable arrays with index access
- Optional type annotations: Variables and parameters can have type annotations (for documentation)
- Complete expressions: Arithmetic, logical, and comparison operators
- Control flow: if-else, while loops
// Fibonacci sequence
def fib(n) {
if n < 2 {
n
} else {
fib(n - 1) + fib(n - 2)
}
}
fib(10) // Output: 55
// Closure example
def make_adder(x) {
fun(y) { x + y }
}
add10 = make_adder(10)
add10(5) // Output: 15
// Array operations
arr = [1, 2, 3, 4, 5]
sum = 0
i = 0
while i < 5 {
sum = sum + arr[i]
i = i + 1
}
sum // Output: 15
This tutorial uses progressive teaching, building a complete language interpreter from scratch.
- Chapter 0: Project Introduction β Background of Stone language and learning objectives
- Chapter 1: Project Setup β Set up Cargo workspace and create project skeleton
- Chapter 2: Lexical Analysis β Tokenization using logos
- Chapter 3: AST Definition β Define abstract syntax tree nodes
- Chapter 4: Syntax Parsing β Expression and control flow parsing (precedence climbing algorithm)
- Chapter 5: Evaluator β AST evaluation and environment management
- Chapter 6: Closure Implementation β Function definition, calling, and closure mechanism
- Chapter 7: Array Support β Array literals and index access
- Chapter 8: CLI Tool β Command-line tool and file execution
- BNF Grammar Reference β Complete syntax definition of Stone language
- Standard Library Documentation β Built-in functions and native methods
- Implementation Plan β Detailed implementation roadmap
# Clone repository
git clone https://github.com/AlexiaChen/stone-lang-rs.git
cd stone-lang-rs
# Check Rust environment
rustc --version # Requires 1.70+
cargo --version
# Install CLI tool (choose one of three methods)
# Method 1: Using Makefile (recommended)
make install
# Method 2: Using install script
./install.sh
# Method 3: Using cargo install
cargo install --path stone-cli --force# Build all crates
cargo build --workspace
# Run tests
cargo test --workspace# Using installed stone command
stone examples/01_arithmetic.stone
stone examples/06_closures.stone
stone examples/08_fibonacci.stone
# Or using cargo run
cargo run --release --bin stone -- examples/01_arithmetic.stoneStone CLI (stone) is the command-line interpreter for the Stone language, supporting file execution and interactive REPL mode.
# Execute single file
stone program.stone
# Execute example programs
stone examples/01_arithmetic.stone # Basic arithmetic operations
stone examples/02_variables.stone # Variable declaration and assignment
stone examples/03_functions.stone # Function definition and calling
stone examples/04_conditionals.stone # Conditional statements if/else
stone examples/05_while.stone # while loops
stone examples/06_closures.stone # Closures and anonymous functions
stone examples/07_arrays.stone # Array operations
stone examples/08_fibonacci.stone # Fibonacci sequence (recursive)
stone examples/09_factorial.stone # Factorial calculation
stone examples/10_map_filter.stone # Higher-order functions
stone examples/11_closure_scope.stone # Closure scope# Start REPL (interactive interpreter)
stone --repl
# Or run without arguments
stoneIn REPL mode:
- Enter Stone code and see results immediately
- Use
quitorexitto quit - Press
Ctrl+Cto interrupt current input - Press
Ctrl+Dto exit REPL
$ stone --repl
Stone REPL v0.1.0
Press Ctrl+D or type 'quit' to exit
>> 1 + 2
=> 3
>> def add(x, y) { x + y }
=> null
>> add(10, 20)
=> 30
>> quit
Goodbye!# Print token stream (for debugging lexical analysis)
stone --print-tokens examples/01_arithmetic.stone
# Print AST (for debugging syntax parsing)
stone --print-ast examples/03_functions.stone# Display help information
stone --help
# Display version number
stone --version| Argument | Description |
|---|---|
[FILE] |
Path to Stone source file (optional) |
-r, --repl |
Enable REPL mode |
--print-ast |
Print AST without executing |
--print-tokens |
Print token stream without executing |
-h, --help |
Display help information |
-V, --version |
Display version number |
| File | Description |
|---|---|
01_arithmetic.stone |
Basic arithmetic operations and operator precedence |
02_variables.stone |
Variable declaration (var) and assignment |
03_functions.stone |
Function definition (def) and calling |
04_conditionals.stone |
if/else conditional statements |
05_while.stone |
while loops and accumulative summation |
06_closures.stone |
Closures and anonymous functions (fun) |
07_arrays.stone |
Array literals and index access |
08_fibonacci.stone |
Recursive function implementing Fibonacci sequence |
09_factorial.stone |
Recursive function implementing factorial |
10_map_filter.stone |
Higher-order functions map/filter simulation |
11_closure_scope.stone |
Multi-level closures and scope |
This project uses modular design with Cargo workspace organizing multiple crates:
stone-lang-rs/
βββ Cargo.toml # workspace configuration
βββ stone-core/ # Core types (Location, error definitions)
βββ stone-lexer/ # Lexer (logos)
βββ stone-ast/ # AST node definitions
βββ stone-parser/ # Parser (nom)
βββ stone-evaluator/ # Interpreter engine
βββ stone-cli/ # Command-line tool
βββ docs/ # Tutorial documentation
βββ examples/ # Stone example code
stone-cli
β
stone-evaluator βββ stone-ast
β β
stone-parser βββ stone-core
β
stone-lexer
If you are new to compiler design, it's recommended to follow the tutorial in order:
- Read Chapter 0 first to understand the project background
- Follow Chapter 1 to set up the project
- Implement chapter by chapter, each with complete code examples and tests
If you already have compiler basics:
- Quickly browse Architecture Overview
- Refer to BNF Grammar to understand the syntax
| Stage | Chapter | Content | Status |
|---|---|---|---|
| 0 | Project Initialization | Workspace setup | β Completed |
| 1 | Lexical Analysis | Tokenization (logos) | β Completed |
| 2 | AST Definition | Node type definitions | β Completed |
| 3 | Syntax Parsing | Expressions and control flow | β Completed |
| 4 | Evaluator | Environment and evaluation | β Completed |
| 5 | Closure Implementation | Functions and closures | β Completed |
| 6 | Array Support | Array literals and indexing | β Completed |
| 7 | CLI Tool | Command-line interface | β Completed |
- Rust 2024 Edition β Main implementation language
- logos 0.16 β Lexical analysis library
- nom 7.1 β Parser combinator library
- thiserror 2.0 β Error handling
- clap 4.5 β Command-line argument parsing
- rustyline 14.0 β REPL readline support
- Java Original Implementation β Original Java code from "Implementing a Script Language in Two Weeks"
- Stone Language DeepWiki β AI-generated language documentation
- nom documentation β Parser combinator library documentation
- logos documentation β Lexer library documentation
- The Nomicon β Rust advanced topics
MIT License
- Author of "Implementing a Script Language in Two Weeks"
- All contributors to the original Java implementation of Stone language
- Authors of nom and logos libraries in the Rust community