-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
164 lines (144 loc) · 3.32 KB
/
main.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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/alecthomas/kong"
"github.com/alecthomas/participle/v2"
"github.com/alecthomas/repr"
)
type AST struct {
Decls []*Decl `@@*`
}
type Decl struct {
Func *Func `@@`
}
// eg. send-receive-skip-search : func(process-id: u64, timeout: u32) -> u32
type Func struct {
Name string `@(Ident ("-" Ident)*)`
Args []*Arg `":" "func" "(" (@@ ("," @@)* ","?)? ")"`
Ret *Ret `("-" ">" @@)?`
}
type Arg struct {
Name string `@(Ident ("-" Ident)*)`
Type *Type `":" @@`
}
type Ret struct {
Type *Type `@@`
}
type Type struct {
Ident string `@Ident`
}
var (
parser = participle.MustBuild[AST]()
witTemplate = template.Must(
template.New("wit").
Funcs(template.FuncMap{
"Public": func(s string) string {
parts := strings.Split(s, "-")
for i := range parts {
parts[i] = strings.Title(parts[i])
}
return strings.Join(parts, "")
},
"Private": func(s string) string {
parts := strings.Split(s, "-")
for i := range parts {
if i > 0 {
parts[i] = strings.Title(parts[i])
} else {
parts[i] = strings.ToLower(parts[i])
}
}
return strings.Join(parts, "")
},
"Type": func(t *Type) string {
switch t.Ident {
case "u8":
return "uint8"
case "u16":
return "uint16"
case "u32":
return "uint32"
case "u64":
return "uint64"
case "s8":
return "int8"
case "s16":
return "int16"
case "s32":
return "int32"
case "s64":
return "int64"
case "float32":
return "float32"
case "float64":
return "float64"
default:
panic("unknown type")
}
},
}).
Parse(`package {{ .Package }}
{{ range .AST.Decls -}}
{{with .Func -}}
//go:wasm-module {{$.Module}}
//go:export {{.Name}}
func {{.Name|Public}}(
{{- range $idx, $arg := .Args -}}
{{- if $idx}}, {{end -}}
{{- .Name|Private}} {{.Type|Type}}
{{- end -}}
){{- if .Ret}} {{.Ret.Type|Type}}{{end}}
{{end -}}
{{end}}
`))
cli struct {
Dump bool `required:"" xor:"action" help:"Dump the AST."`
Dest string `short:"o" required:"" xor:"action" help:"Destination directory."`
Files []string `required:"" arg:"" type:"existingfile" help:"Files to generate from."`
}
)
func main() {
kctx := kong.Parse(&cli)
for _, file := range cli.Files {
f, err := os.Open(file)
kctx.FatalIfErrorf(err)
defer f.Close()
ast, err := parser.Parse(file, f)
kctx.FatalIfErrorf(err)
if cli.Dump {
repr.Println(ast)
} else {
kctx.FatalIfErrorf(codegen(cli.Dest, file, ast))
}
}
}
func codegen(dest, filename string, ast *AST) error {
name := strings.TrimSuffix(filepath.Base(filename), ".wit")
// wit/lunatic_timer.wit -> lunatic::timer
module := strings.ReplaceAll(name, "_", "::")
pkgParts := strings.Split(name, "_")
path := filepath.Join(pkgParts...)
pkg := pkgParts[len(pkgParts)-1]
err := os.MkdirAll(filepath.Join(dest, path), 0755)
if err != nil {
return fmt.Errorf("failed to create directory: %w", err)
}
w, err := os.Create(filepath.Join(dest, path, pkg+".go"))
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
}
defer w.Close()
return witTemplate.Execute(w, struct {
Module string
Package string
AST *AST
}{
Module: module,
Package: pkg,
AST: ast,
})
}