Skip to content
DaniAredduPublic
forked from leo0o7/expr

About

A small Rust expression evaluator based on Dijkstra's shunting-yard algorithm.

Resources

Stars

0 stars

Watchers

0 watching

Forks

 
 

Latest commit

 

History

12 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

expr

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).

Usage

It reads one expression from stdin and prints the result:

echo "1 + 2 * (3 + 4)" | cargo run

Example output:

= 15

Syntax support

  • 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

Tests

Run tests:

cargo test

About

A small Rust expression evaluator based on Dijkstra's shunting-yard algorithm.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages