A small hand-written expression evaluator based on Dijkstra's shunting-yard algorithm. It reads mathematical expressions in normal infix form and evaluates them directly using an operator stack and a value stack. It does not first build an expression tree or convert the expression into another format.
It supports precedence, associativity, brackets, signed numbers, decimals, and implicit multiplication like 2(3 + 4).
It reads one expression from stdin and prints the result:
echo "1 + 2 * (3 + 4)" | cargo runExample output:
= 15- Addition:
1 + 2 - Subtraction:
5 - 2 - Multiplication:
3 * 4 - Division:
8 / 2 - Exponentiation:
2 ^ 3 - Signs:
-2,+3 - Decimals:
1.5 + .5 - Brackets:
(1 + 2),[1 + 2],{1 + 2} - Implicit multiplication:
2(3 + 4) - Scientific notation:
1e3 - Functions:
sin(1),sqrt(4) - Variables:
x + 1 - Constants:
pi,e
Run tests:
cargo test