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