-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |