-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
339 lines (284 loc) · 7.12 KB
/
parser.go
File metadata and controls
339 lines (284 loc) · 7.12 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package mathopt
import (
"fmt"
"strconv"
"unicode"
)
// TokenType represents the type of a token
type TokenType int
const (
TokenNumber TokenType = iota
TokenVariable
TokenOperator
TokenLeftParen
TokenRightParen
TokenFunction
TokenEOF
)
// Token represents a lexical token
type Token struct {
Type TokenType
Value string
}
// Tokenizer converts a string into tokens
type Tokenizer struct {
input string
pos int
}
// NewTokenizer creates a new tokenizer
func NewTokenizer(input string) *Tokenizer {
return &Tokenizer{input: input, pos: 0}
}
// NextToken returns the next token from the input
func (t *Tokenizer) NextToken() (*Token, error) {
// Skip whitespace
for t.pos < len(t.input) && unicode.IsSpace(rune(t.input[t.pos])) {
t.pos++
}
if t.pos >= len(t.input) {
return &Token{Type: TokenEOF}, nil
}
ch := t.input[t.pos]
// Number
if unicode.IsDigit(rune(ch)) || ch == '.' {
return t.readNumber()
}
// Variable or function
if unicode.IsLetter(rune(ch)) {
return t.readIdentifier()
}
// Operators and parentheses
t.pos++
switch ch {
case '+', '*', '/', '^':
return &Token{Type: TokenOperator, Value: string(ch)}, nil
case '-':
// Could be unary minus or binary minus
return &Token{Type: TokenOperator, Value: string(ch)}, nil
case '(':
return &Token{Type: TokenLeftParen, Value: string(ch)}, nil
case ')':
return &Token{Type: TokenRightParen, Value: string(ch)}, nil
default:
return nil, fmt.Errorf("unexpected character: %c at position %d", ch, t.pos-1)
}
}
func (t *Tokenizer) readNumber() (*Token, error) {
start := t.pos
hasDot := false
for t.pos < len(t.input) {
ch := t.input[t.pos]
if unicode.IsDigit(rune(ch)) {
t.pos++
} else if ch == '.' && !hasDot {
hasDot = true
t.pos++
} else {
break
}
}
value := t.input[start:t.pos]
return &Token{Type: TokenNumber, Value: value}, nil
}
func (t *Tokenizer) readIdentifier() (*Token, error) {
start := t.pos
for t.pos < len(t.input) && (unicode.IsLetter(rune(t.input[t.pos])) || unicode.IsDigit(rune(t.input[t.pos]))) {
t.pos++
}
value := t.input[start:t.pos]
// Check if it's a function
functions := []string{"sin", "cos", "exp", "ln"}
for _, fn := range functions {
if value == fn {
return &Token{Type: TokenFunction, Value: value}, nil
}
}
return &Token{Type: TokenVariable, Value: value}, nil
}
// Parser parses tokens into an expression tree
type Parser struct {
tokens []*Token
current int
}
// Parse parses a mathematical expression string into an Expr
func Parse(input string) (Expr, error) {
tokenizer := NewTokenizer(input)
var tokens []*Token
for {
token, err := tokenizer.NextToken()
if err != nil {
return nil, err
}
tokens = append(tokens, token)
if token.Type == TokenEOF {
break
}
}
parser := &Parser{tokens: tokens, current: 0}
return parser.parseExpression()
}
func (p *Parser) parseExpression() (Expr, error) {
return p.parseAddSub()
}
func (p *Parser) parseAddSub() (Expr, error) {
left, err := p.parseMulDiv()
if err != nil {
return nil, err
}
for p.current < len(p.tokens) {
token := p.tokens[p.current]
if token.Type == TokenOperator && (token.Value == "+" || token.Value == "-") {
p.current++
right, err := p.parseMulDiv()
if err != nil {
return nil, err
}
left = &BinaryOp{Op: token.Value, Left: left, Right: right}
} else {
break
}
}
return left, nil
}
func (p *Parser) parseMulDiv() (Expr, error) {
left, err := p.parsePower()
if err != nil {
return nil, err
}
for p.current < len(p.tokens) {
token := p.tokens[p.current]
if token.Type == TokenOperator && (token.Value == "*" || token.Value == "/") {
p.current++
right, err := p.parsePower()
if err != nil {
return nil, err
}
left = &BinaryOp{Op: token.Value, Left: left, Right: right}
} else {
break
}
}
return left, nil
}
func (p *Parser) parsePower() (Expr, error) {
left, err := p.parseUnary()
if err != nil {
return nil, err
}
for p.current < len(p.tokens) {
token := p.tokens[p.current]
if token.Type == TokenOperator && token.Value == "^" {
p.current++
right, err := p.parseUnary()
if err != nil {
return nil, err
}
left = &BinaryOp{Op: token.Value, Left: left, Right: right}
} else {
break
}
}
return left, nil
}
func (p *Parser) parseUnary() (Expr, error) {
if p.current >= len(p.tokens) {
return nil, fmt.Errorf("unexpected end of expression")
}
token := p.tokens[p.current]
// Unary minus
if token.Type == TokenOperator && token.Value == "-" {
p.current++
arg, err := p.parseUnary()
if err != nil {
return nil, err
}
return &UnaryOp{Op: "-", Arg: arg}, nil
}
// Function call
if token.Type == TokenFunction {
fnName := token.Value
p.current++
if p.current >= len(p.tokens) || p.tokens[p.current].Type != TokenLeftParen {
return nil, fmt.Errorf("expected '(' after function %s", fnName)
}
p.current++ // skip '('
arg, err := p.parseExpression()
if err != nil {
return nil, err
}
if p.current >= len(p.tokens) || p.tokens[p.current].Type != TokenRightParen {
return nil, fmt.Errorf("expected ')' after function argument")
}
p.current++ // skip ')'
return &UnaryOp{Op: fnName, Arg: arg}, nil
}
return p.parsePrimary()
}
func (p *Parser) parsePrimary() (Expr, error) {
if p.current >= len(p.tokens) {
return nil, fmt.Errorf("unexpected end of expression")
}
token := p.tokens[p.current]
p.current++
switch token.Type {
case TokenNumber:
value, err := strconv.ParseFloat(token.Value, 64)
if err != nil {
return nil, fmt.Errorf("invalid number: %s", token.Value)
}
return &Constant{Value: value}, nil
case TokenVariable:
return &Variable{Name: token.Value}, nil
case TokenLeftParen:
expr, err := p.parseExpression()
if err != nil {
return nil, err
}
if p.current >= len(p.tokens) || p.tokens[p.current].Type != TokenRightParen {
return nil, fmt.Errorf("expected ')'")
}
p.current++
return expr, nil
default:
return nil, fmt.Errorf("unexpected token: %s", token.Value)
}
}
// MustParse parses an expression and panics on error (useful for tests)
func MustParse(input string) Expr {
expr, err := Parse(input)
if err != nil {
panic(err)
}
return expr
}
// ExpressionEquivalence checks if two expressions are equivalent
// by simplifying both and comparing them structurally
func ExpressionEquivalence(e1, e2 Expr) bool {
s1 := e1.Simplify()
s2 := e2.Simplify()
return s1.Equals(s2)
}
// NormalizeExpression applies algebraic normalization to an expression
// This includes sorting commutative operations, collecting like terms, etc.
func NormalizeExpression(expr Expr) Expr {
simplified := expr.Simplify()
// Additional normalization could be added here
return simplified
}
// ConstantFold performs aggressive constant folding on an expression
func ConstantFold(expr Expr) Expr {
// Simplify already does constant folding, but we can be more aggressive
prev := expr
for {
current := prev.Simplify()
if current.Equals(prev) {
break
}
prev = current
}
return prev
}
// PrettyPrint returns a nicely formatted string representation
func PrettyPrint(expr Expr) string {
return expr.String()
}