| Crates.io | sari |
| lib.rs | sari |
| version | 1.0.0 |
| created_at | 2025-01-04 16:36:09.057634+00 |
| updated_at | 2025-01-04 16:36:09.057634+00 |
| description | Simple arithmetic expression evaluator |
| homepage | |
| repository | https://github.com/dmajda/sari |
| max_upload_size | |
| id | 1504050 |
| size | 32,632 |
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.
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
To evaluate an expression, use the sari::eval function:
use sari::Error;
let result = sari::eval("(1 + 2) * 3");
assert_eq!(result, Ok(9));
let result = sari::eval("(1 + 2");
assert_eq!(result, Err(Error::new("expected `)`")));
let result = sari::eval("1 / 0");
assert_eq!(result, Err(Error::new("division by zero")));
For more details, see the API documentation.
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'
expected `)`
$ sari '1 / 0'
division by zero
The expressions consist of integers combined using +, -, *, and / binary
operators (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.
This project is licensed under the Apache License, Version 2.0 and the MIT License. You may choose either license at your option.