Scicalc-rs

Rust crate for parsing and doing calculations with measurements, typically used in scientific contexts.

TODO

Lexing [OK]

Transform a string(i.e. the input as a sequence of characters) into a sequence of tokens, which can then be fed into the parser.

Parsing [OK]

Read a sequence of tokens — which has a linear structure — and transform it into a tree structure.

Evaluating [OK]

Read the tree structure of the expression and fold it, reducing it into it's final value.

Proper error handling [WIP]

don't panic!

Instead of panic!ing, it'd better if the evaluator and the parser returned a Result<...>

Calculator

Significant figures & Scientific notation

Miscellaneous

BNF grammar for the expressions

Expression ::= Value | UnaryExpression | BinaryExpression | Grouping Grouping ::= "(" Expression ")" Value ::= Constant | Number | Measurement Measurement ::= Number "±" PosNumber Number ::= PosNumber | UnaryMinus PosNumber PosNumber ::= (\d+)(\.\d+)?|(\.\d+) Constant ::= "e" | "π" BinaryExpression ::= Expression BinaryOperator Expression UnaryExpression ::= UnaryOperator Expression BinaryOperator ::= "+" | "-" | "*" | "/" UnaryOperator ::= UnaryMinus UnaryMinus ::= "-"

Note: As observed by Pratt's paper on "Top Down Operator Precedence", a Backus-Naur Form(for which BNF is a shorthand) is very inept at capturing the precedence of infix operators. Even then, I still think that specifying a grammar with BNF is useful for providing a quick-and-easy guide, with which you can see the recursive structure of the language at a glance.

Acknowledgements & Further reading

These are some resources that I used to learn about programming language theory, algorithms and their implementations: