Merge pull request #530 from mpritham/master
[jackhill/mal.git] / impls / tests / step8_macros.mal
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
15 (defmacro! unless2 (fn* (pred a b) (list 'if (list 'not pred) a b)))
16 (unless2 false 7 8)
17 ;=>7
18 (unless2 true 7 8)
19 ;=>8
20
21 ;; Testing macroexpand
22 (macroexpand (one))
23 ;=>1
24 (macroexpand (unless PRED A B))
25 ;=>(if PRED B A)
26 (macroexpand (unless2 PRED A B))
27 ;=>(if (not PRED) A B)
28 (macroexpand (unless2 2 3 4))
29 ;=>(if (not 2) 3 4)
30
31 ;; Testing evaluation of macro result
32 (defmacro! identity (fn* (x) x))
33 (let* (a 123) (macroexpand (identity a)))
34 ;=>a
35 (let* (a 123) (identity a))
36 ;=>123
37
38 ;; Test that macros do not break empty list
39 ()
40 ;=>()
41
42 ;; Test that macros do not break quasiquote
43 `(1)
44 ;=>(1)
45
46 ;>>> deferrable=True
47 ;;
48 ;; -------- Deferrable Functionality --------
49
50 ;; Testing non-macro function
51 (not (= 1 1))
52 ;=>false
53 ;;; This should fail if it is a macro
54 (not (= 1 2))
55 ;=>true
56
57 ;; Testing nth, first and rest functions
58
59 (nth (list 1) 0)
60 ;=>1
61 (nth (list 1 2) 1)
62 ;=>2
63 (nth (list 1 2 nil) 2)
64 ;=>nil
65 (def! x "x")
66 (def! x (nth (list 1 2) 2))
67 x
68 ;=>"x"
69
70 (first (list))
71 ;=>nil
72 (first (list 6))
73 ;=>6
74 (first (list 7 8 9))
75 ;=>7
76
77 (rest (list))
78 ;=>()
79 (rest (list 6))
80 ;=>()
81 (rest (list 7 8 9))
82 ;=>(8 9)
83
84
85 ;; Testing cond macro
86
87 (macroexpand (cond))
88 ;=>nil
89 (cond)
90 ;=>nil
91 (macroexpand (cond X Y))
92 ;=>(if X Y (cond))
93 (cond true 7)
94 ;=>7
95 (cond false 7)
96 ;=>nil
97 (macroexpand (cond X Y Z T))
98 ;=>(if X Y (cond Z T))
99 (cond true 7 true 8)
100 ;=>7
101 (cond false 7 true 8)
102 ;=>8
103 (cond false 7 false 8 "else" 9)
104 ;=>9
105 (cond false 7 (= 2 2) 8 "else" 9)
106 ;=>8
107 (cond false 7 false 8 false 9)
108 ;=>nil
109
110 ;; Testing EVAL in let*
111
112 (let* (x (cond false "no" true "yes")) x)
113 ;=>"yes"
114
115
116 ;; Testing nth, first, rest with vectors
117
118 (nth [1] 0)
119 ;=>1
120 (nth [1 2] 1)
121 ;=>2
122 (nth [1 2 nil] 2)
123 ;=>nil
124 (def! x "x")
125 (def! x (nth [1 2] 2))
126 x
127 ;=>"x"
128
129 (first [])
130 ;=>nil
131 (first nil)
132 ;=>nil
133 (first [10])
134 ;=>10
135 (first [10 11 12])
136 ;=>10
137 (rest [])
138 ;=>()
139 (rest nil)
140 ;=>()
141 (rest [10])
142 ;=>()
143 (rest [10 11 12])
144 ;=>(11 12)
145 (rest (cons 10 [11 12]))
146 ;=>(11 12)
147
148 ;; Testing EVAL in vector let*
149
150 (let* [x (cond false "no" true "yes")] x)
151 ;=>"yes"
152
153 ;>>> soft=True
154 ;>>> optional=True
155 ;;
156 ;; ------- Optional Functionality --------------
157 ;; ------- (Not needed for self-hosting) -------
158
159 ;; Test that macros use closures
160 (def! x 2)
161 (defmacro! a (fn* [] x))
162 (a)
163 ;=>2
164 (let* (x 3) (a))
165 ;=>2