Tiper (Tiny Interpreter) is a Rust-based interpreter implementation with lexical analysis, parsing, and evaluation capabilities. This project is an educational implementation of an interpreter in Rust.
Tiper is an interpreter that processes source code through multiple stages:
- Tokenization - Converts source code into tokens
- Parsing - Builds an abstract syntax tree (AST) from tokens
- Evaluation - Executes the parsed expressions and statements
Tokenizes source code with support for:
- Arithmetic operators (
+,-,*,/) - Comparison operators (
==,!=,>,<,>=,<=) - Logical operators (
!,&&,||) - Literals (numbers, strings, booleans, nil)
- Identifiers and keywords
- Comments (single-line)
Implements recursive descent parsing with operator precedence for:
- Arithmetic expressions
- Comparison expressions
- Logical expressions
- Grouping expressions
- Variable assignments
Evaluates expressions and executes statements including:
- Print statements
- Variable declarations
- Block statements
- Expression statements
src/
├── main.rs # Entry point and CLI interface
├── tokenize.rs # Lexical analysis (tokenizer)
├── parse.rs # Parser and expression handling
├── types.rs # Type definitions (tokens, expressions, statements)
├── evaluate.rs # Expression evaluation
├── statement.rs # Statement parsing and execution
└── scope.rs # (Incomplete) Environment/scope management
# Tokenize a file
tiper tokenize filename.txt
# Parse and display AST
tiper parse filename.txt
# Evaluate expressions
tiper evaluate filename.txt
# Run the program
tiper run filename.txt- Variable declaration:
var x = 5; - Print statements:
print "Hello, world!"; - Arithmetic:
1 + 2 * 3 - Comparisons:
a > b - Logical operations:
!true - Grouping:
(1 + 2) * 3 - Blocks:
{ var x = 1; print x; }
The interpreter provides detailed error messages with line numbers for:
- Syntax errors
- Type errors
- Undefined variables
- Division by zero
- Basic variable scoping (
scope.rsis marked as "TODO") - Limited control flow (no
if/whilestatements implemented yet) - Basic function support
cargo build
cargo run -- tokenize example.txtDO WHAT YOU WANT