-
Notifications
You must be signed in to change notification settings - Fork 0
/
cond.go
213 lines (194 loc) · 4.55 KB
/
cond.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
package ego
import (
"fmt"
"go/ast"
"go/token"
)
func (s *Scope) execIf(stmt *ast.IfStmt) (*Scope, *branch, error) {
origScope := s
if stmt.Init != nil {
var err error
s, _, err = s.ExecStmt(stmt.Init)
if err != nil {
return nil, nil, err
}
}
cond, err := s.Eval1(stmt.Cond)
if err != nil {
return nil, nil, err
}
boolval, err := cond.Bool()
if err != nil {
return nil, nil, err
}
if boolval {
_, b, err := s.ExecStmts(stmt.Body.List)
return origScope, b, err
}
if stmt.Else != nil {
_, b, err := s.ExecStmt(stmt.Else)
return origScope, b, err
}
return origScope, nil, nil
}
func (s *Scope) execSwitch(stmt *ast.SwitchStmt, label string) (*Scope, *branch, error) {
var err error
origScope := s
if stmt.Init != nil {
s, _, err = s.ExecStmt(stmt.Init)
if err != nil {
return nil, nil, err
}
}
var tag *refl.Value
if stmt.Tag != nil {
tag, err = s.Eval1(stmt.Tag)
if err != nil {
return nil, nil, err
}
} else {
tag = refl.ValueOf(true)
}
fallingThrough := false
for _, item := range stmt.Body.List {
clause, ok := item.(*ast.CaseClause)
if !ok {
return nil, nil, fmt.Errorf("got statement type %T in switch, want CaseClause", item)
}
if !fallingThrough && len(clause.List) > 0 {
match := false
for _, e := range clause.List {
v, err := s.Eval1(e)
if err != nil {
return nil, nil, err
}
if xxx /* v == tag, with proper type jiggering */ {
match = true
break
}
}
if !match {
continue
}
}
// default clause, falling through, or match
fallingThrough = false
_, b, err := s.ExecStmts(clause.Body)
if err != nil {
return nil, nil, err
}
if b != nil {
switch b.typ {
case branchBreak:
if b.label == "" || b.label == label {
return origScope, nil, nil
}
return origScope, b, nil
case branchContinue:
return origScope, b, nil
case branchGoto:
return origScope, b, nil
case branchFallthrough:
fallingThrough = true
}
}
if !fallingThrough {
return origScope, nil, nil
}
}
return origScope, nil, nil
}
func (s *Scope) execTypeSwitch(stmt *ast.TypeSwitchStmt, label string) (*Scope, *branch, error) {
var err error
origScope := s
if stmt.Init != nil {
s, _, err = s.ExecStmt(stmt)
if err != nil {
return nil, nil, err
}
}
var (
varName string // for "switch x := y.(type)"
assignExpr ast.Expr
)
switch a := stmt.Assign.(type) {
case *ast.AssignStmt:
if a.Tok != token.DEFINE {
return nil, nil, fmt.Errorf("assignment must use := in type switch")
}
if len(a.Lhs) != 1 {
return nil, nil, fmt.Errorf("got %d items on lhs of assignment in type switch, may only have 1", len(a.Lhs))
}
id, ok := a.Lhs[0].(*ast.Ident)
if !ok {
return nil, nil, fmt.Errorf("got %T on lhs of assignment in type switch, want Ident", a.Lhs[0])
}
varName = id.Name
if len(a.Rhs) != 1 {
return nil, nil, fmt.Errorf("got %d items on rhs of assignment in type switch, may only have 1", len(a.Rhs))
}
assignExpr = a.Rhs[0]
case *ast.ExprStmt:
assignExpr = a.X
default:
return nil, nil, fmt.Errorf("got %T in type switch guard, want ExprStmt or AssignStmt", a)
}
val, err := s.Eval1(assignExpr)
if err != nil {
return nil, nil, err
}
for _, clause := range stmt.Body.List {
clause, ok := clause.(*ast.CaseClause)
if !ok {
return nil, nil, fmt.Errorf("got statement type %T in type switch, want CaseClause", item)
}
var matchType *refl.Type
if len(clause.List) > 0 {
for _, e := range clause.List {
t, err := s.EvalType(e)
if err != nil {
return nil, nil, err
}
if val.Type().AssignableTo(t) {
matchType = t
break
}
}
if matchType == nil {
continue
}
// xxx if len(clause.List) > 1, varName does not get the type
// matchType, but the type of assignExpr.
if varName != "" {
s = NewScope(s)
// This makes sure variable varName has the right type in
// scope s, even if val is a different (but assignable) type.
v := refl.NewValue(matchType)
v.Set(val)
s.Add(varName, v)
}
}
// default clause or match
_, b, err := s.ExecStmts(clause.Body)
if err != nil {
return nil, nil, err
}
if b != nil {
switch b.typ {
case branchBreak:
if b.label == "" || b.label == label {
return origScope, nil, nil
}
return origScope, b, nil
case branchContinue:
return origScope, b, nil
case branchGoto:
return origScope, b, nil
case branchFallthrough:
return nil, nil, fmt.Errorf("fallthrough not allowed in type switch")
}
}
break
}
return origScope, nil, nil
}