#scientific #calculator #parser #expression

nightly bin+lib rsc

A pure-Rust handwritten scientific calculator for solving math equations in strings

10 releases (4 stable)

✓ Uses Rust 2018 edition

1.2.0 Jan 1, 2019
1.1.1 Dec 11, 2018
1.0.0 Sep 9, 2018
0.1.7 Sep 8, 2018
0.1.5 Aug 28, 2018

#71 in Parser tooling

Download history 27/week @ 2018-12-13 44/week @ 2018-12-20 12/week @ 2018-12-27 18/week @ 2019-01-03 7/week @ 2019-01-10 5/week @ 2019-01-17 2/week @ 2019-01-24 2/week @ 2019-01-31 1/week @ 2019-02-07 3/week @ 2019-02-21 21/week @ 2019-02-28 3/week @ 2019-03-07 111/week @ 2019-03-21

104 downloads per month

MIT license

29KB
465 lines

RSC is a handwritten scientific calculator for turning an equation inside a string into a result. RSC is designed to be very lightweight and have as few dependencies as possible. It has the goal of just doing a single thing really well, and enabling anyone to extend it with more features.

RSC is also designed with the goal to not become bloated software. RSC will not receive frequent updates, but that does not mean it is old or unmaintained. That being said, RSC may still receive internal updates with relation to speed or resource usage.

Obtaining RSC

If you would like to install RSC as a program onto your computer, there is information here.

Size

RSC is ~500 lines of Rust code.

Performance

RSC computes next to instantaneously, even with debug builds. The following output is the time (in nanoseconds) the different operations take to process sqrt((6.1--2.22)^2 + (-24-10.5)^2), where "bench_eval" is all of them at once. More info at lib.rs.

PS C:\Users\Luke\Documents\Projects\rsc> cargo bench
    Finished release [optimized] target(s) in 0.02s
     Running target\release\deps\rsc-74a7d2c06ab98eee.exe

running 4 tests
test tests::bench_compute  ... bench:         231 ns/iter (+/- 10)
test tests::bench_eval     ... bench:       6,272 ns/iter (+/- 504)
test tests::bench_parse    ... bench:       1,624 ns/iter (+/- 133)
test tests::bench_tokenize ... bench:       4,302 ns/iter (+/- 1,462)
...

Stability

RSC will not have any major changes to its syntax. It will remain to be consistent for a long time. It is up to forkers to make different tastes of RSC. It will also forever keep the same open-source permissions.

License

RSC is MIT licensed. RSC will always remain free to modify and use without attribution.

Example

Executable

Using RSC as the actual program provides a simple interface for solving expressions. A right arrow shows where the user can input an expression, and upon pressing the Return key, the result of the entered expression is displayed on the next line.

PS C:\Users\Luke> rsc
>2+2
4
>a = 2
2
>a^3
8
>b
Compute error: UnrecognizedIdentifier("b")
>(b = a^2) + 3
7
>b
4
>sqrt((6.1--2.22)^2 + (-24-10.5)^2)
35.48904619738322
>|-3|
3
>abs -3
3

Library

RSC is very painless to use. For simple, one-off expression solving:

extern crate rsc;

use rsc::eval;

fn main() {
    assert!(eval("5^2").unwrap() == 25.0);
    assert!(eval("x = 5").unwrap() == 5.0);
    assert!(eval("x").is_err()); // Previously assigned variables are discarded
}

In order to keep variables, you must create a Computer instance:

extern crate rsc;

use rsc::computer::Computer;

fn main() {
    let mut c = Computer::new();

    assert!(c.eval("x = 5").unwrap() == 5.0);
    assert!(c.eval("x^2").unwrap() == 25.0);
}

Debug

RSC can be run with the ast flag and show the internal expression that was created by the parser. This is most commonly used for entertainment purposes 😛.

PS C:\Users\Luke> rsc ast
>(a = 2)^3
Pow(
    Assignment(
        "a",
        Constant(
            2.0
        )
    ),
    Constant(
        3.0
    )
)
8

Errors

PS C:\Users\lukew> rsc
>oops
Lexer error: InvalidIdentifier("oops")
>3.3.1
Lexer error: InvalidNumber("3.3.1")
>2+
Parser error: ExpectedFactor(None)
>2(3
Parser error: ExpectedClosingParenthesis
>2b
Parser error: UnexpectedNumber(Identifier("b"))
>b
Compute error: UnrecognizedIdentifier("b")

Related Projects

  • rscplot: A graphing calculator dependent on RSC for solving expressions.

No runtime deps