gigaparsec/internal/bindgen/bindgen.go

126 lines
2.1 KiB
Go
Raw Normal View History

2024-09-14 00:28:38 +00:00
package main
import (
_ "embed"
"errors"
"flag"
"fmt"
"os"
"strconv"
"text/template"
)
//go:embed bind.go.tmpl
var bind string
2024-09-14 02:48:33 +00:00
//go:embed seq.go.tmpl
var seq string
var bindTmpl, seqTmpl *template.Template
2024-09-14 00:28:38 +00:00
func main() {
err := run()
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
}
func run() error {
2024-09-14 02:48:33 +00:00
bindPath := flag.String("bindpath", "", "bind file path")
seqPath := flag.String("seqpath", "", "seq file path")
2024-09-14 00:28:38 +00:00
maxBindLen := flag.Int("max", 0, "max bind length")
pkg := flag.String("pkg", "", "output package")
flag.Parse()
2024-09-14 02:48:33 +00:00
if *bindPath == "" {
return errors.New("bindpath required")
}
if *seqPath == "" {
return errors.New("seqpath required")
2024-09-14 00:28:38 +00:00
}
if *maxBindLen == 0 {
return errors.New("maxbind required")
}
if *pkg == "" {
return errors.New("pkg required")
}
2024-09-14 02:48:33 +00:00
bindTmpl = template.Must(template.New("bind").Parse(bind))
seqTmpl = template.Must(template.New("seq").Parse(seq))
2024-09-14 00:28:38 +00:00
2024-09-14 02:48:33 +00:00
fbind, err := os.OpenFile(*bindPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
2024-09-14 00:28:38 +00:00
if err != nil {
return err
}
2024-09-14 02:48:33 +00:00
defer fbind.Close()
2024-09-14 00:28:38 +00:00
data := File{
Package: *pkg,
Counter: Counter(*maxBindLen),
}
2024-09-14 02:48:33 +00:00
err = bindTmpl.Execute(fbind, data)
if err != nil {
return err
}
fseq, err := os.OpenFile(*seqPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
if err != nil {
return err
}
defer fseq.Close()
return seqTmpl.Execute(fseq, data)
2024-09-14 00:28:38 +00:00
}
type File struct {
Package string
Counter
}
// func (f File) Funcs() []Func {
// counters := f.Max.Count()
// funcs := make([]Func, len(counters))
// for i, counter := range counters {
// funcs[i] = Func{
// Max: f.Max,
// Length: counter,
// }
// }
// return funcs
// }
type Func struct {
Max Counter
Length Counter
}
type Counter int
func (c Counter) String() string {
if c == 1 {
return ""
}
return strconv.Itoa(int(c))
}
func (c Counter) Int() int {
return int(c)
}
func (c Counter) Next() Counter {
return c + 1
}
func (c Counter) Prev() Counter {
return c - 1
}
func (c Counter) Count() []Counter {
a := make([]Counter, c)
for i := range a {
a[i] = Counter(i + 1)
}
return a
}