Lua: all steps and self-hosting.
[jackhill/mal.git] / tests / step9_try.mal
CommitLineData
31690700
JM
1;;
2;; Testing try*/catch*
3
f5223195
JM
4(try* 123 (catch* e 456))
5;=>123
6
16354bb4
JM
7(try* (abc 1 2) (catch* exc (prn "exc is:" exc))))
8; "exc is:" "'abc' not found"
31690700
JM
9;=>nil
10
16354bb4 11;;;TODO: fix so long lines don't trigger ANSI escape codes ;;;(try*
8128c69a
JM
12;;;(try* (throw ["data" "foo"]) (catch* exc (do (prn "exc is:" exc) 7))) ;;;;
13;;;; "exc is:" ["data" "foo"] ;;;;=>7
6301e0b6 14;;;;=>7
16354bb4 15
f5223195
JM
16(try* (throw (list "data" "foo")) (catch* exc (do (prn "err:" exc) 7)))
17; "err:" ("data" "foo")
16354bb4 18;=>7
31690700
JM
19
20(try* (throw "my exception") (catch* exc (do (prn "exc:" exc) 7)))
21; "exc:" "my exception"
22;=>7
23
24
25;;
26;; Testing builtin functions
27
28(symbol? 'abc)
29;=>true
30(symbol? "abc")
31;=>false
32
33(nil? nil)
34;=>true
35(nil? true)
36;=>false
37
38(true? true)
39;=>true
40(true? false)
41;=>false
42(true? true?)
43;=>false
44
45(false? false)
46;=>true
47(false? true)
48;=>false
49
31690700
JM
50;; Testing apply function
51(apply + (list 2 3))
52;=>5
53(apply + 4 (list 5))
54;=>9
55(apply prn (list 1 2 "3" (list)))
56; 1 2 "3" ()
57;=>nil
58
59
60;; Testing map function
61(def! nums (list 1 2 3))
62(def! double (fn* (a) (* 2 a)))
63(double 3)
64;=>6
65(map double nums)
66;=>(2 4 6)
f5223195 67(map (fn* (x) (symbol? x)) (list 1 (symbol "two") "three"))
f947d503 68;=>(false true false)
31690700 69
9af8aee6
JM
70;;
71;; Testing read-str and eval
72(read-string "(1 2 (3 4) nil)")
73;=>(1 2 (3 4) nil)
74
89bd4de1
JM
75(read-string "7 ;; comment")
76;=>7
77
6301e0b6 78;;; Differing output, but make sure no fatal error
89bd4de1 79(read-string ";; comment")
6301e0b6 80
89bd4de1 81
9af8aee6
JM
82(eval (read-string "(+ 4 5)"))
83;=>9
84
85;;
86;; Testing readline
87(readline "mal-user> ")
88"hello"
89;=>"\"hello\""
90
91;;
f5223195
JM
92;; ------- Optional Functionality ----------
93;; ------- (Needed for self-hosting) -------
9af8aee6 94
b8ee29b2
JM
95;; Testing symbol and keyword functions
96(symbol? :abc)
97;=>false
98(symbol? 'abc)
99;=>true
100(symbol? "abc")
101;=>false
102(symbol? (symbol "abc"))
103;=>true
104(keyword? :abc)
105;=>true
106(keyword? 'abc)
107;=>false
108(keyword? "abc")
109;=>false
110(keyword? (keyword "abc"))
111;=>true
112
9af8aee6
JM
113;; Testing sequential? function
114
115(sequential? (list 1 2 3))
116;=>true
117(sequential? [15])
118;=>true
119(sequential? sequential?)
120;=>false
121(sequential? nil)
122;=>false
123(sequential? "abc")
124;=>false
31690700 125
f5223195
JM
126
127;; Testing map function with vectors
128(map (fn* (a) (* 2 a)) [1 2 3])
129;=>(2 4 6)
130
89bd4de1
JM
131;; Testing vector functions
132
133(vector? [10 11])
134;=>true
135(vector? '(12 13))
136;=>false
137(vector 3 4 5)
138;=>[3 4 5]
139
b8ee29b2
JM
140(map? {})
141;=>true
142(map? '())
143;=>false
9af8aee6
JM
144(map? [])
145;=>false
b8ee29b2
JM
146(map? 'abc)
147;=>false
148(map? :abc)
149;=>false
31690700
JM
150
151;;
152;; Testing hash-maps
153(hash-map "a" 1)
154;=>{"a" 1}
155
156{"a" 1}
157;=>{"a" 1}
158
159(assoc {} "a" 1)
160;=>{"a" 1}
161
162(def! hm1 (hash-map))
163;=>{}
164
165(map? hm1)
166;=>true
167(map? 1)
168;=>false
9af8aee6 169(map? "abc")
31690700
JM
170;=>false
171
0027e8fe
JM
172(get nil "a")
173;=>nil
174
31690700
JM
175(get hm1 "a")
176;=>nil
177
178(contains? hm1 "a")
179;=>false
180
181(def! hm2 (assoc hm1 "a" 1))
182;=>{"a" 1}
183
184(get hm1 "a")
185;=>nil
186
187(contains? hm1 "a")
188;=>false
189
190(get hm2 "a")
191;=>1
192
193(contains? hm2 "a")
194;=>true
195
77b2da6c
JM
196
197;;; TODO: fix. Clojure returns nil but this breaks mal impl
198(keys hm1)
199;=>()
200
31690700
JM
201(keys hm2)
202;=>("a")
203
77b2da6c
JM
204;;; TODO: fix. Clojure returns nil but this breaks mal impl
205(vals hm1)
206;=>()
207
31690700
JM
208(vals hm2)
209;=>(1)
210
9528bb14
JM
211(count (keys (assoc hm2 "b" 2 "c" 3)))
212;=>3
213
31690700
JM
214(def! hm3 (assoc hm2 "b" 2))
215(count (keys hm3))
216;=>2
217(count (vals hm3))
218;=>2
219
220(dissoc hm3 "a")
221;=>{"b" 2}
222
223(dissoc hm3 "a" "b")
224;=>{}
225
5ce65382
JM
226(dissoc hm3 "a" "b" "c")
227;=>{}
228
31690700
JM
229(count (keys hm3))
230;=>2
231
b8ee29b2
JM
232;; Testing keywords as hash-map keys
233(get {:abc 123} :abc)
234;=>123
235(contains? {:abc 123} :abc)
236;=>true
237(contains? {:abcd 123} :abc)
238;=>false
239(assoc {} :bcd 234)
240;=>{:bcd 234}
241(dissoc {:cde 345 :fgh 456} :cde)
242;=>{:fgh 456}
243(keyword? (nth (keys {:abc 123 :def 456}) 0))
244;=>true
245;;; TODO: support : in strings in make impl
246;;;(keyword? (nth (keys {":abc" 123 ":def" 456}) 0))
247;;;;=>false
248(keyword? (nth (vals {"a" :abc "b" :def}) 0))
249;=>true
250
251
31690700 252
b12d98e4 253;;
f5223195 254;; Testing metadata on functions
b12d98e4 255
31690700 256;;
f5223195 257;; Testing metadata on mal functions
31690700 258
a34b0200
JM
259(meta (fn* (a) a))
260;=>nil
261
f5223195
JM
262(meta (with-meta (fn* (a) a) {"b" 1}))
263;=>{"b" 1}
31690700 264
f5223195 265(meta (with-meta (fn* (a) a) "abc"))
a34b0200
JM
266;=>"abc"
267
f5223195 268(def! l-wm (with-meta (fn* (a) a) {"b" 2}))
a34b0200
JM
269(meta l-wm)
270;=>{"b" 2}
271
272(meta (with-meta l-wm {"new_meta" 123}))
273;=>{"new_meta" 123}
274(meta l-wm)
275;=>{"b" 2}
276
31690700
JM
277(def! f-wm (with-meta (fn* [a] (+ 1 a)) {"abc" 1}))
278(meta f-wm)
279;=>{"abc" 1}
280
a34b0200
JM
281(meta (with-meta f-wm {"new_meta" 123}))
282;=>{"new_meta" 123}
17ae845e
JM
283(meta f-wm)
284;=>{"abc" 1}
285
b079f510 286(def! f-wm2 ^{"abc" 1} (fn* [a] (+ 1 a)))
5ce65382 287(meta f-wm2)
b079f510 288;=>{"abc" 1}
5ce65382 289
a34b0200
JM
290;;
291;; Make sure closures and metadata co-exist
292(def! gen-plusX (fn* (x) (with-meta (fn* (b) (+ x b)) {"meta" 1})))
293(def! plus7 (gen-plusX 7))
294(def! plus8 (gen-plusX 8))
295(plus7 8)
296;=>15
297(meta plus7)
298;=>{"meta" 1}
299(meta plus8)
300;=>{"meta" 1}
301(meta (with-meta plus7 {"meta" 2}))
302;=>{"meta" 2}
303(meta plus8)
304;=>{"meta" 1}
305
31690700
JM
306
307;;
308;; Testing atoms
309
310(def! inc3 (fn* (a) (+ 3 a)))
311
312(def! a (atom 2))
313;=>(atom 2)
314
315;;;(type a)
316;;;;=>"atom"
317
318(deref a)
319;=>2
320
321@a
322;=>2
323
324(reset! a 3)
325;=>3
326
327@a
328;=>3
329
330(swap! a inc3)
331;=>6
332
333@a
334;=>6
335
336(swap! a (fn* (a) a))
337;=>6
338
339(swap! a (fn* (a) (* 2 a)))
340;=>12
341
17ae845e
JM
342(swap! a (fn* (a b) (* a b)) 10)
343;=>120
344
5ce65382 345(swap! a + 3)
17ae845e 346;=>123
31690700 347
0027e8fe
JM
348;; Testing swap!/closure interaction
349(def! inc-it (fn* (a) (+ 1 a)))
350(def! atm (atom 7))
351(def! f (fn* [] (swap! atm inc-it)))
352(f)
353;=>8
354(f)
355;=>9
356
f5223195
JM
357
358;;
359;; ------- Optional Functionality --------------
360;; ------- (Not needed for self-hosting) -------
361
362;;
363;; Testing conj function
364(conj (list) 1)
365;=>(1)
366(conj (list 1) 2)
367;=>(2 1)
368(conj (list 2 3) 4)
369;=>(4 2 3)
370(conj (list 2 3) 4 5 6)
371;=>(6 5 4 2 3)
372(conj (list 1) (list 2 3))
373;=>((2 3) 1)
374
375(conj [] 1)
376;=>[1]
377(conj [1] 2)
378;=>[1 2]
379(conj [2 3] 4)
380;=>[2 3 4]
381(conj [2 3] 4 5 6)
382;=>[2 3 4 5 6]
383(conj [1] [2 3])
384;=>[1 [2 3]]
385
386
387;;
388;; Testing metadata on collections
389
390(meta [1 2 3])
391;=>nil
392
393(with-meta [1 2 3] {"a" 1})
394;=>[1 2 3]
395
396(meta (with-meta [1 2 3] {"a" 1}))
397;=>{"a" 1}
398
399(meta (with-meta [1 2 3] "abc"))
400;=>"abc"
401
402(meta (with-meta (list 1 2 3) {"a" 1}))
403;=>{"a" 1}
404
405(meta (with-meta {"abc" 123} {"a" 1}))
406;=>{"a" 1}
407
408;;; Not actually supported by Clojure
409;;;(meta (with-meta (atom 7) {"a" 1}))
410;;;;=>{"a" 1}
411
412(def! l-wm (with-meta [4 5 6] {"b" 2}))
413;=>[4 5 6]
414(meta l-wm)
415;=>{"b" 2}
416
417(meta (with-meta l-wm {"new_meta" 123}))
418;=>{"new_meta" 123}
419(meta l-wm)
420;=>{"b" 2}
421
422;;
423;; Testing metadata on builtin functions
424(meta +)
425;=>nil
426(def! f-wm3 ^{"def" 2} +)
427(meta f-wm3)
428;=>{"def" 2}
429(meta +)
430;=>nil
431