Skip to content

Commit

Permalink
Add a small example project
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed May 14, 2022
1 parent b0a19cf commit a57ae64
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.
Binary file added example/example
Binary file not shown.
25 changes: 25 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit a57ae64

Please sign in to comment.