nydp 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +25 -13
  3. data/lib/lisp/core-015-documentation.nydp +11 -2
  4. data/lib/lisp/core-030-syntax.nydp +52 -8
  5. data/lib/lisp/core-040-utils.nydp +35 -47
  6. data/lib/lisp/core-045-dox-utils.nydp +85 -0
  7. data/lib/lisp/core-050-test-runner.nydp +20 -2
  8. data/lib/lisp/core-080-pretty-print.nydp +5 -1
  9. data/lib/lisp/tests/boot-tests.nydp +125 -255
  10. data/lib/lisp/tests/car-examples.nydp +16 -0
  11. data/lib/lisp/tests/collect-tests.nydp +28 -0
  12. data/lib/lisp/tests/cons-examples.nydp +8 -0
  13. data/lib/lisp/tests/curry-tests.nydp +17 -18
  14. data/lib/lisp/tests/detect-examples.nydp +24 -0
  15. data/lib/lisp/tests/dot-syntax-examples.nydp +40 -0
  16. data/lib/lisp/tests/dox-tests.nydp +116 -100
  17. data/lib/lisp/tests/dynamic-scope-test.nydp +10 -10
  18. data/lib/lisp/tests/each-tests.nydp +4 -5
  19. data/lib/lisp/tests/error-tests.nydp +17 -16
  20. data/lib/lisp/tests/explain-mac-examples.nydp +24 -0
  21. data/lib/lisp/tests/foundation-test.nydp +57 -223
  22. data/lib/lisp/tests/hash-examples.nydp +41 -0
  23. data/lib/lisp/tests/isa-examples.nydp +7 -0
  24. data/lib/lisp/tests/len-examples.nydp +11 -0
  25. data/lib/lisp/tests/list-tests.nydp +110 -75
  26. data/lib/lisp/tests/parser-tests.nydp +67 -109
  27. data/lib/lisp/tests/pretty-print-tests.nydp +19 -20
  28. data/lib/lisp/tests/quasiquote-examples.nydp +10 -0
  29. data/lib/lisp/tests/rfnwith-tests.nydp +7 -0
  30. data/lib/lisp/tests/string-tests.nydp +60 -31
  31. data/lib/lisp/tests/syntax-tests.nydp +22 -24
  32. data/lib/lisp/tests/type-of-examples.nydp +48 -0
  33. data/lib/lisp/tests/unparse-tests.nydp +2 -3
  34. data/lib/nydp.rb +2 -1
  35. data/lib/nydp/version.rb +1 -1
  36. metadata +14 -1
@@ -30,11 +30,29 @@
30
30
  (if (iso result expected)
31
31
  (passf)
32
32
  (do (p desc " - " (car test) " - FAILED:
33
+ running " (pp (nth 1 test)) ",
33
34
  expected " (inspect expected) ",
34
35
  got " (inspect result) "
35
36
  ")
36
37
  (failf)))))
37
38
 
38
39
  (def execute-tests (desc tests passf failf verbose)
39
- (execute-test desc (car tests) passf failf verbose)
40
- (if (cdr tests) (execute-tests desc (cdr tests) passf failf verbose)))
40
+ (execute-test desc
41
+ (car tests)
42
+ passf
43
+ failf
44
+ verbose)
45
+ (if (cdr tests)
46
+ (execute-tests desc
47
+ (cdr tests)
48
+ passf
49
+ failf
50
+ verbose)))
51
+
52
+ (mac examples-for (name . examples)
53
+ (let suite-name "examples for ~(pp name)"
54
+ `(do
55
+ (register-test '(suite ,suite-name ,@examples))
56
+ (dox-add-examples ',name
57
+ ',(collect (curry !caris 'comment)
58
+ examples)))))
@@ -25,12 +25,16 @@
25
25
  (inspect thing)))
26
26
 
27
27
  (mac pp/def (name args . body)
28
+ ; define a pretty-printer function for forms beginning with the
29
+ ; given name. 'args are usually (form indent), form being the
30
+ ; complete form for pretty-printing, and indent being the current
31
+ ; indent level.
28
32
  `(do
29
33
  (hash-set pp/special-forms ',name
30
34
  (fn ,args ,@body))
31
35
  (dox-add-doc ',name
32
36
  'pp/def
33
- '("pretty-printer for forms starting with ~(quote ,name)")
37
+ (list "pretty-printer for forms starting with ~(quote ,name)")
34
38
  ',args
35
39
  '(pp/def ,name ,args ,@body))))
36
40
 
@@ -1,13 +1,13 @@
1
- ;
2
- ; absurd contrived exaggerated example to test that macros that generate
3
- ; macros that generate macros that generate expressions are likely to work
4
- ;
5
- ; may you never need to do this in real life
6
- ;
1
+ ;;
2
+ ;; absurd contrived exaggerated example to test that macros that generate
3
+ ;; macros that generate macros that generate expressions are likely to work
4
+ ;;
5
+ ;; may you never need to do this in real life
6
+ ;;
7
7
  (mac make-make-op (opname op)
8
8
  `(mac ,opname (name n . body)
9
- `(mac ,name (x)
10
- `(,',',op ,,n ,x))))
9
+ `(mac ,name (x)
10
+ `(,',',op ,,n ,x))))
11
11
 
12
12
  (make-make-op make-mult *)
13
13
  (make-make-op make-plus +)
@@ -20,253 +20,123 @@
20
20
  (make-plus +seven 7)
21
21
  (make-plus +eleven 11)
22
22
 
23
- ;
24
- ; another contrived example to check deeply nested lexical scoping
25
- ;
23
+ ;;
24
+ ;; another contrived example to check deeply nested lexical scoping
25
+ ;;
26
26
  (let test-a0 "a0"
27
27
  (def test-foo (f0 f1)
28
28
  (with (w0 "w0" w1 "w1" w2 "w2" w3 "w3")
29
- (let f (fn (x0) (joinstr " " test-a0 x0 f0 x0 f1))
30
- (map f (list w0 w1 w2 w3))))))
31
-
32
- (register-test
33
- '(suite "Boot Tests"
34
- (suite "conditionals"
35
- ("single-expr 'if is just the expr"
36
- (pre-compile '(if a))
37
- a)
38
-
39
- ("two-expr 'if expands to 'cond"
40
- (pre-compile '(if a b))
41
- (cond a b))
42
-
43
- ("three-expr 'if expands to 'cond"
44
- (pre-compile '(if a b c))
45
- (cond a b c))
46
-
47
- ("four-expr 'if expands to nested 'cond"
48
- (pre-compile '(if a b c d))
49
- (cond a b (cond c d)))
50
-
51
- ("five-expr 'if expands to nested 'cond"
52
- (pre-compile '(if a b c d e))
53
- (cond a b (cond c d e)))
54
-
55
- ("'unless expands to 'if"
56
- (pre-compile '(unless x y z))
57
- (cond (no x) ((fn () y z)))))
58
-
59
- (suite "hashtables"
60
- ("build a hash table from brace-list syntax"
61
- (let hsh { foo 1 bar 2 }
62
- (list 'foo hsh.foo 'bar hsh.bar))
63
- (foo 1 bar 2))
64
-
65
- ("single-item brace list is just the thing itself"
66
- (let zi 10 "finds the ~{zi}th item")
67
- "finds the 10th item")
68
-
69
- ("detects key presence"
70
- (hash-key? { foo 1 bar 2 } 'foo)
71
- t)
72
-
73
- ("detects key absence"
74
- (hash-key? { foo 1 bar 2 } 'zed)
75
- nil)
76
-
77
- ("unquotes hash keys"
78
- (with (zi 'foo chi 'bar yi 'grr)
79
- (let hsh { ,zi 10 ,chi 11 ,yi 12 }
80
- (list zi hsh.foo chi hsh.bar yi hsh.grr)))
81
- (foo 10 bar 11 grr 12))
82
-
83
- ("allows literal and invocation hash keys"
84
- (with (zi "hello" chi "world")
85
- (let hsh { (joinstr " " zi chi) 10 "yesterday" 11 }
86
- (list "hello world" (hash-get hsh "hello world") "yesterday" (hash-get hsh "yesterday"))))
87
- ("hello world" 10 "yesterday" 11)))
88
-
89
- (suite "list management"
90
- ("'pair breaks a list into pairs"
91
- (pairs '(1 a 2 b 3 c))
92
- ((1 a) (2 b) (3 c)))
93
-
94
- ("'rev reverses a list"
95
- (rev '(a b c))
96
- (c b a))
97
-
98
- ("'rev handles nil"
99
- (rev nil)
100
- nil)
101
-
102
- ("'rev doesn't recurse"
103
- (rev '(a b (c d e) f g))
104
- (g f (c d e) b a))
105
-
106
- ("'flatten returns a flat list of things"
107
- (flatten '((poo (x) (* x x)) (1 2 3)))
108
- (poo x * x x 1 2 3)))
109
-
110
- (suite "map"
111
- ("maps a function over a list of numbers"
112
- (map (fn (x) (* x x)) '(1 2 3))
113
- (1 4 9))
114
-
115
- ("maps a string join function over a list of strings"
116
- (test-foo "x" "y")
117
- ("a0 w0 x w0 y" "a0 w1 x w1 y" "a0 w2 x w2 y" "a0 w3 x w3 y"))
118
-
119
- (suite "mapx"
120
- ("provides a convenient simplification for 'map"
121
- (mapx '(1 2 3 4) n (* n 2))
122
- (2 4 6 8))))
123
-
124
- (suite "reduce"
125
- ("it applies a function cumulatively over a list"
126
- (reduce + '(1 2 3))
127
- 6))
128
-
129
- (suite "detect"
130
- ("returns first matching item in a list"
131
- (detect (fn (x) (eq? (len x) 2))
132
- (list "foo" "bar" "xx" "pp"))
133
- "xx")
134
-
135
- ("returns nil when nothing matches"
136
- (detect (fn (x) (eq? (len x) 20))
137
- (list "foo" "bar" "xx" "pp"))
138
- nil)
139
-
140
- ;; kind of pointless
141
- ("returns nil if nil is the matching item"
142
- (detect no (list "foo" "bar" nil "pp"))
143
- nil)
144
-
145
- ("returns item if it's an atom and matches"
146
- (detect (fn (x) (eq? (len x) 2)) "zz")
147
- "zz")
148
-
149
- ("returns nil if it's an atom and doesn't match"
150
- (detect (fn (x) (eq? (len x) 20))
151
- "zz")
152
- nil))
153
-
154
- (suite "pre-compile"
155
- (suite "bang-syntax"
156
- ("expansion"
157
- (pre-compile '(!eq? a b))
158
- ((fn args (no (apply eq? args))) a b))
159
-
160
- ("bang-syntax for 'eq?"
161
- (!eq? 1 2)
162
- t)
163
-
164
- ("bang-syntax for 'caris"
165
- (!caris 'foo '(foo bar))
166
- nil)
167
-
168
- ("bang-syntax for 'caris"
169
- (!caris 'foo '(zozo foo bar))
170
- t))
171
-
172
- (suite "ampersand-syntax"
173
- ("defines a hash-lookup function"
174
- (pre-compile '&first)
175
- (fn (obj) (hash-get obj (quote first))))
176
-
177
- ("defines a hash-lookup function with a dot-syntax arg"
178
- (pre-compile '&teacher.address.city)
179
- (fn (obj) (hash-get (hash-get (hash-get obj (quote teacher)) (quote address)) (quote city))) ))
180
-
181
- (suite "colon-syntax"
182
- ("used for composition"
183
- (pre-compile '(no:eq? a b))
184
- ((fn args (no (apply eq? args))) a b))
185
-
186
- ("used for composition"
187
- (pre-compile '(x:y a b))
188
- ((fn args (x (apply y args))) a b)))
189
-
190
- (suite "bang-colon syntax"
191
- ("special combination with bang"
192
- (pre-compile '(!x:y a b))
193
- ((fn args (no (x (apply y args)))) a b)))
194
-
195
- ("expands 'let"
196
- (do
197
- (def x+3*z (x y)
198
- (let y 3
199
- (fn (z) (* (+ x y) z))))
200
- ((x+3*z 2 99) 5))
201
- 25)
202
-
203
- ("expands 'and"
204
- (pre-compile '(and a b c))
205
- (cond a (cond b c)))
206
-
207
- ("expands 'or"
208
- (do (reset-uniq-counter)
209
- (pre-compile '(or a b c)))
210
- ((fn (ora-1)
211
- (cond ora-1
212
- ora-1
213
- ((fn (ora-2)
214
- (cond ora-2
215
- ora-2
216
- ((fn (ora-3)
217
- (cond ora-3
218
- ora-3
219
- nil)) c))) b))) a))
220
-
221
- ("w/uniq provides unique variables for macro expansion"
222
- (do (reset-uniq-counter)
223
- (pre-compile '(w/uniq a foo)))
224
- ((fn (a) foo) (uniq 'a)))
225
-
226
- (suite "quasiquote"
227
- ("same as quote for standalone item"
228
- `a
229
- a)
230
- ("same as quote for standalone list"
231
- `(a b c)
232
- (a b c))
233
- ("substitutes single variables"
234
- (let b 10 `(a ,b c))
235
- (a 10 c))
236
- ("substitutes a list"
237
- (let b '(1 2 3) `(a ,@b c))
238
- (a 1 2 3 c))
239
- ("substitutes a list at the end of a given list"
240
- (let b '(1 2 3) `(a ,b ,@b))
241
- (a (1 2 3) 1 2 3))
242
- ("more complicated substitution example"
243
- (with (d '(1 2 3) g '(x y z)) `(a (b c ,d (e f ,@g))))
244
- (a (b c (1 2 3) (e f x y z))))
245
- ("peeks inside nested quotes"
246
- `(a b '(c ,(+ 1 2)))
247
- (a b '(c 3)))
248
- ("handles nested unquote-splicing"
249
- ``(a ,,@(list '+ 1 2) b)
250
- `((a ,(+ 1 2) b)))
251
- ("returns nested quasiquotes"
252
- `(a b `(c d ,(+ 1 2) ,,(+ 3 4)))
253
- (a b `(c d ,(+ 1 2) ,7))))
254
-
255
- (suite "build-keyword-args"
256
- ("takes a list of lists and returns the list with the first item of each sublist quoted"
257
- (build-keyword-args '( (a 1) (b c) (d e "f" 22) ))
258
- ((list 'a 1) (list 'b c) (list 'd e "f" 22))))
259
-
260
- (suite "make-macros can create macros"
261
- ("make-plus example generates a plus-seven expression"
262
- (+seven 6)
263
- 13)
264
- ("make-mult example generates a multiply-by-seven expression"
265
- (*seven 6)
266
- 42)
267
- ("make-mult example generates a multiply-by-eleven expression"
268
- (*eleven 20)
269
- 220)
270
- ("make-make example expressions can be nested"
271
- (*eleven (*five (+seven 2)))
272
- 495))))
29
+ (let f (fn (x0) (joinstr " " test-a0 x0 f0 x0 f1))
30
+ (map f (list w0 w1 w2 w3))))))
31
+
32
+ (examples-for map
33
+ ("maps a function over a list of numbers"
34
+ (map (fn (x) (* x x)) '(1 2 3))
35
+ (1 4 9))
36
+
37
+ ("maps a string join function over a list of strings"
38
+ (test-foo "x" "y")
39
+ ("a0 w0 x w0 y" "a0 w1 x w1 y" "a0 w2 x w2 y" "a0 w3 x w3 y"))
40
+
41
+ (suite "mapx"
42
+ ("provides a convenient simplification for 'map"
43
+ (mapx '(1 2 3 4) n (* n 2))
44
+ (2 4 6 8))))
45
+
46
+ (examples-for reduce
47
+ ("it applies a function cumulatively over a list"
48
+ (reduce + '(1 2 3))
49
+ 6))
50
+
51
+ (examples-for bang-syntax
52
+ ("expansion"
53
+ (pre-compile '(!eq? a b))
54
+ ((fn args (no (apply eq? args))) a b))
55
+
56
+ ("bang-syntax for 'eq?"
57
+ (!eq? 1 2)
58
+ t)
59
+
60
+ ("bang-syntax for 'caris"
61
+ (!caris 'foo '(foo bar))
62
+ nil)
63
+
64
+ ("bang-syntax for 'caris"
65
+ (!caris 'foo '(zozo foo bar))
66
+ t))
67
+
68
+ (examples-for ampersand-syntax
69
+ ("defines a hash-lookup function"
70
+ (pre-compile '&first)
71
+ (fn (obj) (hash-get obj (quote first))))
72
+
73
+ ("defines a hash-lookup function with a dot-syntax arg"
74
+ (pre-compile '&teacher.address.city)
75
+ (fn (obj) (hash-get (hash-get (hash-get obj (quote teacher)) (quote address)) (quote city))) ))
76
+
77
+ (examples-for colon-syntax
78
+ ("used for composition"
79
+ (pre-compile '(no:eq? a b))
80
+ ((fn args (no (apply eq? args))) a b))
81
+
82
+ ("used for composition"
83
+ (pre-compile '(x:y a b))
84
+ ((fn args (x (apply y args))) a b))
85
+
86
+ ("special combination with bang"
87
+ (pre-compile '(!x:y a b))
88
+ ((fn args (no (x (apply y args)))) a b)))
89
+
90
+ (examples-for let
91
+ ("expands 'let"
92
+ (do
93
+ (def x+3*z (x y)
94
+ (let y 3
95
+ (fn (z) (* (+ x y) z))))
96
+ ((x+3*z 2 99) 5))
97
+ 25))
98
+
99
+ (examples-for and
100
+ ("expands 'and"
101
+ (pre-compile '(and a b c))
102
+ (cond a (cond b c))))
103
+
104
+ (examples-for or
105
+ ("expands 'or"
106
+ (do (reset-uniq-counter)
107
+ (pre-compile '(or a b c)))
108
+ ((fn (ora-1)
109
+ (cond ora-1
110
+ ora-1
111
+ ((fn (ora-2)
112
+ (cond ora-2
113
+ ora-2
114
+ ((fn (ora-3)
115
+ (cond ora-3
116
+ ora-3
117
+ nil)) c))) b))) a)))
118
+
119
+ (examples-for w/uniq
120
+ ("w/uniq provides unique variables for macro expansion"
121
+ (do (reset-uniq-counter)
122
+ (pre-compile '(w/uniq a foo)))
123
+ ((fn (a) foo) (uniq 'a))))
124
+
125
+ (examples-for build-keyword-args
126
+ ("takes a list of lists and returns the list with the first item of each sublist quoted"
127
+ (build-keyword-args '( (a 1) (b c) (d e "f" 22) ))
128
+ ((list 'a 1) (list 'b c) (list 'd e "f" 22))))
129
+
130
+ (examples-for mac
131
+ ("make-plus example generates a plus-seven expression"
132
+ (+seven 6)
133
+ 13)
134
+ ("make-mult example generates a multiply-by-seven expression"
135
+ (*seven 6)
136
+ 42)
137
+ ("make-mult example generates a multiply-by-eleven expression"
138
+ (*eleven 20)
139
+ 220)
140
+ ("make-make example expressions can be nested"
141
+ (*eleven (*five (+seven 2)))
142
+ 495))
@@ -0,0 +1,16 @@
1
+ (examples-for car
2
+ ("car of nil is nil"
3
+ (car nil)
4
+ nil)
5
+
6
+ ("car of empty list is nil"
7
+ (car '())
8
+ nil)
9
+
10
+ ("car - no need to quote empty list"
11
+ (car ())
12
+ nil)
13
+
14
+ ("car returns car of argument"
15
+ (car '(foo 12.34 "bar"))
16
+ foo))