diff --git a/README.md b/README.md index be77cf3..38500ef 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,32 @@ # eval -Simple and easy to use parser and evaluator for mathematical expressions. +A minimalistic math parser for Go. It implements the [shunting-yard-algorithm](https://brilliant.org/wiki/shunting-yard-algorithm/) +and allows to parse math from strings. + +## Work in progress +The current implementation requires spaces between each math token and does not support trigionometric functions yet. + +## Example +The library can be used as illustrated below: + +```go +package main + +import ( + "fmt" + + "github.com/jacalz/eval" +) + +func main() { + input := "( 6 - 2 * ( 6 / 3 ) ) ^ 3" + + result, err := eval.Evaluate(input) + if err != nil { + panic(err) + } + + fmt.Println(result) +} +``` + +A more elaborate example can be found in the `example` folder. diff --git a/example/example b/example/example new file mode 100755 index 0000000..799371d Binary files /dev/null and b/example/example differ diff --git a/example/main.go b/example/main.go new file mode 100644 index 0000000..0548fc5 --- /dev/null +++ b/example/main.go @@ -0,0 +1,25 @@ +package main + +import ( + "bufio" + "fmt" + "os" + + "github.com/jacalz/eval" +) + +func main() { + fmt.Print("Enter mathematical expression: ") + in := bufio.NewReader(os.Stdin) + line, err := in.ReadString('\n') + if err != nil { + panic(err) + } + + result, err := eval.Evaluate(line) + if err != nil { + panic(err) + } + + fmt.Println("The result is:", result) +}