package main import ( "bufio" "io" "log" "os" "strconv" "strings" ) type callback interface { moveTo(x, y float32) lineTo(x, y float32) draw(argb uint32) } func xy(args []string) (float32, float32) { x, err := strconv.ParseFloat(args[0], 32) if err != nil { log.Fatal(err) } y, err := strconv.ParseFloat(args[1], 32) if err != nil { log.Fatal(err) } return float32(x), float32(y) } func argb(s string) uint32 { i, err := strconv.ParseInt(s, 0, 64) if err != nil { log.Fatal(err) } return uint32(i) } func parse(cb callback) { file, err := os.Open("draw-cmds.txt") if err != nil { log.Fatal(err) } defer file.Close() r := bufio.NewReader(file) for { s, err := r.ReadString('\n') if err != nil { if err == io.EOF { break } log.Fatal(err) } s = strings.TrimSpace(s) if s[0] == '#' { continue } a := strings.Split(strings.TrimSpace(s[1:]), " ") switch s[0] { case 'm': x, y := xy(a) cb.moveTo(x, y) case 'l': x, y := xy(a) cb.lineTo(x, y) case 'd': cb.draw(argb(a[0])) } } }