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
|
package lexpr
import (
"context"
"strings"
)
type Lexpr struct {
operators map[string]Operator
functions map[string]func(ts *TokenStack) error
variables map[string]any
}
func New(opts ...Opt) *Lexpr {
l := &Lexpr{}
for _, o := range opts {
o(l)
}
return l
}
func (l *Lexpr) Eval(ctx context.Context, expression string) chan Result {
lexer := newLex()
lexems := lexer.parse(ctx, expression)
tokens := l.tokenize(ctx, lexems)
rpnTokens := infixToRpn(ctx, tokens)
return l.execute(ctx, rpnTokens)
}
func (l *Lexpr) SetFunction(name string, fn func(ts *TokenStack) error) *Lexpr {
l.functions[strings.ToLower(name)] = fn
return l
}
func (l *Lexpr) SetOperator(name string, fn func(ts *TokenStack) error, priority int, leftAssoc bool) *Lexpr {
l.operators[strings.ToLower(name)] = Operator{
handler: fn,
priority: priority,
leftAssoc: leftAssoc,
}
return l
}
func (l *Lexpr) SetVariable(name string, value any) *Lexpr {
l.variables[strings.ToLower(name)] = value
return l
}
type Result struct {
Value any
Error error
}
|