repl.go 601 B

12345678910111213141516171819202122232425262728293031323334
  1. package repl
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "code.osinet.fr/fgm/waiig15/lexer"
  7. "code.osinet.fr/fgm/waiig15/token"
  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. }