list.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func parseList(s string) []string {
  7. if !isList(&_unknown{s: s}) {
  8. panic(fmt.Errorf("list: %s (%s)", errListExpected, s))
  9. }
  10. list, bracketCnt, start, resList := strings.Split(s, " "), 0, 1, []string{}
  11. for i := 1; i < len(list)-1; i++ {
  12. if list[i] == "[" {
  13. bracketCnt++
  14. }
  15. if list[i] == "]" {
  16. bracketCnt--
  17. }
  18. if bracketCnt == 0 {
  19. resList = append(resList, strings.Join(list[start:i+1], " "))
  20. start = i + 1
  21. }
  22. }
  23. return resList
  24. }
  25. func makeList(list []string) string {
  26. return strings.Join(append(append([]string{"["}, list...), "]"), " ")
  27. }
  28. func indexOfList(list []string, index int) string {
  29. if index < 0 || index >= len(list) {
  30. panic(fmt.Errorf("list: %s (%d / %d)", errIndexOutOfBound, index, len(list)))
  31. }
  32. return list[index]
  33. }
  34. func rangeOfList(list []string, index1, index2 int) string {
  35. if index1 < 0 || index1 > len(list) || index2 < 0 || index2 > len(list) {
  36. panic(fmt.Errorf("list: %s (%d : %d / %d)", errIndexOutOfBound, index1, index2, len(list)))
  37. }
  38. if index1 > index2 {
  39. panic(fmt.Errorf("list: %s (%d : %d)", errIllegalRange, index1, index2))
  40. }
  41. return makeList(list[index1:index2])
  42. }
  43. func spliceList(list []string, index int, cnt int, val ...string) string {
  44. if index < 0 || index > len(list) {
  45. panic(fmt.Errorf("list: %s (%d / %d)", errIndexOutOfBound, index, len(list)))
  46. }
  47. if index+cnt > len(list) {
  48. cnt = len(list) - index
  49. }
  50. return makeList(append(append(list[:index], val...), list[index+cnt:]...))
  51. }
  52. func opWord(val1, val2 value) value {
  53. if !isWord(val1) || isList(val2) {
  54. panic(fmt.Errorf("word: %s (%s, %s)", errWordExpceted, val1.String(), val2.String()))
  55. }
  56. return &_unknown{s: `"` + toWord(val1) + val2.String()}
  57. }
  58. func opSentence(val1, val2 value) value {
  59. list1, list2 := []string{val1.String()}, []string{val2.String()}
  60. if isList(val1) {
  61. list1 = toList(val1)
  62. }
  63. if isList(val2) {
  64. list2 = toList(val2)
  65. }
  66. return &_unknown{s: makeList(append(list1, list2...))}
  67. }
  68. func opList(val1, val2 value) value {
  69. return &_unknown{s: makeList([]string{val1.String(), val2.String()})}
  70. }
  71. func opJoin(val1, val2 value) value {
  72. if !isList(val1) {
  73. panic(fmt.Errorf("join: %s (%s)", errListExpected, val1.String()))
  74. }
  75. return &_unknown{s: makeList(append(toList(val1), val2.String()))}
  76. }
  77. func opFirst(val1 value) value {
  78. if !isWord(val1) && !isList(val1) {
  79. panic(fmt.Errorf("first: %s (%s)", errWordOrListExpected, val1.String()))
  80. }
  81. if isEmpty(val1) {
  82. panic(fmt.Errorf("first: %s (%s)", errEmptyWordOrList, val1.String()))
  83. }
  84. if isWord(val1) {
  85. s := escapeWord(toWord(val1))
  86. return &_unknown{s: `"` + s[:1]}
  87. }
  88. list := toList(val1)
  89. s := list[0]
  90. if !isList(&_unknown{s: s}) {
  91. s = `"` + s
  92. }
  93. return &_unknown{s: s}
  94. }
  95. func opLast(val1 value) value {
  96. if !isWord(val1) && !isList(val1) {
  97. panic(fmt.Errorf("last: %s (%s)", errWordOrListExpected, val1.String()))
  98. }
  99. if isEmpty(val1) {
  100. panic(fmt.Errorf("last: %s (%s)", errEmptyWordOrList, val1.String()))
  101. }
  102. if isWord(val1) {
  103. s := escapeWord(toWord(val1))
  104. return &_unknown{s: `"` + s[len(s)-1:]}
  105. }
  106. list := toList(val1)
  107. s := list[len(list)-1]
  108. if !isList(&_unknown{s: s}) {
  109. s = `"` + s
  110. }
  111. return &_unknown{s: s}
  112. }
  113. func opButFirst(val1 value) value {
  114. if !isWord(val1) && !isList(val1) {
  115. panic(fmt.Errorf("butfirst: %s (%s)", errWordOrListExpected, val1.String()))
  116. }
  117. if isEmpty(val1) {
  118. panic(fmt.Errorf("butfirst: %s (%s)", errEmptyWordOrList, val1.String()))
  119. }
  120. if isWord(val1) {
  121. s := escapeWord(toWord(val1))
  122. return &_unknown{s: `"` + s[1:]}
  123. }
  124. list := toList(val1)
  125. return &_unknown{s: makeList(list[1:])}
  126. }
  127. func opButLast(val1 value) value {
  128. if !isWord(val1) && !isList(val1) {
  129. panic(fmt.Errorf("butlast: %s (%s)", errWordOrListExpected, val1.String()))
  130. }
  131. if isEmpty(val1) {
  132. panic(fmt.Errorf("butlast: %s (%s)", errEmptyWordOrList, val1.String()))
  133. }
  134. if isWord(val1) {
  135. s := escapeWord(toWord(val1))
  136. return &_unknown{s: `"` + s[:len(s)-1]}
  137. }
  138. list := toList(val1)
  139. return &_unknown{s: makeList(list[:len(list)-1])}
  140. }