Implement step 7
[jackhill/mal.git] / tests / step8_macros.mal
1 ;; Testing non-macro function
2 (not (= 1 1))
3 ;=>false
4 ;;; This should fail if it is a macro
5 (not (= 1 2))
6 ;=>true
7
8
9 ;; Testing trivial macros
10 (defmacro! one (fn* () 1))
11 (one)
12 ;=>1
13 (defmacro! two (fn* () 2))
14 (two)
15 ;=>2
16
17 ;; Testing unless macros
18 (defmacro! unless (fn* (pred a b) `(if ~pred ~b ~a)))
19 (unless false 7 8)
20 ;=>7
21 (unless true 7 8)
22 ;=>8
23 (defmacro! unless2 (fn* (pred a b) `(if (not ~pred) ~a ~b)))
24 (unless2 false 7 8)
25 ;=>7
26 (unless2 true 7 8)
27 ;=>8
28
29 ;; Testing macroexpand
30 (macroexpand (unless2 2 3 4))
31 ;=>(if (not 2) 3 4)
32
33 ;; Testing evaluation of macro result
34 (defmacro! identity (fn* (x) x))
35 (let* (a 123) (identity a))
36 ;=>123
37
38
39 ;; Testing nth, first and rest functions
40
41 (nth '(1) 0)
42 ;=>1
43 (nth '(1 2) 1)
44 ;=>2
45 (def! x "x")
46 (def! x (nth '(1 2) 2))
47 x
48 ;=>"x"
49
50 (first '())
51 ;=>nil
52 (first '(6))
53 ;=>6
54 (first '(7 8 9))
55 ;=>7
56
57 (rest '())
58 ;=>()
59 (rest '(6))
60 ;=>()
61 (rest '(7 8 9))
62 ;=>(8 9)
63
64
65 ;; Testing or macro
66 (or)
67 ;=>nil
68 (or 1)
69 ;=>1
70 (or 1 2 3 4)
71 ;=>1
72 (or false 2)
73 ;=>2
74 (or false nil 3)
75 ;=>3
76 (or false nil false false nil 4)
77 ;=>4
78 (or false nil 3 false nil 4)
79 ;=>3
80 (or (or false 4))
81 ;=>4
82
83 ;; Testing cond macro
84
85 (cond)
86 ;=>nil
87 (cond true 7)
88 ;=>7
89 (cond true 7 true 8)
90 ;=>7
91 (cond false 7 true 8)
92 ;=>8
93 (cond false 7 false 8 "else" 9)
94 ;=>9
95 (cond false 7 (= 2 2) 8 "else" 9)
96 ;=>8
97 (cond false 7 false 8 false 9)
98 ;=>nil
99
100 ;; Testing EVAL in let*
101
102 (let* (x (or nil "yes")) x)
103 ;=>"yes"
104
105 ;;
106 ;; -------- Optional Functionality --------
107
108 ;; Testing nth, first, rest with vectors
109
110 (nth [1] 0)
111 ;=>1
112 (nth [1 2] 1)
113 ;=>2
114 (def! x "x")
115 (def! x (nth [1 2] 2))
116 x
117 ;=>"x"
118
119 (first [])
120 ;=>nil
121 (first nil)
122 ;=>nil
123 (first [10])
124 ;=>10
125 (first [10 11 12])
126 ;=>10
127 (rest [])
128 ;=>()
129 (rest nil)
130 ;=>()
131 (rest [10])
132 ;=>()
133 (rest [10 11 12])
134 ;=>(11 12)
135
136 ;; Testing EVAL in vector let*
137
138 (let* [x (or nil "yes")] x)
139 ;=>"yes"
140
141 ;;
142 ;; Loading core.mal
143 (load-file "../core.mal")
144
145 ;; Testing -> macro
146 (-> 7)
147 ;=>7
148 (-> (list 7 8 9) first)
149 ;=>7
150 (-> (list 7 8 9) (first))
151 ;=>7
152 (-> (list 7 8 9) first (+ 7))
153 ;=>14
154 (-> (list 7 8 9) rest (rest) first (+ 7))
155 ;=>16
156
157 ;; Testing ->> macro
158 (->> "L")
159 ;=>"L"
160 (->> "L" (str "A") (str "M"))
161 ;=>"MAL"
162 (->> [4] (concat [3]) (concat [2]) rest (concat [1]))
163 ;=>(1 3 4)
164