impl step2, remove interleaved calls in READ/EVAL/PRINT
[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 (unless2 2 3 4))
23 ;=>(if (not 2) 3 4)
24
25 ;; Testing evaluation of macro result
26 (defmacro! identity (fn* (x) x))
27 (let* (a 123) (identity a))
28 ;=>123
29
30 ;; Test that macros do not break empty list
31 ()
32 ;=>()
33
34
35 ;>>> deferrable=True
36 ;;
37 ;; -------- Deferrable Functionality --------
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
46 ;; Testing nth, first and rest functions
47
48 (nth (list 1) 0)
49 ;=>1
50 (nth (list 1 2) 1)
51 ;=>2
52 (nth (list 1 2 nil) 2)
53 ;=>nil
54 (def! x "x")
55 (def! x (nth (list 1 2) 2))
56 x
57 ;=>"x"
58
59 (first (list))
60 ;=>nil
61 (first (list 6))
62 ;=>6
63 (first (list 7 8 9))
64 ;=>7
65
66 (rest (list))
67 ;=>()
68 (rest (list 6))
69 ;=>()
70 (rest (list 7 8 9))
71 ;=>(8 9)
72
73
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
91 ;; Testing EVAL in let*
92
93 (let* (x (cond false "no" true "yes")) x)
94 ;=>"yes"
95
96
97 ;; Testing nth, first, rest with vectors
98
99 (nth [1] 0)
100 ;=>1
101 (nth [1 2] 1)
102 ;=>2
103 (nth [1 2 nil] 2)
104 ;=>nil
105 (def! x "x")
106 (def! x (nth [1 2] 2))
107 x
108 ;=>"x"
109
110 (first [])
111 ;=>nil
112 (first nil)
113 ;=>nil
114 (first [10])
115 ;=>10
116 (first [10 11 12])
117 ;=>10
118 (rest [])
119 ;=>()
120 (rest nil)
121 ;=>()
122 (rest [10])
123 ;=>()
124 (rest [10 11 12])
125 ;=>(11 12)
126 (rest (cons 10 [11 12]))
127 ;=>(11 12)
128
129 ;; Testing EVAL in vector let*
130
131 (let* [x (cond false "no" true "yes")] x)
132 ;=>"yes"
133
134 ;>>> soft=True
135 ;>>> optional=True
136 ;;
137 ;; ------- Optional Functionality --------------
138 ;; ------- (Not needed for self-hosting) -------
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