-
Notifications
You must be signed in to change notification settings - Fork 0
/
expr.go
166 lines (146 loc) · 2.79 KB
/
expr.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
package ast
import (
"fmt"
participle "github.com/alecthomas/participle/v2"
"github.com/alecthomas/participle/v2/lexer"
)
var (
unaryParser = participle.MustBuild(
&Unary{},
participle.Lexer(Lexer),
)
operatorToken = Lexer.Symbols()["Operator"]
)
type Expr struct {
Unary *Unary
Left *Expr
Op Op
Right *Expr
}
// Parse expressions with a precedence climbing implementation.
func (e *Expr) Parse(lex *lexer.PeekingLexer) error {
ex, err := parseExpr(lex, 0)
if err != nil {
return err
}
*e = *ex
return nil
}
type Op int
const (
OpNone Op = iota //
OpGe // >=
OpLe // <=
OpAnd // &&
OpOr // ||
OpEq // ==
OpNe // !=
OpSub // -
OpAdd // +
OpMul // *
OpDiv // /
OpLt // <
OpGt // >
OpMod // %
OpPow // ^
OpNot // !
OpMrg // &
)
func (o *Op) Capture(values []string) error {
switch values[0] {
case ">=":
*o = OpGe
case "<=":
*o = OpLe
case "&&":
*o = OpAnd
case "||":
*o = OpOr
case "==":
*o = OpEq
case "!=":
*o = OpNe
case "-":
*o = OpSub
case "+":
*o = OpAdd
case "*":
*o = OpMul
case "/":
*o = OpDiv
case "%":
*o = OpMod
case "<":
*o = OpLt
case ">":
*o = OpGt
case "^":
*o = OpPow
case "!":
*o = OpNot
case "&":
*o = OpMrg
default:
return fmt.Errorf("invalid expression operator %q", values[0])
}
return nil
}
type opInfo struct {
RightAssociative bool
Priority int
}
var opTable = map[Op]opInfo{
OpAdd: {Priority: 1},
OpSub: {Priority: 1},
OpMul: {Priority: 2},
OpDiv: {Priority: 2},
OpMod: {Priority: 2},
OpPow: {RightAssociative: true, Priority: 3},
OpMrg: {Priority: 4},
}
// Precedence climbing implementation based on
// https://eli.thegreenplace.net/2012/08/02/parsing-expressions-by-precedence-climbing
func parseExpr(lex *lexer.PeekingLexer, minPrec int) (*Expr, error) {
lhs, err := parseOperand(lex)
if err != nil {
return nil, err
}
for {
token, err := lex.Peek(0)
if err != nil {
return nil, err
}
if token.Type != operatorToken {
break
}
expr := &Expr{}
err = expr.Op.Capture([]string{token.Value})
if err != nil {
return lhs, nil
}
if opTable[expr.Op].Priority < minPrec {
break
}
_, _ = lex.Next()
nextMinPrec := opTable[expr.Op].Priority
if !opTable[expr.Op].RightAssociative {
nextMinPrec++
}
rhs, err := parseExpr(lex, nextMinPrec)
if err != nil {
return nil, err
}
expr.Left = lhs
expr.Right = rhs
lhs = expr
}
return lhs, nil
}
func parseOperand(lex *lexer.PeekingLexer) (*Expr, error) {
u := &Unary{}
err := unaryParser.ParseFromLexer(lex, u, participle.AllowTrailing(true))
if err != nil {
return nil, err
}
return &Expr{Unary: u}, nil
}