Skip to content

Rust based really simple calculator, with basic functionnalities

License

Notifications You must be signed in to change notification settings

xigh/calculator-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Really simple calculator in Rust

This is a really simple calculator that can parse and compute mathematical expressions.

Rust

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.

Examples

cargo run -- "1 + 2 * sin(1.9)"
1 + 2 * sin(1.9) -> 2.892600175374829

Usage

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

License

This project is licensed under the MIT license. See the LICENSE file for more details.

About

Rust based really simple calculator, with basic functionnalities

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages