forked from zjryan/e8vm
/
imports.go
59 lines (47 loc) · 1006 Bytes
/
imports.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
package g8
import (
"io"
"e8vm.io/e8vm/build8"
"e8vm.io/e8vm/g8/ast"
"e8vm.io/e8vm/g8/parse"
"e8vm.io/e8vm/lex8"
)
type importDecl struct {
as string
path string
pos *lex8.Pos
}
func listImport(
f string, rc io.ReadCloser, imp build8.Importer, golike bool,
) []*lex8.Error {
fast, _, es := parse.File(f, rc, golike)
if es != nil {
return es
}
if fast.Imports == nil {
return nil
}
m := make(map[string]*importDecl)
log := lex8.NewErrorList()
for _, d := range fast.Imports.Decls {
p, as, e := ast.ImportPathAs(d)
if e != nil {
log.Errorf(d.Path.Pos, "invalid path string %s", d.Path.Lit)
continue
}
pos := ast.ImportPos(d)
if other, found := m[as]; found {
log.Errorf(pos, "%s already imported", as)
log.Errorf(other.pos, " previously imported here")
continue
}
m[as] = &importDecl{as: as, path: p, pos: pos}
}
if errs := log.Errs(); errs != nil {
return errs
}
for as, d := range m {
imp.Import(as, d.path, d.pos)
}
return nil
}