-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypeinst.go
More file actions
61 lines (54 loc) · 1.28 KB
/
typeinst.go
File metadata and controls
61 lines (54 loc) · 1.28 KB
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
package main
import (
"fmt"
"log"
"os"
"path"
"strings"
)
const fileSuffix = "_ti" // generated file suffix
func main() {
gofile := os.Getenv("GOFILE")
fmt.Printf("$GOPATH = %v\n$GOFILE = %v\n", os.Getenv("GOPATH"), gofile)
fatalIfErr(Run(gofile))
}
// Run - convenience func for tests.
func Run(gofile string) (err error) {
defer bpan.RecoverTo(&err)
implFile := implFilename(gofile, fileSuffix)
dsl, err := ParseDSL(gofile, "")
bpan.Check(err)
impl := newImpl(implFile, dsl.PkgName)
dsl2Impl(dsl, impl)
return
}
func dsl2Impl(dsl *DSL, impl *Impl) {
for _, it := range dsl.Items {
for _, g := range it.GenericTypes {
p, err := impl.Package(g.PkgName, dsl.Imports)
bpan.Check(err)
log.Printf("dsl: type %s = %s with args: %v", it.InstName, g.Type, it.TypeArgs)
bpan.Check(p.Inst(g.Type, it.InstName, it.TypeArgs))
}
}
for path, pdesc := range impl.pkg {
log.Printf("walk: %s", path)
bpan.Check(pdesc.resolveGeneric())
}
log.Printf("printing...")
bpan.Check(impl.Print())
}
func implFilename(p, suf string) string {
f := path.Base(p)
pos := strings.LastIndex(f, ".go")
if pos == -1 {
log.Fatal("not a .go file")
}
f = f[0:pos]
return path.Join(path.Dir(p), f+suf+".go")
}
func fatalIfErr(err error) {
if err != nil {
log.Fatalf("error: %v", err)
}
}