nendo 0.6.8 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/nendo_spec_2.rb DELETED
@@ -1,2135 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # -*- encoding: utf-8 -*-
3
- #
4
- # nendo_spec.rb - "RSpec file for nendo language"
5
- #
6
- # Copyright (c) 2009-2011 Kiyoka Nishiyama <kiyoka@sumibi.org>
7
- #
8
- # Redistribution and use in source and binary forms, with or without
9
- # modification, are permitted provided that the following conditions
10
- # are met:
11
- #
12
- # 1. Redistributions of source code must retain the above copyright
13
- # notice, this list of conditions and the following disclaimer.
14
- #
15
- # 2. Redistributions in binary form must reproduce the above copyright
16
- # notice, this list of conditions and the following disclaimer in the
17
- # documentation and/or other materials provided with the distribution.
18
- #
19
- # 3. Neither the name of the authors nor the names of its contributors
20
- # may be used to endorse or promote products derived from this
21
- # software without specific prior written permission.
22
- #
23
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
- # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
- # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26
- # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27
- # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28
- # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29
- # TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30
- # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31
- # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32
- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33
- # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
- #
35
- require 'nendo'
36
- include Nendo
37
-
38
-
39
-
40
- describe Nendo, "when use quote and syntax-quote " do
41
- before do
42
- @nendo = Nendo::Core.new()
43
- end
44
- it "should" do
45
- @nendo.evalStr( " '() " ).should == "()"
46
- @nendo.evalStr( " '1 " ).should == "1"
47
- @nendo.evalStr( " '\"str\" " ).should == '"str"'
48
- @nendo.evalStr( " '(1 . 2) " ).should == "(1 . 2)"
49
- @nendo.evalStr( " (quote ())" ).should == "()"
50
- @nendo.evalStr( " (quote 1)" ).should == "1"
51
- @nendo.evalStr( " (quote \"str\") " ).should == '"str"'
52
- @nendo.evalStr( " (quote (1 . 2)) " ).should == "(1 . 2)"
53
- @nendo.evalStr( " (syntax-quote ())" ).should == "()"
54
- @nendo.evalStr( " (syntax-quote 1)" ).should == "1"
55
- @nendo.evalStr( ' (syntax-quote "str") ' ).should == '"str"'
56
- @nendo.evalStr( " (syntax-quote (1 . 2)) " ).should == "(1 . 2)"
57
- @nendo.evalStr( " (quote quote) " ).should == "quote"
58
- @nendo.evalStr( " 'quote " ).should == "quote"
59
- @nendo.evalStr( " ''1 " ).should == "(quote 1)"
60
- @nendo.evalStr( " (quote syntax-quote) " ).should == "quote"
61
- @nendo.evalStr( " (syntax-quote '1) " ).should == "(quote 1)"
62
- @nendo.evalStr( " (syntax-quote (quote 1)) " ).should == "(quote 1)"
63
- @nendo.evalStr( " (quote (syntax-quote 1)) " ).should == "(quote 1)"
64
- end
65
- end
66
-
67
- describe Nendo, "when read various list expressions" do
68
- before do
69
- @nendo = Nendo::Core.new()
70
- end
71
- it "should" do
72
- @nendo.evalStr( " '() " ).should == "()"
73
- @nendo.evalStr( " '[] " ).should == "()"
74
- @nendo.evalStr( " '(1 . 1) " ).should == "(1 . 1)"
75
- @nendo.evalStr( " '[1 . 1) " ).should == "(1 . 1)"
76
- @nendo.evalStr( " '(1 . 1] " ).should == "(1 . 1)"
77
- @nendo.evalStr( " '(1 1 . 1) " ).should == "(1 1 . 1)"
78
- @nendo.evalStr( " '(1 2 . 3) " ).should == "(1 2 . 3)"
79
- @nendo.evalStr( " '(1 2 3) " ).should == "(1 2 3)"
80
- @nendo.evalStr( " '(1.1 2.2 3.3) " ).should == "(1.1 2.2 3.3)"
81
- @nendo.evalStr( " '(a bb ccc dddd) " ).should == "(a bb ccc dddd)"
82
- @nendo.evalStr( " '(a (b) ((c)) (((d)))) " ).should == "(a (b) ((c)) (((d))))"
83
- @nendo.evalStr( " '[a (b) ((c)) (((d)))] " ).should == "(a (b) ((c)) (((d))))"
84
- @nendo.evalStr( " '(a [b] ([c]) (([d]))) " ).should == "(a (b) ((c)) (((d))))"
85
- @nendo.evalStr( " '[a [b] [[c]] [[[d]]]] " ).should == "(a (b) ((c)) (((d))))"
86
- @nendo.evalStr( " '('a)" ).should == "((quote a))"
87
- @nendo.evalStr( " '(''a)" ).should == "((quote (quote a)))"
88
- @nendo.evalStr( " '('a 'b 'c)" ).should == "((quote a) (quote b) (quote c))"
89
- @nendo.evalStr( ' \'("str") ' ).should == '("str")'
90
- @nendo.evalStr( ' \'("str" . 1) ' ).should == '("str" . 1)'
91
- @nendo.evalStr( ' \'(1 . "str") ' ).should == '(1 . "str")'
92
- @nendo.evalStr( ' \'(1 2 . "str") ' ).should == '(1 2 . "str")'
93
- @nendo.evalStr( " '((a)(b)(c)) " ).should == "((a) (b) (c))"
94
- @nendo.evalStr( " 'a " ).should == "a"
95
- @nendo.evalStr( " 'symbol " ).should == "symbol"
96
- @nendo.evalStr( " 'SYMBOL " ).should == "SYMBOL"
97
- @nendo.evalStr( " 'SyMbOl " ).should == "SyMbOl"
98
- @nendo.evalStr( " ''a " ).should == "(quote a)"
99
- @nendo.evalStr( " '1 " ).should == "1"
100
- @nendo.evalStr( " ''1 " ).should == "(quote 1)"
101
- @nendo.evalStr( " '''1 " ).should == "(quote (quote 1))"
102
- @nendo.evalStr( " '1.1 " ).should == "1.1"
103
- @nendo.evalStr( " ''1.1 " ).should == "(quote 1.1)"
104
- @nendo.evalStr( " '''1.1 " ).should == "(quote (quote 1.1))"
105
- @nendo.evalStr( " '() " ).should == "()"
106
- @nendo.evalStr( " '(()) " ).should == "(())"
107
- @nendo.evalStr( " '((())) " ).should == "((()))"
108
- @nendo.evalStr( " '(((()))) " ).should == "(((())))"
109
- @nendo.evalStr( " '(() . ()) " ).should == "(())"
110
- @nendo.evalStr( " '(a . ()) " ).should == "(a)"
111
- @nendo.evalStr( " '(a . #t) " ).should == "(a . #t)"
112
- @nendo.evalStr( " '(a . #f) " ).should == "(a . #f)"
113
- @nendo.evalStr( " '(a . nil) " ).should == "(a . nil)"
114
- @nendo.evalStr( " '(a b c d e . ()) " ).should == "(a b c d e)"
115
- @nendo.evalStr( " '(#t #t #f #f nil nil '() '()) " ).should == "(#t #t #f #f nil nil (quote ()) (quote ()))"
116
- end
117
- end
118
-
119
-
120
-
121
-
122
- class TestClassForBlockArgument
123
- def arg1
124
- yield 100
125
- end
126
- def arg2
127
- yield 100,200
128
- end
129
- def arg5
130
- yield 10,20,30,40,50
131
- end
132
-
133
- def arg1_plus_1( arg1 )
134
- yield arg1, 200
135
- end
136
-
137
- end
138
-
139
- describe Nendo, "when use &block(Ruby's block) " do
140
- before do
141
- @nendo = Nendo::Core.new()
142
- end
143
-
144
- it "should" do
145
- @nendo.evalStr( " (define testclass (TestClassForBlockArgument.new)) testclass.class" ).should == "TestClassForBlockArgument"
146
- @nendo.evalStr( " (testclass.arg1 (&block (a) (list a))) " ).should == "(100)"
147
- @nendo.evalStr( " (testclass.arg2 (&block (a b) (cons a b))) " ).should == "(100 . 200)"
148
- @nendo.evalStr( " (testclass.arg5 (&block (a b c d e) (list a b c d e))) " ).should == "(10 20 30 40 50)"
149
- @nendo.evalStr( " (testclass.arg5 (&block (a b c d e) (to-arr (list a b c d e)))) " ).should == "#(10 20 30 40 50)"
150
- end
151
- end
152
-
153
- describe Nendo, "when call variable length functions" do
154
- before do
155
- @nendo = Nendo::Core.new( )
156
- @nendo.loadInitFile
157
- end
158
- it "should" do
159
- # fixed length
160
- @nendo.evalStr( <<EOS
161
- (define (arg0) 0)
162
- (define (arg1 a) a)
163
- (define (arg2 a b) b)
164
- (define (arg3 a b c) c)
165
- (define (arg4 a b c d) d)
166
- (list
167
- (arg0)
168
- (arg1 1)
169
- (arg2 1 2)
170
- (arg3 1 2 3)
171
- (arg4 1 2 3 4))
172
- EOS
173
- ).should == "(0 1 2 3 4)"
174
-
175
- @nendo.evalStr( <<EOS
176
- (define (func-var-arg . arg) arg)
177
- (list
178
- (func-var-arg)
179
- (func-var-arg 1)
180
- (func-var-arg 1 2)
181
- (func-var-arg 1 2 3)
182
- (func-var-arg 1 2 3 4))
183
- EOS
184
- ).should == "(() (1) (1 2) (1 2 3) (1 2 3 4))"
185
-
186
- @nendo.evalStr( <<EOS
187
- (define (func-var-arg first . rest) rest)
188
- (list
189
- (func-var-arg 0)
190
- (func-var-arg 0 1)
191
- (func-var-arg 0 1 2)
192
- (func-var-arg 0 1 2 3)
193
- (func-var-arg 0 1 2 3 4))
194
- EOS
195
- ).should == "(() (1) (1 2) (1 2 3) (1 2 3 4))"
196
-
197
- @nendo.evalStr( <<EOS
198
- (define (func-var-arg first second . rest) (cons second rest))
199
- (list
200
- (func-var-arg "f" "s")
201
- (func-var-arg "f" "s" 1)
202
- (func-var-arg "f" "s" 1 2)
203
- (func-var-arg "f" "s" 1 2 3)
204
- (func-var-arg "f" "s" 1 2 3 4))
205
- EOS
206
- ).should == '(("s") ("s" 1) ("s" 1 2) ("s" 1 2 3) ("s" 1 2 3 4))'
207
-
208
- # Ruby method with block
209
- @nendo.evalStr( <<EOS
210
- (define testclass (TestClassForBlockArgument.new)) testclass.class
211
- (list
212
- (testclass.arg1 (&block (a) (list a)))
213
- (testclass.arg2 (&block (a b) (cons a b)))
214
- (testclass.arg1_plus_1 "a" (&block (a b) (list a b)))
215
- )
216
- EOS
217
- ).should == '((100) (100 . 200) ("a" 200))'
218
-
219
-
220
- # Ruby method with block
221
- @nendo.evalStr( <<EOS
222
- (define (read-first-line fobj) (fobj.readline.chomp))
223
- (let1 filename "./VERSION.yml"
224
- (list
225
- (with-open filename read-first-line "r")
226
- (with-open filename read-first-line)))
227
- EOS
228
- ).should == '("---" "---")'
229
- end
230
- end
231
-
232
-
233
- describe Nendo, "when read various vector expressions" do
234
- before do
235
- @nendo = Nendo::Core.new()
236
- @nendo.setDisplayErrors( false )
237
- end
238
- it "should" do
239
- @nendo.evalStr( " '() " ).should == "()"
240
- @nendo.evalStr( " '[] " ).should == "()"
241
- @nendo.evalStr( " '#( 1 ) " ).should == "#(1)"
242
- lambda { @nendo.evalStr( " '#(( 1 ) " ) }.should raise_error( RuntimeError )
243
- @nendo.evalStr( " '#( 1 2 ) " ).should == "#(1 2)"
244
- @nendo.evalStr( " '#( 1 () ) " ).should == "#(1 ())"
245
- @nendo.evalStr( " '#( () 2 ) " ).should == "#(() 2)"
246
- lambda { @nendo.evalStr( " '#( 1 . 2 ) " ) }.should raise_error( RuntimeError )
247
- lambda { @nendo.evalStr( " #(+ 1 2) " ) }.should raise_error( RuntimeError )
248
- @nendo.evalStr( " '#( 1 #( 11 )) " ).should == "#(1 #(11))"
249
- @nendo.evalStr( " '#( 1 #( 11 12 )) " ).should == "#(1 #(11 12))"
250
- @nendo.evalStr( " '#( 1 #( 11 #( 111 ))) " ).should == "#(1 #(11 #(111)))"
251
- @nendo.evalStr( " '#( 1 #( 11 #( 111 112))) " ).should == "#(1 #(11 #(111 112)))"
252
- @nendo.evalStr( " '#(1 2 3) " ).should == "#(1 2 3)"
253
- @nendo.evalStr( " '#(1.1 2.2 3.3) " ).should == "#(1.1 2.2 3.3)"
254
- @nendo.evalStr( " '#(a bb ccc dddd) " ).should == "#(a bb ccc dddd)"
255
- @nendo.evalStr( " '#(a (b) ((c)) (((d)))) " ).should == "#(a (b) ((c)) (((d))))"
256
- end
257
- end
258
-
259
- describe Nendo, "when call evalStr() with built-in functions" do
260
- before do
261
- @nendo = Nendo::Core.new()
262
- @nendo.setDisplayErrors( false )
263
- end
264
- it "should" do
265
- @nendo.evalStr( " (car '(1 2 3 4)) " ).should == "1"
266
- @nendo.evalStr( " (cdr '(1 2 3 4)) " ).should == "(2 3 4)"
267
- @nendo.evalStr( " (null? '()) " ).should == "#t"
268
- @nendo.evalStr( " (null? '(1)) " ).should == "#f"
269
- @nendo.evalStr( " (null? false) " ).should == "#f"
270
- @nendo.evalStr( " (null? nil) " ).should == "#f"
271
- @nendo.evalStr( " (null? true) " ).should == "#f"
272
- @nendo.evalStr( " (cons 1 2) " ).should == "(1 . 2)"
273
- @nendo.evalStr( " (cons 1 '(2 3)) " ).should == "(1 2 3)"
274
- @nendo.evalStr( " (cons '(1 2) '(3 4)) " ).should == "((1 2) 3 4)"
275
- @nendo.evalStr( " (cons '(1 2) '((3 4))) " ).should == "((1 2) (3 4))"
276
- @nendo.evalStr( " (cons '() '()) " ).should == "(())"
277
- @nendo.evalStr( " (cons 1 '()) " ).should == "(1)"
278
- @nendo.evalStr( " (cons '() (cdr '(100))) " ).should == "(())"
279
- @nendo.evalStr( " (cons '() (car '(()))) " ).should == "(())"
280
- @nendo.evalStr( " (cons (car '(())) '()) " ).should == "(())"
281
- @nendo.evalStr( " (cons (car '(())) (car '(()))) " ).should == "(())"
282
- @nendo.evalStr( " (cons '() (cdr '(100))) " ).should == "(())"
283
- @nendo.evalStr( " (cons (cdr '(100)) '()) " ).should == "(())"
284
- @nendo.evalStr( " (cons (cdr '(100)) (cdr '(100))) " ).should == "(())"
285
- lambda { @nendo.evalStr( " (cons 1 2 3) " ) }.should raise_error(ArgumentError)
286
- lambda { @nendo.evalStr( " (cons 1) " ) }.should raise_error(ArgumentError)
287
- lambda { @nendo.evalStr( " (cons) " ) }.should raise_error(ArgumentError)
288
- @nendo.evalStr( " (list 1 2 3) " ).should == "(1 2 3)"
289
- @nendo.evalStr( " (list '(1) '(2) '(3)) " ).should == "((1) (2) (3))"
290
- @nendo.evalStr( " (list 'a 'b 'c) " ).should == "(a b c)"
291
- @nendo.evalStr( " (list '(a) '((b c))) " ).should == "((a) ((b c)))"
292
- @nendo.evalStr( " (list) " ).should == "()"
293
- @nendo.evalStr( " (list 1) " ).should == "(1)"
294
- @nendo.evalStr( " (reverse '(1)) " ).should == "(1)"
295
- @nendo.evalStr( " (reverse '(1 2 3)) " ).should == "(3 2 1)"
296
- @nendo.evalStr( " (reverse '(1 2 ())) " ).should == "(() 2 1)"
297
- @nendo.evalStr( " (reverse (list 1 2 (cdr '(100)))) " ).should == "(() 2 1)"
298
- @nendo.evalStr( " (define !a 10) !a" ).should == "10"
299
- @nendo.evalStr( " (define $a 11) $a" ).should == "11"
300
- @nendo.evalStr( " (define %a 12) %a" ).should == "12"
301
- @nendo.evalStr( " (define &a 13) &a" ).should == "13"
302
- @nendo.evalStr( " (define *a 14) *a" ).should == "14"
303
- @nendo.evalStr( " (define +a 15) +a" ).should == "15"
304
- @nendo.evalStr( " (define -a 16) -a" ).should == "16"
305
- @nendo.evalStr( " (define /a 17) /a" ).should == "17"
306
- @nendo.evalStr( " (define <a 18) <a" ).should == "18"
307
- @nendo.evalStr( " (define =a 19) =a" ).should == "19"
308
- @nendo.evalStr( " (define ?a 20) ?a" ).should == "20"
309
- @nendo.evalStr( " (define @a 21) @a" ).should == "21"
310
- @nendo.evalStr( " (define ^a 22) ^a" ).should == "22"
311
- @nendo.evalStr( " (define ~a 23) ~a" ).should == "23"
312
- @nendo.evalStr( " (define a! 30) a!" ).should == "30"
313
- @nendo.evalStr( " (define a$ 31) a$" ).should == "31"
314
- @nendo.evalStr( " (define a% 32) a%" ).should == "32"
315
- @nendo.evalStr( " (define a& 33) a&" ).should == "33"
316
- @nendo.evalStr( " (define a* 34) a*" ).should == "34"
317
- @nendo.evalStr( " (define a+ 35) a+" ).should == "35"
318
- @nendo.evalStr( " (define a- 36) a-" ).should == "36"
319
- @nendo.evalStr( " (define a/ 37) a/" ).should == "37"
320
- @nendo.evalStr( " (define a< 38) a<" ).should == "38"
321
- @nendo.evalStr( " (define a= 39) a=" ).should == "39"
322
- @nendo.evalStr( " (define a? 40) a?" ).should == "40"
323
- @nendo.evalStr( " (define a@ 41) a@" ).should == "41"
324
- @nendo.evalStr( " (define a^ 42) a^" ).should == "42"
325
- @nendo.evalStr( " (define a~ 43) a~" ).should == "43"
326
- @nendo.evalStr( " (define aFunc (lambda (x) x)) true" ).should == "#t"
327
- @nendo.evalStr( " (define aMacro (macro (x) x)) true" ).should == "#t"
328
- @nendo.evalStr( " (define a! 123) a!" ).should == "123"
329
- @nendo.evalStr( " (define b? 321) b?" ).should == "321"
330
- @nendo.evalStr( " (define a-b 1234) a-b" ).should == "1234"
331
- @nendo.evalStr( " (define start-end!? 4321) start-end!?" ).should == "4321"
332
- @nendo.evalStr( " (procedure? car) " ).should == "#t"
333
- @nendo.evalStr( " (procedure? aFunc) " ).should == "#t"
334
- @nendo.evalStr( " (procedure? aMacro) " ).should == "#f"
335
- @nendo.evalStr( " (procedure? 1) " ).should == "#f"
336
- @nendo.evalStr( " (procedure? 1.1) " ).should == "#f"
337
- @nendo.evalStr( " (procedure? \"str\") " ).should == "#f"
338
- @nendo.evalStr( " (procedure? 'a) " ).should == "#f"
339
- @nendo.evalStr( " (procedure? '(1)) " ).should == "#f"
340
- @nendo.evalStr( " (procedure? '()) " ).should == "#f"
341
- @nendo.evalStr( " (symbol? car) " ).should == "#f"
342
- @nendo.evalStr( " (symbol? aFunc) " ).should == "#f"
343
- @nendo.evalStr( " (symbol? aMacro) " ).should == "#f"
344
- @nendo.evalStr( " (symbol? 1) " ).should == "#f"
345
- @nendo.evalStr( " (symbol? 1.1) " ).should == "#f"
346
- @nendo.evalStr( " (symbol? \"str\") " ).should == "#f"
347
- @nendo.evalStr( " (symbol? 'a) " ).should == "#t"
348
- @nendo.evalStr( " (symbol? '(1)) " ).should == "#f"
349
- @nendo.evalStr( " (symbol? '()) " ).should == "#f"
350
- @nendo.evalStr( " (pair? car) " ).should == "#f"
351
- @nendo.evalStr( " (pair? aFunc) " ).should == "#f"
352
- @nendo.evalStr( " (pair? aMacro) " ).should == "#f"
353
- @nendo.evalStr( " (pair? 1) " ).should == "#f"
354
- @nendo.evalStr( " (pair? 1.1) " ).should == "#f"
355
- @nendo.evalStr( " (pair? \"str\") " ).should == "#f"
356
- @nendo.evalStr( " (pair? 'a) " ).should == "#f"
357
- @nendo.evalStr( " (pair? '(1)) " ).should == "#t"
358
- @nendo.evalStr( " (pair? '()) " ).should == "#f"
359
- @nendo.evalStr( " (number? car) " ).should == "#f"
360
- @nendo.evalStr( " (number? aFunc) " ).should == "#f"
361
- @nendo.evalStr( " (number? aMacro) " ).should == "#f"
362
- @nendo.evalStr( " (number? 1) " ).should == "#t"
363
- @nendo.evalStr( " (number? 1.1) " ).should == "#t"
364
- @nendo.evalStr( " (number? \"str\") " ).should == "#f"
365
- @nendo.evalStr( " (number? 'a) " ).should == "#f"
366
- @nendo.evalStr( " (number? '(1)) " ).should == "#f"
367
- @nendo.evalStr( " (number? '()) " ).should == "#f"
368
- @nendo.evalStr( " (integer? car) " ).should == "#f"
369
- @nendo.evalStr( " (integer? aFunc) " ).should == "#f"
370
- @nendo.evalStr( " (integer? aMacro) " ).should == "#f"
371
- @nendo.evalStr( " (integer? 1) " ).should == "#t"
372
- @nendo.evalStr( " (integer? 1.1) " ).should == "#f"
373
- @nendo.evalStr( " (integer? \"str\") " ).should == "#f"
374
- @nendo.evalStr( " (integer? 'a) " ).should == "#f"
375
- @nendo.evalStr( " (integer? '(1)) " ).should == "#f"
376
- @nendo.evalStr( " (integer? '()) " ).should == "#f"
377
- @nendo.evalStr( " (string? car) " ).should == "#f"
378
- @nendo.evalStr( " (string? aFunc) " ).should == "#f"
379
- @nendo.evalStr( " (string? aMacro) " ).should == "#f"
380
- @nendo.evalStr( " (string? 1) " ).should == "#f"
381
- @nendo.evalStr( " (string? 1.1) " ).should == "#f"
382
- @nendo.evalStr( " (string? \"str\") " ).should == "#t"
383
- @nendo.evalStr( " (string? 'a) " ).should == "#f"
384
- @nendo.evalStr( " (string? '(1)) " ).should == "#f"
385
- @nendo.evalStr( " (string? '()) " ).should == "#f"
386
- @nendo.evalStr( " (macro? car) " ).should == "#f"
387
- @nendo.evalStr( " (macro? aFunc) " ).should == "#f"
388
- @nendo.evalStr( " (macro? aMacro) " ).should == "#t"
389
- @nendo.evalStr( " (macro? 1) " ).should == "#f"
390
- @nendo.evalStr( " (macro? 1.1) " ).should == "#f"
391
- @nendo.evalStr( " (macro? \"str\") " ).should == "#f"
392
- @nendo.evalStr( " (macro? 'a) " ).should == "#f"
393
- @nendo.evalStr( " (macro? '(1)) " ).should == "#f"
394
- @nendo.evalStr( " (macro? '()) " ).should == "#f"
395
- @nendo.evalStr( " (length '()) " ).should == "0"
396
- @nendo.evalStr( " (length '(1)) " ).should == "1"
397
- @nendo.evalStr( " (length '((1))) " ).should == "1"
398
- @nendo.evalStr( " (length '(1 2)) " ).should == "2"
399
- lambda { @nendo.evalStr( " (length \"str\") " ) }.should raise_error(TypeError)
400
- lambda { @nendo.evalStr( " (length 1) " ) }.should raise_error(TypeError)
401
- @nendo.evalStr( " (symbol->string 'sym) " ).should == '"sym"'
402
- @nendo.evalStr( " (string->symbol \"sym\") " ).should == 'sym'
403
- @nendo.evalStr( ' (string-join \'("Aa" "Bb" "Cc") ) ' ).should == '"AaBbCc"'
404
- @nendo.evalStr( ' (string-join \'("Aa" "Bb" "Cc") ":") ' ).should == '"Aa:Bb:Cc"'
405
- @nendo.evalStr( ' (string-join \'("Aa" "Bb" "Cc") "//") ' ).should == '"Aa//Bb//Cc"'
406
- lambda { @nendo.evalStr( ' (string-join \'("Aa" "Bb" "Cc") 100) ' ) }.should raise_error(TypeError)
407
- lambda { @nendo.evalStr( ' (string-join \'("Aa" "Bb" "Cc") :xx) ' ) }.should raise_error(TypeError)
408
- @nendo.evalStr( ' (read-from-string "1") ' ).should == '1'
409
- @nendo.evalStr( ' (read-from-string "(+ 1 2)") ' ).should == '(+ 1 2)'
410
- @nendo.evalStr( ' (read-from-string "(\"Aa\" \"Bb\" \"Cc\")") ' ).should == '("Aa" "Bb" "Cc")'
411
- @nendo.evalStr( ' (eq? \'Aa (car (read-from-string "(Aa Bb Cc)"))) ' ).should == '#t'
412
- @nendo.evalStr( ' (= \'Aa (car (read-from-string "(Aa Bb Cc)"))) ' ).should == '#t'
413
- lambda { @nendo.evalStr( ' (read-from-string 100) ' ) }.should raise_error(TypeError)
414
- @nendo.evalStr( ' (write-to-string 1) ' ).should == '"1"'
415
- @nendo.evalStr( ' (write-to-string \'(+ 1 2)) ' ).should == '"(+ 1 2)"'
416
- @nendo.evalStr( ' (write-to-string \'("Aa" "Bb" "Cc")) ' ).should == '"(\"Aa\" \"Bb\" \"Cc\")"'
417
- end
418
- end
419
-
420
- describe Nendo, "when call evalStr() with variable modifications" do
421
- before do
422
- @nendo = Nendo::Core.new()
423
- end
424
- it "should" do
425
- @nendo.evalStr( " (define x 1) x " ).should == "1"
426
- @nendo.evalStr( " (define x 2) x " ).should == "2"
427
- @nendo.evalStr( " (define x 100) x " ).should == "100"
428
- @nendo.evalStr( " (define x true) x " ).should == "#t"
429
- @nendo.evalStr( " (define x false) x " ).should == "#f"
430
- @nendo.evalStr( " (define x nil) x " ).should == "nil"
431
- @nendo.evalStr( " (define x '()) x " ).should == "()"
432
- @nendo.evalStr( " (define x '(1)) x " ).should == "(1)"
433
- @nendo.evalStr( " (define x (+ 1 2 3)) x " ).should == "6"
434
- @nendo.evalStr( " (define x (sprintf \"$%02X\" 17)) x x x " ).should == '"$11"'
435
- @nendo.evalStr( " 1 2 3 " ).should == "3"
436
- @nendo.evalStr( " (define x 3.14) (set! x (* x 2)) x " ).should == "6.28"
437
- @nendo.evalStr( " 1 \n 2 \n 3 \n " ).should == "3"
438
- @nendo.evalStr( " (define a '(1 . 2)) (set-car! a 100) a " ).should == "(100 . 2)"
439
- @nendo.evalStr( " (define a '(1 . 2)) (set-car! a '()) a " ).should == "(() . 2)"
440
- @nendo.evalStr( " (define a '(1 . 2)) (set-car! a #t) a " ).should == "(#t . 2)"
441
- @nendo.evalStr( " (define a '(1 . 2)) (set-car! a #f) a " ).should == "(#f . 2)"
442
- @nendo.evalStr( " (define a '(1 . 2)) (set-car! a nil) a " ).should == "(nil . 2)"
443
- @nendo.evalStr( " (define a '(1 . 2)) (set-cdr! a 200) a " ).should == "(1 . 200)"
444
- @nendo.evalStr( " (define a '(1 . 2)) (set-cdr! a '(2)) a " ).should == "(1 2)"
445
- @nendo.evalStr( " (define a '(1 . 2)) (set-cdr! a '()) a " ).should == "(1)"
446
- @nendo.evalStr( " (define a '(1 . 2)) (set-cdr! a #t) a " ).should == "(1 . #t)"
447
- @nendo.evalStr( " (define a '(1 . 2)) (set-cdr! a #f) a " ).should == "(1 . #f)"
448
- @nendo.evalStr( " (define a '(1 . 2)) (set-cdr! a nil) a " ).should == "(1 . nil)"
449
- @nendo.evalStr( " (define a '((1 . 2) 3)) (set-car! (car a) 100) a " ).should == "((100 . 2) 3)"
450
- @nendo.evalStr( " (define a '((1 . 2) 3)) (set-cdr! (car a) 200) a " ).should == "((1 . 200) 3)"
451
- @nendo.evalStr( " (define a '((1 . 2) . 3)) (set-cdr! a 300) a " ).should == "((1 . 2) . 300)"
452
- end
453
- end
454
-
455
- describe Nendo, "when call evalStr() with undefined variable" do
456
- before do
457
- @nendo = Nendo::Core.new()
458
- @nendo.setDisplayErrors( false )
459
- end
460
- it "should" do
461
- lambda { @nendo.evalStr( " true " ) }.should_not raise_error
462
- lambda { @nendo.evalStr( " false " ) }.should_not raise_error
463
- lambda { @nendo.evalStr( " nil " ) }.should_not raise_error
464
- lambda { @nendo.evalStr( " line1 " ) }.should raise_error( NameError )
465
- lambda { @nendo.evalStr( " true \n line2 " ) }.should raise_error( NameError )
466
- lambda { @nendo.evalStr( " true \n true \n line3 " ) }.should raise_error( NameError )
467
- lambda { @nendo.evalStr( " (+ 1 x) " ) }.should raise_error( NameError )
468
- lambda { @nendo.evalStr( " true \n (+ 1 y) " ) }.should raise_error( NameError )
469
- end
470
- end
471
-
472
- describe Nendo, "when call evalStr() with built-in special forms" do
473
- before do
474
- @nendo = Nendo::Core.new()
475
- @nendo.setDisplayErrors( false )
476
- end
477
- it "should" do
478
- @nendo.evalStr( " (begin 1) " ).should == "1"
479
- @nendo.evalStr( " (begin 1 2) " ).should == "2"
480
- @nendo.evalStr( " (begin 1 2 3) " ).should == "3"
481
- @nendo.evalStr( <<EOS
482
- (set! x 2)
483
- (set! y (begin
484
- (set! x (* x 2))
485
- (set! x (* x 2))
486
- (set! x (* x 2))
487
- 100))
488
- (+ x y)
489
- EOS
490
- ).should == "116"
491
- @nendo.evalStr( " (%let () 100) " ).should == "100"
492
- @nendo.evalStr( " (%let ((a 11)) a) " ).should == "11"
493
- @nendo.evalStr( " (%let ((a 11) (b 22)) (+ a b)) " ).should == "33"
494
- @nendo.evalStr( " (%let ((a 22)) (%let ((b 33)) (+ a b))) " ).should == "55"
495
- @nendo.evalStr( " (%let ((a 22)(b 33)) (%let ((c 44) (d 55)) (+ a b c d))) " ).should == "154"
496
- @nendo.evalStr( " (%let ((a (%let ((b 2)) (+ 100 b)))) a) " ).should == "102"
497
- @nendo.evalStr( " (letrec () 100) " ).should == "100"
498
- @nendo.evalStr( " (letrec ((a 11)) a) " ).should == "11"
499
- @nendo.evalStr( " (letrec ((a 11) (b 22)) (+ a b)) " ).should == "33"
500
- @nendo.evalStr( " (letrec ((a 22)) (%let ((b 33)) (+ a b))) " ).should == "55"
501
- @nendo.evalStr( " (letrec ((a 22)(b 33)) (%let ((c 44) (d 55)) (+ a b c d))) " ).should == "154"
502
- @nendo.evalStr( " (letrec ((a (%let ((b 2)) (+ 100 b)))) a) " ).should == "102"
503
- @nendo.evalStr( <<EOS
504
- (letrec ((func1 (lambda () 13))
505
- (func2 (lambda () (* 2 (func1)))))
506
- (list (func2) (func1)))
507
- EOS
508
- ).should == "(26 13)"
509
- @nendo.evalStr( <<EOS
510
- (letrec ((func2 (lambda () (* 2 (func1))))
511
- (func1 (lambda () 7)))
512
- (list (func2) (func1)))
513
- EOS
514
- ).should == "(14 7)"
515
- @nendo.evalStr( " (if true 't 'f)" ).should == "t"
516
- @nendo.evalStr( " (if true '(1) '(2))" ).should == "(1)"
517
- @nendo.evalStr( " (if false 't 'f)" ).should == "f"
518
- @nendo.evalStr( " (if false '(1) '(2))" ).should == "(2)"
519
- @nendo.evalStr( " (set! x 0) (if true (set! x 1) (set! x 2)) x" ).should == "1"
520
- @nendo.evalStr( " (set! x 0) (if false (set! x 1) (set! x 2)) x" ).should == "2"
521
- @nendo.evalStr( " (set! func (lambda (arg1) arg1)) (list (func 1) (func 2))" ).should == "(1 2)"
522
- @nendo.evalStr( " ((lambda (arg1) arg1) 3)" ).should == "3"
523
- @nendo.evalStr( " ((lambda (arg1) arg1) (+ 1 2 3))" ).should == "6"
524
- @nendo.evalStr( " ((lambda (arg1 . arg2) arg1) 1 '(2))" ).should == "1"
525
- @nendo.evalStr( " ((lambda (arg1 . arg2) arg2) 1 '(2))" ).should == "((2))"
526
- @nendo.evalStr( " ((lambda (arg1 . arg2) arg2) 1 '(2 3))" ).should == "((2 3))"
527
- @nendo.evalStr( " ((lambda (arg1 . arg2) arg1) '() '())" ).should == "()"
528
- @nendo.evalStr( " ((lambda (arg1 . arg2) arg2) '() '())" ).should == "(())"
529
- @nendo.evalStr( " ((lambda (arg1 . arg2) arg1) (cdr '(100)) (cdr '(200)))" ).should == "()"
530
- @nendo.evalStr( " ((lambda (arg1 . arg2) arg2) (cdr '(100)) (cdr '(200)))" ).should == "(())"
531
- @nendo.evalStr( " ((if #t + *) 3 4)" ).should == "7"
532
- @nendo.evalStr( " ((if #f + *) 3 4)" ).should == "12"
533
- @nendo.evalStr( " (apply1 + '(1 2))" ).should == "3"
534
- @nendo.evalStr( " (apply1 + (range 10 1))" ).should == "55"
535
- @nendo.evalStr( " (apply1 cons '(1 2))" ).should == "(1 . 2)"
536
- @nendo.evalStr( " (apply1 list '(1 2 3))" ).should == "(1 2 3)"
537
- lambda { @nendo.evalStr( " (error \"My Runtime Error\") " ) }.should raise_error( RuntimeError, /My Runtime Error/ )
538
- lambda { @nendo.evalStr( " (error \"My Runtime Error\" '(a b c)) " ) }.should raise_error( RuntimeError, /My Runtime Error [(]a b c[)]/ )
539
- @nendo.evalStr( "((lambda (arg1) (+ 1 arg1)) 2)" ).should == "3"
540
- lambda { @nendo.evalStr( "((lambda (arg1) (+ 1 arg1)))" ) }.should raise_error( ArgumentError, /wrong number of arguments/ )
541
- end
542
- end
543
-
544
- describe Nendo, "when call evalStr() with built-in special forms (renamed symbol)" do
545
- before do
546
- @nendo = Nendo::Core.new( )
547
- @nendo.setDisplayErrors( false )
548
- @nendo.loadInitFile
549
- end
550
- it "should" do
551
- @nendo.evalStr( " (/nendo/core/begin 1) " ).should == "1"
552
- @nendo.evalStr( " (/nendo/core/begin 1 2) " ).should == "2"
553
- @nendo.evalStr( " (/nendo/core/begin 1 2 3) " ).should == "3"
554
- @nendo.evalStr( <<EOS
555
- (/nendo/core/set! x 2)
556
- (/nendo/core/set! y (/nendo/core/begin
557
- (/nendo/core/set! x (* x 2))
558
- (/nendo/core/set! x (* x 2))
559
- (/nendo/core/set! x (* x 2))
560
- 100))
561
- (+ x y)
562
- EOS
563
- ).should == "116"
564
- @nendo.evalStr( " (/nendo/core/%let () 100) " ).should == "100"
565
- @nendo.evalStr( " (/nendo/core/%let ((a 11)) a) " ).should == "11"
566
- @nendo.evalStr( " (/nendo/core/%let ((a 11) (b 22)) (+ a b)) " ).should == "33"
567
- @nendo.evalStr( " (/nendo/core/%let ((a 22)) (let ((b 33)) (+ a b))) " ).should == "55"
568
- @nendo.evalStr( " (/nendo/core/%let ((a 22)(b 33)) (let ((c 44) (d 55)) (+ a b c d))) " ).should == "154"
569
- @nendo.evalStr( " (/nendo/core/%let ((a (let ((b 2)) (+ 100 b)))) a) " ).should == "102"
570
- @nendo.evalStr( " (/nendo/core/letrec () 100) " ).should == "100"
571
- @nendo.evalStr( " (/nendo/core/letrec ((a 11)) a) " ).should == "11"
572
- @nendo.evalStr( " (/nendo/core/letrec ((a 11) (b 22)) (+ a b)) " ).should == "33"
573
- @nendo.evalStr( " (/nendo/core/letrec ((a 22)) (let ((b 33)) (+ a b))) " ).should == "55"
574
- @nendo.evalStr( " (/nendo/core/letrec ((a 22)(b 33)) (let ((c 44) (d 55)) (+ a b c d))) " ).should == "154"
575
- @nendo.evalStr( " (/nendo/core/letrec ((a (let ((b 2)) (+ 100 b)))) a) " ).should == "102"
576
- @nendo.evalStr( <<EOS
577
- (/nendo/core/letrec ((func1 (/nendo/core/lambda () 13))
578
- (func2 (/nendo/core/lambda () (* 2 (func1)))))
579
- (list (func2) (func1)))
580
- EOS
581
- ).should == "(26 13)"
582
- @nendo.evalStr( <<EOS
583
- (/nendo/core/letrec ((func2 (/nendo/core/lambda () (* 2 (func1))))
584
- (func1 (/nendo/core/lambda () 7)))
585
- (list (func2) (func1)))
586
- EOS
587
- ).should == "(14 7)"
588
- @nendo.evalStr( " (/nendo/core/if true 't 'f)" ).should == "t"
589
- @nendo.evalStr( " (/nendo/core/if true '(1) '(2))" ).should == "(1)"
590
- @nendo.evalStr( " (/nendo/core/if false 't 'f)" ).should == "f"
591
- @nendo.evalStr( " (/nendo/core/if false '(1) '(2))" ).should == "(2)"
592
- @nendo.evalStr( " (/nendo/core/set! x 0) (/nendo/core/if true (set! x 1) (set! x 2)) x" ).should == "1"
593
- @nendo.evalStr( " (/nendo/core/set! x 0) (/nendo/core/if false (set! x 1) (set! x 2)) x" ).should == "2"
594
- @nendo.evalStr( <<EOS
595
- (/nendo/core/set! func (/nendo/core/lambda (arg1) arg1))
596
- (list (func 1) (func 2))
597
- EOS
598
- ).should == "(1 2)"
599
- @nendo.evalStr( " ((/nendo/core/lambda (arg1) arg1) 3)" ).should == "3"
600
- @nendo.evalStr( " ((/nendo/core/lambda (arg1) arg1) (+ 1 2 3))" ).should == "6"
601
- @nendo.evalStr( " ((/nendo/core/if #t + *) 3 4)" ).should == "7"
602
- @nendo.evalStr( " ((/nendo/core/if #f + *) 3 4)" ).should == "12"
603
- lambda { @nendo.evalStr( " (/nendo/core/error \"My Runtime Error\") " ) }.should raise_error( RuntimeError )
604
- end
605
- end
606
-
607
-
608
- describe Nendo, "when redefined built-in functions(1)." do
609
- before do
610
- @nendo = Nendo::Core.new()
611
- @nendo.setDisplayErrors( false )
612
- @nendo.loadInitFile
613
- end
614
- it "should" do
615
- @nendo.evalStr( " (define (+ a b) (list a b)) (+ 1 2)" ).should == "(1 2)"
616
- lambda { @nendo.evalStr( " (define (+ a b) (list a b)) (+ 1 2 3)" ) }.should raise_error( ArgumentError )
617
- @nendo.evalStr( " (define (eq? a b) \"eq?\") (eq? 1 1)" ).should == '"eq?"'
618
- end
619
- end
620
-
621
- describe Nendo, "when redefined built-in functions(2)." do
622
- before do
623
- @nendo = Nendo::Core.new()
624
- @nendo.loadInitFile
625
- end
626
- it "should" do
627
- @nendo.evalStr( " (define (< a b) \"<\") (< 1 1)" ).should == '"<"'
628
- end
629
- end
630
-
631
- describe Nendo, "when redefined built-in functions(3)." do
632
- before do
633
- @nendo = Nendo::Core.new()
634
- @nendo.loadInitFile
635
- end
636
- it "should" do
637
- @nendo.evalStr( " (define (car lst) \"car\") (car '(1 2))" ).should == '"car"'
638
- end
639
- end
640
-
641
- describe Nendo, "when call evalStr() with global and lexical scope variable" do
642
- before do
643
- @nendo = Nendo::Core.new()
644
- end
645
- it "should" do
646
- @nendo.evalStr( " (define var 111) " ).should == "111"
647
- @nendo.evalStr( " (%let ((var 222)) var) " ).should == "222"
648
- @nendo.evalStr( " (%let ((var 222)) (set! var 333) var) " ).should == "333"
649
- @nendo.evalStr( " (%let ((var 222)) (set! var 333)) var " ).should == "111"
650
- @nendo.evalStr( " (define global1 \"G\") " ).should == '"G"'
651
- @nendo.evalStr( <<EOS
652
- (%let ((local1 \"L\")
653
- (local2 \"L\"))
654
- (set! global1 (+ global1 \"lobal1\"))
655
- (set! local1 (+ local1 \"ocal1\"))
656
- (set! local2 (+ local2 \"ocal2\"))
657
- (list global1
658
- local1
659
- local2
660
- (%let ((local1 \"A\")
661
- (local2 \"B\"))
662
- (set! local1 (+ local1 \"a\"))
663
- (set! local2 (+ local2 \"b\"))
664
- (list local1 local2
665
- (%let ((local1 \"CCC\"))
666
- (list global1 local1 local2))))))
667
- EOS
668
- ).should == '("Global1" "Local1" "Local2" ("Aa" "Bb" ("Global1" "CCC" "Bb")))'
669
- end
670
- end
671
-
672
- describe Nendo, "when call evalStr() with macroexpand-1 function" do
673
- before do
674
- @nendo = Nendo::Core.new()
675
- end
676
- it "should" do
677
- @nendo.evalStr( <<EOS
678
- (set! twice (macro (x) (list 'begin x x)))
679
- (macroexpand-1 '(twice (+ 1 1)))
680
- EOS
681
- ).should == "(begin (+ 1 1) (+ 1 1))"
682
- @nendo.evalStr( <<EOS
683
- (set! inc (macro (x) (list 'set! x (list '+ x 1))))
684
- (macroexpand-1 '(inc a))
685
- EOS
686
- ).should == "(set! a (+ a 1))"
687
- @nendo.evalStr( " (set! a 10) (inc a) " ).should == "11"
688
- @nendo.evalStr( " (set! a 10) (inc a) (inc a)" ).should == "12"
689
- @nendo.evalStr( <<EOS
690
- (macroexpand-1
691
- '(twice (twice (inc a))))
692
- EOS
693
- ).should ==
694
- "(begin (twice (inc a)) (twice (inc a)))"
695
- @nendo.evalStr( <<EOS
696
- (macroexpand-1
697
- (macroexpand-1
698
- '(twice (twice (inc a)))))
699
- EOS
700
- ).should ==
701
- "(begin (begin (inc a) (inc a)) (twice (inc a)))"
702
- @nendo.evalStr( <<EOS
703
- (macroexpand-1
704
- (macroexpand-1
705
- (macroexpand-1
706
- '(twice (twice (inc a))))))
707
- EOS
708
- ).should ==
709
- "(begin (begin (set! a (+ a 1)) (inc a)) (twice (inc a)))"
710
- @nendo.evalStr( <<EOS
711
- (macroexpand-1
712
- (macroexpand-1
713
- (macroexpand-1
714
- (macroexpand-1
715
- '(twice (twice (inc a)))))))
716
- EOS
717
- ).should ==
718
- "(begin (begin (set! a (+ a 1)) (set! a (+ a 1))) (twice (inc a)))"
719
- @nendo.evalStr( " (set! a 10) (twice (twice (inc a)))" ).should == "14"
720
- end
721
- end
722
-
723
-
724
- describe Nendo, "when use #xxxx syntax " do
725
- before do
726
- @nendo = Nendo::Core.new()
727
- @nendo.setDisplayErrors( false )
728
- @nendo.loadInitFile
729
- end
730
- it "should" do
731
- @nendo.evalStr( " #t " ).should == "#t"
732
- @nendo.evalStr( " #f " ).should == "#f"
733
- @nendo.evalStr( " '#( 1 ) " ).should == "#(1)"
734
- @nendo.evalStr( " '#() " ).should == "#()"
735
- @nendo.evalStr( " #! \n #t" ).should == "#t"
736
- @nendo.evalStr( " #! \n 100" ).should == "100"
737
- @nendo.evalStr( " #! 123 \n 100" ).should == "100"
738
- @nendo.evalStr( " '#?=1" ).should == "(debug-print 1 \"(string)\" 1 (quote 1))"
739
- @nendo.evalStr( " '#?." ).should == '"(string):1"'
740
- @nendo.evalStr( " '#?." ).should == '"(string):1"'
741
- @nendo.evalStr( " (begin #?. (+ 1 1))" ).should == "2"
742
- @nendo.evalStr( " (rxmatch #/[a-z]/ \"abc\")" ).should == "a"
743
- @nendo.evalStr( " (quote #?=(rxmatch #/[a-z]/ \"abc\"))" ).should == '(debug-print (rxmatch #/[a-z]/ "abc") "(string)" 1 (quote (rxmatch #/[a-z]/ "abc")))'
744
- @nendo.evalStr( <<EOS
745
- (begin
746
- #?.
747
- (+ 1 1)
748
- #?. )
749
- EOS
750
- ).should == '"(string):4"'
751
- @nendo.evalStr( " #b0 " ).should == Integer("0b0").to_s
752
- @nendo.evalStr( " #b01 " ).should == Integer("0b01").to_s
753
- @nendo.evalStr( " #b10 " ).should == Integer("0b10").to_s
754
- @nendo.evalStr( " #b00000001 " ).should == Integer("0b00000001").to_s
755
- @nendo.evalStr( " #b1010101010101010 " ).should == Integer("0b1010101010101010").to_s
756
- lambda { @nendo.evalStr( " #b2 " ) }.should raise_error(RuntimeError)
757
- lambda { @nendo.evalStr( " #b02 " ) }.should raise_error(RuntimeError)
758
- lambda { @nendo.evalStr( " #bF " ) }.should raise_error(RuntimeError)
759
- @nendo.evalStr( " #o0 " ).should == Integer("0o0").to_s
760
- @nendo.evalStr( " #o7 " ).should == Integer("0o7").to_s
761
- @nendo.evalStr( " #o01 " ).should == Integer("0o01").to_s
762
- @nendo.evalStr( " #o10 " ).should == Integer("0o10").to_s
763
- @nendo.evalStr( " #o777 " ).should == Integer("0o777").to_s
764
- @nendo.evalStr( " #o00000007 " ).should == Integer("0o00000007").to_s
765
- @nendo.evalStr( " #o0123456701234567 " ).should == Integer("0o0123456701234567").to_s
766
- lambda { @nendo.evalStr( " #o8 " ) }.should raise_error(RuntimeError)
767
- lambda { @nendo.evalStr( " #o08 " ) }.should raise_error(RuntimeError)
768
- lambda { @nendo.evalStr( " #oA " ) }.should raise_error(RuntimeError)
769
- @nendo.evalStr( " #d0 " ).should == Integer("0d0").to_s
770
- @nendo.evalStr( " #d9 " ).should == Integer("0d9").to_s
771
- @nendo.evalStr( " #d01 " ).should == Integer("0d01").to_s
772
- @nendo.evalStr( " #d10 " ).should == Integer("0d10").to_s
773
- @nendo.evalStr( " #d999 " ).should == Integer("0d999").to_s
774
- @nendo.evalStr( " #d00000009 " ).should == Integer("0d00000009").to_s
775
- @nendo.evalStr( " #d0123456701234567 " ).should == Integer("0d0123456701234567").to_s
776
- lambda { @nendo.evalStr( " #dA " ) }.should raise_error(RuntimeError)
777
- lambda { @nendo.evalStr( " #dF " ) }.should raise_error(RuntimeError)
778
- @nendo.evalStr( " #x0 " ).should == Integer("0x0").to_s
779
- @nendo.evalStr( " #x9 " ).should == Integer("0x9").to_s
780
- @nendo.evalStr( " #xA " ).should == Integer("0xA").to_s
781
- @nendo.evalStr( " #xF " ).should == Integer("0xF").to_s
782
- @nendo.evalStr( " #x01 " ).should == Integer("0x01").to_s
783
- @nendo.evalStr( " #x10 " ).should == Integer("0x10").to_s
784
- @nendo.evalStr( " #xFFF " ).should == Integer("0xFFF").to_s
785
- @nendo.evalStr( " #x0000000F " ).should == Integer("0x0000000F").to_s
786
- @nendo.evalStr( " #x0123456789ABCDEF0123456789ABCDEF " ).should == Integer("0x0123456789ABCDEF0123456789ABCDEF").to_s
787
- lambda { @nendo.evalStr( " #xg " ) }.should raise_error(RuntimeError)
788
- lambda { @nendo.evalStr( " #xh " ) }.should raise_error(RuntimeError)
789
- lambda { @nendo.evalStr( " #xz " ) }.should raise_error(RuntimeError)
790
- lambda { @nendo.evalStr( " #xG " ) }.should raise_error(RuntimeError)
791
- lambda { @nendo.evalStr( " #xH " ) }.should raise_error(RuntimeError)
792
- lambda { @nendo.evalStr( " #xZ " ) }.should raise_error(RuntimeError)
793
- lambda { @nendo.evalStr( " #a " ) }.should raise_error(NameError)
794
- lambda { @nendo.evalStr( " #c " ) }.should raise_error(NameError)
795
- lambda { @nendo.evalStr( " #e " ) }.should raise_error(NameError)
796
- lambda { @nendo.evalStr( " #tt " ) }.should raise_error(NameError)
797
- lambda { @nendo.evalStr( " #ff " ) }.should raise_error(NameError)
798
- lambda { @nendo.evalStr( " #abc " ) }.should raise_error(NameError)
799
- lambda { @nendo.evalStr( " #? " ) }.should raise_error(NameError)
800
- lambda { @nendo.evalStr( " #?a " ) }.should raise_error(NameError)
801
- lambda { @nendo.evalStr( " #= " ) }.should raise_error(NameError)
802
- lambda { @nendo.evalStr( " #?? " ) }.should raise_error(NameError)
803
- end
804
- end
805
-
806
- describe Nendo, "when use regexp litteral and library functions " do
807
- before do
808
- @nendo = Nendo::Core.new()
809
- @nendo.setDisplayErrors( false )
810
- @nendo.loadInitFile
811
- end
812
- it "should" do
813
- @nendo.evalStr( " #/abc/ " ).should == "#/abc/"
814
- @nendo.evalStr( " #/[a-z]/ " ).should == "#/[a-z]/"
815
- @nendo.evalStr( " #/[a-zA-Z0-9]+/ " ).should == "#/[a-zA-Z0-9]+/"
816
- @nendo.evalStr( ' #/\d/ ' ).should == '#/\d/'
817
- @nendo.evalStr( ' #/[\/]/ ' ).should == '#/[\/]/'
818
- @nendo.evalStr( ' #/\]/ ' ).should == '#/\]/'
819
- @nendo.evalStr( ' #/^\]/ ' ).should == '#/^\]/'
820
- @nendo.evalStr( ' #/\[/ ' ).should == '#/\[/'
821
- @nendo.evalStr( ' #/^\[/ ' ).should == '#/^\[/'
822
- @nendo.evalStr( ' #/\.\^\$\/\+\-\(\)\|/ ' ).should == '#/\.\^\$\/\+\-\(\)\|/'
823
- @nendo.evalStr( " #/abc/i " ).should == "#/abc/i"
824
- @nendo.evalStr( " #/[a-z]/i " ).should == "#/[a-z]/i"
825
- lambda { @nendo.evalStr( " #/[a-z]/I " ) }.should raise_error(NameError)
826
- lambda { @nendo.evalStr( " #/[a-z]/a " ) }.should raise_error(NameError)
827
-
828
- @nendo.evalStr( ' (string->regexp "abc") ' ).should == '#/abc/'
829
- @nendo.evalStr( ' (string->regexp "[a-z]") ' ).should == '#/[a-z]/'
830
- @nendo.evalStr( ' (string->regexp "[a-zA-Z0-9]+" ) ' ).should == '#/[a-zA-Z0-9]+/'
831
- @nendo.evalStr( ' (string->regexp "\\\\d" ) ' ).should == '#/\d/'
832
- @nendo.evalStr( " (regexp? #/str/ ) " ).should == "#t"
833
- @nendo.evalStr( " (regexp? #/str/i ) " ).should == "#t"
834
- @nendo.evalStr( " (regexp? \"str\" ) " ).should == "#f"
835
- @nendo.evalStr( " (regexp? 'str) " ).should == "#f"
836
- @nendo.evalStr( " (regexp? (. \"str\" intern)) " ).should == "#f"
837
- @nendo.evalStr( " (regexp? 100) " ).should == "#f"
838
-
839
- @nendo.evalStr( " (regexp->string #/abc/ ) " ).should == '"abc"'
840
- @nendo.evalStr( " (regexp->string #/[a-z]/ ) " ).should == '"[a-z]"'
841
- @nendo.evalStr( " (regexp->string #/[a-zA-Z0-9]+/ ) " ).should == '"[a-zA-Z0-9]+"'
842
- @nendo.evalStr( ' (regexp->string #/\d+/ ) ' ).should == '"\\\\d+"'
843
-
844
- @nendo.evalStr( ' (define matchdata (rxmatch #/(\d+):(\d+)/ "foo314:2000bar")) ' ).should == '314:2000'
845
- @nendo.evalStr( ' (rxmatch-start matchdata) ' ).should == '3'
846
- @nendo.evalStr( ' (rxmatch-start matchdata 0) ' ).should == '3'
847
- @nendo.evalStr( ' (rxmatch-start matchdata 1) ' ).should == '3'
848
- @nendo.evalStr( ' (rxmatch-start matchdata 2) ' ).should == '7'
849
- @nendo.evalStr( ' (rxmatch-end matchdata) ' ).should == '11'
850
- @nendo.evalStr( ' (rxmatch-end matchdata 0) ' ).should == '11'
851
- @nendo.evalStr( ' (rxmatch-end matchdata 1) ' ).should == '6'
852
- @nendo.evalStr( ' (rxmatch-end matchdata 2) ' ).should == '11'
853
- @nendo.evalStr( ' (rxmatch-substring matchdata) ' ).should == '"314:2000"'
854
- @nendo.evalStr( ' (rxmatch-substring matchdata 0) ' ).should == '"314:2000"'
855
- @nendo.evalStr( ' (rxmatch-substring matchdata 1) ' ).should == '"314"'
856
- @nendo.evalStr( ' (rxmatch-substring matchdata 2) ' ).should == '"2000"'
857
- @nendo.evalStr( ' (rxmatch-num-matches matchdata) ' ).should == '3'
858
-
859
- @nendo.evalStr( ' (define matchdata (rxmatch #/(\w+)@([\w.]+)/ "foo@example.com")) ' ).should == 'foo@example.com'
860
- @nendo.evalStr( ' (rxmatch-substring matchdata) ' ).should == '"foo@example.com"'
861
- @nendo.evalStr( ' (rxmatch-substring matchdata 0) ' ).should == '"foo@example.com"'
862
- @nendo.evalStr( ' (rxmatch-substring matchdata 1) ' ).should == '"foo"'
863
- @nendo.evalStr( ' (rxmatch-substring matchdata 2) ' ).should == '"example.com"'
864
-
865
- @nendo.evalStr( ' (rxmatch->string #/(\w+)@([\w.]+)/ "foo@example.com")' ).should == '"foo@example.com"'
866
- @nendo.evalStr( ' (rxmatch->string #/(\w+)@([\w.]+)/ "foo@example.com" 0)' ).should == '"foo@example.com"'
867
- @nendo.evalStr( ' (rxmatch->string #/(\w+)@([\w.]+)/ "foo@example.com" 1)' ).should == '"foo"'
868
- @nendo.evalStr( ' (rxmatch->string #/(\w+)@([\w.]+)/ "foo@example.com" 2)' ).should == '"example.com"'
869
-
870
- @nendo.evalStr( ' (rxmatch->string #/abc/ "000abc00ABC000")' ).should == '"abc"'
871
- @nendo.evalStr( ' (rxmatch->string #/ABC/ "000abc00ABC000")' ).should == '"ABC"'
872
- @nendo.evalStr( ' (rxmatch->string #/abc/i "abc")' ).should == '"abc"'
873
- @nendo.evalStr( ' (rxmatch->string #/abc/i "ABC")' ).should == '"ABC"'
874
- @nendo.evalStr( ' (rxmatch->string #/ABC/i "abc")' ).should == '"abc"'
875
- @nendo.evalStr( ' (rxmatch->string #/abc/i "AbC")' ).should == '"AbC"'
876
-
877
- @nendo.evalStr( ' (rxmatch #/abc/i "xxx")' ).should == '#f'
878
- @nendo.evalStr( ' (rxmatch #/XXX/ "xxx")' ).should == '#f'
879
- @nendo.evalStr( ' (rxmatch->string #/abc/i "xxx")' ).should == '#f'
880
- @nendo.evalStr( ' (rxmatch->string #/XXX/ "xxx")' ).should == '#f'
881
-
882
- pending( "JRuby can't compute correctly" ) if defined? JRUBY_VERSION
883
- @nendo.evalStr( <<EOS
884
- (define matchdata
885
- (rxmatch #/([あ-ん])([あ-ん])([あ-ん])([あ-ん])([あ-ん])/
886
- "ABC漢字あいうえお漢字ABC"))
887
- EOS
888
- ).should == 'あいうえお'
889
- @nendo.evalStr( ' (rxmatch-start matchdata) ' ).should == '5'
890
- @nendo.evalStr( ' (rxmatch-end matchdata) ' ).should == '10'
891
- @nendo.evalStr( ' (rxmatch-substring matchdata) ' ).should == '"あいうえお"'
892
- @nendo.evalStr( ' (rxmatch-substring matchdata 1) ' ).should == '"あ"'
893
- @nendo.evalStr( ' (rxmatch-substring matchdata 2) ' ).should == '"い"'
894
- @nendo.evalStr( ' (rxmatch-substring matchdata 3) ' ).should == '"う"'
895
- end
896
- end
897
-
898
-
899
- describe Nendo, "when call functions in init.nnd " do
900
- before do
901
- @nendo = Nendo::Core.new()
902
- @nendo.loadInitFile
903
- @nendo.loadInitFile # to self optimizing. The init.nnd file will be loaded twice, so `map' can be optimized on second loading phase.
904
- end
905
- it "should" do
906
- @nendo.evalStr( " (cadr '(1 2 3 4)) " ).should == "2"
907
- @nendo.evalStr( " (caddr '(1 2 3 4)) " ).should == "3"
908
- @nendo.evalStr( " (cadddr '(1 2 3 4)) " ).should == "4"
909
- @nendo.evalStr( " (caar '((5 6 7 8))) " ).should == "5"
910
- @nendo.evalStr( " (cdar '((5 6 7 8))) " ).should == "(6 7 8)"
911
- @nendo.evalStr( " (cadar '((5 6 7 8))) " ).should == "6"
912
- @nendo.evalStr( " (cddar '((5 6 7 8))) " ).should == "(7 8)"
913
- @nendo.evalStr( " (iota 1) " ).should == "(0)"
914
- @nendo.evalStr( " (iota 3) " ).should == "(0 1 2)"
915
- @nendo.evalStr( " (append '() '()) " ).should == "()"
916
- @nendo.evalStr( " (append '(1) '()) " ).should == "(1)"
917
- @nendo.evalStr( " (append '() '(2)) " ).should == "(2)"
918
- @nendo.evalStr( " (append '(1) '(2)) " ).should == "(1 2)"
919
- @nendo.evalStr( " (append '(1 2) '(3 4)) " ).should == "(1 2 3 4)"
920
- @nendo.evalStr( " (append '(1 2) (cdr '(200))) " ).should == "(1 2)"
921
- @nendo.evalStr( " (append (cdr '(100)) '(10 20)) " ).should == "(10 20)"
922
- @nendo.evalStr( " (define lst '()) " ).should == "()"
923
- @nendo.evalStr( " (push! lst 1) " ).should == "(1)"
924
- @nendo.evalStr( " lst " ).should == "(1)"
925
- @nendo.evalStr( " (push! lst 2) " ).should == "(2 1)"
926
- @nendo.evalStr( " lst " ).should == "(2 1)"
927
- @nendo.evalStr( " (push! lst 3) " ).should == "(3 2 1)"
928
- @nendo.evalStr( " lst " ).should == "(3 2 1)"
929
- @nendo.evalStr( ' (push! lst "str") ' ).should == '("str" 3 2 1)'
930
- @nendo.evalStr( " lst " ).should == '("str" 3 2 1)'
931
- @nendo.evalStr( " (pair? '()) " ).should == "#f"
932
- @nendo.evalStr( " (pair? '(1)) " ).should == "#t"
933
- @nendo.evalStr( " (pair? '(1 2)) " ).should == "#t"
934
- @nendo.evalStr( " (pair? '(1 2 3)) " ).should == "#t"
935
- @nendo.evalStr( " (pair? '(1 . 2)) " ).should == "#t"
936
- @nendo.evalStr( " (pair? '(())) " ).should == "#t"
937
- @nendo.evalStr( " (pair? 1) " ).should == "#f"
938
- @nendo.evalStr( " (pair? \"str\") " ).should == "#f"
939
- @nendo.evalStr( " (list? '()) " ).should == "#t"
940
- @nendo.evalStr( " (list? '(1)) " ).should == "#t"
941
- @nendo.evalStr( " (list? '(1 2)) " ).should == "#t"
942
- @nendo.evalStr( " (list? '(1 2 3)) " ).should == "#t"
943
- @nendo.evalStr( " (list? '(1 . 2)) " ).should == "#f"
944
- @nendo.evalStr( " (list? '(1 2 . 3)) " ).should == "#f"
945
- @nendo.evalStr( " (list? '(())) " ).should == "#t"
946
- @nendo.evalStr( " (list? 1) " ).should == "#f"
947
- @nendo.evalStr( " (list? \"str\") " ).should == "#f"
948
- @nendo.evalStr( " (even? 2) " ).should == "#t"
949
- @nendo.evalStr( " (even? 1) " ).should == "#f"
950
- @nendo.evalStr( " (even? 0) " ).should == "#t"
951
- @nendo.evalStr( " (even? -1) " ).should == "#f"
952
- @nendo.evalStr( " (even? -2) " ).should == "#t"
953
- @nendo.evalStr( " (odd? 2) " ).should == "#f"
954
- @nendo.evalStr( " (odd? 1) " ).should == "#t"
955
- @nendo.evalStr( " (odd? 0) " ).should == "#f"
956
- @nendo.evalStr( " (odd? -1) " ).should == "#t"
957
- @nendo.evalStr( " (odd? -2) " ).should == "#f"
958
- @nendo.evalStr( " (zero? 0) " ).should == "#t"
959
- @nendo.evalStr( " (zero? #f) " ).should == "#f"
960
- @nendo.evalStr( " (zero? #t) " ).should == "#f"
961
- @nendo.evalStr( " (zero? 1) " ).should == "#f"
962
- @nendo.evalStr( " (zero? 2) " ).should == "#f"
963
- @nendo.evalStr( " (zero? -1) " ).should == "#f"
964
- @nendo.evalStr( " (zero? \"str\") " ).should == "#f"
965
- @nendo.evalStr( " (zero? zero?) " ).should == "#f"
966
- @nendo.evalStr( " (positive? 1) " ).should == "#t"
967
- @nendo.evalStr( " (positive? 0) " ).should == "#f"
968
- @nendo.evalStr( " (positive? -1) " ).should == "#f"
969
- @nendo.evalStr( " (negative? 1) " ).should == "#f"
970
- @nendo.evalStr( " (negative? 0) " ).should == "#f"
971
- @nendo.evalStr( " (negative? -1) " ).should == "#t"
972
- @nendo.evalStr( " (abs -1) " ).should == "1"
973
- @nendo.evalStr( " (abs 1) " ).should == "1"
974
- @nendo.evalStr( " (abs -1000) " ).should == "1000"
975
- @nendo.evalStr( " (abs 1000) " ).should == "1000"
976
- @nendo.evalStr( " (max -2 1 0 1 2 3 4 5) " ).should == "5"
977
- @nendo.evalStr( " (max 5 4 3 2 1 0 -1 -2) " ).should == "5"
978
- @nendo.evalStr( " (max 1000000000 10 -10000) " ).should == "1000000000"
979
- @nendo.evalStr( " (min -2 1 0 1 2 3 4 5) " ).should == "-2"
980
- @nendo.evalStr( " (min 5 4 3 2 1 0 -1 -2) " ).should == "-2"
981
- @nendo.evalStr( " (min 1000000000 10 -10000) " ).should == "-10000"
982
- @nendo.evalStr( " (succ -1) " ).should == "0"
983
- @nendo.evalStr( " (succ 0) " ).should == "1"
984
- @nendo.evalStr( " (succ 1) " ).should == "2"
985
- @nendo.evalStr( " (pred -1) " ).should == "-2"
986
- @nendo.evalStr( " (pred 0) " ).should == "-1"
987
- @nendo.evalStr( " (pred 1) " ).should == "0"
988
- @nendo.evalStr( " (pred 2) " ).should == "1"
989
- @nendo.evalStr( " (min 1000000000 10 -10000) " ).should == "-10000"
990
- @nendo.evalStr( " (nth 0 '(100 200 300)) " ).should == "100"
991
- @nendo.evalStr( " (nth 1 '(100 200 300)) " ).should == "200"
992
- @nendo.evalStr( " (nth 2 '(100 200 300)) " ).should == "300"
993
- @nendo.evalStr( " (nth 3 '(100 200 300)) " ).should == "()"
994
- @nendo.evalStr( " (nth -1 '(100 200 300)) " ).should == "()"
995
- @nendo.evalStr( " (first '(100 200 300 400 500 600 700 800 900 1000)) " ).should == "100"
996
- @nendo.evalStr( " (second '(100 200 300 400 500 600 700 800 900 1000)) " ).should == "200"
997
- @nendo.evalStr( " (third '(100 200 300 400 500 600 700 800 900 1000)) " ).should == "300"
998
- @nendo.evalStr( " (fourth '(100 200 300 400 500 600 700 800 900 1000)) " ).should == "400"
999
- @nendo.evalStr( " (fifth '(100 200 300 400 500 600 700 800 900 1000)) " ).should == "500"
1000
- @nendo.evalStr( " (sixth '(100 200 300 400 500 600 700 800 900 1000)) " ).should == "600"
1001
- @nendo.evalStr( " (seventh '(100 200 300 400 500 600 700 800 900 1000)) " ).should == "700"
1002
- @nendo.evalStr( " (eighth '(100 200 300 400 500 600 700 800 900 1000)) " ).should == "800"
1003
- @nendo.evalStr( " (ninth '(100 200 300 400 500 600 700 800 900 1000)) " ).should == "900"
1004
- @nendo.evalStr( " (tenth '(100 200 300 400 500 600 700 800 900 1000)) " ).should == "1000"
1005
- @nendo.evalStr( " (first '()) " ).should == "()"
1006
- @nendo.evalStr( " (tenth '()) " ).should == "()"
1007
- @nendo.evalStr( " (to-s 10) " ).should == '"10"'
1008
- @nendo.evalStr( " (to_s 10) " ).should == '"10"'
1009
- @nendo.evalStr( " (x->string 10) " ).should == '"10"'
1010
- @nendo.evalStr( " (number->string 11) " ).should == '"11"'
1011
- @nendo.evalStr( " (to-s 2.1) " ).should == '"2.1"'
1012
- @nendo.evalStr( " (to_s 2.1) " ).should == '"2.1"'
1013
- @nendo.evalStr( " (x->string 2.1) " ).should == '"2.1"'
1014
- @nendo.evalStr( " (number->string 2.2) " ).should == '"2.2"'
1015
- @nendo.evalStr( ' (string-append) ' ).should == '""'
1016
- @nendo.evalStr( ' (string-append "a") ' ).should == '"a"'
1017
- @nendo.evalStr( ' (string-append "a" "b") ' ).should == '"ab"'
1018
- @nendo.evalStr( ' (string-append "a" "B" "c" "D" "e") ' ).should == '"aBcDe"'
1019
- @nendo.evalStr( ' (string=? "ABC" "ABC") ' ).should == '#t'
1020
- @nendo.evalStr( ' (string=? "ABC" "ABc") ' ).should == '#f'
1021
- @nendo.evalStr( ' (string=? "ABC" "abc") ' ).should == '#f'
1022
- @nendo.evalStr( ' (string=? 100 100) ' ).should == '#t'
1023
- @nendo.evalStr( ' (string=? (+ "A" "B") "AB") ' ).should == '#t'
1024
- @nendo.evalStr( " (to_i \"22\") " ).should == '22'
1025
- @nendo.evalStr( " (to-i \"22\") " ).should == '22'
1026
- @nendo.evalStr( " (to_i \"10000\") " ).should == '10000'
1027
- @nendo.evalStr( " (to-i \"10000\") " ).should == '10000'
1028
- @nendo.evalStr( " (let1 aaa 111 aaa) " ).should == "111"
1029
- @nendo.evalStr( " (let1 aaa (+ 2 3) aaa) " ).should == "5"
1030
- @nendo.evalStr( " (let1 aaa 333 (let1 bbb 444 (+ aaa bbb))) " ).should == "777"
1031
- @nendo.evalStr( " (let1 aaa 333 (let1 bbb 444 (set! bbb 555) (+ aaa bbb))) " ).should == "888"
1032
- @nendo.evalStr( " (memq 'b '(a b c d)) " ).should == "(b c d)"
1033
- @nendo.evalStr( " (memq 'c '(a b c d)) " ).should == "(c d)"
1034
- @nendo.evalStr( " (memq 'd '(a b c d)) " ).should == "(d)"
1035
- @nendo.evalStr( " (memq 'e '(a b c d)) " ).should == "#f"
1036
- @nendo.evalStr( " (memq 'e '(a b c d . e)) " ).should == "#f"
1037
- @nendo.evalStr( ' (memq "b" \'("a" "b" "c" "d")) ' ).should == '("b" "c" "d")'
1038
- @nendo.evalStr( ' (memv "b" \'("a" "b" "c" "d")) ' ).should == '("b" "c" "d")'
1039
- @nendo.evalStr( ' (memv "c" \'("a" "b" "c" "d")) ' ).should == '("c" "d")'
1040
- @nendo.evalStr( ' (memv "d" \'("a" "b" "c" "d")) ' ).should == '("d")'
1041
- @nendo.evalStr( ' (memv "e" \'("a" "b" "c" "d")) ' ).should == '#f'
1042
- @nendo.evalStr( ' (memv "e" \'("a" "b" "c" "d" . "e")) ' ).should == '#f'
1043
- @nendo.evalStr( ' (memv \'("b") \'(("a") ("b") ("c") ("d"))) ' ).should == '#f'
1044
- @nendo.evalStr( ' (member \'("b") \'(("a") ("b") ("c") ("d"))) ' ).should == '(("b") ("c") ("d"))'
1045
- @nendo.evalStr( ' (member \'("c") \'(("a") ("b") ("c") ("d"))) ' ).should == '(("c") ("d"))'
1046
- @nendo.evalStr( ' (member \'("d") \'(("a") ("b") ("c") ("d"))) ' ).should == '(("d"))'
1047
- @nendo.evalStr( ' (member \'("e") \'(("a") ("b") ("c") ("d"))) ' ).should == '#f'
1048
- @nendo.evalStr( ' (member \'("e") \'(("a") ("b") ("c") ("d") . ("e"))) ' ).should == '#f'
1049
- @nendo.evalStr( " (let1 v (map (lambda (x) x) '(1 2 3)) v) " ).should == "(1 2 3)"
1050
- @nendo.evalStr( " (let ((v (map (lambda (x) x) '(1 2 3)))) v) " ).should == "(1 2 3)"
1051
- @nendo.evalStr( " (cond (true 1) (false 2)) " ).should == "1"
1052
- @nendo.evalStr( " (cond (true ) (false )) " ).should == "#t"
1053
- @nendo.evalStr( " (cond (false 1) (true 2)) " ).should == "2"
1054
- @nendo.evalStr( " (cond (true 1) (true 2)) " ).should == "1"
1055
- @nendo.evalStr( " (cond (false 1) (false 2)) " ).should == "()"
1056
- @nendo.evalStr( " (cond (false 1) (false 2) (else 3)) " ).should == "3"
1057
- @nendo.evalStr( <<EOS
1058
- (cond
1059
- ((- 10 9)
1060
- =>
1061
- (lambda (x)
1062
- (+ \"<\" (to_s x) \">\")))
1063
- (else
1064
- 2))
1065
- EOS
1066
- ).should == '"<1>"'
1067
- @nendo.evalStr( <<EOS
1068
- (cond
1069
- (true 1)
1070
- ((- 10 8)
1071
- =>
1072
- (lambda (x)
1073
- (+ \"<\" (to_s x) \">\")))
1074
- (else
1075
- 3))
1076
- EOS
1077
- ).should == "1"
1078
- @nendo.evalStr( " (or) " ).should == "#f"
1079
- @nendo.evalStr( " (or true) " ).should == "#t"
1080
- @nendo.evalStr( " (or false) " ).should == "#f"
1081
- @nendo.evalStr( " (or nil) " ).should == "#f"
1082
- @nendo.evalStr( " (or '(1)) " ).should == "(1)"
1083
- @nendo.evalStr( " (or '()) " ).should == "()"
1084
- @nendo.evalStr( " (or true true true) " ).should == "#t"
1085
- @nendo.evalStr( " (or 1 2 3) " ).should == "1"
1086
- @nendo.evalStr( " (or false 2) " ).should == "2"
1087
- @nendo.evalStr( " (or false false 3) " ).should == "3"
1088
- @nendo.evalStr( " (or false '(2) false) " ).should == "(2)"
1089
- @nendo.evalStr( " (and) " ).should == "#t"
1090
- @nendo.evalStr( " (and true) " ).should == "#t"
1091
- @nendo.evalStr( " (and false) " ).should == "#f"
1092
- @nendo.evalStr( " (and nil) " ).should == "nil"
1093
- @nendo.evalStr( " (and '(1)) " ).should == "(1)"
1094
- @nendo.evalStr( " (and '()) " ).should == "()"
1095
- @nendo.evalStr( " (and true true true) " ).should == "#t"
1096
- @nendo.evalStr( " (and 1 2 3) " ).should == "3"
1097
- @nendo.evalStr( " (and false 2) " ).should == "#f"
1098
- @nendo.evalStr( " (and false false 3) " ).should == "#f"
1099
- @nendo.evalStr( " (and true 2) " ).should == "2"
1100
- @nendo.evalStr( " (and true true 3) " ).should == "3"
1101
- @nendo.evalStr( " (and true true 3 false) " ).should == "#f"
1102
- @nendo.evalStr( " (and true '(2) true) " ).should == "#t"
1103
- @nendo.evalStr( " (and true true '(2)) " ).should == "(2)"
1104
- @nendo.evalStr( " (and true '(2) false) " ).should == "#f"
1105
- @nendo.evalStr( <<EOS
1106
- (define total 0)
1107
- (and 1
1108
- 2
1109
- (set! total (+ total 1))
1110
- (set! total (+ total 2))
1111
- 5)
1112
- total
1113
- EOS
1114
- ).should == "3"
1115
- @nendo.evalStr( <<EOS
1116
- (define total 1)
1117
- (and 1
1118
- 2
1119
- false
1120
- (set! total (+ total 2))
1121
- (set! total (+ total 3))
1122
- 5)
1123
- total
1124
- EOS
1125
- ).should == "1"
1126
- @nendo.evalStr( " (apply + 100 '()) " ).should == "100"
1127
- @nendo.evalStr( " (apply + '(1 2)) " ).should == "3"
1128
- @nendo.evalStr( " (apply + 1 2 '(3)) " ).should == "6"
1129
- @nendo.evalStr( " (apply + 1 2 '(3 4)) " ).should == "10"
1130
- @nendo.evalStr( " (apply + 1 2 3 '(4)) " ).should == "10"
1131
- @nendo.evalStr( ' (apply + \'("a" "b" "c")) ' ).should == '"abc"'
1132
- @nendo.evalStr( " (range 5) " ).should == "(0 1 2 3 4)"
1133
- @nendo.evalStr( " (range 5 1) " ).should == "(1 2 3 4 5)"
1134
- @nendo.evalStr( " (range 5 2) " ).should == "(2 3 4 5 6)"
1135
- @nendo.evalStr( " (iota 5 2) " ).should == "(2 3 4 5 6)"
1136
- @nendo.evalStr( " (apply + (range 11)) " ).should == "55"
1137
- @nendo.evalStr( " (apply + (map (lambda (x) (+ x 1)) (range 10))) " ).should == "55"
1138
- @nendo.evalStr( " (apply + (append (range 11) '(100))) " ).should == "155"
1139
- @nendo.evalStr( " (apply cons '(1 2)) " ).should == "(1 . 2)"
1140
- @nendo.evalStr( " (apply list '(1 2)) " ).should == "(1 2)"
1141
- @nendo.evalStr( " (apply list (list 1 '())) " ).should == "(1 ())"
1142
- @nendo.evalStr( " (apply list (list 1 (cdr '(100)))) " ).should == "(1 ())"
1143
- @nendo.evalStr( " (map (lambda (x) 1) '()) " ).should == "()"
1144
- @nendo.evalStr( " (map (lambda (x) 1) (to-list '#())) " ).should == "()"
1145
- @nendo.evalStr( " (map (lambda (x) (* x 2)) '(1 2 3)) " ).should == "(2 4 6)"
1146
- @nendo.evalStr( " (map (lambda (x) (+ x 1)) '(1 2 3)) " ).should == "(2 3 4)"
1147
- @nendo.evalStr( <<EOS
1148
- (map
1149
- (lambda (a b) (+ a b))
1150
- '(1 2 3)
1151
- '(10 20 30))
1152
- EOS
1153
- ).should == "(11 22 33)"
1154
- @nendo.evalStr( <<EOS
1155
- (map
1156
- (lambda (a b)
1157
- (- b a))
1158
- '(1 2 3)
1159
- '(10 20 30))
1160
- EOS
1161
- ).should == "(9 18 27)"
1162
- @nendo.evalStr( <<EOS
1163
- (map
1164
- (lambda (a b c)
1165
- (+ a b c))
1166
- '(1 2 3)
1167
- '(10 20 30)
1168
- '(100 200 300))
1169
- EOS
1170
- ).should == "(111 222 333)"
1171
- @nendo.evalStr( <<EOS
1172
- (define _result
1173
- (map
1174
- (lambda (x)
1175
- (* x 2))
1176
- (range 10000 1)))
1177
- (list
1178
- (first _result)
1179
- (second _result)
1180
- (last-pair _result))
1181
- EOS
1182
- ).should == "(2 4 (20000))"
1183
-
1184
- @nendo.evalStr( <<EOS
1185
- (list
1186
- (list* 1)
1187
- (list* 1 2)
1188
- (list* 1 2 3)
1189
- (list* 1 2 3 4))
1190
- EOS
1191
- ).should == "(1 (1 . 2) (1 2 . 3) (1 2 3 . 4))"
1192
-
1193
- @nendo.evalStr( <<EOS
1194
- (list
1195
- (list* '())
1196
- (list* 1 '())
1197
- (list* 1 2 '())
1198
- (list* 1 2 3 '()))
1199
- EOS
1200
- ).should == "(() (1) (1 2) (1 2 3))"
1201
-
1202
- @nendo.evalStr( <<EOS
1203
- (list
1204
- (list* (cdr '(100)))
1205
- (list* 1 (cdr '(100)))
1206
- (list* 1 2 (cdr '(100)))
1207
- (list* 1 2 3 (cdr '(100))))
1208
- EOS
1209
- ).should == "(() (1) (1 2) (1 2 3))"
1210
-
1211
- @nendo.evalStr( <<EOS
1212
- (list
1213
- (list* '())
1214
- (list* '() '())
1215
- (list* '() '() '())
1216
- (list* '() '() '() '()))
1217
- EOS
1218
- ).should == "(() (()) (() ()) (() () ()))"
1219
-
1220
- @nendo.evalStr( <<EOS
1221
- (define _lst '())
1222
- (for-each
1223
- (lambda (x)
1224
- (set! _lst (cons 1 _lst)))
1225
- '())
1226
- _lst
1227
- EOS
1228
- ).should == "()"
1229
- @nendo.evalStr( <<EOS
1230
- (define _lst '())
1231
- (for-each
1232
- (lambda (x)
1233
- (set! _lst (cons (* x 2) _lst)))
1234
- '(1 2 3))
1235
- _lst
1236
- EOS
1237
- ).should == "(6 4 2)"
1238
- @nendo.evalStr( <<EOS
1239
- (define _lst '())
1240
- (for-each
1241
- (lambda (x)
1242
- (set! _lst (cons (+ x 1) _lst)))
1243
- '(1 2 3))
1244
- _lst
1245
- EOS
1246
- ).should == "(4 3 2)"
1247
- @nendo.evalStr( <<EOS
1248
- (define _lst '())
1249
- (for-each
1250
- (lambda (a b)
1251
- (set! _lst (cons (cons a b) _lst)))
1252
- '(1 2 3)
1253
- '(10 20 30))
1254
- _lst
1255
- EOS
1256
- ).should ==
1257
- "((3 . 30) (2 . 20) (1 . 10))"
1258
- @nendo.evalStr( <<EOS
1259
- (define _cnt 0)
1260
- (for-each
1261
- (lambda (x)
1262
- (set! _cnt (+ _cnt 1)))
1263
- (range 10000))
1264
- _cnt
1265
- EOS
1266
- ).should == "10000"
1267
- @nendo.evalStr( " (filter (lambda (x) x) '()) " ).should == "()"
1268
- @nendo.evalStr( " (filter (lambda (x) (= x 100)) '(1 2 3)) " ).should == "()"
1269
- @nendo.evalStr( " (filter (lambda (x) (= x 2)) '(1 2 3)) " ).should == "(2)"
1270
- @nendo.evalStr( " (filter (lambda (x) (not (= x 2))) '(1 2 3)) " ).should == "(1 3)"
1271
- @nendo.evalStr( " (filter (lambda (x) (if (= x 2) (* x 100) false)) '(1 2 3)) " ).should == "(2)"
1272
- @nendo.evalStr( " (find (lambda (x) (= x 100)) '(1 2 3)) " ).should == "#f"
1273
- @nendo.evalStr( " (find (lambda (x) (= x 2)) '(1 2 3)) " ).should == "2"
1274
- @nendo.evalStr( " (find (lambda (x) (not (= x 2))) '(1 2 3)) " ).should == "1"
1275
- @nendo.evalStr( " (find (lambda (x) (if (= x 2) (* x 100) false)) '(1 2 3)) " ).should == "2"
1276
- @nendo.evalStr( " (eq? find any)" ).should == "#f"
1277
- @nendo.evalStr( " (find even? '(1 2 3)) " ).should == "2"
1278
- @nendo.evalStr( " (filter-map (lambda (x) x) '()) " ).should == "()"
1279
- @nendo.evalStr( " (filter-map (lambda (x) (= x 100)) '(1 2 3)) " ).should == "()"
1280
- @nendo.evalStr( " (filter-map (lambda (x) (= x 2)) '(1 2 3)) " ).should == "(#t)"
1281
- @nendo.evalStr( " (filter-map (lambda (x) (not (= x 2))) '(1 2 3)) " ).should == "(#t #t)"
1282
- @nendo.evalStr( " (fold (lambda (x y) x y) 0 '()) " ).should == "0"
1283
- @nendo.evalStr( " (fold (lambda (x y) (+ x y)) 0 '(1 2 3)) " ).should == "6"
1284
- @nendo.evalStr( " (fold (lambda (x y) (+ x y)) 1 '(2 3 4)) " ).should == "10"
1285
- @nendo.evalStr( " (fold (lambda (x y) (* x y)) 1 '(2 3 4 5)) " ).should == "120"
1286
- @nendo.evalStr( " (fold (lambda (x y) (* x y)) 0 '(2 3 4 5)) " ).should == "0"
1287
- @nendo.evalStr( <<EOS
1288
- (filter-map
1289
- (lambda (x)
1290
- (if (= x 2) (* x 10) false))
1291
- '(1 2 3))
1292
- EOS
1293
- ).should == "(20)"
1294
- @nendo.evalStr( <<EOS
1295
- (filter-map
1296
- (lambda (x)
1297
- (if (not (= x 2)) (* x 10) false))
1298
- '(1 2 3))
1299
- EOS
1300
- ).should == "(10 30)"
1301
- @nendo.evalStr( " (any even? '(1 2 3)) " ).should == "#t"
1302
- @nendo.evalStr( " (any even? '(1 3)) " ).should == "#f"
1303
- @nendo.evalStr( " (any odd? '(1 3)) " ).should == "#t"
1304
- @nendo.evalStr( " (any (lambda (x) x) '(1 2 3)) " ).should == "1"
1305
- @nendo.evalStr( <<EOS
1306
- (let1 result '()
1307
- (do
1308
- ((i 0 (+ i 1)))
1309
- ((< 10 i) #f)
1310
- (set! result (cons i result)))
1311
- (reverse result))
1312
- EOS
1313
- ).should == "(0 1 2 3 4 5 6 7 8 9 10)"
1314
- @nendo.evalStr( <<EOS
1315
- (let1 result '()
1316
- (do
1317
- ((i 0 (+ i 3)))
1318
- ((< (* 3 10) i) #f)
1319
- (set! result (cons i result)))
1320
- (reverse result))
1321
- EOS
1322
- ).should == "(0 3 6 9 12 15 18 21 24 27 30)"
1323
- end
1324
- end
1325
-
1326
- describe Nendo, "when use raise function " do
1327
- before do
1328
- @nendo = Nendo::Core.new()
1329
- @nendo.setDisplayErrors( false )
1330
- @nendo.loadInitFile
1331
- end
1332
- it "should" do
1333
- lambda { @nendo.evalStr( ' (%raise TypeError "typeError" "nendo_spec.rb:1 typeError" ) ' ) }.should raise_error( TypeError )
1334
- lambda { @nendo.evalStr( ' (%raise ArgumentError "argumentError" "nendo_spec.rb:1 argumentError") ' ) }.should raise_error( ArgumentError )
1335
- lambda { @nendo.evalStr( ' (raise TypeError "typeError" ) ' ) }.should raise_error( TypeError )
1336
- lambda { @nendo.evalStr( ' (raise ArgumentError "argumentError" ) ' ) }.should raise_error( ArgumentError )
1337
- lambda { @nendo.evalStr( ' (raise TypeError ) ' ) }.should raise_error( TypeError )
1338
- lambda { @nendo.evalStr( ' (raise ArgumentError ) ' ) }.should raise_error( ArgumentError )
1339
- end
1340
- end
1341
-
1342
- describe Nendo, "when use values " do
1343
- before do
1344
- @nendo = Nendo::Core.new()
1345
- @nendo.setDisplayErrors( false )
1346
- @nendo.loadInitFile
1347
- end
1348
- it "should" do
1349
- @nendo.evalStr( " (values? (make-values '())) " ).should == "#t"
1350
- lambda { @nendo.evalStr( " (make-values '(1))) " ) }.should raise_error(ArgumentError)
1351
- @nendo.evalStr( " (values? (make-values '(1 2))) " ).should == "#t"
1352
- @nendo.evalStr( " (values? (make-values '(1 2 3))) " ).should == "#t"
1353
- @nendo.evalStr( " (values? (values)) " ).should == "#t"
1354
- @nendo.evalStr( " (values? (values 1)) " ).should == "#f"
1355
- @nendo.evalStr( " (values 1) " ).should == "1"
1356
- @nendo.evalStr( " (values? (values 1 2)) " ).should == "#t"
1357
- @nendo.evalStr( " (values? (values 1 2 3)) " ).should == "#t"
1358
- @nendo.evalStr( " (values? (values '(a) \"b\" '(\"C\"))) " ).should == "#t"
1359
- @nendo.evalStr( " (values-values (values)) " ).should == "()"
1360
- lambda { @nendo.evalStr( " (values-values (values 1)) " ) }.should raise_error(TypeError)
1361
- @nendo.evalStr( " (values-values (values 1 2)) " ).should == "(1 2)"
1362
- @nendo.evalStr( " (values-values (values 1 2 3)) " ).should == "(1 2 3)"
1363
- @nendo.evalStr( " (values-values (values '(a) \"b\" '(\"C\"))) " ).should == '((a) "b" ("C"))'
1364
- @nendo.evalStr( <<EOS
1365
- (call-with-values
1366
- (lambda () (values 4 5))
1367
- (lambda (a b) b))
1368
- EOS
1369
- ).should == "5"
1370
- @nendo.evalStr( <<EOS
1371
- (call-with-values
1372
- (lambda () (values 1 2))
1373
- cons)
1374
- EOS
1375
- ).should == "(1 . 2)"
1376
- @nendo.evalStr( <<EOS
1377
- (call-with-values
1378
- (lambda () (values 10))
1379
- list)
1380
- EOS
1381
- ).should == "(10)"
1382
- @nendo.evalStr( <<EOS
1383
- (call-with-values
1384
- (lambda () (values))
1385
- list)
1386
- EOS
1387
- ).should == "()"
1388
- @nendo.evalStr( " (call-with-values * -) " ).should == "-1"
1389
- @nendo.evalStr( " (receive (a) (values 10) (list a)) " ).should == "(10)"
1390
- @nendo.evalStr( " (receive (a b) (values 10 20) (list a b)) " ).should == "(10 20)"
1391
- @nendo.evalStr( " (receive (a b c) (values 10 20 30) (list a b c)) " ).should == "(10 20 30)"
1392
- @nendo.evalStr( " (receive (a . b) (values 10) (list a b)) " ).should == "(10 ())"
1393
- @nendo.evalStr( " (receive (a . b) (values 10 20) (list a b)) " ).should == "(10 (20))"
1394
- @nendo.evalStr( " (receive (a . b) (values 10 20 30) (list a b)) " ).should == "(10 (20 30))"
1395
- @nendo.evalStr( " (receive all (values) all) " ).should == "()"
1396
- @nendo.evalStr( " (receive all (values 10) all) " ).should == "(10)"
1397
- @nendo.evalStr( " (receive all (values 10 20) all) " ).should == "(10 20)"
1398
- @nendo.evalStr( " (receive all (values 10 20 30) all) " ).should == "(10 20 30)"
1399
- @nendo.evalStr( " (receive (a b) (values '(1) '(2)) (list a b)) " ).should == "((1) (2))"
1400
- @nendo.evalStr( " (receive (a b) (values '() '(2)) (list a b)) " ).should == "(() (2))"
1401
- @nendo.evalStr( " (receive (a b) (values '(1) '()) (list a b)) " ).should == "((1) ())"
1402
- @nendo.evalStr( " (receive (a b) (values #t #t) (cons a b)) " ).should == "(#t . #t)"
1403
- @nendo.evalStr( " (receive (a b) (values #t #f) (cons a b)) " ).should == "(#t . #f)"
1404
- @nendo.evalStr( " (receive (a b) (values nil #t) (cons a b)) " ).should == "(nil . #t)"
1405
- @nendo.evalStr( " (receive (a b) (values nil #f) (cons a b)) " ).should == "(nil . #f)"
1406
- @nendo.evalStr( " (receive (a b) (values nil nil) (cons a b)) " ).should == "(nil . nil)"
1407
- lambda { @nendo.evalStr( " (receive (a) (values) (list a)) " ) }.should raise_error( ArgumentError, /wrong number of arguments/ )
1408
- lambda { @nendo.evalStr( <<EOS
1409
- (map
1410
- (lambda (a) a))
1411
- EOS
1412
- ) }.should raise_error( ArgumentError, /wrong number of arguments/ )
1413
- lambda { @nendo.evalStr( <<EOS
1414
- (map
1415
- (lambda () 1)
1416
- '(1 2 3))
1417
- EOS
1418
- ) }.should raise_error( ArgumentError, /wrong number of arguments/ )
1419
- lambda { @nendo.evalStr( <<EOS
1420
- (map
1421
- (lambda (a) a)
1422
- '(1 2 3)
1423
- '(10 20 30))
1424
- EOS
1425
- ) }.should raise_error( ArgumentError, /wrong number of arguments/ )
1426
- lambda { @nendo.evalStr( <<EOS
1427
- (map
1428
- (lambda (a b) (+ a b))
1429
- '(1 2 3))
1430
- EOS
1431
- ) }.should raise_error( ArgumentError, /wrong number of arguments/ )
1432
- end
1433
- end
1434
-
1435
- describe Nendo, "when toplevel variable was overwritten " do
1436
- before do
1437
- @nendo = Nendo::Core.new()
1438
- @nendo.setDisplayErrors( false )
1439
- @nendo.loadInitFile
1440
- end
1441
- it "should" do
1442
- @nendo.evalStr( " (define a 1) a" ).should == "1"
1443
- lambda { @nendo.evalStr( " (define (c-func) (+ a b)) (c-func)" ) }.should raise_error( NameError )
1444
- @nendo.evalStr( " (define b 2) b" ).should == "2"
1445
- @nendo.evalStr( " (c-func) " ).should == "3"
1446
- @nendo.evalStr( " (define b 20) " ).should == "20"
1447
- @nendo.evalStr( " (c-func) " ).should == "21"
1448
-
1449
- @nendo.evalStr( " (define (a-func) 10) (a-func)" ).should == "10"
1450
- lambda { @nendo.evalStr( " (define (c-func) (* (a-func) (b-func))) (c-func)" ) }.should raise_error( NameError )
1451
- @nendo.evalStr( " (define (b-func) 20) (b-func)" ).should == "20"
1452
- @nendo.evalStr( " (c-func) " ).should == "200"
1453
- @nendo.evalStr( " (define (b-func) 200) (b-func)" ).should == "200"
1454
- @nendo.evalStr( " (c-func) " ).should == "2000"
1455
- end
1456
- end
1457
-
1458
- describe Nendo, "when use quasiquote macro " do
1459
- before do
1460
- @nendo = Nendo::Core.new()
1461
- @nendo.loadInitFile
1462
- end
1463
- it "should" do
1464
- @nendo.evalStr( " '(a b c) " ).should == "(a b c)"
1465
- @nendo.evalStr( " `(a b c) " ).should == "(a b c)"
1466
- @nendo.evalStr( " `(1 2 3) " ).should == "(1 2 3)"
1467
- @nendo.evalStr( " (set! a 3) `(1 2 ,a) " ).should == "(1 2 3)"
1468
- @nendo.evalStr( " (set! a 3) `(1 2 ,@(list a)) " ).should == "(1 2 3)"
1469
- @nendo.evalStr( " (set! a 3) `(1 ,@(list 2 a)) " ).should == "(1 2 3)"
1470
- @nendo.evalStr( " (set! a 11) `,a " ).should == "11"
1471
- @nendo.evalStr( " (set! a 12) ``,a " ).should == "(quasiquote (unquote a))"
1472
- @nendo.evalStr( ' (define str "string") str ' ).should == '"string"'
1473
- @nendo.evalStr( ' `(,str) ' ).should == '("string")'
1474
- @nendo.evalStr( ' `("STRING") ' ).should == '("STRING")'
1475
- @nendo.evalStr( ' `(,str "STRING") ' ).should == '("string" "STRING")'
1476
- @nendo.evalStr( ' `("STRING" ,str) ' ).should == '("STRING" "string")'
1477
- @nendo.evalStr( ' (car `("STRING" ,str)) ' ).should == '"STRING"'
1478
- @nendo.evalStr( ' (second `("STRING" ,str)) ' ).should == '"string"'
1479
- @nendo.evalStr( ' (caar `(("STRING" ,str))) ' ).should == '"STRING"'
1480
- @nendo.evalStr( ' (string-join `("A" "B" "C" "D")) ' ).should == '"ABCD"'
1481
- @nendo.evalStr( " `(list ,(+ 1 2) 4) " ).should == "(list 3 4)"
1482
- @nendo.evalStr( " (let ((name 'a)) `(list ,name ',name)) " ).should == "(list a (quote a))"
1483
- @nendo.evalStr( " `(a `(b ,(+ 1 2) ,(foo ,(+ 1 3) d) e) f) " ).should == "(a (quasiquote (b (unquote (+ 1 2)) (unquote (foo 4 d)) e)) f)"
1484
- @nendo.evalStr( " `(( foo ,(- 10 3)) ,@(cdr '(c)) . ,(car '(cons))) " ).should == "((foo 7) . cons)"
1485
- end
1486
- end
1487
-
1488
- describe Nendo, "when use macros made by quasiquote. " do
1489
- before do
1490
- @nendo = Nendo::Core.new()
1491
- @nendo.loadInitFile
1492
- end
1493
- it "should" do
1494
- @nendo.evalStr( " (case (length '(1 )) ((1) \"one\") ((2) \"two\") (else \"else\")) " ).should == '"one"'
1495
- @nendo.evalStr( " (case (length '(1 2 )) ((1) \"one\") ((2) \"two\") (else \"else\")) " ).should == '"two"'
1496
- @nendo.evalStr( " (case (length '(1 2 3 )) ((1) \"one\") ((2) \"two\") (else \"else\")) " ).should == '"else"'
1497
- @nendo.evalStr( " (case (length '(1 2 3 4)) ((1) \"one\") ((2) \"two\") (else \"else\")) " ).should == '"else"'
1498
- @nendo.evalStr( " (case 100 ((1) \"one\") ((2) \"two\") (else \"else\")) " ).should == '"else"'
1499
- @nendo.evalStr( " (case (car '(a b 1)) ((a) 'a) ((b) 'b) (else 'else)) " ).should == 'a'
1500
- @nendo.evalStr( " (case (cadr '(a b 1)) ((a) 'a) ((b) 'b) (else 'else)) " ).should == 'b'
1501
- @nendo.evalStr( " (case (caddr '(a b 1)) ((a b) 'a) ((1 2) 'number) (else 'else)) " ).should == 'number'
1502
- @nendo.evalStr( " (case (cadddr '(a b 1 2)) ((a b) 'a) ((1 2) 'number) (else 'else)) " ).should == 'number'
1503
- @nendo.evalStr( " (let* ((a 1) (b (+ a 2))) (cons a b)) " ).should == "(1 . 3)"
1504
- @nendo.evalStr( " (let* ((a 10) (b (* a 2))) (cons a b)) " ).should == "(10 . 20)"
1505
- @nendo.evalStr( " (let* ((a 10) (b (* a 2)) (c (* b 3))) (list a b c)) " ).should == "(10 20 60)"
1506
- @nendo.evalStr( " (begin0 1 2 3) " ).should == "1"
1507
- @nendo.evalStr( <<EOS
1508
- (define a 2)
1509
- (begin0 (set! a (* a 2))
1510
- (set! a (* a 2))
1511
- (set! a (* a 2)))
1512
- EOS
1513
- ).should == "4"
1514
- @nendo.evalStr( " (begin0 100) " ).should == "100"
1515
- @nendo.evalStr( " (begin0) " ).should == "#f"
1516
- @nendo.evalStr( <<EOS
1517
- (receive (a b)
1518
- (begin0
1519
- (values 1 2)
1520
- (values 10 20)
1521
- (values 100 200))
1522
- (cons a b))
1523
- EOS
1524
- ).should == "(1 . 2)"
1525
- @nendo.evalStr( " (macroexpand '(when #t (print \"1\") (print \"2\"))) " ).should == '(if #t (begin (print "1") (print "2")))'
1526
- @nendo.evalStr( " (macroexpand '(unless #t (print \"1\") (print \"2\"))) " ).should == '(if (not #t) (begin (print "1") (print "2")))'
1527
- @nendo.evalStr( " (macroexpand '(if-let1 a #t (print \"T\") (print \"F\"))) " ).should == '(%let ((a #t)) (if a (print "T") (print "F")))'
1528
- @nendo.evalStr( " (let1 count 0 (when #t (set! count (+ count 1)) (set! count (+ count 1))) count) " ).should == "2"
1529
- @nendo.evalStr( " (let1 count 0 (when #f (set! count (+ count 1)) (set! count (+ count 1))) count) " ).should == "0"
1530
- @nendo.evalStr( " (let1 count 0 (unless #t (set! count (+ count 1)) (set! count (+ count 1))) count) " ).should == "0"
1531
- @nendo.evalStr( " (let1 count 0 (unless #f (set! count (+ count 1)) (set! count (+ count 1))) count) " ).should == "2"
1532
- @nendo.evalStr( " (if-let1 m (rxmatch #/([0-9]+)/ \"abc100abc\") (rxmatch-substring m 1)) " ).should == '"100"'
1533
- @nendo.evalStr( " (macroexpand '#?.) " ).should == '"(string):1"'
1534
- @nendo.evalStr( " (macroexpand '(begin 1 2 #?. 4 5)) " ).should == '(begin 1 2 "(string):1" 4 5)'
1535
- end
1536
- end
1537
-
1538
- describe Nendo, "when use define and lambda macro " do
1539
- before do
1540
- @nendo = Nendo::Core.new()
1541
- @nendo.loadInitFile
1542
- end
1543
- it "should" do
1544
- @nendo.evalStr( <<EOS
1545
- (macroexpand
1546
- '(define (main argv)
1547
- (newline)
1548
- 0))
1549
- EOS
1550
- ).should == "(define main (lambda (argv) (newline) 0))"
1551
- @nendo.evalStr( <<EOS
1552
- (macroexpand
1553
- '(define (main argv)
1554
- (define (foo x) x)
1555
- (+ 10 20)
1556
- 0
1557
- (foo 111)))
1558
- EOS
1559
- ).should == "(define main (lambda (argv) (letrec ((foo (lambda (x) x))) (+ 10 20) 0 (foo 111))))"
1560
- @nendo.evalStr( <<EOS
1561
- (macroexpand
1562
- '(define (main argv)
1563
- (define result '())
1564
- (define (foo x) x)
1565
- (define val 0)
1566
- (define (bar x)
1567
- (+ val 10))))
1568
- EOS
1569
- ).should == "(define main (lambda (argv) (letrec ((result (quote ())) (foo (lambda (x) x)) (val 0) (bar (lambda (x) (+ val 10)))))))"
1570
- @nendo.evalStr( <<EOS
1571
- (define (main . argv)
1572
- (define (foo x) x)
1573
- (+ 10 20)
1574
- 0
1575
- (foo 111))
1576
- (main)
1577
- EOS
1578
- ).should == "111"
1579
- @nendo.evalStr( <<EOS
1580
- (define (main argv)
1581
- (define (foo1 x) (+ 1 x))
1582
- (define (foo2 x) (+ 2 x))
1583
- (*
1584
- (foo1 20)
1585
- (foo2 30)))
1586
- (main '())
1587
- EOS
1588
- ).should == "672"
1589
- @nendo.evalStr( " (macroexpand '(define (main argv) 0)) " ).should == "(define main (lambda (argv) 0))"
1590
- end
1591
- end
1592
-
1593
- describe Nendo, "when use macro macro " do
1594
- before do
1595
- @nendo = Nendo::Core.new()
1596
- @nendo.loadInitFile
1597
- end
1598
- it "should" do
1599
- @nendo.evalStr( <<EOS
1600
- (define inc!-macro
1601
- (macro (x)
1602
- (+ x 1)))
1603
- EOS
1604
- ).should match( /Nendo::LispMacro/ )
1605
- @nendo.evalStr( " (inc!-macro 10) " ).should == "11"
1606
- @nendo.evalStr( <<EOS
1607
- (define dec!-macro
1608
- (macro (x)
1609
- (define (dec! v)
1610
- (- v 1))
1611
- (dec! x)))
1612
- EOS
1613
- ).should match( /Nendo::LispMacro/ )
1614
- @nendo.evalStr( " (dec!-macro 10) " ).should == "9"
1615
- @nendo.evalStr( " (. (dec!-macro 10) class) " ).should == 'Fixnum'
1616
- end
1617
- end
1618
-
1619
- describe Nendo, "when use macros expand some syntax. " do
1620
- before do
1621
- @nendo = Nendo::Core.new()
1622
- @nendo.loadInitFile
1623
- end
1624
- it "should" do
1625
- @nendo.evalStr( "" +
1626
- " (let1 total 0" +
1627
- " (let loop ((cnt 10))" +
1628
- " (if (< 0 cnt)" +
1629
- " (begin" +
1630
- " (set! total (+ total cnt))" +
1631
- " (loop (- cnt 1)))))" +
1632
- " total)" +
1633
- "" ).should == "55"
1634
- @nendo.evalStr( "" +
1635
- "(let label ((a 2)" +
1636
- " (b 3))" +
1637
- " (if (<= 9 (+ a b))" +
1638
- " (* a b)" +
1639
- " (label 4 5)))" +
1640
- "" ).should == "20"
1641
- @nendo.evalStr( "" +
1642
- "(macroexpand '(let loop ((x 1)) "+
1643
- " 1"+
1644
- " 2"+
1645
- " (loop 100)))" ).should == "(letrec ((loop (lambda (x) 1 2 (loop 100)))) (loop 1))"
1646
- end
1647
- end
1648
-
1649
- describe Nendo, "when occur illegal syntax. " do
1650
- before do
1651
- @nendo = Nendo::Core.new()
1652
- @nendo.loadInitFile
1653
- end
1654
- it "should" do
1655
-
1656
- lambda { @nendo.evalStr( <<EOS
1657
- (let abc 1 ;; let1 style form is illegal syntax for let form.
1658
- (print abc))
1659
- EOS
1660
- ) }.should raise_error( SyntaxError, /^named let requires/ )
1661
-
1662
- lambda { @nendo.evalStr( "(let 1)" ) }.should raise_error( SyntaxError, /^let requires/ )
1663
- lambda { @nendo.evalStr( "(let ())" ) }.should raise_error( SyntaxError, /^let requires/ )
1664
- @nendo.evalStr( "(let () 1)" ).should == '1'
1665
- lambda { @nendo.evalStr( "(let loop 1)" ) }.should raise_error( SyntaxError, /^named let requires/ )
1666
- lambda { @nendo.evalStr( "(let loop ())" ) }.should raise_error( SyntaxError, /^named let requires/ )
1667
- @nendo.evalStr( "(let loop () 1)" ).should == '1'
1668
-
1669
- lambda { @nendo.evalStr( "(let1)" ) }.should raise_error( SyntaxError, /^let1 requires/ )
1670
- lambda { @nendo.evalStr( "(let1 a)" ) }.should raise_error( SyntaxError, /^let1 requires/ )
1671
- lambda { @nendo.evalStr( "(let1 a 1)" ) }.should raise_error( SyntaxError, /^let1 requires/ )
1672
- lambda { @nendo.evalStr( "(let1 (a 1) (print a))" ) }.should raise_error( SyntaxError, /^let1 requires/ )
1673
- lambda { @nendo.evalStr( "(let1 ((a 1)) (print a))" ) }.should raise_error( SyntaxError, /^let1 requires/ )
1674
- @nendo.evalStr( "(let1 a 123 a)" ).should == '123'
1675
- @nendo.evalStr( "(let1 b (+ 100 20 3) b)" ).should == '123'
1676
- end
1677
- end
1678
-
1679
-
1680
- describe Nendo, "when use dot-operator (.) macro " do
1681
- before do
1682
- @nendo = Nendo::Core.new()
1683
- @nendo.setDisplayErrors( false )
1684
- @nendo.loadInitFile
1685
- end
1686
- it "should" do
1687
- @nendo.evalStr( " (macroexpand '(. a b)) " ).should == "(a.b)"
1688
- @nendo.evalStr( " (macroexpand '(. a b c)) " ).should == "(a.b c)"
1689
- @nendo.evalStr( " (macroexpand '(. Kernel open)) " ).should == "(Kernel.open)"
1690
- lambda { @nendo.evalStr( " (macroexpand '(. open)) " ) }.should raise_error( ArgumentError )
1691
- lambda { @nendo.evalStr( " (macroexpand '(. open \"aaa\")) " ) }.should raise_error( TypeError )
1692
- @nendo.evalStr( " (macroexpand '(. a size)) " ).should == "(a.size)"
1693
- @nendo.evalStr( " (macroexpand '(. (. a size) to_s)) " ).gsub( /_[0-9][0-9][0-9][0-9][0-9]/, "_X0000" ).should == "(%let ((__gensym__fb4e25e49e9fb4e46342224606faf2e3eabf1251_X0000 (a.size))) (__gensym__fb4e25e49e9fb4e46342224606faf2e3eabf1251_X0000.to_s))"
1694
- @nendo.evalStr( " (macroexpand '(. (. (. a size) to_s) to_i)) " ).gsub( /_[0-9][0-9][0-9][0-9][0-9]/, "_X0000" ).should == "(%let ((__gensym__fb4e25e49e9fb4e46342224606faf2e3eabf1251_X0000 (%let ((__gensym__fb4e25e49e9fb4e46342224606faf2e3eabf1251_X0000 (a.size))) (__gensym__fb4e25e49e9fb4e46342224606faf2e3eabf1251_X0000.to_s)))) (__gensym__fb4e25e49e9fb4e46342224606faf2e3eabf1251_X0000.to_i))"
1695
- lambda { @nendo.evalStr( " (macroexpand '(. (. a size))) " ) }.should raise_error( ArgumentError )
1696
- @nendo.evalStr( " (set! str \"str\") str.size " ).should == "3"
1697
- @nendo.evalStr( " (set! str \"str\") (. str size) " ).should == "3"
1698
- @nendo.evalStr( " (set! str \"str\") (+ 1 (. str size)) " ).should == "4"
1699
- @nendo.evalStr( " (set! str \"string\") (. (. str size) to_s) " ).should == '"6"'
1700
- @nendo.evalStr( " (to-s str.size) " ).should == '"6"'
1701
- @nendo.evalStr( " (to-s 'str.size) " ).should == '"str.size"'
1702
- @nendo.evalStr( " (require \"date\") " ).should == "#f"
1703
- @nendo.evalStr( " (. (Date.new 0) strftime \"%F %r\") " ).should == '"0000-01-01 12:00:00 AM"'
1704
- @nendo.evalStr( " (require \"digest/md5\") " ).should == "#f"
1705
- @nendo.evalStr( " (Digest::MD5.hexdigest \"abc\") " ).should == '"900150983cd24fb0d6963f7d28e17f72"'
1706
- @nendo.evalStr( " (Digest::MD5.hexdigest \"source text\") " ).should == '"20f79a1416626eeacc0bd9a8db87faa2"'
1707
- @nendo.evalStr( " (define a 1) (a.is_a? Fixnum) " ).should == "#t"
1708
- @nendo.evalStr( " (define a 1) (a.is_a? Float) " ).should == "#f"
1709
- @nendo.evalStr( " (define a 1) (a.is_a? String) " ).should == "#f"
1710
- @nendo.evalStr( " (define a 1.1) (a.is_a? Fixnum) " ).should == "#f"
1711
- @nendo.evalStr( " (define a 1.1) (a.is_a? Float) " ).should == "#t"
1712
- @nendo.evalStr( " (define a 1.1) (a.is_a? String) " ).should == "#f"
1713
- @nendo.evalStr( " (define a \"s\") (a.is_a? Fixnum) " ).should == "#f"
1714
- @nendo.evalStr( " (define a \"s\") (a.is_a? Float) " ).should == "#f"
1715
- @nendo.evalStr( " (define a \"s\") (a.is_a? String) " ).should == "#t"
1716
- @nendo.evalStr( ' (equal? (read-from-string "\"100\"") (. 100 to_s)) ' ).should == "#t"
1717
- end
1718
- end
1719
-
1720
- describe Nendo, "when use dot-operator (.) macro and (&block ...) " do
1721
- before do
1722
- @nendo = Nendo::Core.new()
1723
- @nendo.loadInitFile
1724
- end
1725
- it "should" do
1726
- @nendo.evalStr( " (define arr '#(10 50 40 10000 20 30))" ).should == "#(10 50 40 10000 20 30)"
1727
- @nendo.evalStr( " (arr.sort)" ).should == "#(10 20 30 40 50 10000)"
1728
- @nendo.evalStr( " (arr.sort (&block (a b) (if (le? a b) 1 -1))) " ).should == "#(10000 50 40 30 20 10)"
1729
- @nendo.evalStr( " (arr.sort_by (&block (item) item.to_s)) " ).should == "#(10 10000 20 30 40 50)"
1730
- end
1731
- end
1732
-
1733
- describe Nendo, "when use sort libraries " do
1734
- before do
1735
- @nendo = Nendo::Core.new()
1736
- @nendo.loadInitFile
1737
- end
1738
- it "should" do
1739
- @nendo.evalStr( " (define lst '(1 50 60 30000 4000 200)) " ).should == "(1 50 60 30000 4000 200)"
1740
- @nendo.evalStr( " (sort lst) " ).should == "(1 50 60 200 4000 30000)"
1741
- @nendo.evalStr( " (sort '()) " ).should == "()"
1742
- @nendo.evalStr( " (sort lst (lambda (a b) (- b a))) " ).should == "(30000 4000 200 60 50 1)"
1743
- @nendo.evalStr( " (sort-by lst (lambda (item) item)) " ).should == "(1 50 60 200 4000 30000)"
1744
- @nendo.evalStr( " (sort-by '() (lambda (item) item)) " ).should == "()"
1745
- @nendo.evalStr( " (define lst2 '((1 . \"ddd\") (2 . \"cc\") (3 . \"bbbb\") (4 . \"a\"))) " ).should == '((1 . "ddd") (2 . "cc") (3 . "bbbb") (4 . "a"))'
1746
- @nendo.evalStr( " (sort lst2 (lambda (a b) (- (car a) (car b)))) " ).should == '((1 . "ddd") (2 . "cc") (3 . "bbbb") (4 . "a"))'
1747
- @nendo.evalStr( " (sort lst2 (lambda (a b) (if (>= (cdr a) (cdr b)) 1 -1))) " ).should == '((4 . "a") (3 . "bbbb") (2 . "cc") (1 . "ddd"))'
1748
- @nendo.evalStr( " (sort-by lst2 (lambda (item) (car item))) " ).should == '((1 . "ddd") (2 . "cc") (3 . "bbbb") (4 . "a"))'
1749
- @nendo.evalStr( " (sort-by lst2 (lambda (item) (cdr item))) " ).should == '((4 . "a") (3 . "bbbb") (2 . "cc") (1 . "ddd"))'
1750
- @nendo.evalStr( " (sort-by lst2 (lambda (item) (. (cdr item) size))) " ).should == '((4 . "a") (2 . "cc") (1 . "ddd") (3 . "bbbb"))'
1751
- end
1752
- end
1753
-
1754
- describe Nendo, "when use with-open libraries " do
1755
- before do
1756
- @nendo = Nendo::Core.new()
1757
- @nendo.setDisplayErrors( false )
1758
- @nendo.loadInitFile
1759
- @fn = "/tmp/for-with-open.dat"
1760
- open( @fn, "w" ) { |f|
1761
- f.puts( "line1" )
1762
- f.puts( "line2" )
1763
- f.puts( "line3" )
1764
- }
1765
- end
1766
- it "should" do
1767
- @nendo.evalStr( sprintf( " (with-open \"%s\" (lambda (f) (f.readline.chop))) ", @fn )).should == '"line1"'
1768
- @nendo.evalStr( sprintf( " (with-open \"%s\" (lambda (f) (f.readline.chop))) ", @fn )).should == '"line1"'
1769
- @nendo.evalStr( sprintf( " (with-open \"%s\" (lambda (f) (f.readline.chop) (f.readline.chop))) ", @fn )).should == '"line2"'
1770
- @nendo.evalStr( sprintf( " (with-open \"%s\" (lambda (f) (map (lambda (line) (line.chop)) f))) ", @fn )).should == '#("line1" "line2" "line3")'
1771
- @nendo.evalStr( sprintf( " (with-open \"%s\" (lambda (f) (f.puts \"Wrote from Nendo.\")) \"w\") #t", @fn )).should == "#t"
1772
- @nendo.evalStr( sprintf( " (with-open \"%s\" (lambda (f) (f.readline.chop))) ", @fn )).should == '"Wrote from Nendo."'
1773
- lambda { @nendo.evalStr( sprintf( " (with-open \"%s\" (lambda (f) #t) 1 2 ", @fn )) }.should raise_error(RuntimeError)
1774
- lambda { @nendo.evalStr( sprintf( " (with-open \"%s\" \"string\" 1 2 ", @fn )) }.should raise_error(RuntimeError)
1775
- end
1776
-
1777
- after do
1778
- File.unlink( @fn )
1779
- end
1780
- end
1781
-
1782
- describe Nendo, "when use (use ...) macro " do
1783
- before do
1784
- @nendo = Nendo::Core.new()
1785
- @nendo.setDisplayErrors( false )
1786
- @nendo.loadInitFile
1787
- end
1788
- it "should" do
1789
- @nendo.evalStr( " (macroexpand '(use abc)) " ).should == '(load "abc")'
1790
- @nendo.evalStr( " (macroexpand '(use a.b)) " ).should == '(load "a/b")'
1791
- @nendo.evalStr( " (macroexpand '(use a.b.c)) " ).should == '(load "a/b/c")'
1792
- @nendo.evalStr( " (macroexpand '(use a.b.c.d.e.f.g)) " ).should == '(load "a/b/c/d/e/f/g")'
1793
- @nendo.evalStr( " (macroexpand '(use srfi-1)) " ).should == '(load "srfi-1")'
1794
- @nendo.evalStr( " (macroexpand '(use text.tree)) " ).should == '(load "text/tree")'
1795
- @nendo.evalStr( " (macroexpand '(use debug.syslog)) " ).should == '(load "debug/syslog")'
1796
- @nendo.evalStr( " (macroexpand `(use ,(string->symbol (+ \"text\" \".\" \"tree\")))) " ).should == '(load "text/tree")'
1797
- lambda { @nendo.evalStr( " (macroexpand '(use '(a)) " ) }.should raise_error( RuntimeError )
1798
- lambda { @nendo.evalStr( " (macroexpand '(use \"srfi-1\") " ) }.should raise_error( RuntimeError )
1799
- lambda { @nendo.evalStr( " (macroexpand '(use 1)) " ) }.should raise_error( RuntimeError )
1800
- lambda { @nendo.evalStr( " (macroexpand '(use (+ 10 20))) " ) }.should raise_error( RuntimeError )
1801
- end
1802
- end
1803
-
1804
- describe Nendo, "when use keyword feature " do
1805
- before do
1806
- @nendo = Nendo::Core.new()
1807
- @nendo.setDisplayErrors( false )
1808
- @nendo.loadInitFile
1809
- end
1810
- it "should" do
1811
- @nendo.evalStr( " (keyword? :a) " ).should == "#t"
1812
- @nendo.evalStr( ' (keyword? (intern ":a")) ' ).should == "#f"
1813
- @nendo.evalStr( ' (symbol? (intern ":a")) ' ).should == "#t"
1814
- @nendo.evalStr( " (keyword? ':a) " ).should == "#t"
1815
- @nendo.evalStr( " (symbol? ':a) " ).should == "#f"
1816
- @nendo.evalStr( " (eq? :a :a) " ).should == "#t"
1817
- @nendo.evalStr( " (eqv? :a :a) " ).should == "#t"
1818
- @nendo.evalStr( ' (eq? :a (intern ":a")) ' ).should == "#f"
1819
- @nendo.evalStr( ' (eqv? :a (intern ":a")) ' ).should == "#f"
1820
- @nendo.evalStr( ' (eq? :a (intern "a")) ' ).should == "#f"
1821
- @nendo.evalStr( ' (eqv? :a (intern "a")) ' ).should == "#f"
1822
- @nendo.evalStr( " (= :a :a) " ).should == "#t"
1823
- @nendo.evalStr( ' (= :a (intern ":a")) ' ).should == "#f"
1824
- @nendo.evalStr( ' (= :a (intern "a")) ' ).should == "#f"
1825
- @nendo.evalStr( ' (keyword? (make-keyword "a")) ' ).should == "#t"
1826
- @nendo.evalStr( " :a " ).should == ":a"
1827
- @nendo.evalStr( " ::a " ).should == "::a"
1828
- @nendo.evalStr( " :::key " ).should == ":::key"
1829
- @nendo.evalStr( ' (make-keyword "a") ' ).should == ":a"
1830
- @nendo.evalStr( ' (make-keyword ":a") ' ).should == "::a"
1831
- @nendo.evalStr( " (make-keyword 'a) " ).should == ":a"
1832
- @nendo.evalStr( " (keyword->string :a) " ).should == '"a"'
1833
- @nendo.evalStr( " (keyword->string :abcde) " ).should == '"abcde"'
1834
- lambda { @nendo.evalStr( " (keyword->string 'a) " ) }.should raise_error( TypeError )
1835
- @nendo.evalStr( " : " ).should == ':'
1836
- @nendo.evalStr( " (keyword->string :) " ).should == '""'
1837
- @nendo.evalStr( ' (make-keyword "") ' ).should == ":"
1838
- @nendo.evalStr( " (get-keyword :y '(:x 1 :y 2 :z 3)) " ).should == "2"
1839
- @nendo.evalStr( " (get-keyword 'z '(x 1 y 2 z 3)) " ).should == "3"
1840
- lambda { @nendo.evalStr( " (get-keyword 'z '(x 1 y 2 z)) " ) }.should raise_error( RuntimeError )
1841
- lambda { @nendo.evalStr( " (get-keyword :t '(:x 1 :y 2 :z 3)) " ) }.should raise_error( RuntimeError )
1842
- @nendo.evalStr( " (get-keyword :t '() #f) " ).should == "#f"
1843
- @nendo.evalStr( " (get-keyword :t '(:x) #f) " ).should == "#f"
1844
- lambda { @nendo.evalStr( " (get-keyword :t '()) " ) }.should raise_error( RuntimeError )
1845
- lambda { @nendo.evalStr( " (get-keyword :t '(:x)) " ) }.should raise_error( RuntimeError )
1846
- lambda { @nendo.evalStr( " (get-keyword :t 1) " ) }.should raise_error( RuntimeError )
1847
- @nendo.evalStr( " (get-keyword :t 1 #f) " ).should == "#f"
1848
- end
1849
- end
1850
-
1851
- describe Nendo, "when use hash-table feature " do
1852
- before do
1853
- @nendo = Nendo::Core.new()
1854
- @nendo.setDisplayErrors( false )
1855
- @nendo.loadInitFile
1856
- end
1857
- it "should" do
1858
- @nendo.evalStr( " (define h (make-hash-table)) " ).should == "{}"
1859
- @nendo.evalStr( " (hash-table? 1) " ).should == "#f"
1860
- @nendo.evalStr( " (hash-table? '(1)) " ).should == "#f"
1861
- @nendo.evalStr( " (hash-table? (Array.new)) " ).should == "#f"
1862
- @nendo.evalStr( " (hash-table? (Hash.new)) " ).should == "#t"
1863
- @nendo.evalStr( " h " ).should == "{}"
1864
- @nendo.evalStr( " (hash-table-put! h 'k1 'v1) h" ).should == "{:k1=>:v1}"
1865
- @nendo.evalStr( " (hash-table-put! h 'k2 200) h" ).should == "{:k1=>:v1, :k2=>200}"
1866
- @nendo.evalStr( " (hash-table-get h 'k1)" ).should == "v1"
1867
- @nendo.evalStr( " (hash-table-get h 'k2)" ).should == "200"
1868
- @nendo.evalStr( " (hash-table-exist? h 'k1)" ).should == "#t"
1869
- @nendo.evalStr( " (hash-table-exist? h 'k2)" ).should == "#t"
1870
- @nendo.evalStr( " (hash-table-exist? h 'k3)" ).should == "#f"
1871
- @nendo.evalStr( " (hash-table-exist? h \"k1\")" ).should == "#f"
1872
- @nendo.evalStr( " (hash-table-num-entries h)" ).should == "2"
1873
- @nendo.evalStr( " (hash-table-delete! h 'k1)" ).should == "v1"
1874
- lambda { @nendo.evalStr( " (hash-table-get h 'k1)" ) }.should raise_error( RuntimeError )
1875
- @nendo.evalStr( " (hash-table-get h 'k1 #f) " ).should == "#f"
1876
- @nendo.evalStr( " (hash-table-num-entries h)" ).should == "1"
1877
- @nendo.evalStr( " (hash-table-clear! h)" ).should == "{}"
1878
- @nendo.evalStr( " (hash-table-num-entries h)" ).should == "0"
1879
- @nendo.evalStr( <<EOS
1880
- (set! h (hash-table eq?
1881
- '("a" "AAA")
1882
- '("b" "BBB")
1883
- '("c" "CCC" 100)))
1884
- (hash-table-keys h)
1885
- EOS
1886
- ).should == "(\"a\" \"b\" \"c\")"
1887
- @nendo.evalStr( <<EOS
1888
- (set! h (hash-table eq?
1889
- '("a" "AAA")
1890
- '("b" "BBB")
1891
- '("c" "CCC" 100)))
1892
- (hash-table-values h)
1893
- EOS
1894
- ).should == "((\"AAA\") (\"BBB\") (\"CCC\" 100))"
1895
- @nendo.evalStr( <<EOS
1896
- (set! h (hash-table eq?
1897
- '("a" . "AAA")
1898
- '("b" . "BBB")
1899
- '("c" . "CCC")))
1900
- h
1901
- EOS
1902
- ).should == "{\"a\"=>\"AAA\", \"b\"=>\"BBB\", \"c\"=>\"CCC\"}"
1903
- @nendo.evalStr( " (hash-table-keys h)" ).should == '("a" "b" "c")'
1904
- @nendo.evalStr( " (hash-table-values h)" ).should == '("AAA" "BBB" "CCC")'
1905
- @nendo.evalStr( " (hash-table-map h (lambda (a b) (+ a b)))" ).should == '("aAAA" "bBBB" "cCCC")'
1906
- @nendo.evalStr( " (hash-table-for-each h (lambda (a b) (+ a b)))" ).should == '("aAAA" "bBBB" "cCCC")'
1907
- @nendo.evalStr( " (set! h (make-hash-table)) " ).should == "{}"
1908
- @nendo.evalStr( <<EOS
1909
- (set! h (hash-table eq?
1910
- '(true . 1)
1911
- '(false . 2)
1912
- '(nil . 3)))
1913
- h
1914
- EOS
1915
- ).should == "{true=>1, false=>2, nil=>3}"
1916
- @nendo.evalStr( " (hash-table-keys h) " ).should == "(#t #f nil)"
1917
- @nendo.evalStr( <<EOS
1918
- (set! h (hash-table eq?
1919
- '(1 . true)
1920
- '(2 . false)
1921
- '(3 . nil)))
1922
- h
1923
- EOS
1924
- ).should == "{1=>true, 2=>false, 3=>nil}"
1925
- @nendo.evalStr( " (hash-table-keys h) " ).should == "(1 2 3)"
1926
- end
1927
- end
1928
-
1929
- describe Nendo, "when use vector feature " do
1930
- before do
1931
- @nendo = Nendo::Core.new()
1932
- @nendo.setDisplayErrors( false )
1933
- @nendo.loadInitFile
1934
- end
1935
- it "should" do
1936
- @nendo.evalStr( " (define v (vector)) v" ).should == "#()"
1937
- @nendo.evalStr( " (vector? 1) " ).should == "#f"
1938
- @nendo.evalStr( " (vector? (vector)) " ).should == "#t"
1939
- @nendo.evalStr( " (vector? (vector 1)) " ).should == "#t"
1940
- @nendo.evalStr( " (vector? (vector 1 2)) " ).should == "#t"
1941
- @nendo.evalStr( " (vector? '#(1)) " ).should == "#t"
1942
- @nendo.evalStr( " (vector? '#(1 2)) " ).should == "#t"
1943
- @nendo.evalStr( " (vector? (Array.new)) " ).should == "#t"
1944
- @nendo.evalStr( " (vector? (Hash.new)) " ).should == "#f"
1945
- @nendo.evalStr( " (vector? 1.1) " ).should == "#f"
1946
- @nendo.evalStr( " (vector? \"str\") " ).should == "#f"
1947
- @nendo.evalStr( " (define v (make-vector 4))" ).should == "#(nil nil nil nil)"
1948
- @nendo.evalStr( " (vector-set! v 0 'v1) v" ).should == "#(v1 nil nil nil)"
1949
- @nendo.evalStr( " (vector-set! v 1 '100) v" ).should == "#(v1 100 nil nil)"
1950
- @nendo.evalStr( " (vector-set! v 2 '200) v" ).should == "#(v1 100 200 nil)"
1951
- @nendo.evalStr( " (vector-set! v 3 '(a b c)) v" ).should == "#(v1 100 200 (a b c))"
1952
- @nendo.evalStr( " (vector-length v)" ).should == "4"
1953
- @nendo.evalStr( " (vector-ref v 0) " ).should == "v1"
1954
- @nendo.evalStr( " (vector-ref v 1) " ).should == "100"
1955
- @nendo.evalStr( " (vector-ref v 2) " ).should == "200"
1956
- @nendo.evalStr( " (vector-ref v 3) " ).should == "(a b c)"
1957
- lambda { @nendo.evalStr( " (vector-ref v -1)" ) }.should raise_error( RuntimeError )
1958
- lambda { @nendo.evalStr( " (vector-ref v -2)" ) }.should raise_error( RuntimeError )
1959
- lambda { @nendo.evalStr( " (vector-ref v 5)" ) }.should raise_error( RuntimeError )
1960
- lambda { @nendo.evalStr( " (vector-ref v 6)" ) }.should raise_error( RuntimeError )
1961
- @nendo.evalStr( " (vector-ref v -1 1000)" ).should == "1000"
1962
- @nendo.evalStr( " (vector-ref v -2 2000)" ).should == "2000"
1963
- @nendo.evalStr( " (vector-ref v 5 3000)" ).should == "3000"
1964
- @nendo.evalStr( " (vector-ref v 6 4000)" ).should == "4000"
1965
- @nendo.evalStr( " (vector-ref v 7 #f)" ).should == "#f"
1966
- @nendo.evalStr( " (define v (make-vector 10)) v" ).should == "#(nil nil nil nil nil nil nil nil nil nil)"
1967
- @nendo.evalStr( " (vector->list v)" ).should == "(nil nil nil nil nil nil nil nil nil nil)"
1968
- @nendo.evalStr( " (define lst '(a b c (d)))" ).should == "(a b c (d))"
1969
- @nendo.evalStr( " (list->vector lst)" ).should == "#(a b c (d))"
1970
- @nendo.evalStr( " (list->vector (range 10 1))" ).should == "#(1 2 3 4 5 6 7 8 9 10)"
1971
- end
1972
- end
1973
-
1974
- describe Nendo, "tail call optimization " do
1975
- before do
1976
- @nendo = Nendo::Core.new( )
1977
- @nendo.setDisplayErrors( false )
1978
- @nendo.loadInitFile
1979
- @nendo.loadInitFile # to self optimizing. The init.nnd file will be loaded twice, so `filter' can be optimized on second loading phase.
1980
- end
1981
- it "should" do
1982
- @nendo.evalStr( " (%setup-%tailcall-mark '(print \"abc\")) " ).should == "(%tailcall (print \"abc\"))"
1983
- @nendo.evalStr( " (%setup-%tailcall-mark '(begin (print \"abc\") 1 2 3)) " ).should == "(begin (print \"abc\") 1 2 3)"
1984
- @nendo.evalStr( " (%setup-%tailcall-mark '(begin 1 2 (print \"abc\") 3)) " ).should == "(begin 1 2 (print \"abc\") 3)"
1985
- @nendo.evalStr( " (%setup-%tailcall-mark '(begin 1 2 3 (print \"abc\"))) " ).should == "(begin 1 2 3 (%tailcall (print \"abc\")))"
1986
- @nendo.evalStr( " (%setup-%tailcall-mark '(lambda (x) x)) " ).should == "(lambda (x) x)"
1987
- @nendo.evalStr( " (%setup-%tailcall-mark '(macro (x) x)) " ).should == "(macro (x) x)"
1988
- @nendo.evalStr( " (%setup-%tailcall-mark '(%syntax (x) x)) " ).should == "(%syntax (x) x)"
1989
- @nendo.evalStr( " (%setup-%tailcall-mark '(%syntax (a b c) (begin a b c))) " ).should == "(%syntax (a b c) (begin a b c))"
1990
- @nendo.evalStr( " (%setup-%tailcall-mark '(lambda (x) (%syntax (y) x))) " ).should == "(lambda (x) (%syntax (y) x))"
1991
- @nendo.evalStr( " (%setup-%tailcall-mark '(lambda (x) (syntax-quote x))) " ).should == "(lambda (x) (quote x))"
1992
- @nendo.evalStr( " (%setup-%tailcall-mark '(lambda (x) (quote x))) " ).should == "(lambda (x) (quote x))"
1993
- @nendo.evalStr( <<EOS
1994
- (%setup-%tailcall-mark
1995
- '(lambda '(x)
1996
- 1
1997
- 2
1998
- (print \"abc\")))
1999
- EOS
2000
- ).should == "(lambda (quote (x)) 1 2 (%tailcall (print \"abc\")))"
2001
- @nendo.evalStr( <<EOS
2002
- (%setup-%tailcall-mark
2003
- '(lambda (x)
2004
- 1
2005
- 2
2006
- (if #t
2007
- (begin 1 2 (print "abc"))
2008
- (begin 1 2 (print "ABC")))))
2009
- EOS
2010
- ).should == "(lambda (x) 1 2 (if #t (begin 1 2 (%tailcall (print \"abc\"))) (begin 1 2 (%tailcall (print \"ABC\")))))"
2011
- @nendo.evalStr( <<EOS
2012
- (%setup-%tailcall-mark (macroexpand
2013
- '(define (foo) (foo))
2014
- ))
2015
- EOS
2016
- ).should == "(define foo (lambda () (%tailcall (foo))))"
2017
- @nendo.evalStr( <<EOS
2018
- (%setup-%tailcall-mark (macroexpand
2019
- '(values? (make-values '()))
2020
- ))
2021
- EOS
2022
- ).should == "(%tailcall (values? (make-values (quote ()))))"
2023
- @nendo.evalStr( <<EOS
2024
- (%setup-%tailcall-mark (macroexpand
2025
- '(cond
2026
- (false 1)
2027
- (false 2))
2028
- ))
2029
- EOS
2030
- ).should == "(if #f (begin 1) (if #f (begin 2) ()))"
2031
- @nendo.evalStr( <<EOS
2032
- (%setup-%tailcall-mark (macroexpand
2033
- '(cond
2034
- (false 1)
2035
- (false 2)
2036
- (else 3))
2037
- ))
2038
- EOS
2039
- ).should == "(if #f (begin 1) (if #f (begin 2) (if #t (begin 3) ())))"
2040
- @nendo.evalStr( <<EOS
2041
- (%setup-%tailcall-mark (macroexpand
2042
- '(and
2043
- (foo 1)
2044
- (bar 2))
2045
- ))
2046
- EOS
2047
- ).should == "(if (not (eq? #f (foo 1))) (%tailcall (bar 2)) #f)"
2048
- @nendo.evalStr( <<EOS
2049
- (%setup-%tailcall-mark (macroexpand
2050
- '(or
2051
- (foo 1)
2052
- (bar 2))
2053
- ))
2054
- EOS
2055
- ).gsub( /_[0-9][0-9][0-9][0-9][0-9][ ]/, "_X0000 " ).should == "(%let ((__gensym__fb4e25e49e9fb4e46342224606faf2e3eabf1251_X0000 (foo 1))) (if __gensym__fb4e25e49e9fb4e46342224606faf2e3eabf1251_X0000 __gensym__fb4e25e49e9fb4e46342224606faf2e3eabf1251_X0000 (%let ((__gensym__fb4e25e49e9fb4e46342224606faf2e3eabf1251_X0000 (bar 2))) (if __gensym__fb4e25e49e9fb4e46342224606faf2e3eabf1251_X0000 __gensym__fb4e25e49e9fb4e46342224606faf2e3eabf1251_X0000 #f))))"
2056
- @nendo.evalStr( <<EOS
2057
- (%setup-%tailcall-mark (macroexpand
2058
- '(let loop ((x 1))
2059
- 1
2060
- 2
2061
- (loop 100))
2062
- ))
2063
- EOS
2064
- ).should == "(letrec ((loop (lambda (x) 1 2 (%tailcall (loop 100))))) (%tailcall (loop 1)))"
2065
- @nendo.evalStr( "(%setup-%tailcall-mark (macroexpand "+
2066
- " '(let1 aaa 111 aaa)"+
2067
- " ))" ).should == "(%let ((aaa 111)) aaa)"
2068
- @nendo.evalStr( <<EOS
2069
- (%setup-%tailcall-mark
2070
- '(letrec ((func1
2071
- (lambda (x)
2072
- 1
2073
- (func2)))
2074
- (func2
2075
- (lambda (x)
2076
- 2
2077
- (func1))))
2078
- (func1 100)))
2079
- EOS
2080
- ).should == "(letrec ((func1 (lambda (x) 1 (%tailcall (func2)))) (func2 (lambda (x) 2 (%tailcall (func1))))) (%tailcall (func1 100)))"
2081
- @nendo.evalStr( "(set-optimize-level 0) " ).should == "0"
2082
- lambda { @nendo.evalStr( <<EOS
2083
- (filter
2084
- (lambda (x) (< x 10))
2085
- (range 10000))
2086
- EOS
2087
- ) }.should raise_error(SystemStackError)
2088
- @nendo.evalStr( "(set-optimize-level 1) " ).should == "1"
2089
- @nendo.evalStr( <<EOS
2090
- (apply +
2091
- (map
2092
- (lambda (x) (* x 2))
2093
- (range 10000)))
2094
- EOS
2095
- ).should == "99990000"
2096
- @nendo.evalStr( <<EOS
2097
- (filter
2098
- (lambda (x) (< x 10))
2099
- (range 1000))
2100
- EOS
2101
- ).should == "(0 1 2 3 4 5 6 7 8 9)"
2102
- @nendo.evalStr( <<EOS
2103
- (filter-map
2104
- (lambda (x)
2105
- (when (< x 10) (- x)))
2106
- (range 1000))
2107
- EOS
2108
- ).should == "(0 -1 -2 -3 -4 -5 -6 -7 -8 -9)"
2109
- @nendo.evalStr( <<EOS
2110
- (define str
2111
- (if #t
2112
- (car '("a"))
2113
- (car '("b"))))
2114
- (sprintf "A%sZ" str)
2115
- EOS
2116
- ).should == '"AaZ"'
2117
- @nendo.evalStr( <<EOS
2118
- (letrec ((str (if #t
2119
- (+ "a" "A")
2120
- '())))
2121
- str.class)
2122
- EOS
2123
- ).should == 'String'
2124
- @nendo.evalStr( <<EOS
2125
- (letrec ((str
2126
- (if #t
2127
- (+ \"a\" \"A\")
2128
- '())))
2129
- (+ str \"...\"))
2130
- EOS
2131
- ).should == '"aA..."'
2132
- end
2133
- end
2134
-
2135
-