(Work in progress...)
LangLab (language lab) is a lightweight, recursive-descent math expression evaluator written in Rust.
It transforms raw strings into an Abstract Syntax Tree (AST) while strictly respecting operator precedence (PEMDAS).
Place your math expression in query.txt:
2 * (5 - 2)
[
{
"Binary": {
"left": {
"Integer": 2
},
"op": "Multiply",
"right": {
"Binary": {
"left": {
"Integer": 5
},
"op": "Minus",
"right": {
"Integer": 2
}
}
}
}
}
]
-----------------
Input: 2 * (5 - 2)
=> Result: 6
cargo run
cargo test
- AST Generation: Visual representation using
serde - Lexical Analysis: Lexer that handles numbers, operators and whitespace
- Precedence Climbing: Parser that respects operator priority (
*before+) - Parentheses Support: Nest expressions as deep as you want using
(...) - Integer Arithmetic: Uses
i32for fast evaluation
.
├── src/
│ ├── lexer.rs # Token definitions & scanner
│ ├── parser.rs # AST, Parser logic & Evaluator
│ ├── lib.rs # Module exports
│ └── main.rs # CLI entry point
├── tests/
│ └── lexer_tests.rs # Test tokenization
│ └── parser_tests.rs # Test AST & Evaluation
└── query.txt # Input file
LangLab follows a three-stage pipeline:
-
Tokenize The Lexer scans the input and produces a stream of tokens
-
Parse The Parser builds a
BinaryExprtree using operator precedence -
Evaluate The Evaluator walks the tree and computes the final result
| Operator | Action | Precedence |
|---|---|---|
| = | Equality (returns 1 for true, 0 for false) | 1 |
| + - | Addition and Subtraction | 2 |
| * / | Multiplication and Division | 3 |
- Integer Only: Uses integer division (e.g.,
1 / 2 = 0) - Error Handling: Uses
panic!for division by zero and syntax errors - Types: Limited to
i32range