This repository has been archived by the owner on Apr 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathworld.go
391 lines (344 loc) · 8.92 KB
/
world.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package eval is the beginning of an interpreter for Go.
// It can run simple Go programs but does not implement
// interface values or packages.
// go/importer is only available in go1.6 and later
// +build go1.6
package eval
import (
"errors"
"fmt"
"go/ast"
"go/importer"
"go/parser"
"go/scanner"
"go/token"
"go/types"
"regexp"
"strconv"
)
// track the status of each package we visit (unvisited/visiting/done)
var g_visiting = make(map[string]status)
// config used for type checking
var g_typesConfig = types.Config{Importer: importer.Default()}
type status int // status for visiting map
const (
unvisited status = iota
visiting
done
)
type World struct {
scope *Scope
frame *Frame
inits []Code
}
func NewWorld() *World {
w := &World{
scope: universe.ChildScope(),
}
w.scope.global = true // this block's vars allocate directly
return w
}
type Code interface {
// The type of the value Run returns, or nil if Run returns nil.
Type() Type
// Run runs the code; if the code is a single expression
// with a value, it returns the value; otherwise it returns nil.
Run() (Value, error)
}
type pkgCode struct {
w *World
code code
}
func (p *pkgCode) Type() Type { return nil }
func (p *pkgCode) Run() (Value, error) {
t := new(Thread)
t.f = p.w.scope.NewFrame(nil)
return nil, t.Try(func(t *Thread) { p.code.exec(t) })
}
func (w *World) CompilePackage(fset *token.FileSet, files []*ast.File, pkgpath string) (Code, error) {
pkgFiles := make(map[string]*ast.File)
for _, f := range files {
pkgFiles[f.Name.Name] = f
}
//pkg, err := ast.NewPackage(fset, pkgFiles, srcImporter, types.Universe)
pkg, err := g_typesConfig.Check(files[0].Name.String(), fset, files, nil)
if err != nil {
return nil, err
}
if pkg == nil {
return nil, errors.New("could not create an AST package out of ast.Files")
}
switch g_visiting[pkgpath] {
case done:
return &pkgCode{w, make(code, 0)}, nil
case visiting:
//fmt.Printf("** package dependency cycle **\n")
return nil, errors.New("package dependency cycle")
}
g_visiting[pkgpath] = visiting
// create a new scope in which to process this new package
imports := []*ast.ImportSpec{}
for _, f := range files {
imports = append(imports, f.Imports...)
}
for _, imp := range imports {
path, _ := strconv.Unquote(imp.Path.Value)
if _, ok := universe.pkgs[path]; ok {
// already compiled
continue
}
imp_files, err := findPkgFiles(path)
if err != nil {
return nil, errors.New(fmt.Sprintf("could not find files for package [%s]", path))
}
code, err := w.CompilePackage(fset, imp_files, path)
if err != nil {
return nil, err
}
_, err = code.Run()
if err != nil {
return nil, err
}
}
prev_scope := w.scope
w.scope = w.scope.ChildScope()
w.scope.global = true
defer func() {
g_visiting[pkgpath] = done
// add this scope (the package's scope) to the lookup-table of packages
universe.pkgs[pkgpath] = w.scope
// restore the previous scope
w.scope.exit()
if pkgpath != "main" {
w.scope = prev_scope
}
}()
decls := make([]ast.Decl, 0)
for _, f := range files {
decls = append(decls, f.Decls...)
}
code, err := w.CompileDeclList(fset, decls)
if err != nil {
return nil, err
}
_, err = code.Run()
if err != nil {
return nil, err
}
//FIXME: make sure all the imports are used at this point.
{
// store the init function (if any) for later use
init_code, init_err := w.Compile(fset, "init()")
if init_code != nil {
if init_err == nil || init_err != nil {
w.inits = append(w.inits, init_code)
}
}
}
return code, err
}
type stmtCode struct {
w *World
code code
}
func (w *World) CompileStmtList(fset *token.FileSet, stmts []ast.Stmt) (Code, error) {
if len(stmts) == 1 {
if s, ok := stmts[0].(*ast.ExprStmt); ok {
return w.CompileExpr(fset, s.X)
}
}
errors := new(scanner.ErrorList)
cc := &compiler{fset, errors, 0, 0}
cb := newCodeBuf()
fc := &funcCompiler{
compiler: cc,
fnType: nil,
outVarsNamed: false,
codeBuf: cb,
flow: newFlowBuf(cb),
labels: make(map[string]*label),
}
bc := &blockCompiler{
funcCompiler: fc,
block: w.scope.block,
}
nerr := cc.numError()
for _, stmt := range stmts {
bc.compileStmt(stmt)
}
fc.checkLabels()
if nerr != cc.numError() {
errors.Sort()
return nil, errors.Err()
}
return &stmtCode{w, fc.get()}, nil
}
func (w *World) CompileDeclList(fset *token.FileSet, decls []ast.Decl) (Code, error) {
stmts := make([]ast.Stmt, len(decls))
for i, d := range decls {
stmts[i] = &ast.DeclStmt{d}
}
return w.CompileStmtList(fset, stmts)
}
func (s *stmtCode) Type() Type { return nil }
func (s *stmtCode) Run() (Value, error) {
t := new(Thread)
t.f = s.w.scope.NewFrame(nil)
return nil, t.Try(func(t *Thread) { s.code.exec(t) })
}
type exprCode struct {
w *World
e *expr
eval func(Value, *Thread)
}
func (w *World) CompileExpr(fset *token.FileSet, e ast.Expr) (Code, error) {
errors := new(scanner.ErrorList)
cc := &compiler{fset, errors, 0, 0}
ec := cc.compileExpr(w.scope.block, false, e)
if ec == nil {
errors.Sort()
return nil, errors.Err()
}
var eval func(Value, *Thread)
switch t := ec.t.(type) {
case *idealIntType:
// nothing
case *idealFloatType:
// nothing
default:
if tm, ok := t.(*MultiType); ok && len(tm.Elems) == 0 {
return &stmtCode{w, code{ec.exec}}, nil
}
eval = genAssign(ec.t, ec)
}
return &exprCode{w, ec, eval}, nil
}
func (e *exprCode) Type() Type { return e.e.t }
func (e *exprCode) Run() (Value, error) {
t := new(Thread)
t.f = e.w.scope.NewFrame(nil)
switch e.e.t.(type) {
case *idealIntType:
return &idealIntV{e.e.asIdealInt()()}, nil
case *idealFloatType:
return &idealFloatV{e.e.asIdealFloat()()}, nil
}
v := e.e.t.Zero()
eval := e.eval
err := t.Try(func(t *Thread) { eval(v, t) })
return v, err
}
func (w *World) run_init() error {
// run the 'init()' function of all dependent packages
for _, init_code := range w.inits {
_, err := init_code.Run()
if err != nil {
return err
}
}
// reset
w.inits = make([]Code, 0)
return nil
}
// Regexp to match the import keyword
var import_regexp = regexp.MustCompile("[ \t\n]*import[ \t(]")
func (w *World) Compile(fset *token.FileSet, text string) (Code, error) {
if text == "main()" {
err := w.run_init()
if err != nil {
return nil, err
}
}
if i := import_regexp.FindStringIndex(text); i != nil && i[0] == 0 {
// special case for import-ing on the command line...
//return w.compileImport(fset, text)
return nil, fmt.Errorf("sorry. import \"foo\" not (yet?) implemented")
}
stmts, err := parseStmtList(fset, text)
if err == nil {
return w.CompileStmtList(fset, stmts)
}
// Otherwise try as DeclList
decls, err1 := parseDeclList(fset, text)
if err1 == nil {
return w.CompileDeclList(fset, decls)
}
// Have to pick an error.
// Parsing as statement list admits more forms,
// its error is more likely to be useful.
return nil, err
}
func (w *World) compileImport(fset *token.FileSet, text string) (Code, error) {
f, err := parser.ParseFile(fset, "input", "package main;"+text, 0)
if err != nil {
return nil, err
}
imp := f.Imports[0]
path, _ := strconv.Unquote(imp.Path.Value)
imp_files, err := findPkgFiles(path)
if err != nil {
return nil, errors.New(fmt.Sprintf("could not find files for package [%s]", path))
}
{
code, err := w.CompilePackage(fset, imp_files, path)
if err != nil {
return nil, err
}
_, err = code.Run()
if err != nil {
return nil, err
}
err = w.run_init()
if err != nil {
return nil, err
}
}
return w.CompileDeclList(fset, f.Decls)
}
func parseStmtList(fset *token.FileSet, src string) ([]ast.Stmt, error) {
f, err := parser.ParseFile(fset, "input", "package p;func _(){"+src+"\n}", 0)
if err != nil {
return nil, err
}
return f.Decls[0].(*ast.FuncDecl).Body.List, nil
}
func parseDeclList(fset *token.FileSet, src string) ([]ast.Decl, error) {
f, err := parser.ParseFile(fset, "input", "package p;"+src, 0)
if err != nil {
return nil, err
}
return f.Decls, nil
}
type RedefinitionError struct {
Name string
Prev Def
}
func (e *RedefinitionError) Error() string {
res := "identifier " + e.Name + " redeclared"
pos := e.Prev.Pos()
if pos.IsValid() {
// TODO: fix this - currently this code is not reached by the tests
// need to get a file set (fset) from somewhere
//res += "; previous declaration at " + fset.Position(pos).String()
panic(0)
}
return res
}
func (w *World) DefineConst(name string, t Type, val Value) error {
_, prev := w.scope.DefineConst(name, token.NoPos, t, val)
if prev != nil {
return &RedefinitionError{name, prev}
}
return nil
}
func (w *World) DefineVar(name string, t Type, val Value) error {
v, prev := w.scope.DefineVar(name, token.NoPos, t)
if prev != nil {
return &RedefinitionError{name, prev}
}
v.Init = val
return nil
}