Finish step8
[jackhill/mal.git] / impls / tests / step8_macros.mal
CommitLineData
31690700
JM
1;; Testing trivial macros
2(defmacro! one (fn* () 1))
3(one)
4;=>1
5(defmacro! two (fn* () 2))
6(two)
7;=>2
8
9;; Testing unless macros
10(defmacro! unless (fn* (pred a b) `(if ~pred ~b ~a)))
11(unless false 7 8)
12;=>7
13(unless true 7 8)
14;=>8
3d352522 15(defmacro! unless2 (fn* (pred a b) (list 'if (list 'not pred) a b)))
31690700
JM
16(unless2 false 7 8)
17;=>7
18(unless2 true 7 8)
19;=>8
20
6b9eb18e
JM
21;; Testing macroexpand
22(macroexpand (unless2 2 3 4))
23;=>(if (not 2) 3 4)
24
9bca0b57
JM
25;; Testing evaluation of macro result
26(defmacro! identity (fn* (x) x))
27(let* (a 123) (identity a))
28;=>123
29
90ca8485
JM
30;; Test that macros do not break empty list
31()
32;=>()
33
6b9eb18e 34
a1eb30fc 35;>>> deferrable=True
46e25689 36;;
a1eb30fc 37;; -------- Deferrable Functionality --------
46e25689
JM
38
39;; Testing non-macro function
40(not (= 1 1))
41;=>false
42;;; This should fail if it is a macro
43(not (= 1 2))
44;=>true
45
6b9eb18e
JM
46;; Testing nth, first and rest functions
47
46e25689 48(nth (list 1) 0)
6b9eb18e 49;=>1
46e25689 50(nth (list 1 2) 1)
6b9eb18e 51;=>2
0fa01f40
DM
52(nth (list 1 2 nil) 2)
53;=>nil
6b9eb18e 54(def! x "x")
46e25689 55(def! x (nth (list 1 2) 2))
6b9eb18e
JM
56x
57;=>"x"
58
46e25689 59(first (list))
6b9eb18e 60;=>nil
46e25689 61(first (list 6))
6b9eb18e 62;=>6
46e25689 63(first (list 7 8 9))
6b9eb18e
JM
64;=>7
65
46e25689 66(rest (list))
6b9eb18e 67;=>()
46e25689 68(rest (list 6))
6b9eb18e 69;=>()
46e25689 70(rest (list 7 8 9))
6b9eb18e
JM
71;=>(8 9)
72
73
77b2da6c
JM
74;; Testing cond macro
75
76(cond)
77;=>nil
78(cond true 7)
79;=>7
80(cond true 7 true 8)
81;=>7
82(cond false 7 true 8)
83;=>8
84(cond false 7 false 8 "else" 9)
85;=>9
86(cond false 7 (= 2 2) 8 "else" 9)
87;=>8
88(cond false 7 false 8 false 9)
89;=>nil
90
9af8aee6
JM
91;; Testing EVAL in let*
92
26ced15b 93(let* (x (cond false "no" true "yes")) x)
9af8aee6
JM
94;=>"yes"
95
46e25689 96
8128c69a
JM
97;; Testing nth, first, rest with vectors
98
8128c69a
JM
99(nth [1] 0)
100;=>1
101(nth [1 2] 1)
102;=>2
0fa01f40
DM
103(nth [1 2 nil] 2)
104;=>nil
b8ee29b2
JM
105(def! x "x")
106(def! x (nth [1 2] 2))
107x
108;=>"x"
8128c69a 109
9af8aee6
JM
110(first [])
111;=>nil
4e284171
DM
112(first nil)
113;=>nil
9af8aee6
JM
114(first [10])
115;=>10
116(first [10 11 12])
117;=>10
118(rest [])
119;=>()
4e284171
DM
120(rest nil)
121;=>()
9af8aee6
JM
122(rest [10])
123;=>()
124(rest [10 11 12])
125;=>(11 12)
ae0427fb
BH
126(rest (cons 10 [11 12]))
127;=>(11 12)
9af8aee6
JM
128
129;; Testing EVAL in vector let*
130
26ced15b 131(let* [x (cond false "no" true "yes")] x)
31690700 132;=>"yes"
a6887204
NB
133
134;>>> soft=True
77fd710c
JM
135;>>> optional=True
136;;
137;; ------- Optional Functionality --------------
138;; ------- (Not needed for self-hosting) -------
a6887204
NB
139
140;; Test that macros use closures
141(def! x 2)
142(defmacro! a (fn* [] x))
143(a)
144;=>2
145(let* (x 3) (a))
146;=>2