-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse.go
More file actions
27 lines (23 loc) · 734 Bytes
/
parse.go
File metadata and controls
27 lines (23 loc) · 734 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package arithmetic
// Parse the input and calculates the value. The parser uses a shunting-yard algorithm
// that transforms the input from infix to postfix notation (aka reverse-polish notation).
// The input can contain spaces. They will be used to separate tokens, but are not mandatory.
//
// More about the shunting-yard algorithm here:
// http://wcipeg.com/wiki/Shunting_yard_algorithm
// https://en.wikipedia.org/wiki/Shunting-yard_algorithm
func Parse(input string) (interface{}, error) {
infix, err := tokenize(input)
if err != nil {
return nil, err
}
postfix, err := shuntingYard(infix)
if err != nil {
return nil, err
}
value, err := solve(postfix)
if err != nil {
return nil, err
}
return value, nil
}