repl.go 600 B

123456789101112131415161718192021222324252627282930313233
  1. package repl
  2. import (
  3. "bufio"
  4. "code.osinet.fr/fgm/waiig15/lexer"
  5. "code.osinet.fr/fgm/waiig15/token"
  6. "fmt"
  7. "io"
  8. )
  9. // PROMPT is the prompt shown to the user of the REPL to invite them to type
  10. // some code.
  11. const PROMPT = ">> "
  12. // Start implements the Read-Eval-Print Loop.
  13. func Start(in io.Reader, out io.Writer) {
  14. scanner := bufio.NewScanner(in)
  15. for {
  16. fmt.Print(PROMPT)
  17. scanned := scanner.Scan()
  18. if !scanned {
  19. return
  20. }
  21. line := scanner.Text()
  22. l := lexer.New(line)
  23. for tok := l.NextToken(); tok.Type != token.EOF; tok = l.NextToken() {
  24. fmt.Printf("%+v\n", tok)
  25. }
  26. }
  27. }