-
Notifications
You must be signed in to change notification settings - Fork 0
/
interp.go
270 lines (242 loc) · 5.88 KB
/
interp.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package ego
import (
"go/ast"
"go/parser"
"go/token"
"io"
"path"
"unicode"
)
type Interp struct {
scopes map[string]*Scope
}
func NewInterp() *Interp {
return &Interp{scopes: make(map[string]*Scope)}
}
func (in *Interp) Load(importPath string, r io.Reader) error {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, xxxfilename, r, parser.DeclarationErrors)
if err != nil {
return err
}
// xxx check f.Name matches importPath
pkgScope, ok := in.scopes[importPath]
if !ok {
pkgScope = NewScope(nil)
in.scopes[importPath] = pkgScope
}
return in.ImportFile(pkgScope, f)
}
// ImportPath imports the package located at importPath. It creates a
// new top-level scope for the package, and subordinate scopes for
// each of its files. It also runs the init() function in each file, if any.
//
// If the path has already been imported by this interpreter, it is
// not re-imported; the previous resulting scope is returned instead.
func (in *Interp) ImportPath(importPath string) (*Scope, error) {
if s, ok := in.scopes[importPath]; ok {
return s, nil
}
fset := token.NewFileSet()
pkgs, err := parser.ParseDir(fset, importPath, pkgFilesOnly, parser.DeclarationErrors)
if err != nil {
return nil, err
}
pkgName := path.Base(importPath)
pkg, ok := pkgs[pkgName]
if !ok {
// xxx err
}
pkgScope := NewScope(nil)
in.scopes[importPath] = pkgScope
for _, f := range pkg.Files {
err = in.doImportFile(pkgScope, f)
if err != nil {
return nil, err
}
}
err = pkgScope.initializeVars()
return pkgScope, err
}
func (in *Interp) ImportFile(pkgScope *Scope, f *ast.File) error {
err := doImportFile(pkgScope, f)
if err != nil {
return err
}
err = pkgScope.initializeVars()
return err
}
func (in *Interp) doImportFile(pkgScope *Scope, f *ast.File) error {
fileScope := NewScope(pkgScope)
// Process imports first.
for _, spec := range f.Imports {
p, err := literalStr(spec.Path.Value)
if err != nil {
return nil, err
}
impScope, err := in.ImportPath(p)
if err != nil {
return nil, err
}
var importName string
if spec.Name == nil {
importName = path.Base(p)
} else {
importName = spec.Name.Name
}
if importName == "." {
for k, v := range impScope.objs {
if !isExported(k) {
continue
}
// xxx should imported symbols overwrite symbols already in scope?
fileScope.objs[k] = v
}
} else {
// xxx SetScope (and other Scope.Set* functions) should ignore a
// name of "_"
fileScope.SetScope(importName, impScope)
}
}
// Process type declarations second, before var, func, and const
// decls (which might depend on them).
for _, decl := range f.Decls {
decl, ok := decl.(*ast.GenDecl)
if !ok {
continue
}
if decl.Tok != token.TYPE {
continue
}
for i, spec := range decl.Specs {
spec, ok := spec.(*ast.TypeSpec)
if !ok {
// xxx err
}
pkgScope.SetType(spec.Name.Name, spec.Type)
}
}
for _, decl := range f.Decls {
// Each top-level decl gets a slot in pkgScope, but is evaluated
// in fileScope (to allow imported symbols to resolve).
switch decl := decl.(type) {
case *ast.GenDecl:
switch decl.Tok {
case token.IMPORT:
// ignore - already handled in the f.Imports clause above
continue
case token.CONST:
var (
lastExprList []ast.Expr
lastType ast.Expr
)
constScope := NewScope(fileScope)
for i, spec := range decl.Specs {
constScope.SetInt("iota", i)
spec, ok := spec.(*ast.ValueSpec)
if !ok {
// xxx err
}
var (
exprList = spec.Values
constType = spec.Type
)
if len(exprList) == 0 {
if i == 0 {
// xxx err
}
exprList = lastExprList
constType = lastType // xxx can type be present when exprlist is absent?
} else {
lastExprList = exprList
lastType = spec.Type
}
if len(spec.Names) != len(exprList) {
// xxx err
}
var consts []Const
for _, expr := range exprList {
c, err := constScope.EvalConst(expr, constType)
if err != nil {
return nil, err
}
consts = append(consts, c)
}
for j, name := range spec.Names {
pkgScope.SetConst(name.Name, consts[j])
}
}
case token.TYPE:
// ignore - already handled above
case token.VAR:
for _, spec := range decl.Specs {
spec, ok := spec.(*ast.ValueSpec)
if !ok {
// xxx err
}
if len(spec.Values) > 0 {
// xxx let spec.Values be a single expr producing len(spec.Names) values
if len(spec.Values) != len(spec.Names) {
// xxx err
}
for i, name := range spec.Names {
pkgScope.Add(name.Name, uninitialized(spec.Values[i]))
}
} else {
for _, name := range spec.Names {
pkgScope.Add(name.Name, NewValue(typ))
}
}
}
}
case *ast.FuncDecl:
// xxx check for init()
if decl.Recv == nil {
// regular function
f, err := fileScope.makeFunc(decl.Type, decl.Body.List)
if err != nil {
return nil, err
}
pkgScope.Add(decl.Name.Name, f)
} else {
// method
if len(decl.Recv.List) != 1 {
// xxx err
}
field := decl.Recv.List[0]
if len(field.Names) != 1 {
// xxx err
}
recvName := field.Names[0].Name
recvType, isPtr, err := parseRecvType(field.Type)
// xxx find recvType in pkgScope.
// xxx add method (recvName, isPtr, decl.Type, decl.Body.List)
// to type's method set
}
default:
// xxx err
}
}
return nil
}
func isExported(name string) bool {
return unicode.IsUpper(rune(name[0]))
}
func parseRecvType(expr ast.Expr) (string, bool, err) {
switch expr := expr.(type) {
case *ast.Ident:
return expr.Name, false, nil
case *ast.ParenExpr:
return parseRecvType(expr.X)
case *ast.StarExpr:
name, isPtr, err := parseRecvType(expr.X)
if err != nil {
return "", false, err
}
if isPtr {
// xxx err
}
return name, true, nil
}
// xxx err
}