2 stable releases
Uses new Rust 2024
| new 2.0.0 | Nov 4, 2025 |
|---|---|
| 1.0.0 | Jan 4, 2025 |
#692 in Programming languages
51KB
1.5K
SLoC
Sari
Sari is a simple arithmetic expression evaluator written in Rust. It can be used as a library or as a command-line tool.
I wrote Sari as part of learning Rust. In the future, I’d like to use it as a playground for exploring parsers, compilers, and various related technologies. It’s not very useful besides that.
Installation
To use Sari as a library, add it as a dependency to your project:
$ cargo add sari
To use it as a command-line tool, install it:
$ cargo install sari
Usage
Library
To evaluate an expression, use the sari::eval function:
let result = sari::eval("(1 + 2) * 3");
assert_eq!(result, Ok(9));
let result = sari::eval("(1 + 2");
assert_eq!(result.unwrap_err().message(), "expected `)`");
let result = sari::eval("1 / 0");
assert_eq!(result.unwrap_err().message(), "division by zero");
For more details, see the API documentation.
Command line
To evaluate one or more expressions, pass them to the sari binary as
arguments:
$ sari '(1 + 2) * 3'
9
$ sari '(1 + 2) * 3' '(4 + 5) * 6' '(7 + 8) * 9'
9
54
135
$ sari '(1 + 2'
1:7-1:7: expected `)`
$ sari '1 / 0'
1:1-1:6: division by zero
Expressions
The expressions consist of integers combined using +, -, *, and / binary
operators, + and - unary operators (all with the usual precedence and
associativity), and grouped using parentheses. These elements can be separated
by whitespace.
The expressions use wrapping 32-bit signed arithmetic. Division by zero is an error.
License
This project is licensed under the Apache License, Version 2.0 and the MIT License. You may choose either license at your option.