This repository has been archived by the owner on Mar 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 652
/
pkgc.go
114 lines (107 loc) · 2.52 KB
/
pkgc.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
// pkgc is a tool for generating wrappers for Go packages imported by Grumpy
// programs.
//
// usage: pkgc PACKAGE
//
// Where PACKAGE is the full Go package name. Generated code is dumped to
// stdout. Packages generated in this way can be imported by Grumpy programs
// using string literal import syntax, e.g.:
//
// import "__go__/encoding/json"
//
// Or:
//
// from "__go__/time" import Duration
package main
import (
"bytes"
"fmt"
"go/constant"
"go/importer"
"go/types"
"math"
"os"
"path"
)
const packageTemplate = `package %[1]s
import (
"grumpy"
"reflect"
mod %[2]q
)
func fun(f *grumpy.Frame, _ []*grumpy.Object) (*grumpy.Object, *grumpy.BaseException) {
%[3]s
return nil, nil
}
var Code = grumpy.NewCode("<module>", %[2]q, nil, 0, fun)
func init() {
grumpy.RegisterModule("__go__/%[2]s", Code)
}
`
const typeTemplate = ` if true {
var x mod.%[1]s
if o, raised := grumpy.WrapNative(f, reflect.ValueOf(x)); raised != nil {
return nil, raised
} else if raised = f.Globals().SetItemString(f, %[1]q, o.Type().ToObject()); raised != nil {
return nil, raised
}
}
`
const varTemplate = ` if o, raised := grumpy.WrapNative(f, reflect.ValueOf(%[1]s)); raised != nil {
return nil, raised
} else if raised = f.Globals().SetItemString(f, %[2]q, o); raised != nil {
return nil, raised
}
`
func getConst(name string, v constant.Value) string {
format := "%s"
switch v.Kind() {
case constant.Int:
if constant.Sign(v) >= 0 {
if i, exact := constant.Uint64Val(v); exact {
if i > math.MaxInt64 {
format = "uint64(%s)"
}
} else {
format = "float64(%s)"
}
}
case constant.Float:
format = "float64(%s)"
}
return fmt.Sprintf(format, name)
}
func main() {
if len(os.Args) != 2 {
fmt.Fprint(os.Stderr, "usage: pkgc PACKAGE")
os.Exit(1)
}
pkgPath := os.Args[1]
pkg, err := importer.Default().Import(pkgPath)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to import: %q: %v\n", pkgPath, err)
os.Exit(2)
}
var buf bytes.Buffer
scope := pkg.Scope()
for _, name := range scope.Names() {
o := scope.Lookup(name)
if !o.Exported() {
continue
}
switch x := o.(type) {
case *types.TypeName:
if types.IsInterface(x.Type()) {
continue
}
buf.WriteString(fmt.Sprintf(typeTemplate, name))
case *types.Const:
expr := getConst("mod." + name, x.Val())
buf.WriteString(fmt.Sprintf(varTemplate, expr, name))
default:
expr := "mod." + name
buf.WriteString(fmt.Sprintf(varTemplate, expr, name))
}
}
fmt.Printf(packageTemplate, path.Base(pkgPath), pkgPath, buf.Bytes())
}