/
builtins.go
146 lines (124 loc) · 3.14 KB
/
builtins.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
//go:generate go run ../../cmd/builtingen ../../language/builtin.hlb ../lookup.go
package gen
import (
"bytes"
"context"
"fmt"
"go/format"
"html/template"
"io"
"log"
"os"
"strconv"
"strings"
"github.com/openllb/hlb/parser"
"github.com/openllb/hlb/parser/ast"
"github.com/openllb/hlb/pkg/filebuffer"
)
type BuiltinData struct {
Command string
FuncsByKind map[ast.Kind][]ParsedFunc
Reference string
}
type ParsedFunc struct {
Name string
Params []*ast.Field
Effects []*ast.Field
}
func GenerateBuiltins(ctx context.Context, r io.Reader) ([]byte, error) {
files := filebuffer.NewBuffers()
ctx = filebuffer.WithBuffers(ctx, files)
mod, err := parser.Parse(ctx, r)
if err != nil {
return nil, err
}
funcsByKind := make(map[ast.Kind][]ParsedFunc)
for _, decl := range mod.Decls {
fd := decl.Func
if fd == nil {
continue
}
var effects []*ast.Field
if fd.Sig.Effects != nil && fd.Sig.Effects.Effects != nil {
effects = fd.Sig.Effects.Effects.Fields()
}
kind := fd.Kind()
funcsByKind[kind] = append(funcsByKind[kind], ParsedFunc{
Name: fd.Sig.Name.Text,
Params: fd.Sig.Params.Fields(),
Effects: effects,
})
}
fb := files.Get(mod.Pos.Filename)
data := BuiltinData{
Command: fmt.Sprintf("builtingen %s", strings.Join(os.Args[1:], " ")),
FuncsByKind: funcsByKind,
Reference: fmt.Sprintf("`%s`", string(fb.Bytes())),
}
var buf bytes.Buffer
err = referenceTmpl.Execute(&buf, &data)
if err != nil {
return nil, err
}
src, err := format.Source(buf.Bytes())
if err != nil {
log.Printf("warning: internal error: invalid Go generated: %s", err)
log.Printf("warning: compile the package to analyze the error")
src = buf.Bytes()
}
return src, nil
}
var tmplFunctions = template.FuncMap{
"kind": func(kind ast.Kind) template.HTML {
switch kind {
case ast.String:
return template.HTML("ast.String")
case ast.Int:
return template.HTML("ast.Int")
case ast.Bool:
return template.HTML("ast.Bool")
case ast.Filesystem:
return template.HTML("ast.Filesystem")
default:
return template.HTML(strconv.Quote(string(kind)))
}
},
}
var referenceTmpl = template.Must(template.New("reference").Funcs(tmplFunctions).Parse(`
// Code generated by {{.Command}}; DO NOT EDIT.
package builtin
import "github.com/openllb/hlb/parser/ast"
type BuiltinLookup struct {
ByKind map[ast.Kind]LookupByKind
}
type LookupByKind struct {
Func map[string]FuncLookup
}
type FuncLookup struct {
Params []*ast.Field
Effects []*ast.Field
}
var (
Lookup = BuiltinLookup{
ByKind: map[ast.Kind]LookupByKind{
{{range $kind, $funcs := .FuncsByKind}}{{kind $kind}}: {
Func: map[string]FuncLookup{
{{range $i, $func := $funcs}}"{{$func.Name}}": {
Params: []*ast.Field{
{{range $i, $param := $func.Params}}ast.NewField({{kind $param.Type.Kind}}, "{{$param.Name}}", {{if $param.Modifier}}true{{else}}false{{end}}),
{{end}}
},
Effects: []*ast.Field{
{{range $i, $effect := $func.Effects}}ast.NewField({{kind $effect.Type.Kind}}, "{{$effect.Name}}", false),
{{end}}
},
},
{{end}}
},
},
{{end}}
},
}
Reference = {{.Reference}}
)
`))