list.go 4.4 KB

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