1 stable release

1.0.0 Jan 4, 2025

#643 in Math

Download history 119/week @ 2025-01-01 14/week @ 2025-01-08

133 downloads per month

MIT/Apache

20KB
472 lines

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:

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.

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'
expected `)`
$ sari '1 / 0'
division by zero

Expressions

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.

License

This project is licensed under the Apache License, Version 2.0 and the MIT License. You may choose either license at your option.

No runtime deps