A fork of Crisp for HARP
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
3.9 KiB

  1. ;;
  2. ;; Testing try*/catch*
  3. (try* 123 (catch* e 456))
  4. ;=>123
  5. (try* (abc 1 2) (catch* exc (prn "exc is:" exc)))
  6. ; "exc is:" "'abc' not found"
  7. ;=>nil
  8. ;;;TODO: fix so long lines don't trigger ANSI escape codes ;;;(try*
  9. ;;;(try* (throw ["data" "foo"]) (catch* exc (do (prn "exc is:" exc) 7))) ;;;;
  10. ;;;; "exc is:" ["data" "foo"] ;;;;=>7
  11. ;;;;=>7
  12. (try* (throw (list 1 2 3)) (catch* exc (do (prn "err:" exc) 7)))
  13. ; "err:" (1 2 3)
  14. ;=>7
  15. (try* (throw "my exception") (catch* exc (do (prn "exc:" exc) 7)))
  16. ; "exc:" "my exception"
  17. ;=>7
  18. ;;; Test that throw is a function:
  19. (try* (map throw (list 7)) (catch* exc exc))
  20. ;=>7
  21. ;;
  22. ;; Testing builtin functions
  23. (symbol? 'abc)
  24. ;=>true
  25. (symbol? "abc")
  26. ;=>false
  27. (nil? nil)
  28. ;=>true
  29. (nil? true)
  30. ;=>false
  31. (true? true)
  32. ;=>true
  33. (true? false)
  34. ;=>false
  35. (true? true?)
  36. ;=>false
  37. (false? false)
  38. ;=>true
  39. (false? true)
  40. ;=>false
  41. ;; Testing apply function with core functions
  42. (apply + (list 2 3))
  43. ;=>5
  44. (apply + 4 (list 5))
  45. ;=>9
  46. (apply prn (list 1 2 "3" (list)))
  47. ; 1 2 "3" ()
  48. (apply prn 1 2 (list "3" (list)))
  49. ; 1 2 "3" ()
  50. ;; Testing apply function with user functions
  51. (apply (fn* (a b) (+ a b)) (list 2 3))
  52. ;=>5
  53. (apply (fn* (a b) (+ a b)) 4 (list 5))
  54. ;=>9
  55. ;; Testing map function
  56. (def! nums (list 1 2 3))
  57. (def! double (fn* (a) (* 2 a)))
  58. (double 3)
  59. ;=>6
  60. (map double nums)
  61. ;=>(2 4 6)
  62. (map (fn* (x) (symbol? x)) (list 1 (symbol "two") "three"))
  63. ;=>(false true false)
  64. ;;
  65. ;; ------- Optional Functionality ----------
  66. ;; ------- (Needed for self-hosting) -------
  67. ;; Testing symbol and keyword functions
  68. (symbol? :abc)
  69. ;=>false
  70. (symbol? 'abc)
  71. ;=>true
  72. (symbol? "abc")
  73. ;=>false
  74. (symbol? (symbol "abc"))
  75. ;=>true
  76. (keyword? :abc)
  77. ;=>true
  78. (keyword? 'abc)
  79. ;=>false
  80. (keyword? "abc")
  81. ;=>false
  82. (keyword? (keyword "abc"))
  83. ;=>true
  84. (symbol "abc")
  85. ;=>abc
  86. ;;;TODO: all implementations should suppport this too
  87. ;;;(keyword :abc)
  88. ;;;;=>:abc
  89. (keyword "abc")
  90. ;=>:abc
  91. ;; Testing sequential? function
  92. (sequential? (list 1 2 3))
  93. ;=>true
  94. (sequential? [15])
  95. ;=>true
  96. (sequential? sequential?)
  97. ;=>false
  98. (sequential? nil)
  99. ;=>false
  100. (sequential? "abc")
  101. ;=>false
  102. ;; Testing apply function with core functions and arguments in vector
  103. (apply + 4 [5])
  104. ;=>9
  105. (apply prn 1 2 ["3" 4])
  106. ; 1 2 "3" 4
  107. ;=>nil
  108. ;; Testing apply function with user functions and arguments in vector
  109. (apply (fn* (a b) (+ a b)) [2 3])
  110. ;=>5
  111. (apply (fn* (a b) (+ a b)) 4 [5])
  112. ;=>9
  113. ;; Testing map function with vectors
  114. (map (fn* (a) (* 2 a)) [1 2 3])
  115. ;=>(2 4 6)
  116. ;; Testing vector functions
  117. (vector? [10 11])
  118. ;=>true
  119. (vector? '(12 13))
  120. ;=>false
  121. (vector 3 4 5)
  122. ;=>[3 4 5]
  123. (map? {})
  124. ;=>true
  125. (map? '())
  126. ;=>false
  127. (map? [])
  128. ;=>false
  129. (map? 'abc)
  130. ;=>false
  131. (map? :abc)
  132. ;=>false
  133. ;;
  134. ;; Testing hash-maps
  135. (hash-map "a" 1)
  136. ;=>{"a" 1}
  137. {"a" 1}
  138. ;=>{"a" 1}
  139. (assoc {} "a" 1)
  140. ;=>{"a" 1}
  141. (get (assoc (assoc {"a" 1 } "b" 2) "c" 3) "a")
  142. ;=>1
  143. (def! hm1 (hash-map))
  144. ;=>{}
  145. (map? hm1)
  146. ;=>true
  147. (map? 1)
  148. ;=>false
  149. (map? "abc")
  150. ;=>false
  151. (get nil "a")
  152. ;=>nil
  153. (get hm1 "a")
  154. ;=>nil
  155. (contains? hm1 "a")
  156. ;=>false
  157. (def! hm2 (assoc hm1 "a" 1))
  158. ;=>{"a" 1}
  159. (get hm1 "a")
  160. ;=>nil
  161. (contains? hm1 "a")
  162. ;=>false
  163. (get hm2 "a")
  164. ;=>1
  165. (contains? hm2 "a")
  166. ;=>true
  167. ;;; TODO: fix. Clojure returns nil but this breaks mal impl
  168. (keys hm1)
  169. ;=>()
  170. (keys hm2)
  171. ;=>("a")
  172. ;;; TODO: fix. Clojure returns nil but this breaks mal impl
  173. (vals hm1)
  174. ;=>()
  175. (vals hm2)
  176. ;=>(1)
  177. (count (keys (assoc hm2 "b" 2 "c" 3)))
  178. ;=>3
  179. (def! hm3 (assoc hm2 "b" 2))
  180. (count (keys hm3))
  181. ;=>2
  182. (count (vals hm3))
  183. ;=>2
  184. (dissoc hm3 "a")
  185. ;=>{"b" 2}
  186. (dissoc hm3 "a" "b")
  187. ;=>{}
  188. (dissoc hm3 "a" "b" "c")
  189. ;=>{}
  190. (count (keys hm3))
  191. ;=>2
  192. ;; Testing keywords as hash-map keys
  193. (get {:abc 123} :abc)
  194. ;=>123
  195. (contains? {:abc 123} :abc)
  196. ;=>true
  197. (contains? {:abcd 123} :abc)
  198. ;=>false
  199. (assoc {} :bcd 234)
  200. ;=>{:bcd 234}
  201. (dissoc {:cde 345 :fgh 456} :cde)
  202. ;=>{:fgh 456}
  203. (keyword? (nth (keys {:abc 123 :def 456}) 0))
  204. ;=>true
  205. ;;; TODO: support : in strings in make impl
  206. ;;;(keyword? (nth (keys {":abc" 123 ":def" 456}) 0))
  207. ;;;;=>false
  208. (keyword? (nth (vals {"a" :abc "b" :def}) 0))
  209. ;=>true