list.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func parseList(s string) ([]string, []string) {
  7. if !isList(value{word: 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, list
  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", errWordExpceted))
  55. }
  56. return value{tp: typeWord, word: val1.word + toString(val2)}
  57. }
  58. func opSentence(val1, val2 value) value {
  59. list1, list2 := []string{toString(val1)}, []string{toString(val2)}
  60. if isList(val1) {
  61. list1 = val1.list
  62. }
  63. if isList(val2) {
  64. list2 = val2.list
  65. }
  66. return value{word: makeList(append(list1, list2...))}
  67. }
  68. func opList(val1, val2 value) value {
  69. return value{word: makeList([]string{toString(val1), toString(val2)})}
  70. }
  71. func opJoin(val1, val2 value) value {
  72. if !isList(val1) {
  73. fmt.Println(val1, val2)
  74. panic(fmt.Errorf("join: %s", errListExpected))
  75. }
  76. return value{word: makeList(append(val1.list, toString(val2)))}
  77. }
  78. func opFirst(val1 value) value {
  79. if !isWord(val1) && !isList(val1) {
  80. panic(fmt.Errorf("first: %s", errWordOrListExpected))
  81. }
  82. if isEmpty(val1) {
  83. panic(fmt.Errorf("first: %s", errEmptyWordOrList))
  84. }
  85. if isWord(val1) {
  86. return value{word: val1.word[:2]}
  87. }
  88. val := value{word: val1.list[0]}
  89. if !isList(val) {
  90. val.word = `"` + val.word
  91. }
  92. return val
  93. }
  94. func opLast(val1 value) value {
  95. if !isWord(val1) && !isList(val1) {
  96. panic(fmt.Errorf("last: %s", errWordOrListExpected))
  97. }
  98. if isEmpty(val1) {
  99. panic(fmt.Errorf("last: %s", errEmptyWordOrList))
  100. }
  101. if isWord(val1) {
  102. return value{word: `"` + val1.word[len(val1.word)-1:]}
  103. }
  104. val := value{word: val1.list[len(val1.list)-1]}
  105. if !isList(val) {
  106. val.word = `"` + val.word
  107. }
  108. return val
  109. }
  110. func opButFirst(val1 value) value {
  111. if !isWord(val1) && !isList(val1) {
  112. panic(fmt.Errorf("butfirst: %s", errWordOrListExpected))
  113. }
  114. if isEmpty(val1) {
  115. panic(fmt.Errorf("butfirst: %s", errEmptyWordOrList))
  116. }
  117. if isWord(val1) {
  118. return value{word: `"` + val1.word[2:]}
  119. }
  120. return value{word: makeList(val1.list[1:])}
  121. }
  122. func opButLast(val1 value) value {
  123. if !isWord(val1) && !isList(val1) {
  124. panic(fmt.Errorf("butlast: %s", errWordOrListExpected))
  125. }
  126. if isEmpty(val1) {
  127. panic(fmt.Errorf("butlast: %s", errEmptyWordOrList))
  128. }
  129. if isWord(val1) {
  130. return value{word: val1.word[:len(val1.word)-1]}
  131. }
  132. return value{word: makeList(val1.list[:len(val1.list)-1])}
  133. }