forked from kanaka/mal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep6_file.mal
192 lines (152 loc) · 2.72 KB
/
step6_file.mal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
;;; TODO: really a step5 test
;;
;; Testing that (do (do)) not broken by TCO
(do (do 1 2))
;=>2
;;
;; Testing read-string, eval and slurp
(read-string "(1 2 (3 4) nil)")
;=>(1 2 (3 4) nil)
(= nil (read-string "nil"))
;=>true
(read-string "(+ 2 3)")
;=>(+ 2 3)
(read-string "\"\n\"")
;=>"\n"
(read-string "7 ;; comment")
;=>7
;;; Differing output, but make sure no fatal error
(read-string ";; comment")
(eval (read-string "(+ 2 3)"))
;=>5
(slurp "../tests/test.txt")
;=>"A line of text\n"
;;; Load the same file twice.
(slurp "../tests/test.txt")
;=>"A line of text\n"
;; Testing load-file
(load-file "../tests/inc.mal")
;=>nil
(inc1 7)
;=>8
(inc2 7)
;=>9
(inc3 9)
;=>12
;;
;; Testing atoms
(def! inc3 (fn* (a) (+ 3 a)))
(def! a (atom 2))
;=>(atom 2)
(atom? a)
;=>true
(atom? 1)
;=>false
(deref a)
;=>2
(reset! a 3)
;=>3
(deref a)
;=>3
(swap! a inc3)
;=>6
(deref a)
;=>6
(swap! a (fn* (a) a))
;=>6
(swap! a (fn* (a) (* 2 a)))
;=>12
(swap! a (fn* (a b) (* a b)) 10)
;=>120
(swap! a + 3)
;=>123
;; Testing swap!/closure interaction
(def! inc-it (fn* (a) (+ 1 a)))
(def! atm (atom 7))
(def! f (fn* () (swap! atm inc-it)))
(f)
;=>8
(f)
;=>9
;; Testing whether closures can retain atoms
(def! g (let* (atm (atom 0)) (fn* () (deref atm))))
(def! atm (atom 1))
(g)
;=>0
;>>> deferrable=True
;;
;; -------- Deferrable Functionality --------
;; Testing reading of large files
(load-file "../tests/computations.mal")
;=>nil
(sumdown 2)
;=>3
(fib 2)
;=>1
;; Testing `@` reader macro (short for `deref`)
(def! atm (atom 9))
@atm
;=>9
;;; TODO: really a step5 test
;; Testing that vector params not broken by TCO
(def! g (fn* [] 78))
(g)
;=>78
(def! g (fn* [a] (+ a 78)))
(g 3)
;=>81
;;
;; Testing that *ARGV* exists and is an empty list
(list? *ARGV*)
;=>true
*ARGV*
;=>()
;;
;; Testing that eval sets aa in root scope, and that it is found in nested scope
(let* (b 12) (do (eval (read-string "(def! aa 7)")) aa ))
;=>7
;>>> soft=True
;>>> optional=True
;;
;; -------- Optional Functionality --------
;; Testing comments in a file
(load-file "../tests/incB.mal")
;=>nil
(inc4 7)
;=>11
(inc5 7)
;=>12
;; Testing map literal across multiple lines in a file
(load-file "../tests/incC.mal")
;=>nil
mymap
;=>{"a" 1}
;; Checking that eval does not use local environments.
(def! a 1)
;=>1
(let* (a 2) (eval (read-string "a")))
;=>1
;; Non alphanumeric characters in comments in read-string
(read-string "1;!")
;=>1
(read-string "1;\"")
;=>1
(read-string "1;#")
;=>1
(read-string "1;$")
;=>1
(read-string "1;%")
;=>1
(read-string "1;'")
;=>1
(read-string "1;\\")
;=>1
(read-string "1;\\\\")
;=>1
(read-string "1;\\\\\\")
;=>1
(read-string "1;`")
;=>1
;;; Hopefully less problematic characters can be checked together
(read-string "1; &()*+,-./:;<=>?@[]^_{|}~")
;=>1