repl.go 706 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package repl
  2. import (
  3. "bufio"
  4. "fmt"
  5. "github/runnignwater/monkey/lexer"
  6. "github/runnignwater/monkey/token"
  7. "io"
  8. )
  9. /**
  10. * @Author: simon
  11. * @Author: ynwdlxm@163.com
  12. * @Date: 2022/10/2 下午5:41
  13. * @Desc: REPL that tokenizes Monkey source code and prints the tokens
  14. */
  15. const PROMPT = ">>> "
  16. func Start(in io.Reader, out io.Writer) {
  17. scanner := bufio.NewScanner(in)
  18. for {
  19. fmt.Printf(PROMPT)
  20. scan := scanner.Scan()
  21. if !scan {
  22. return
  23. }
  24. line := scanner.Text()
  25. l := lexer.New(line)
  26. for tok := l.NextToken(); tok.Type != token.EOF; tok = l.NextToken() {
  27. fmt.Printf("%+v\n", tok)
  28. }
  29. }
  30. }