Files
subcoding/grow/cli/main.go
2021-11-20 20:02:16 -10:00

60 lines
993 B
Go

package main
import "flag"
import "log"
import "math/rand"
import "os"
import "time"
import "github.com/firestuff/subcoding/asm"
import "github.com/firestuff/subcoding/grow"
func main() {
defPath := flag.String("def-path", "", "path to definition YAML file")
flag.Parse()
if *defPath == "" {
log.Fatal("Please specify --def-path")
}
rand.Seed(time.Now().UnixNano())
defFile, err := os.Open(*defPath)
if err != nil {
log.Fatal(err)
}
def, err := grow.NewDefinition(defFile)
if err != nil {
log.Fatal(err)
}
statusChan := make(chan grow.Status)
go func() {
_, err = def.Grow(statusChan)
if err != nil {
log.Fatal(err)
}
}()
for {
status, ok := <-statusChan
if !ok {
break
}
if status.BestProgram == nil {
continue
}
src, err := asm.Disassemble(status.BestProgram)
if err != nil {
log.Fatal(err)
}
log.Printf("New best score %d / %d (after %d attempts):\n%s", status.BestScore, status.TargetScore, status.Attempts, src)
}
}