This is a really simple calculator that can parse and compute mathematical expressions.
This project is written in Rust and uses the tracing
crate for logging.
The parser is a top-down operator precedence parser. It supports the following operators:
+
-
*
/
And the following functions:
sin
cos
tan
exp
log
log2
log10
sqrt
It also supports parentheses and nested expressions.
And supports unary operators +
and -
.
This has been written to be a simple educational tool to understand how to parse mathematical expressions in Rust without any external dependencies.
cargo run -- "1 + 2 * sin(1.9)"
1 + 2 * sin(1.9) -> 2.892600175374829
cargo run -- --help
You can also see the tokens in the expression with the --show-tokens
flag:
cargo run -- "1 + 2 * sin(1.9)" --show-tokens
[Number(1.0), Operator('+'), Number(2.0), Operator('*'), Function("sin"), LeftParen, Number(1.9), RightParen]
1 + 2 * sin(1.9) -> 2.892600175374829
And the AST with the --show-ast
flag:
cargo run -- "1 + 2 * sin(1.9)" --show-ast
BinaryOp(Number(1.0), '+', BinaryOp(Number(2.0), '*', Function("sin", Number(1.9))))
1 + 2 * sin(1.9) -> 2.892600175374829
This project is licensed under the MIT license. See the LICENSE file for more details.