gigaparsec/internal/bindgen/bindgen.go
2024-09-13 20:48:33 -06:00

126 lines
2.1 KiB
Go

package main
import (
_ "embed"
"errors"
"flag"
"fmt"
"os"
"strconv"
"text/template"
)
//go:embed bind.go.tmpl
var bind string
//go:embed seq.go.tmpl
var seq string
var bindTmpl, seqTmpl *template.Template
func main() {
err := run()
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
}
func run() error {
bindPath := flag.String("bindpath", "", "bind file path")
seqPath := flag.String("seqpath", "", "seq file path")
maxBindLen := flag.Int("max", 0, "max bind length")
pkg := flag.String("pkg", "", "output package")
flag.Parse()
if *bindPath == "" {
return errors.New("bindpath required")
}
if *seqPath == "" {
return errors.New("seqpath required")
}
if *maxBindLen == 0 {
return errors.New("maxbind required")
}
if *pkg == "" {
return errors.New("pkg required")
}
bindTmpl = template.Must(template.New("bind").Parse(bind))
seqTmpl = template.Must(template.New("seq").Parse(seq))
fbind, err := os.OpenFile(*bindPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
if err != nil {
return err
}
defer fbind.Close()
data := File{
Package: *pkg,
Counter: Counter(*maxBindLen),
}
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)
}
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
}