-
Notifications
You must be signed in to change notification settings - Fork 5
/
parser.go
228 lines (198 loc) · 4.08 KB
/
parser.go
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package calculator
import (
"fmt"
"math"
"strings"
)
type nodeKind string
const (
addNode nodeKind = "+"
subNode nodeKind = "-"
mulNode nodeKind = "*"
divNode nodeKind = "/"
funcNode nodeKind = "func"
numNode nodeKind = "num"
)
type node struct {
kind nodeKind
left *node
right *node
funcName string
args []*node
val float64
}
type parser struct {
tokens []token
i int
}
func newParser(tokens []token) *parser {
return &parser{tokens: tokens, i: 0}
}
func (p *parser) numberNode() (*node, error) {
t := p.tokens[p.i]
if t.kind != numberToken {
return nil, fmt.Errorf("expected a number: %s", t.str)
}
p.i++
return &node{kind: numNode, val: t.val}, nil
}
func (p *parser) constantNode(str string) (*node, error) {
constants := map[string]float64{
"e": math.E,
"pi": math.Pi,
"phi": math.Phi,
"sqrt2": math.Sqrt2,
"sqrte": math.SqrtE,
"sqrtpi": math.SqrtPi,
"sqrtphi": math.SqrtPhi,
"ln2": math.Ln2,
"log2e": math.Log2E,
"ln10": math.Ln10,
"log10e": math.Log10E,
}
val, ok := constants[strings.ToLower(str)]
if !ok {
return nil, fmt.Errorf("unknown constant: %s", str)
}
p.i++
return &node{kind: numNode, val: val}, nil
}
func argumentNumber(funcName string) (int, error) {
f, ok := functions[funcName]
if !ok {
return 0, fmt.Errorf("unknown function: %s", funcName)
}
switch f.(type) {
case func() float64:
return 0, nil
case func(float64) float64:
return 1, nil
case func(float64, float64) float64:
return 2, nil
case func(float64, float64, float64) float64:
return 3, nil
default:
return 0, fmt.Errorf("invalid function: %s", funcName)
}
}
func (p *parser) functionNode(str string) (*node, error) {
funcName := strings.ToLower(str)
num, err := argumentNumber(funcName)
if err != nil {
return nil, err
}
if p.consume(")") {
if num != 0 {
return nil, fmt.Errorf("%s should have argument(s)", funcName)
}
return &node{kind: funcNode, funcName: funcName}, nil
}
args := []*node{}
n, err := p.add()
if err != nil {
return nil, err
}
args = append(args, n)
for p.consume(",") {
n, err := p.add()
if err != nil {
return nil, err
}
args = append(args, n)
}
if len(args) != num {
return nil, fmt.Errorf("%s should have %d argument(s) but has %d arguments(s)",
funcName, num, len(args))
}
p.consume(")")
return &node{kind: funcNode, funcName: funcName, args: args}, nil
}
func (p *parser) consume(s string) bool {
t := p.tokens[p.i]
if t.kind != reservedToken || t.str != s {
return false
}
p.i++
return true
}
func (p *parser) parse() (*node, error) {
return p.add()
}
func (p *parser) insert(n *node, f func() (*node, error), kind nodeKind) (*node, error) {
left := n
right, err := f()
if err != nil {
return n, err
}
return &node{kind: kind, left: left, right: right}, err
}
func (p *parser) add() (*node, error) {
n, err := p.mul()
if err != nil {
return nil, err
}
for {
if p.consume("+") {
n, err = p.insert(n, p.mul, addNode)
if err != nil {
return nil, err
}
} else if p.consume("-") {
n, err = p.insert(n, p.mul, subNode)
if err != nil {
return nil, err
}
} else {
return n, nil
}
}
}
func (p *parser) mul() (*node, error) {
n, err := p.unary()
if err != nil {
return nil, err
}
for {
if p.consume("*") {
n, err = p.insert(n, p.unary, mulNode)
if err != nil {
return nil, err
}
} else if p.consume("/") {
n, err = p.insert(n, p.unary, divNode)
if err != nil {
return nil, err
}
} else {
return n, nil
}
}
}
func (p *parser) unary() (*node, error) {
if p.consume("+") {
return p.primary()
} else if p.consume("-") {
return p.insert(&node{kind: numNode, val: 0.0}, p.primary, subNode)
}
return p.primary()
}
func (p *parser) primary() (*node, error) {
if p.consume("(") {
n, err := p.add()
if err != nil {
return nil, err
}
p.consume(")")
return n, nil
}
if p.tokens[p.i].kind == identToken {
str := p.tokens[p.i].str
p.i++
if p.i < len(p.tokens) && p.consume("(") {
return p.functionNode(str)
}
p.i--
return p.constantNode(str)
}
return p.numberNode()
}