core.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package core
  2. import "fmt"
  3. type Any interface{}
  4. func MOD(a, b Any) int {
  5. var n, m int
  6. if isInt(a) {
  7. n = a.(int)
  8. } else if isFloat(a) {
  9. n = int(a.(float64))
  10. } else {
  11. panic("need int/float argument to mod!")
  12. }
  13. if isInt(b) {
  14. m = b.(int)
  15. } else if isFloat(a) {
  16. m = int(b.(float64))
  17. } else {
  18. panic("need int/float argument to mod!")
  19. }
  20. return n % m
  21. }
  22. func ADD(args ...Any) float64 {
  23. var sum float64 = 0
  24. for i := 0; i < len(args); i++ {
  25. switch n := args[i]; {
  26. case isInt(n):
  27. sum += float64(n.(int))
  28. case isFloat(n):
  29. sum += n.(float64)
  30. }
  31. }
  32. return sum
  33. }
  34. func SUB(args ...Any) float64 {
  35. var result float64
  36. if isInt(args[0]) {
  37. result = float64(args[0].(int))
  38. } else if isFloat(args[0]) {
  39. result = args[0].(float64)
  40. } else {
  41. panic("need int/float for SUB")
  42. }
  43. for i := 1; i < len(args); i++ {
  44. switch n := args[i]; {
  45. case isInt(n):
  46. result -= float64(n.(int))
  47. case isFloat(n):
  48. result -= n.(float64)
  49. }
  50. }
  51. return result
  52. }
  53. func MUL(args ...Any) float64 {
  54. var prod float64 = 1
  55. for i := 0; i < len(args); i++ {
  56. switch n := args[i]; {
  57. case isInt(n):
  58. prod *= float64(n.(int))
  59. case isFloat(n):
  60. prod *= n.(float64)
  61. }
  62. }
  63. return prod
  64. }
  65. func DIV() {}
  66. // TODO: can only compare ints and slice lens for now.
  67. func LT(args ...Any) bool {
  68. if len(args) < 2 {
  69. panic("can't compare less than 2 values!")
  70. }
  71. for i := 0; i < len(args)-1; i++ {
  72. var n float64
  73. if isInt(args[i]) {
  74. n = float64(args[i].(int))
  75. } else if isFloat(args[i]) {
  76. n = args[i].(float64)
  77. } else {
  78. panic("you can't compare that!")
  79. }
  80. var m float64
  81. if isInt(args[i+1]) {
  82. m = float64(args[i+1].(int))
  83. } else if isFloat(args[i+1]) {
  84. m = args[i+1].(float64)
  85. } else {
  86. panic("you can't compare that!")
  87. }
  88. if n >= m {
  89. return false
  90. }
  91. }
  92. return true
  93. }
  94. // TODO: can only compare ints and slice lens for now.
  95. func GT(args ...Any) bool {
  96. if len(args) < 2 {
  97. panic("can't compare less than 2 values!")
  98. }
  99. for i := 0; i < len(args)-1; i++ {
  100. var n float64
  101. if isInt(args[i]) {
  102. n = float64(args[i].(int))
  103. } else if isFloat(args[i]) {
  104. n = args[i].(float64)
  105. } else {
  106. panic("you can't compare that!")
  107. }
  108. var m float64
  109. if isInt(args[i+1]) {
  110. m = float64(args[i+1].(int))
  111. } else if isFloat(args[i+1]) {
  112. m = args[i+1].(float64)
  113. } else {
  114. panic("you can't compare that!")
  115. }
  116. if n <= m {
  117. return false
  118. }
  119. }
  120. return true
  121. }
  122. func EQ(args ...Any) bool {
  123. if len(args) < 2 {
  124. panic("can't compare less than 2 values!")
  125. }
  126. for i := 0; i < len(args)-1; i++ {
  127. var n float64
  128. if isInt(args[i]) {
  129. n = float64(args[i].(int))
  130. } else if isFloat(args[i]) {
  131. n = args[i].(float64)
  132. } else {
  133. panic("you can't compare that!")
  134. }
  135. var m float64
  136. if isInt(args[i+1]) {
  137. m = float64(args[i+1].(int))
  138. } else if isFloat(args[i+1]) {
  139. m = args[i+1].(float64)
  140. } else {
  141. panic("you can't compare that!")
  142. }
  143. if n != m {
  144. return false
  145. }
  146. }
  147. return true
  148. }
  149. // greater than or equal
  150. func GTEQ(args ...Any) bool {
  151. if GT(args...) || EQ(args...) {
  152. return true
  153. }
  154. return false
  155. }
  156. // less than or equal
  157. func LTEQ(args ...Any) bool {
  158. if LT(args...) || EQ(args...) {
  159. return true
  160. }
  161. return false
  162. }
  163. func isFloat(n Any) bool {
  164. _, ok := n.(float64)
  165. return ok
  166. }
  167. func isInt(n Any) bool {
  168. _, ok := n.(int)
  169. return ok
  170. }
  171. func Get(args ...Any) Any {
  172. if len(args) != 2 && len(args) != 3 {
  173. panic(fmt.Sprintf("get needs 2 or 3 arguments %d given.", len(args)))
  174. }
  175. if len(args) == 2 {
  176. if a, ok := args[1].([]Any); ok {
  177. return a[args[0].(int)]
  178. } else if a, ok := args[1].(string); ok {
  179. return a[args[0].(int)]
  180. } else {
  181. panic("arguments to get must include slice/vector/string")
  182. }
  183. } else {
  184. if a, ok := args[2].([]Any); ok {
  185. if args[1].(int) == -1 {
  186. return a[args[0].(int):]
  187. }
  188. return a[args[0].(int):args[1].(int)]
  189. } else if a, ok := args[2].(string); ok {
  190. if args[1].(int) == -1 {
  191. return a[args[0].(int):]
  192. }
  193. return a[args[0].(int):args[1].(int)]
  194. } else {
  195. panic("arguments to get must include slice/vector/string")
  196. }
  197. }
  198. }