sointu/vm/generate/generate.go
5684185+vsariola@users.noreply.github.com e010b2da9d docs: improve go doc comments for vm package
2023-10-19 11:54:12 +03:00

50 lines
1.1 KiB
Go

//go:build ignore
// +build ignore
package main
import (
"fmt"
"os"
"strings"
"github.com/vsariola/sointu/vm"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
outputFile, err := os.Create("opcodes.go")
check(err)
defer outputFile.Close()
fmt.Fprintln(outputFile, "package vm")
fmt.Fprintln(outputFile, "")
fmt.Fprintln(outputFile, "// Code generated by go generate; DO NOT EDIT.")
fmt.Fprintln(outputFile, "const (")
features := vm.AllFeatures{}
max := 0
for _, instr := range features.Instructions() {
if l := len(instr); max < l {
max = l
}
}
for i, instr := range features.Instructions() {
format := fmt.Sprintf("\top%%-%vv = %%v\n", max)
fmt.Fprintf(outputFile, format, strings.Title(instr), i+1)
}
fmt.Fprintln(outputFile, ")")
fmt.Fprintln(outputFile, "")
fmt.Fprintf(outputFile, "var transformCounts = [...]int{")
for i, instr := range features.Instructions() {
if i > 0 {
fmt.Fprintf(outputFile, ", ")
}
fmt.Fprintf(outputFile, "%v", features.TransformCount(instr))
}
fmt.Fprintln(outputFile, "}")
}