-
Notifications
You must be signed in to change notification settings - Fork 0
/
stmt.go
165 lines (131 loc) · 2.97 KB
/
stmt.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
package ego
import (
"go/ast"
"github.com/bobg/ego/refl"
)
type branchType int
const (
branchBreak branchType = 1 + iota
branchContinue
branchGoto
branchFallthrough
branchReturn
)
type branch struct {
typ branchType
vals []refl.Value
label string
}
func (s *Scope) ExecStmts(stmts []ast.Stmt) (*Scope, *branch, error) {
origScope := s
scopes := make([]*Scope, len(stmts))
for i := 0; i < len(stmts); {
stmt := stmts[i]
scopes[i] = s
s, b, err := s.ExecStmt(stmt)
if err != nil {
return nil, nil, err
}
if b != nil {
switch b.typ {
case branchBreak:
return origScope, b, nil
case branchContinue:
return origScope, b, nil
case branchGoto:
var found bool
for j := 0; j <= i; j++ {
if l, ok := stmts[j].(*ast.LabeledStmt); ok && l.Label.Name == b.label {
i = j
s = scopes[j]
found = true
break
}
}
if !found {
var newScope bool
for j := i + 1; j < len(stmts); j++ {
if l, ok := stmts[j].(*ast.LabeledStmt); ok && l.Label.Name == b.label {
if newScope {
// xxx err - cannot goto into a new scope
}
i = j
found = true
break
}
if isNewScopeStmt(stmt) {
newScope = true
}
}
}
if found {
continue
}
return origScope, b, nil
case branchFallthrough:
return origScope, b, nil
case branchReturn:
return origScope, b, nil
}
}
}
return origScope, nil, nil
}
func (s *Scope) ExecStmt(stmt ast.Stmt) (*Scope, *branch, error) {
switch stmt := stmt.(type) {
case *ast.DeclStmt:
// xxx
case *ast.EmptyStmt:
return s, nil, nil
case *ast.LabeledStmt:
switch inner := stmt.Stmt.(type) {
case *ast.ForStmt:
return s.execFor(inner, stmt.Label.Name)
case *ast.SwitchStmt:
return s.execSwitch(inner, stmt.Label.Name)
case *ast.TypeSwitchStmt:
return s.execTypeSwitch(inner, stmt.Label.Name)
case *ast.SelectStmt:
return s.execSelect(inner, stmt.Label.Name)
}
return s.ExecStmt(stmt.Stmt)
case *ast.ExprStmt:
_, err := s.Eval(stmt.X)
return s, nil, err
case *ast.SendStmt:
return s.execSend(stmt)
case *ast.IncDecStmt:
return s.execIncDec(stmt)
case *ast.AssignStmt:
return s.execAssign(stmt)
case *ast.GoStmt:
go func() {
// xxx need some way to communicate errors
s.evalCall(stmt.Call)
}()
case *ast.DeferStmt:
return s.execDefer(stmt)
case *ast.ReturnStmt:
return s.execReturn(stmt)
case *ast.BranchStmt:
return s.execBranch(stmt)
case *ast.BlockStmt:
return s.ExecStmts(stmt.List)
case *ast.IfStmt:
return s.execIf(stmt)
case *ast.CaseClause:
// xxx error - outside switch or typeswitch
case *ast.SwitchStmt:
return s.execSwitch(stmt, "")
case *ast.TypeSwitchStmt:
return s.execTypeSwitch(stmt, "")
case *ast.CommClause:
// xxx error - outside select
case *ast.SelectStmt:
return s.execSelect(stmt, "")
case *ast.ForStmt:
return s.execFor(stmt, "")
case *ast.RangeStmt:
return s.execRange(stmt)
}
}