io.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "strings"
  6. )
  7. func opRead(scanner *bufio.Scanner) value {
  8. s := "\n"
  9. for s == "\n" && scanner.Scan() {
  10. s = scanner.Text()
  11. }
  12. if s == "\n" {
  13. panic(fmt.Errorf("read: %s", errUnexpectedEndOfInput))
  14. }
  15. return value{val: `"` + s}
  16. }
  17. func opPrint(val1 value) value {
  18. s := val1.val
  19. if s != "" {
  20. if s[0] == '"' {
  21. fmt.Println(s[1:])
  22. } else if s[0] == '[' {
  23. list, space := strings.Split(s, " "), false
  24. for i := 0; i < len(list); i++ {
  25. if space && list[i] != "]" {
  26. fmt.Print(" ")
  27. }
  28. fmt.Print(list[i])
  29. space = list[i] != "["
  30. }
  31. fmt.Println()
  32. } else {
  33. fmt.Println(s)
  34. }
  35. }
  36. return val1
  37. }
  38. func opReadList(scanner *bufio.Scanner) value {
  39. list, line := []string{}, false
  40. for scanner.Scan() {
  41. s := scanner.Text()
  42. if s == "\n" && line {
  43. break
  44. }
  45. line = true
  46. if s[0] == '[' || s[len(s)-1] == ']' {
  47. panic(fmt.Errorf("readlist: %s, (%s)", errWordExpceted, s))
  48. }
  49. if s != "\n" {
  50. list = append(list, s)
  51. }
  52. }
  53. return value{val: "[ " + strings.Join(list, " ") + " ]"}
  54. }