package main import ( "bufio" "fmt" "io" "os" ) var DEBUG = false func args() { filename := os.Args[1] f, err := os.Open(filename) if err != nil { fmt.Fprintln(os.Stderr, "unable to read file ", filename) os.Exit(1) } defer f.Close() c := make(chan token, 32) go lex(bufio.NewReader(f), c) for s := range c { fmt.Printf("%11s %s\n", s.t, s.lexeme) } } func main() { if len(os.Args) > 1 { args() return } r := bufio.NewReader(os.Stdin) for { fmt.Print("> ") line, prefix, err := r.ReadLine() if prefix { fmt.Println("(prefix)") } switch err { case nil: break case io.EOF: fmt.Print("\n") return default: fmt.Println("error: ", err) continue } c := make(chan token, 32) go lexs(string(line)+"\n", c) for s := range c { fmt.Printf("%11s %s\n", s.t, s.lexeme) } } }