-
Notifications
You must be signed in to change notification settings - Fork 7
/
interpstmt.go
233 lines (221 loc) · 5.48 KB
/
interpstmt.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
229
230
231
232
233
package eval
import (
"fmt"
"reflect"
"go/token"
)
type State struct {
Last Stmt
Env Env
}
func InterpStmt(stmt Stmt, env Env) (last *State, err error) {
switch s := stmt.(type) {
case nil:
case *AssignStmt:
if len(s.Rhs) == 1 {
rs, err := evalTypedExpr(s.Rhs[0], s.types, env)
if err != nil {
if _, ok := err.(PanicInterfaceConversion); !ok || len(s.types) != 2 {
return nil, err
}
}
for i, lhs := range s.Lhs {
if name, ok := s.newNames[i]; !ok {
assign(lhs, rs[i], env)
} else if name != "_" {
v := hackedNew(s.types[i])
v.Elem().Set(rs[i])
env.AddVar(name, v)
}
}
} else {
for i, lhs := range s.Lhs {
r, err := evalTypedExpr(s.Rhs[i], s.types[i:i+1], env)
if err != nil {
return nil, err
}
if name, ok := s.newNames[i]; !ok {
assign(lhs, r[0], env)
} else if name != "_" {
v := hackedNew(s.types[i])
v.Elem().Set(r[0])
env.AddVar(name, v)
}
}
}
case *BranchStmt:
return &State{s, env}, nil
case *BlockStmt:
return interpBlock(s.List, env)
case *CaseClause:
return interpBlock(s.Body, env)
case *EmptyStmt:
return nil, nil
case *ExprStmt:
_, err := EvalExpr(s.X, env)
return nil, err
case *IfStmt:
env = env.PushScope()
if _ , err = InterpStmt(s.Init, env); err != nil {
return nil, err
} else if rs, err := EvalExpr(s.Cond, env); err != nil {
return nil, err
} else if rs[0].Bool() {
return InterpStmt(s.Body, env)
} else {
return InterpStmt(s.Else, env)
}
case *ForStmt:
env = env.PushScope()
if _, err = InterpStmt(s.Init, env); err != nil {
return nil, err
}
for {
if s.Cond != nil {
if rs, err := EvalExpr(s.Cond, env); err != nil {
return nil, err
} else if !rs[0].Bool() {
break
}
}
if last, err = InterpStmt(s.Body, env); err != nil {
return last, err
}
if last != nil {
if branch, ok := last.Last.(*BranchStmt); ok {
// Are we the target of this branch?
if branch.Label == nil || branch.Label.Name == s.label {
last = nil
if branch.Tok == token.CONTINUE {
goto cont
}
}
}
return last, nil
}
cont:
if _, err = InterpStmt(s.Post, env); err != nil {
return nil, err
}
}
case *LabeledStmt:
return InterpStmt(s.Stmt, env)
case *ReturnStmt:
return &State{s, env}, nil
case *SwitchStmt:
env = env.PushScope()
t := knownType{s.tagT}
if _, err := InterpStmt(s.Init, env); err != nil {
return nil, err
}
tag, err := evalTypedExpr(s.Tag, t, env)
if err != nil {
return nil, err
}
env = env.PushScope()
for _, stmt := range s.Body.List {
clause := stmt.(*CaseClause)
for _, expr := range clause.List {
if sw, err := evalTypedExpr(expr, t, env); err != nil {
return nil, err
} else if eq, err := equal(tag[0], sw[0]); err != nil {
return nil, err
} else if eq {
return InterpStmt(clause, env)
}
}
}
return InterpStmt(s.def, env)
case *TypeSwitchStmt:
env = env.PushScope()
if _, err = InterpStmt(s.Init, env); err != nil {
return nil, err
}
x, err := EvalExpr(s.Tag(), env)
if err != nil {
return nil, err
}
// interface.elem()
dynamicX := x[0].Elem()
dynamicT := dynamicX.Type()
env = env.PushScope()
if name := s.Name(); name != "" {
// dynamicX may not be addressable
x := reflect.New(dynamicT)
x.Elem().Set(dynamicX)
env.AddVar(name, x)
}
for _, stmt := range s.Body.List {
clause := stmt.(*CaseClause)
for _, expr := range clause.List {
t := expr.KnownType()[0]
if t.Kind() == reflect.Interface {
if dynamicT.Implements(t) {
return InterpStmt(clause, env)
}
} else if t == dynamicT {
return InterpStmt(clause, env)
}
}
}
return InterpStmt(s.def, env)
default:
panic(dytc(fmt.Sprintf("Unsupported statement %T", s)))
}
return nil, nil
}
func assign(lhs Expr, rhs reflect.Value, env Env) error {
lhs = skipSuperfluousParens(lhs)
// Always evaluate even if we are doing a map index assign. There are some nasty
// corner cases with map index comparibility that is best left not reimplemented.
if l, err := evalTypedExpr(lhs, lhs.KnownType(), env); err != nil {
return err
} else if index, ok := lhs.(*IndexExpr); ok && index.X.KnownType()[0].Kind() == reflect.Map {
mT := index.X.KnownType()[0]
// known to succeed from above
m, _ := evalTypedExpr(index.X, knownType{mT}, env)
k, _ := evalTypedExpr(index.Index, knownType{mT.Elem()}, env)
m[0].SetMapIndex(k[0], rhs)
} else {
l[0].Set(rhs)
}
return nil
}
func interpBlock(list []Stmt, env Env) (last *State, err error) {
for i := 0; i < len(list); i += 1 {
if last, err = InterpStmt(list[i], env); err != nil {
return last, err
} else if last != nil {
if last, i = branch(list, last, env); last != nil {
break
}
}
}
return last, nil
}
// Really naive implementation which simply scans block for the branch target
func branch(list []Stmt, last *State, env Env) (*State, int) {
branch, ok := last.Last.(*BranchStmt)
if !ok {
return last, 0
}
// breaks should go to the next stmt, goto should go to the labeled stmt
brk := 0
if branch.Tok == token.GOTO {
brk = -1
}
for i, stmt := range list {
switch s := stmt.(type) {
case *LabeledStmt:
if branch.Label != nil && branch.Label.Name == s.Label.Name {
return nil, i+brk
}
// TODO[crc] add SelectStmt and RangeStmt here when implemented
case *ForStmt, *SwitchStmt, *TypeSwitchStmt:
if branch.Label == nil {
return nil, i+brk
}
}
}
return last, 0
}