nendo 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,1566 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- encoding: utf-8 -*-
3
+ #
4
+ # nendo_spec.rb - "RSpec file for nendo language"
5
+ #
6
+ # Copyright (c) 2009-2010 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
+ describe Cell, "when initialized as '()" do
39
+ before do
40
+ @cell = Cell.new
41
+ end
42
+
43
+ it "should" do
44
+ @cell.isNull.should be_true
45
+ @cell.length.should == 0
46
+ @cell.size.should == 0
47
+ @cell.to_arr.should == []
48
+ @cell.class.should == Cell
49
+ @cell.map{ |x| x }.should == []
50
+ @cell.lastAtom.should be_false
51
+ @cell.getLastAtom.class.should == Nil
52
+ end
53
+ end
54
+
55
+ describe Cell, "when initialized as '(100)" do
56
+ before do
57
+ @cell = Cell.new( 100 )
58
+ end
59
+
60
+ it "should" do
61
+ @cell.isNull.should_not be_true
62
+ @cell.length.should == 1
63
+ @cell.size.should == 1
64
+ @cell.car.should == 100
65
+ @cell.cdr.class.should == Nil
66
+ @cell.to_arr.should == [ 100 ]
67
+ @cell.lastCell.car.should == 100
68
+ @cell.lastCell.cdr.class.should == Nil
69
+ @cell.lastAtom.should be_false
70
+ @cell.getLastAtom.class.should == Nil
71
+ end
72
+ end
73
+
74
+ describe Cell, "when initialized as '(100 . 200)" do
75
+ before do
76
+ @cell = Cell.new( 100, 200 )
77
+ end
78
+
79
+ it "should" do
80
+ @cell.isNull.should_not be_true
81
+ @cell.isDotted.should be_true
82
+ @cell.length.should == 1
83
+ @cell.size.should == 1
84
+ @cell.car.should == 100
85
+ @cell.cdr.should == 200
86
+ @cell.to_arr.should == [ 100 ]
87
+ @cell.lastCell.car.should == 100
88
+ @cell.lastCell.cdr.should == 200
89
+ @cell.lastAtom.should be_true
90
+ @cell.getLastAtom.should == 200
91
+ end
92
+ end
93
+
94
+ describe Cell, "when initialized as '(cons 100 (cons 200 300)) " do
95
+ before do
96
+ @cell = Cell.new( 100, Cell.new( 200, 300 ))
97
+ end
98
+
99
+ it "should" do
100
+ @cell.isNull.should_not be_true
101
+ @cell.isDotted.should_not be_true
102
+ @cell.length.should == 2
103
+ @cell.size.should == 2
104
+ @cell.car.should == 100
105
+ @cell.cdr.car.should == 200
106
+ @cell.cdr.cdr.should == 300
107
+ @cell.to_arr.should == [ 100, 200 ]
108
+ @cell.lastCell.car.should == 200
109
+ @cell.lastCell.cdr.should == 300
110
+ @cell.lastAtom.should be_true
111
+ @cell.getLastAtom.should == 300
112
+ end
113
+ end
114
+
115
+ describe Nendo, "Ruby's arity rules " do
116
+ it "should" do
117
+ Proc.new {}.arity.should <= 0
118
+ Proc.new {||}.arity.should == 0
119
+ Proc.new {|a|}.arity.should == 1
120
+ Proc.new {|a,b|}.arity.should == 2
121
+ Proc.new {|a,b,c|}.arity.should == 3
122
+ Proc.new {|*a|}.arity.should == -1
123
+ Proc.new {|a,*b|}.arity.should == -2
124
+ end
125
+ end
126
+
127
+ describe Nendo, "Ruby's lexical closure " do
128
+ it "should" do
129
+ lambda1 = lambda { |val|
130
+ val = 'a'
131
+ lambda2 = lambda { |val| val }
132
+ lambda2.call( 'X' )
133
+ val
134
+ }
135
+ lambda1.call( 'A' ).should == 'a'
136
+ end
137
+ end
138
+
139
+
140
+ describe Nendo, "Ruby's undefined instance variable " do
141
+ it "should" do
142
+ @undefined_variable.should be_nil
143
+ defined?( @undefined_variable ).should be_nil
144
+ end
145
+ end
146
+
147
+ describe Evaluator, "When use Evaluator's util methods" do
148
+ before do
149
+ @evaluator = Evaluator.new()
150
+ end
151
+ it "should" do
152
+ @evaluator.toRubySymbol( "a" ).should == "_a"
153
+ @evaluator.toRubySymbol( "a_b_c" ).should == "_a_b_c"
154
+ @evaluator.toRubySymbol( "_a_b_c_" ).should == "__a_b_c_"
155
+ @evaluator.toRubySymbol( '!' ).should == '__EXMARK'
156
+ @evaluator.toRubySymbol( '$' ).should == '__DOMARK'
157
+ @evaluator.toRubySymbol( '%' ).should == '__PAMARK'
158
+ @evaluator.toRubySymbol( '&' ).should == '__ANMARK'
159
+ @evaluator.toRubySymbol( '*' ).should == '__ASMARK'
160
+ @evaluator.toRubySymbol( '+' ).should == '__PLMARK'
161
+ @evaluator.toRubySymbol( '-' ).should == '__MIMARK'
162
+ @evaluator.toRubySymbol( '/' ).should == '__SLMARK'
163
+ @evaluator.toRubySymbol( ':' ).should == '__COMARK'
164
+ @evaluator.toRubySymbol( '::' ).should == '_::'
165
+ @evaluator.toRubySymbol( '<' ).should == '__LTMARK'
166
+ @evaluator.toRubySymbol( '=' ).should == '__EQMARK'
167
+ @evaluator.toRubySymbol( '>' ).should == '__GTMARK'
168
+ @evaluator.toRubySymbol( '?' ).should == '__QUMARK'
169
+ @evaluator.toRubySymbol( '@' ).should == '__ATMARK'
170
+ @evaluator.toRubySymbol( '^' ).should == '__NKMARK'
171
+ @evaluator.toRubySymbol( "_" ).should == "__"
172
+ @evaluator.toRubySymbol( '~' ).should == '__CHMARK'
173
+ @evaluator.toRubySymbol( "a?" ).should == "_a_QUMARK"
174
+ @evaluator.toRubySymbol( "a?" ).should == "_a_QUMARK"
175
+ @evaluator.toRubySymbol( "a!" ).should == "_a_EXMARK"
176
+ @evaluator.toRubySymbol( "a?b" ).should == "_a_QUMARKb"
177
+ @evaluator.toRubySymbol( "a!b" ).should == "_a_EXMARKb"
178
+ @evaluator.toRubySymbol( "a-b" ).should == "_a_MIMARKb"
179
+ @evaluator.toRubySymbol( "a-b-c" ).should == "_a_MIMARKb_MIMARKc"
180
+ @evaluator.toRubySymbol( "-a-b-c" ).should == "__MIMARKa_MIMARKb_MIMARKc"
181
+ @evaluator.toRubySymbol( "-a-!-b" ).should == "__MIMARKa_MIMARK_EXMARK_MIMARKb"
182
+ @evaluator.toRubySymbol( "-a-!-?b" ).should == "__MIMARKa_MIMARK_EXMARK_MIMARK_QUMARKb"
183
+ @evaluator.toRubySymbol( "a.b" ).should == "_a.b"
184
+ @evaluator.toRubySymbol( "aa.bb" ).should == "_aa.bb"
185
+ @evaluator.toRubySymbol( "aa.bb.cc" ).should == "_aa.bb.cc"
186
+ @evaluator.toLispSymbol( "_a_QUMARK" ).should == "a?"
187
+ @evaluator.toLispSymbol( "_a_EXMARK" ).should == "a!"
188
+ @evaluator.toLispSymbol( "_a_b" ).should == "a_b"
189
+ @evaluator.toLispSymbol( "_a_b_c" ).should == "a_b_c"
190
+ @evaluator.toLispSymbol( "_A_B_C" ).should == "A_B_C"
191
+ @evaluator.toRubyValue( "a" ).should == "a"
192
+ @evaluator.toRubyValue( "b" ).should == "b"
193
+ @evaluator.toRubyValue( "true" ).should == "true"
194
+ @evaluator.toRubyValue( "nil" ).should == "nil"
195
+ @evaluator.toRubyValue( "false" ).should == "false"
196
+ @evaluator.toRubyValue( :a ).should == "a"
197
+ @evaluator.toRubyValue( :b ).should == "b"
198
+ @evaluator.toRubyValue( true ).should == "true"
199
+ @evaluator.toRubyValue( nil ).should == "nil"
200
+ @evaluator.toRubyValue( false ).should == "false"
201
+ end
202
+ end
203
+
204
+ describe Nendo, "when call replStr() with literals" do
205
+ before do
206
+ @nendo = Nendo::Core.new()
207
+ end
208
+ it "should" do
209
+ @nendo.replStr( " 1 " ).should == "1"
210
+ @nendo.replStr( " 100000 " ).should == "100000"
211
+ @nendo.replStr( " 1.1 " ).should == "1.1"
212
+ @nendo.replStr( " 1.0 " ).should == "1.0"
213
+ @nendo.replStr( ' "a" ' ).should == '"a"'
214
+ @nendo.replStr( ' "日本語" ' ).should == '"日本語"'
215
+ @nendo.replStr( ' "\n" ' ).should == "\"\n\""
216
+ @nendo.replStr( ' "\r" ' ).should == "\"\\r\""
217
+ @nendo.replStr( ' "\t" ' ).should == "\"\\t\""
218
+ @nendo.replStr( ' "a" ' ).should == '"a"'
219
+ @nendo.replStr( ' "a\"b" ' ).should == '"a\"b"'
220
+ @nendo.replStr( ' "日\"本\"語" ' ).should == '"日\"本\"語"'
221
+ @nendo.replStr( " true " ).should == "#t"
222
+ @nendo.replStr( " false " ).should == "#f"
223
+ @nendo.replStr( " nil " ).should == "nil"
224
+ @nendo.replStr( " #t " ).should == "#t"
225
+ @nendo.replStr( " #f " ).should == "#f"
226
+
227
+ end
228
+ end
229
+
230
+ describe Nendo, "when call replStr() with comparative operators" do
231
+ before do
232
+ @nendo = Nendo::Core.new()
233
+ end
234
+ it "should" do
235
+ @nendo.replStr( " (= 1 1) " ).should == "#t"
236
+ @nendo.replStr( " (= 1 2) " ).should == "#f"
237
+ @nendo.replStr( " (= #t #t) " ).should == "#t"
238
+ @nendo.replStr( " (= #f #f) " ).should == "#t"
239
+ @nendo.replStr( " (= true true) " ).should == "#t"
240
+ @nendo.replStr( " (= false false) " ).should == "#t"
241
+ @nendo.replStr( " (= #t true) " ).should == "#t"
242
+ @nendo.replStr( " (= #f false) " ).should == "#t"
243
+ @nendo.replStr( " (= #t #f) " ).should == "#f"
244
+ @nendo.replStr( " (= true false) " ).should == "#f"
245
+ @nendo.replStr( " (eq? 1 1) " ).should == "#t"
246
+ @nendo.replStr( " (eq? 1 2) " ).should == "#f"
247
+ @nendo.replStr( " (eq? 'a 'a) " ).should == "#t"
248
+ @nendo.replStr( " (eq? 'b 'b) " ).should == "#t"
249
+ @nendo.replStr( " (eq? 'a-b 'a-b) " ).should == "#t"
250
+ @nendo.replStr( " (eq? 'a_b 'a-b) " ).should == "#f"
251
+ @nendo.replStr( " (eq? 'a-b 'a_b) " ).should == "#f"
252
+ @nendo.replStr( " (< 1 1) " ).should == "#f"
253
+ @nendo.replStr( " (< 1 2) " ).should == "#t"
254
+ @nendo.replStr( " (> 1 1) " ).should == "#f"
255
+ @nendo.replStr( " (> 2 1) " ).should == "#t"
256
+ @nendo.replStr( " (<= 1 0) " ).should == "#f"
257
+ @nendo.replStr( " (<= 1 1) " ).should == "#t"
258
+ @nendo.replStr( " (<= 1 2) " ).should == "#t"
259
+ @nendo.replStr( " (>= 0 1) " ).should == "#f"
260
+ @nendo.replStr( " (>= 1 1) " ).should == "#t"
261
+ @nendo.replStr( " (equal? 1 1) " ).should == "#t"
262
+ @nendo.replStr( " (equal? 1 2) " ).should == "#f"
263
+ @nendo.replStr( " (equal? 2 2) " ).should == "#t"
264
+ @nendo.replStr( " (equal? '() '()) " ).should == "#t"
265
+ @nendo.replStr( " (equal? '(1) '(1)) " ).should == "#t"
266
+ @nendo.replStr( " (equal? '(1) '(2)) " ).should == "#f"
267
+ @nendo.replStr( " (equal? '(1 2 3) '(1 2 3)) " ).should == "#t"
268
+ @nendo.replStr( " (equal? '(1 2 . 3) '(1 2 . 3)) " ).should == "#t"
269
+ @nendo.replStr( " (equal? '(1 2 (3)) '(1 2 (3))) " ).should == "#t"
270
+ @nendo.replStr( " (equal? '(1 2 (3)) '(1 2 (4))) " ).should == "#f"
271
+ @nendo.replStr( " (equal? '(1 2 (3 (4))) '(1 2 (3 (4)))) " ).should == "#t"
272
+ @nendo.replStr( " (equal? '((1) 2 3 4) '((2) 2 3 4)) " ).should == "#f"
273
+ @nendo.replStr( " (equal? \"aaa\" \"aaa\") " ).should == "#t"
274
+ @nendo.replStr( " (equal? \"aaa\" \"aax\") " ).should == "#f"
275
+ @nendo.replStr( " (equal? '(\"aaa\") '(\"aaa\")) " ).should == "#t"
276
+ @nendo.replStr( " (equal? '(\"aaa\" (1)) '(\"aaa\" (1))) " ).should == "#t"
277
+ @nendo.replStr( " (equal? '(\"aaa\" (1)) '(\"aaa\" (2))) " ).should == "#f"
278
+ end
279
+ end
280
+
281
+ describe Nendo, "when call replStr() with boolean operators" do
282
+ before do
283
+ @nendo = Nendo::Core.new()
284
+ end
285
+ it "should" do
286
+ @nendo.replStr( " true " ).should == "#t"
287
+ @nendo.replStr( " false " ).should == "#f"
288
+ @nendo.replStr( " #t " ).should == "#t"
289
+ @nendo.replStr( " #f " ).should == "#f"
290
+ @nendo.replStr( " (not true) " ).should == "#f"
291
+ @nendo.replStr( " (not #t) " ).should == "#f"
292
+ @nendo.replStr( " (not 1) " ).should == "#f"
293
+ @nendo.replStr( " (not false) " ).should == "#t"
294
+ @nendo.replStr( " (not #f) " ).should == "#t"
295
+ @nendo.replStr( " (not \"str\") " ).should == "#f"
296
+ @nendo.replStr( " (not not) " ).should == "#f"
297
+ @nendo.replStr( " (not (not true)) " ).should == "#t"
298
+ @nendo.replStr( " (not (not #t)) " ).should == "#t"
299
+ @nendo.replStr( " (not '()) " ).should == "#f"
300
+ @nendo.replStr( " (not '(1)) " ).should == "#f"
301
+ end
302
+ end
303
+
304
+ describe Nendo, "when call replStr() with `+' function" do
305
+ before do
306
+ @nendo = Nendo::Core.new()
307
+ end
308
+ it "should" do
309
+ @nendo.replStr( " (+ 1) " ).should == "1"
310
+ @nendo.replStr( " (+ 1 1) " ).should == "2"
311
+ @nendo.replStr( " (+ 1 1 1 1 1 1 1 1 1 1) " ).should == "10"
312
+ @nendo.replStr( " (+ 1 2 3 4 5) " ).should == "15"
313
+ @nendo.replStr( " (+ 1 (+ 2 (+ 3 (+ 4 (+ 5))))) " ).should == "15"
314
+ @nendo.replStr( " (+ 1 1.1) " ).should == "2.1"
315
+ @nendo.replStr( " (+ 1.1 1.2) " ).should == "2.3"
316
+ @nendo.replStr( " (+ \"a\" ) " ).should == '"a"'
317
+ @nendo.replStr( " (+ \"a\" \"B\" \"c\" ) " ).should == '"aBc"'
318
+ @nendo.replStr( " (+) " ).should == "0"
319
+ lambda { @nendo.replStr( " (+ '() ) " ) }.should raise_error(TypeError)
320
+ lambda { @nendo.replStr( " (+ 1 '() ) " ) }.should raise_error(TypeError)
321
+ lambda { @nendo.replStr( " (+ 1.1 '() ) " ) }.should raise_error(TypeError)
322
+ lambda { @nendo.replStr( " (+ '(1) ) " ) }.should raise_error(TypeError)
323
+ lambda { @nendo.replStr( " (+ 1.1 \"a\" ) " ) }.should raise_error(TypeError)
324
+ lambda { @nendo.replStr( " (+ \"a\" 1) " ) }.should raise_error(TypeError)
325
+ lambda { @nendo.replStr( " (+ \"a\" 1.1) " ) }.should raise_error(TypeError)
326
+ end
327
+ end
328
+
329
+ describe Nendo, "when call replStr() with `-' function" do
330
+ before do
331
+ @nendo = Nendo::Core.new()
332
+ end
333
+ it "should" do
334
+ @nendo.replStr( " (- 1) " ).should == "-1"
335
+ @nendo.replStr( " (- 2 1) " ).should == "1"
336
+ @nendo.replStr( " (- 2 5) " ).should == "-3"
337
+ @nendo.replStr( " (- 100 1 1 1 1 1 1 1 1 1 1) " ).should == "90"
338
+ @nendo.replStr( " (- 100 (- 10 3)) " ).should == "93"
339
+ @nendo.replStr( " (- 1.1 1) " ).should == (1.1-1).to_s
340
+ @nendo.replStr( " (- 1.3 1.1) " ).should == (1.3-1.1).to_s
341
+ lambda { @nendo.replStr( " (- 1 '() ) " ) }.should raise_error(TypeError)
342
+ lambda { @nendo.replStr( " (- 1.1 '() ) " ) }.should raise_error(TypeError)
343
+ lambda { @nendo.replStr( " (-) " ) }.should raise_error(ArgumentError)
344
+ lambda { @nendo.replStr( " (- '(1) ) " ) }.should raise_error(TypeError)
345
+ lambda { @nendo.replStr( " (- '() ) " ) }.should raise_error(TypeError)
346
+ lambda { @nendo.replStr( " (- 1.1 \"a\" ) " ) }.should raise_error(TypeError)
347
+ end
348
+ end
349
+
350
+ describe Nendo, "when call replStr() with `*' function" do
351
+ before do
352
+ @nendo = Nendo::Core.new()
353
+ end
354
+ it "should" do
355
+ @nendo.replStr( " (* 1) " ).should == "1"
356
+ @nendo.replStr( " (* 2 1) " ).should == "2"
357
+ @nendo.replStr( " (* 2 5) " ).should == "10"
358
+ @nendo.replStr( " (* 1 2 3 4 5 6 7 8 9 10) " ).should == "3628800"
359
+ @nendo.replStr( " (* 100 (* 10 10 10)) " ).should == "100000"
360
+ @nendo.replStr( " (* 1.1 1) " ).should == "1.1"
361
+ @nendo.replStr( " (* 1.3 1.1) " ).should == (1.3*1.1).to_s
362
+ @nendo.replStr( " (*) " ).should == "1"
363
+ lambda { @nendo.replStr( " (* '() ) " ) }.should raise_error(TypeError)
364
+ lambda { @nendo.replStr( " (* 1 '() ) " ) }.should raise_error(TypeError)
365
+ lambda { @nendo.replStr( " (* 1.1 '() ) " ) }.should raise_error(TypeError)
366
+ lambda { @nendo.replStr( " (* '(1) ) " ) }.should raise_error(TypeError)
367
+ lambda { @nendo.replStr( " (* 1.1 \"a\" ) " ) }.should raise_error(TypeError)
368
+ lambda { @nendo.replStr( " (* \"a\" 1) " ) }.should raise_error(TypeError)
369
+ lambda { @nendo.replStr( " (* \"a\" 1.1) " ) }.should raise_error(TypeError)
370
+ end
371
+ end
372
+
373
+ describe Nendo, "when call replStr() with `/' function" do
374
+ before do
375
+ @nendo = Nendo::Core.new()
376
+ end
377
+ it "should" do
378
+ @nendo.replStr( " (/ 1) " ).should == "1"
379
+ @nendo.replStr( " (/ 1.1) " ).should ==
380
+ (1/1.1).to_s
381
+ @nendo.replStr( " (/ 2 1) " ).should == "2"
382
+ @nendo.replStr( " (/ 2 2) " ).should == "1"
383
+ @nendo.replStr( " (/ 2 2.0) " ).should == "1.0"
384
+ @nendo.replStr( " (/ 2 5.0) " ).should == "0.4"
385
+ @nendo.replStr( " (/ 10.0 2 2 2 2 2 2 2 2 2 2) " ).should == "0.009765625"
386
+ @nendo.replStr( " (/ 100 (/ 100 10) 10) " ).should == "1"
387
+ @nendo.replStr( " (/ 1 1.11) " ).should ==
388
+ (1/1.11).to_s
389
+ @nendo.replStr( " (/ 1.3 1.1) " ).should ==
390
+ (1.3/1.1).to_s
391
+ lambda { @nendo.replStr( " (/ 1 '() ) " ) }.should raise_error(TypeError)
392
+ lambda { @nendo.replStr( " (/ 1.1 '() ) " ) }.should raise_error(TypeError)
393
+ lambda { @nendo.replStr( " (/) " ) }.should raise_error(ArgumentError)
394
+ lambda { @nendo.replStr( " (/ '() ) " ) }.should raise_error(TypeError)
395
+ lambda { @nendo.replStr( " (/ 1.1 \"a\" ) " ) }.should raise_error(TypeError)
396
+ end
397
+ end
398
+
399
+ describe Nendo, "when call replStr() with `%' function" do
400
+ before do
401
+ @nendo = Nendo::Core.new()
402
+ end
403
+ it "should" do
404
+ @nendo.replStr( " (% 1) " ).should == "0"
405
+ @nendo.replStr( " (% 1.1) " ).should == "1.0"
406
+ @nendo.replStr( " (% 2 1) " ).should == "0"
407
+ @nendo.replStr( " (% 2 2) " ).should == "0"
408
+ @nendo.replStr( " (% 2 2.0) " ).should == "0.0"
409
+ @nendo.replStr( " (% 2 5.0) " ).should == "2.0"
410
+ @nendo.replStr( " (% 100 (% 103 10)) " ).should == "1"
411
+ @nendo.replStr( " (% 1 1.11) " ).should == "1.0"
412
+ @nendo.replStr( " (% 1.3 1.1) " ).should == (1.3%1.1).to_s
413
+ lambda { @nendo.replStr( " (% 1 '() ) " ) }.should raise_error(TypeError)
414
+ lambda { @nendo.replStr( " (% 1.1 '() ) " ) }.should raise_error(TypeError)
415
+ lambda { @nendo.replStr( " (\%) " ) }.should raise_error(ArgumentError)
416
+ lambda { @nendo.replStr( " (\% '() ) " ) }.should raise_error(TypeError)
417
+ lambda { @nendo.replStr( " (\% 1.1 \"a\" ) " ) }.should raise_error(TypeError)
418
+ end
419
+ end
420
+
421
+ describe Nendo, "when call replStr() with `quotient' function" do
422
+ before do
423
+ @nendo = Nendo::Core.new()
424
+ end
425
+ it "should" do
426
+ @nendo.replStr( " (quotient 2 1) " ).should == "2"
427
+ @nendo.replStr( " (quotient 2 2) " ).should == "1"
428
+ @nendo.replStr( " (quotient 2 2.0) " ).should == "1"
429
+ @nendo.replStr( " (quotient 2 5.0) " ).should == "0"
430
+ @nendo.replStr( " (quotient 1 1.11) " ).should == "0"
431
+ @nendo.replStr( " (quotient 10 3) " ).should == "3"
432
+ @nendo.replStr( " (quotient -10 3) " ).should == "-3"
433
+ @nendo.replStr( " (quotient 10 -3) " ).should == "-3"
434
+ @nendo.replStr( " (quotient 10 -2) " ).should == "-5"
435
+ lambda { @nendo.replStr( " (quotient 1 ) " ) }.should raise_error(ArgumentError)
436
+ lambda { @nendo.replStr( " (quotient 1.1 ) " ) }.should raise_error(ArgumentError)
437
+ lambda { @nendo.replStr( " (quotient) " ) }.should raise_error(ArgumentError)
438
+ lambda { @nendo.replStr( " (quotient '() ) " ) }.should raise_error(ArgumentError)
439
+ lambda { @nendo.replStr( " (quotient 1.1 \"a\" ) " ) }.should raise_error(TypeError)
440
+ lambda { @nendo.replStr( " (quotient \"a\" 1.1 ) " ) }.should raise_error(TypeError)
441
+ end
442
+ end
443
+
444
+ describe Nendo, "when call replStr() with `remainder' function" do
445
+ before do
446
+ @nendo = Nendo::Core.new()
447
+ end
448
+ it "should" do
449
+ @nendo.replStr( " (remainder 2 1) " ).should == "0"
450
+ @nendo.replStr( " (remainder 2 2) " ).should == "0"
451
+ @nendo.replStr( " (remainder 2 2.0) " ).should == "0.0"
452
+ @nendo.replStr( " (remainder 2 5.0) " ).should == "2.0"
453
+ @nendo.replStr( " (remainder 1 1.11) " ).should == "1.0"
454
+ @nendo.replStr( " (remainder 10 3) " ).should == "1"
455
+ @nendo.replStr( " (remainder -10 3) " ).should == "-1"
456
+ @nendo.replStr( " (remainder 10 -3) " ).should == "1"
457
+ @nendo.replStr( " (remainder -10 -3) " ).should == "-1"
458
+ @nendo.replStr( " (remainder 10 -2) " ).should == "0"
459
+ lambda { @nendo.replStr( " (remainder 1 '() ) " ) }.should raise_error(TypeError)
460
+ lambda { @nendo.replStr( " (remainder 1.1 '() ) " ) }.should raise_error(TypeError)
461
+ lambda { @nendo.replStr( " (remainder) " ) }.should raise_error(ArgumentError)
462
+ lambda { @nendo.replStr( " (remainder '() ) " ) }.should raise_error(ArgumentError)
463
+ lambda { @nendo.replStr( " (remainder 1.1 \"a\" ) " ) }.should raise_error(TypeError)
464
+ end
465
+ end
466
+
467
+ describe Nendo, "when call replStr() with `modulo' function" do
468
+ before do
469
+ @nendo = Nendo::Core.new()
470
+ end
471
+ it "should" do
472
+ @nendo.replStr( " (modulo 2 1) " ).should == "0"
473
+ @nendo.replStr( " (modulo 2 2) " ).should == "0"
474
+ @nendo.replStr( " (modulo 2 2.0) " ).should == "0.0"
475
+ @nendo.replStr( " (modulo 2 5.0) " ).should == "2.0"
476
+ @nendo.replStr( " (modulo 100 (modulo 103 10)) " ).should == "1"
477
+ @nendo.replStr( " (modulo 1 1.11) " ).should == "1.0"
478
+ @nendo.replStr( " (modulo 10 3) " ).should == "1"
479
+ @nendo.replStr( " (modulo -10 3) " ).should == "2"
480
+ @nendo.replStr( " (modulo 10 -3) " ).should == "-2"
481
+ @nendo.replStr( " (modulo -10 -3) " ).should == "-1"
482
+ @nendo.replStr( " (modulo 10 -2) " ).should == "0"
483
+ lambda { @nendo.replStr( " (modulo 1 '() ) " ) }.should raise_error(TypeError)
484
+ lambda { @nendo.replStr( " (modulo 1.1 '() ) " ) }.should raise_error(TypeError)
485
+ lambda { @nendo.replStr( " (modulo) " ) }.should raise_error(ArgumentError)
486
+ lambda { @nendo.replStr( " (modulo '() ) " ) }.should raise_error(TypeError)
487
+ lambda { @nendo.replStr( " (modulo 1.1 \"a\" ) " ) }.should raise_error(TypeError)
488
+ end
489
+ end
490
+
491
+ describe Nendo, "when read various list expressions" do
492
+ before do
493
+ @nendo = Nendo::Core.new()
494
+ end
495
+ it "should" do
496
+ @nendo.replStr( " '() " ).should == "()"
497
+ @nendo.replStr( " '[] " ).should == "()"
498
+ @nendo.replStr( " '(1 . 1) " ).should == "(1 . 1)"
499
+ @nendo.replStr( " '[1 . 1) " ).should == "(1 . 1)"
500
+ @nendo.replStr( " '(1 . 1] " ).should == "(1 . 1)"
501
+ @nendo.replStr( " '(1 1 . 1) " ).should == "(1 1 . 1)"
502
+ @nendo.replStr( " '(1 2 . 3) " ).should == "(1 2 . 3)"
503
+ @nendo.replStr( " '(1 2 3) " ).should == "(1 2 3)"
504
+ @nendo.replStr( " '(1.1 2.2 3.3) " ).should == "(1.1 2.2 3.3)"
505
+ @nendo.replStr( " '(a bb ccc dddd) " ).should == "(a bb ccc dddd)"
506
+ @nendo.replStr( " '(a (b) ((c)) (((d)))) " ).should == "(a (b) ((c)) (((d))))"
507
+ @nendo.replStr( " '[a (b) ((c)) (((d)))] " ).should == "(a (b) ((c)) (((d))))"
508
+ @nendo.replStr( " '(a [b] ([c]) (([d]))) " ).should == "(a (b) ((c)) (((d))))"
509
+ @nendo.replStr( " '[a [b] [[c]] [[[d]]]] " ).should == "(a (b) ((c)) (((d))))"
510
+ @nendo.replStr( " '('a)" ).should == "('a)"
511
+ @nendo.replStr( " '(''a)" ).should == "(''a)"
512
+ @nendo.replStr( " '('a 'b 'c)" ).should == "('a 'b 'c)"
513
+ @nendo.replStr( ' \'("str") ' ).should == '("str")'
514
+ @nendo.replStr( ' \'("str" . 1) ' ).should == '("str" . 1)'
515
+ @nendo.replStr( ' \'(1 . "str") ' ).should == '(1 . "str")'
516
+ @nendo.replStr( ' \'(1 2 . "str") ' ).should == '(1 2 . "str")'
517
+ @nendo.replStr( " '((a)(b)(c)) " ).should == "((a) (b) (c))"
518
+ @nendo.replStr( " 'a " ).should == "a"
519
+ @nendo.replStr( " 'symbol " ).should == "symbol"
520
+ @nendo.replStr( " 'SYMBOL " ).should == "SYMBOL"
521
+ @nendo.replStr( " 'SyMbOl " ).should == "SyMbOl"
522
+ @nendo.replStr( " ''a " ).should == "'a"
523
+ @nendo.replStr( " '1 " ).should == "1"
524
+ @nendo.replStr( " ''1 " ).should == "'1"
525
+ @nendo.replStr( " '''1 " ).should == "''1"
526
+ @nendo.replStr( " '1.1 " ).should == "1.1"
527
+ @nendo.replStr( " ''1.1 " ).should == "'1.1"
528
+ @nendo.replStr( " '''1.1 " ).should == "''1.1"
529
+ @nendo.replStr( " '() " ).should == "()"
530
+ @nendo.replStr( " '(()) " ).should == "(())"
531
+ @nendo.replStr( " '((())) " ).should == "((()))"
532
+ @nendo.replStr( " '(((()))) " ).should == "(((())))"
533
+ @nendo.replStr( " '(() . ()) " ).should == "(())"
534
+ @nendo.replStr( " '(a . ()) " ).should == "(a)"
535
+ @nendo.replStr( " '(a . #t) " ).should == "(a . #t)"
536
+ @nendo.replStr( " '(a . #f) " ).should == "(a . #f)"
537
+ @nendo.replStr( " '(a . nil) " ).should == "(a . nil)"
538
+ @nendo.replStr( " '(a b c d e . ()) " ).should == "(a b c d e)"
539
+ @nendo.replStr( " '(#t #t #f #f nil nil '() '()) " ).should == "(#t #t #f #f nil nil '() '())"
540
+ end
541
+ end
542
+
543
+
544
+ describe Nendo, "when use #xxxx syntax " do
545
+ before do
546
+ @nendo = Nendo::Core.new()
547
+ @nendo.loadInitFile
548
+ end
549
+ it "should" do
550
+ @nendo.replStr( " #t " ).should == "#t"
551
+ @nendo.replStr( " #f " ).should == "#f"
552
+ @nendo.replStr( " '#( 1 ) " ).should == "#(1)"
553
+ @nendo.replStr( " '#() " ).should == "#()"
554
+ @nendo.replStr( " #! \n #t" ).should == "#t"
555
+ @nendo.replStr( " #! \n 100" ).should == "100"
556
+ @nendo.replStr( " #! 123 \n 100" ).should == "100"
557
+ @nendo.replStr( " '#?=1" ).should == "(debug-print 1 \"(string)\" 1 '1)"
558
+ @nendo.replStr( " #b0 " ).should == Integer("0b0").to_s
559
+ @nendo.replStr( " #b01 " ).should == Integer("0b01").to_s
560
+ @nendo.replStr( " #b10 " ).should == Integer("0b10").to_s
561
+ @nendo.replStr( " #b00000001 " ).should == Integer("0b00000001").to_s
562
+ @nendo.replStr( " #b1010101010101010 " ).should == Integer("0b1010101010101010").to_s
563
+ lambda { @nendo.replStr( " #b2 " ) }.should raise_error(RuntimeError)
564
+ lambda { @nendo.replStr( " #b02 " ) }.should raise_error(RuntimeError)
565
+ lambda { @nendo.replStr( " #bF " ) }.should raise_error(RuntimeError)
566
+ @nendo.replStr( " #o0 " ).should == Integer("0o0").to_s
567
+ @nendo.replStr( " #o7 " ).should == Integer("0o7").to_s
568
+ @nendo.replStr( " #o01 " ).should == Integer("0o01").to_s
569
+ @nendo.replStr( " #o10 " ).should == Integer("0o10").to_s
570
+ @nendo.replStr( " #o777 " ).should == Integer("0o777").to_s
571
+ @nendo.replStr( " #o00000007 " ).should == Integer("0o00000007").to_s
572
+ @nendo.replStr( " #o0123456701234567 " ).should == Integer("0o0123456701234567").to_s
573
+ lambda { @nendo.replStr( " #o8 " ) }.should raise_error(RuntimeError)
574
+ lambda { @nendo.replStr( " #o08 " ) }.should raise_error(RuntimeError)
575
+ lambda { @nendo.replStr( " #oA " ) }.should raise_error(RuntimeError)
576
+ @nendo.replStr( " #d0 " ).should == Integer("0d0").to_s
577
+ @nendo.replStr( " #d9 " ).should == Integer("0d9").to_s
578
+ @nendo.replStr( " #d01 " ).should == Integer("0d01").to_s
579
+ @nendo.replStr( " #d10 " ).should == Integer("0d10").to_s
580
+ @nendo.replStr( " #d999 " ).should == Integer("0d999").to_s
581
+ @nendo.replStr( " #d00000009 " ).should == Integer("0d00000009").to_s
582
+ @nendo.replStr( " #d0123456701234567 " ).should == Integer("0d0123456701234567").to_s
583
+ lambda { @nendo.replStr( " #dA " ) }.should raise_error(RuntimeError)
584
+ lambda { @nendo.replStr( " #dF " ) }.should raise_error(RuntimeError)
585
+ @nendo.replStr( " #x0 " ).should == Integer("0x0").to_s
586
+ @nendo.replStr( " #x9 " ).should == Integer("0x9").to_s
587
+ @nendo.replStr( " #xA " ).should == Integer("0xA").to_s
588
+ @nendo.replStr( " #xF " ).should == Integer("0xF").to_s
589
+ @nendo.replStr( " #x01 " ).should == Integer("0x01").to_s
590
+ @nendo.replStr( " #x10 " ).should == Integer("0x10").to_s
591
+ @nendo.replStr( " #xFFF " ).should == Integer("0xFFF").to_s
592
+ @nendo.replStr( " #x0000000F " ).should == Integer("0x0000000F").to_s
593
+ @nendo.replStr( " #x0123456789ABCDEF0123456789ABCDEF " ).should == Integer("0x0123456789ABCDEF0123456789ABCDEF").to_s
594
+ lambda { @nendo.replStr( " #xg " ) }.should raise_error(RuntimeError)
595
+ lambda { @nendo.replStr( " #xh " ) }.should raise_error(RuntimeError)
596
+ lambda { @nendo.replStr( " #xz " ) }.should raise_error(RuntimeError)
597
+ lambda { @nendo.replStr( " #xG " ) }.should raise_error(RuntimeError)
598
+ lambda { @nendo.replStr( " #xH " ) }.should raise_error(RuntimeError)
599
+ lambda { @nendo.replStr( " #xZ " ) }.should raise_error(RuntimeError)
600
+ lambda { @nendo.replStr( " #a " ) }.should raise_error(NameError)
601
+ lambda { @nendo.replStr( " #c " ) }.should raise_error(NameError)
602
+ lambda { @nendo.replStr( " #e " ) }.should raise_error(NameError)
603
+ lambda { @nendo.replStr( " #tt " ) }.should raise_error(NameError)
604
+ lambda { @nendo.replStr( " #ff " ) }.should raise_error(NameError)
605
+ lambda { @nendo.replStr( " #abc " ) }.should raise_error(NameError)
606
+ lambda { @nendo.replStr( " #? " ) }.should raise_error(NameError)
607
+ lambda { @nendo.replStr( " #?a " ) }.should raise_error(NameError)
608
+ lambda { @nendo.replStr( " #= " ) }.should raise_error(NameError)
609
+ lambda { @nendo.replStr( " #?? " ) }.should raise_error(NameError)
610
+ end
611
+ end
612
+
613
+ describe Nendo, "when read various vector expressions" do
614
+ before do
615
+ @nendo = Nendo::Core.new()
616
+ end
617
+ it "should" do
618
+ @nendo.replStr( " '() " ).should == "()"
619
+ @nendo.replStr( " '[] " ).should == "()"
620
+ @nendo.replStr( " '#( 1 ) " ).should == "#(1)"
621
+ lambda { @nendo.replStr( " '#(( 1 ) " ) }.should raise_error( RuntimeError )
622
+ @nendo.replStr( " '#( 1 2 ) " ).should == "#(1 2)"
623
+ @nendo.replStr( " '#( 1 () ) " ).should == "#(1 ())"
624
+ @nendo.replStr( " '#( () 2 ) " ).should == "#(() 2)"
625
+ lambda { @nendo.replStr( " '#( 1 . 2 ) " ) }.should raise_error( RuntimeError )
626
+ lambda { @nendo.replStr( " #(+ 1 2) " ) }.should raise_error( RuntimeError )
627
+ @nendo.replStr( " '#( 1 #( 11 )) " ).should == "#(1 #(11))"
628
+ @nendo.replStr( " '#( 1 #( 11 12 )) " ).should == "#(1 #(11 12))"
629
+ @nendo.replStr( " '#( 1 #( 11 #( 111 ))) " ).should == "#(1 #(11 #(111)))"
630
+ @nendo.replStr( " '#( 1 #( 11 #( 111 112))) " ).should == "#(1 #(11 #(111 112)))"
631
+ @nendo.replStr( " '#(1 2 3) " ).should == "#(1 2 3)"
632
+ @nendo.replStr( " '#(1.1 2.2 3.3) " ).should == "#(1.1 2.2 3.3)"
633
+ @nendo.replStr( " '#(a bb ccc dddd) " ).should == "#(a bb ccc dddd)"
634
+ @nendo.replStr( " '#(a (b) ((c)) (((d)))) " ).should == "#(a (b) ((c)) (((d))))"
635
+ end
636
+ end
637
+
638
+ describe Nendo, "when call replStr() with built-in functions" do
639
+ before do
640
+ @nendo = Nendo::Core.new()
641
+ end
642
+ it "should" do
643
+ @nendo.replStr( " (car '(1 2 3 4)) " ).should == "1"
644
+ @nendo.replStr( " (cdr '(1 2 3 4)) " ).should == "(2 3 4)"
645
+ @nendo.replStr( " (null? '()) " ).should == "#t"
646
+ @nendo.replStr( " (null? '(1)) " ).should == "#f"
647
+ @nendo.replStr( " (null? false) " ).should == "#f"
648
+ @nendo.replStr( " (null? nil) " ).should == "#f"
649
+ @nendo.replStr( " (null? true) " ).should == "#f"
650
+ @nendo.replStr( " (cons 1 2) " ).should == "(1 . 2)"
651
+ @nendo.replStr( " (cons 1 '(2 3)) " ).should == "(1 2 3)"
652
+ @nendo.replStr( " (cons '(1 2) '(3 4)) " ).should == "((1 2) 3 4)"
653
+ @nendo.replStr( " (cons '(1 2) '((3 4))) " ).should == "((1 2) (3 4))"
654
+ @nendo.replStr( " (cons '() '()) " ).should == "(())"
655
+ @nendo.replStr( " (cons '() (cdr '(100))) " ).should == "(())"
656
+ @nendo.replStr( " (cons '() (car '(()))) " ).should == "(())"
657
+ @nendo.replStr( " (cons (car '(())) '()) " ).should == "(())"
658
+ @nendo.replStr( " (cons (car '(())) (car '(()))) " ).should == "(())"
659
+ @nendo.replStr( " (cons '() (cdr '(100))) " ).should == "(())"
660
+ @nendo.replStr( " (cons (cdr '(100)) '()) " ).should == "(())"
661
+ @nendo.replStr( " (cons (cdr '(100)) (cdr '(100))) " ).should == "(())"
662
+ lambda { @nendo.replStr( " (cons 1 2 3) " ) }.should raise_error(ArgumentError)
663
+ lambda { @nendo.replStr( " (cons 1) " ) }.should raise_error(ArgumentError)
664
+ lambda { @nendo.replStr( " (cons) " ) }.should raise_error(ArgumentError)
665
+ @nendo.replStr( " (list 1 2 3) " ).should == "(1 2 3)"
666
+ @nendo.replStr( " (list '(1) '(2) '(3)) " ).should == "((1) (2) (3))"
667
+ @nendo.replStr( " (list 'a 'b 'c) " ).should == "(a b c)"
668
+ @nendo.replStr( " (list '(a) '((b c))) " ).should == "((a) ((b c)))"
669
+ @nendo.replStr( " (list) " ).should == "()"
670
+ @nendo.replStr( " (list 1) " ).should == "(1)"
671
+ @nendo.replStr( " (define !a 10) !a" ).should == "10"
672
+ @nendo.replStr( " (define $a 11) $a" ).should == "11"
673
+ @nendo.replStr( " (define %a 12) %a" ).should == "12"
674
+ @nendo.replStr( " (define &a 13) &a" ).should == "13"
675
+ @nendo.replStr( " (define *a 14) *a" ).should == "14"
676
+ @nendo.replStr( " (define +a 15) +a" ).should == "15"
677
+ @nendo.replStr( " (define -a 16) -a" ).should == "16"
678
+ @nendo.replStr( " (define /a 17) /a" ).should == "17"
679
+ @nendo.replStr( " (define <a 18) <a" ).should == "18"
680
+ @nendo.replStr( " (define =a 19) =a" ).should == "19"
681
+ @nendo.replStr( " (define ?a 20) ?a" ).should == "20"
682
+ @nendo.replStr( " (define @a 21) @a" ).should == "21"
683
+ @nendo.replStr( " (define ^a 22) ^a" ).should == "22"
684
+ @nendo.replStr( " (define ~a 23) ~a" ).should == "23"
685
+ @nendo.replStr( " (define a! 30) a!" ).should == "30"
686
+ @nendo.replStr( " (define a$ 31) a$" ).should == "31"
687
+ @nendo.replStr( " (define a% 32) a%" ).should == "32"
688
+ @nendo.replStr( " (define a& 33) a&" ).should == "33"
689
+ @nendo.replStr( " (define a* 34) a*" ).should == "34"
690
+ @nendo.replStr( " (define a+ 35) a+" ).should == "35"
691
+ @nendo.replStr( " (define a- 36) a-" ).should == "36"
692
+ @nendo.replStr( " (define a/ 37) a/" ).should == "37"
693
+ @nendo.replStr( " (define a< 38) a<" ).should == "38"
694
+ @nendo.replStr( " (define a= 39) a=" ).should == "39"
695
+ @nendo.replStr( " (define a? 40) a?" ).should == "40"
696
+ @nendo.replStr( " (define a@ 41) a@" ).should == "41"
697
+ @nendo.replStr( " (define a^ 42) a^" ).should == "42"
698
+ @nendo.replStr( " (define a~ 43) a~" ).should == "43"
699
+ @nendo.replStr( " (define aFunc (lambda (x) x)) true" ).should == "#t"
700
+ @nendo.replStr( " (define aMacro (macro (x) x)) true" ).should == "#t"
701
+ @nendo.replStr( " (define a! 123) a!" ).should == "123"
702
+ @nendo.replStr( " (define b? 321) b?" ).should == "321"
703
+ @nendo.replStr( " (define a-b 1234) a-b" ).should == "1234"
704
+ @nendo.replStr( " (define start-end!? 4321) start-end!?" ).should == "4321"
705
+ @nendo.replStr( " (procedure? car) " ).should == "#t"
706
+ @nendo.replStr( " (procedure? aFunc) " ).should == "#t"
707
+ @nendo.replStr( " (procedure? aMacro) " ).should == "#f"
708
+ @nendo.replStr( " (procedure? 1) " ).should == "#f"
709
+ @nendo.replStr( " (procedure? 1.1) " ).should == "#f"
710
+ @nendo.replStr( " (procedure? \"str\") " ).should == "#f"
711
+ @nendo.replStr( " (procedure? 'a) " ).should == "#f"
712
+ @nendo.replStr( " (procedure? '(1)) " ).should == "#f"
713
+ @nendo.replStr( " (procedure? '()) " ).should == "#f"
714
+ @nendo.replStr( " (symbol? car) " ).should == "#f"
715
+ @nendo.replStr( " (symbol? aFunc) " ).should == "#f"
716
+ @nendo.replStr( " (symbol? aMacro) " ).should == "#f"
717
+ @nendo.replStr( " (symbol? 1) " ).should == "#f"
718
+ @nendo.replStr( " (symbol? 1.1) " ).should == "#f"
719
+ @nendo.replStr( " (symbol? \"str\") " ).should == "#f"
720
+ @nendo.replStr( " (symbol? 'a) " ).should == "#t"
721
+ @nendo.replStr( " (symbol? '(1)) " ).should == "#f"
722
+ @nendo.replStr( " (symbol? '()) " ).should == "#f"
723
+ @nendo.replStr( " (pair? car) " ).should == "#f"
724
+ @nendo.replStr( " (pair? aFunc) " ).should == "#f"
725
+ @nendo.replStr( " (pair? aMacro) " ).should == "#f"
726
+ @nendo.replStr( " (pair? 1) " ).should == "#f"
727
+ @nendo.replStr( " (pair? 1.1) " ).should == "#f"
728
+ @nendo.replStr( " (pair? \"str\") " ).should == "#f"
729
+ @nendo.replStr( " (pair? 'a) " ).should == "#f"
730
+ @nendo.replStr( " (pair? '(1)) " ).should == "#t"
731
+ @nendo.replStr( " (pair? '()) " ).should == "#f"
732
+ @nendo.replStr( " (number? car) " ).should == "#f"
733
+ @nendo.replStr( " (number? aFunc) " ).should == "#f"
734
+ @nendo.replStr( " (number? aMacro) " ).should == "#f"
735
+ @nendo.replStr( " (number? 1) " ).should == "#t"
736
+ @nendo.replStr( " (number? 1.1) " ).should == "#t"
737
+ @nendo.replStr( " (number? \"str\") " ).should == "#f"
738
+ @nendo.replStr( " (number? 'a) " ).should == "#f"
739
+ @nendo.replStr( " (number? '(1)) " ).should == "#f"
740
+ @nendo.replStr( " (number? '()) " ).should == "#f"
741
+ @nendo.replStr( " (integer? car) " ).should == "#f"
742
+ @nendo.replStr( " (integer? aFunc) " ).should == "#f"
743
+ @nendo.replStr( " (integer? aMacro) " ).should == "#f"
744
+ @nendo.replStr( " (integer? 1) " ).should == "#t"
745
+ @nendo.replStr( " (integer? 1.1) " ).should == "#f"
746
+ @nendo.replStr( " (integer? \"str\") " ).should == "#f"
747
+ @nendo.replStr( " (integer? 'a) " ).should == "#f"
748
+ @nendo.replStr( " (integer? '(1)) " ).should == "#f"
749
+ @nendo.replStr( " (integer? '()) " ).should == "#f"
750
+ @nendo.replStr( " (string? car) " ).should == "#f"
751
+ @nendo.replStr( " (string? aFunc) " ).should == "#f"
752
+ @nendo.replStr( " (string? aMacro) " ).should == "#f"
753
+ @nendo.replStr( " (string? 1) " ).should == "#f"
754
+ @nendo.replStr( " (string? 1.1) " ).should == "#f"
755
+ @nendo.replStr( " (string? \"str\") " ).should == "#t"
756
+ @nendo.replStr( " (string? 'a) " ).should == "#f"
757
+ @nendo.replStr( " (string? '(1)) " ).should == "#f"
758
+ @nendo.replStr( " (string? '()) " ).should == "#f"
759
+ @nendo.replStr( " (macro? car) " ).should == "#f"
760
+ @nendo.replStr( " (macro? aFunc) " ).should == "#f"
761
+ @nendo.replStr( " (macro? aMacro) " ).should == "#t"
762
+ @nendo.replStr( " (macro? 1) " ).should == "#f"
763
+ @nendo.replStr( " (macro? 1.1) " ).should == "#f"
764
+ @nendo.replStr( " (macro? \"str\") " ).should == "#f"
765
+ @nendo.replStr( " (macro? 'a) " ).should == "#f"
766
+ @nendo.replStr( " (macro? '(1)) " ).should == "#f"
767
+ @nendo.replStr( " (macro? '()) " ).should == "#f"
768
+ @nendo.replStr( " (length '()) " ).should == "0"
769
+ @nendo.replStr( " (length '(1)) " ).should == "1"
770
+ @nendo.replStr( " (length '((1))) " ).should == "1"
771
+ @nendo.replStr( " (length '(1 2)) " ).should == "2"
772
+ lambda { @nendo.replStr( " (length \"str\") " ) }.should raise_error(TypeError)
773
+ lambda { @nendo.replStr( " (length 1) " ) }.should raise_error(TypeError)
774
+ @nendo.replStr( " (symbol->string 'sym) " ).should == '"sym"'
775
+ @nendo.replStr( " (string->symbol \"sym\") " ).should == 'sym'
776
+ @nendo.replStr( ' (string-join \'("Aa" "Bb" "Cc") ) ' ).should == '"AaBbCc"'
777
+ @nendo.replStr( ' (string-join \'("Aa" "Bb" "Cc") ":") ' ).should == '"Aa:Bb:Cc"'
778
+ @nendo.replStr( ' (string-join \'("Aa" "Bb" "Cc") "//") ' ).should == '"Aa//Bb//Cc"'
779
+ lambda { @nendo.replStr( ' (string-join \'("Aa" "Bb" "Cc") 100) ' ) }.should raise_error(TypeError)
780
+ lambda { @nendo.replStr( ' (string-join \'("Aa" "Bb" "Cc") :xx) ' ) }.should raise_error(TypeError)
781
+ end
782
+ end
783
+
784
+ describe Nendo, "when call replStr() with variable modifications" do
785
+ before do
786
+ @nendo = Nendo::Core.new()
787
+ end
788
+ it "should" do
789
+ @nendo.replStr( " (define x 1) x " ).should == "1"
790
+ @nendo.replStr( " (define x 2) x " ).should == "2"
791
+ @nendo.replStr( " (define x 100) x " ).should == "100"
792
+ @nendo.replStr( " (define x true) x " ).should == "#t"
793
+ @nendo.replStr( " (define x false) x " ).should == "#f"
794
+ @nendo.replStr( " (define x nil) x " ).should == "nil"
795
+ @nendo.replStr( " (define x '()) x " ).should == "()"
796
+ @nendo.replStr( " (define x '(1)) x " ).should == "(1)"
797
+ @nendo.replStr( " (define x (+ 1 2 3)) x " ).should == "6"
798
+ @nendo.replStr( " (define x (sprintf \"$%02X\" 17)) x x x " ).should == '"$11"'
799
+ @nendo.replStr( " 1 2 3 " ).should == "3"
800
+ @nendo.replStr( " (define x 3.14) (set! x (* x 2)) x " ).should == "6.28"
801
+ @nendo.replStr( " 1 \n 2 \n 3 \n " ).should == "3"
802
+ @nendo.replStr( " (define a '(1 . 2)) (set-car! a 100) a " ).should == "(100 . 2)"
803
+ @nendo.replStr( " (define a '(1 . 2)) (set-car! a '()) a " ).should == "(() . 2)"
804
+ @nendo.replStr( " (define a '(1 . 2)) (set-car! a #t) a " ).should == "(#t . 2)"
805
+ @nendo.replStr( " (define a '(1 . 2)) (set-car! a #f) a " ).should == "(#f . 2)"
806
+ @nendo.replStr( " (define a '(1 . 2)) (set-car! a nil) a " ).should == "(nil . 2)"
807
+ @nendo.replStr( " (define a '(1 . 2)) (set-cdr! a 200) a " ).should == "(1 . 200)"
808
+ @nendo.replStr( " (define a '(1 . 2)) (set-cdr! a '(2)) a " ).should == "(1 2)"
809
+ @nendo.replStr( " (define a '(1 . 2)) (set-cdr! a '()) a " ).should == "(1)"
810
+ @nendo.replStr( " (define a '(1 . 2)) (set-cdr! a #t) a " ).should == "(1 . #t)"
811
+ @nendo.replStr( " (define a '(1 . 2)) (set-cdr! a #f) a " ).should == "(1 . #f)"
812
+ @nendo.replStr( " (define a '(1 . 2)) (set-cdr! a nil) a " ).should == "(1 . nil)"
813
+ @nendo.replStr( " (define a '((1 . 2) 3)) (set-car! (car a) 100) a " ).should == "((100 . 2) 3)"
814
+ @nendo.replStr( " (define a '((1 . 2) 3)) (set-cdr! (car a) 200) a " ).should == "((1 . 200) 3)"
815
+ @nendo.replStr( " (define a '((1 . 2) . 3)) (set-cdr! a 300) a " ).should == "((1 . 2) . 300)"
816
+ end
817
+ end
818
+
819
+ describe Nendo, "when call replStr() with undefined variable" do
820
+ before do
821
+ @nendo = Nendo::Core.new()
822
+ end
823
+ it "should" do
824
+ lambda { @nendo.replStr( " true " ) }.should_not raise_error
825
+ lambda { @nendo.replStr( " false " ) }.should_not raise_error
826
+ lambda { @nendo.replStr( " nil " ) }.should_not raise_error
827
+ lambda { @nendo.replStr( " line1 " ) }.should raise_error( NameError )
828
+ lambda { @nendo.replStr( " true \n line2 " ) }.should raise_error( NameError )
829
+ lambda { @nendo.replStr( " true \n true \n line3 " ) }.should raise_error( NameError )
830
+ lambda { @nendo.replStr( " (+ 1 x) " ) }.should raise_error( NameError )
831
+ lambda { @nendo.replStr( " true \n (+ 1 y) " ) }.should raise_error( NameError )
832
+ end
833
+ end
834
+
835
+ describe Nendo, "when call replStr() with built-in special forms" do
836
+ before do
837
+ @nendo = Nendo::Core.new()
838
+ end
839
+ it "should" do
840
+ @nendo.replStr( " (begin 1) " ).should == "1"
841
+ @nendo.replStr( " (begin 1 2) " ).should == "2"
842
+ @nendo.replStr( " (begin 1 2 3) " ).should == "3"
843
+ @nendo.replStr( " (set! x 2) (set! y (begin (set! x (* x 2)) (set! x (* x 2)) (set! x (* x 2)) 100)) (+ x y)" ).should == "116"
844
+ @nendo.replStr( " (let () 100) " ).should == "100"
845
+ @nendo.replStr( " (let ((a 11)) a) " ).should == "11"
846
+ @nendo.replStr( " (let ((a 11) (b 22)) (+ a b)) " ).should == "33"
847
+ @nendo.replStr( " (let ((a 22)) (let ((b 33)) (+ a b))) " ).should == "55"
848
+ @nendo.replStr( " (let ((a 22)(b 33)) (let ((c 44) (d 55)) (+ a b c d))) " ).should == "154"
849
+ @nendo.replStr( " (let ((a (let ((b 2)) (+ 100 b)))) a) " ).should == "102"
850
+ @nendo.replStr( " (letrec () 100) " ).should == "100"
851
+ @nendo.replStr( " (letrec ((a 11)) a) " ).should == "11"
852
+ @nendo.replStr( " (letrec ((a 11) (b 22)) (+ a b)) " ).should == "33"
853
+ @nendo.replStr( " (letrec ((a 22)) (let ((b 33)) (+ a b))) " ).should == "55"
854
+ @nendo.replStr( " (letrec ((a 22)(b 33)) (let ((c 44) (d 55)) (+ a b c d))) " ).should == "154"
855
+ @nendo.replStr( " (letrec ((a (let ((b 2)) (+ 100 b)))) a) " ).should == "102"
856
+ @nendo.replStr( " (letrec ( (func1 (lambda (x) 13)) (func2 (lambda (x) (* 2 (func1)))) ) (list (func2) (func1))) " ).should == "(26 13)"
857
+ @nendo.replStr( " (letrec ( (func2 (lambda (x) (* 2 (func1)))) (func1 (lambda (x) 7)) ) (list (func2) (func1))) " ).should == "(14 7)"
858
+ @nendo.replStr( " (if true 't 'f)" ).should == "t"
859
+ @nendo.replStr( " (if true '(1) '(2))" ).should == "(1)"
860
+ @nendo.replStr( " (if false 't 'f)" ).should == "f"
861
+ @nendo.replStr( " (if false '(1) '(2))" ).should == "(2)"
862
+ @nendo.replStr( " (set! x 0) (if true (set! x 1) (set! x 2)) x" ).should == "1"
863
+ @nendo.replStr( " (set! x 0) (if false (set! x 1) (set! x 2)) x" ).should == "2"
864
+ @nendo.replStr( " (set! func (lambda (arg1) arg1)) (list (func 1) (func 2))" ).should == "(1 2)"
865
+ @nendo.replStr( " ((lambda (arg1) arg1) 3)" ).should == "3"
866
+ @nendo.replStr( " ((lambda (arg1) arg1) (+ 1 2 3))" ).should == "6"
867
+ @nendo.replStr( " ((if #t + *) 3 4)" ).should == "7"
868
+ @nendo.replStr( " ((if #f + *) 3 4)" ).should == "12"
869
+ lambda { @nendo.replStr( " (error \"My Runtime Error\") " ) }.should raise_error( RuntimeError )
870
+ end
871
+ end
872
+
873
+ describe Nendo, "when call replStr() with global and lexical scope variable" do
874
+ before do
875
+ @nendo = Nendo::Core.new()
876
+ end
877
+ it "should" do
878
+ @nendo.replStr( " (define var 111) " ).should == "111"
879
+ @nendo.replStr( " (let ((var 222)) var) " ).should == "222"
880
+ @nendo.replStr( " (let ((var 222)) (set! var 333) var) " ).should == "333"
881
+ @nendo.replStr( " (let ((var 222)) (set! var 333)) var " ).should == "111"
882
+ @nendo.replStr( " (define global1 \"G\") " ).should == '"G"'
883
+ @nendo.replStr( " " +
884
+ "(let ((local1 \"L\")" +
885
+ " (local2 \"L\"))" +
886
+ " (set! global1 (+ global1 \"lobal1\"))" +
887
+ " (set! local1 (+ local1 \"ocal1\"))" +
888
+ " (set! local2 (+ local2 \"ocal2\"))" +
889
+ " (list global1" +
890
+ " local1" +
891
+ " local2" +
892
+ " (let ((local1 \"A\")" +
893
+ " (local2 \"B\"))" +
894
+ " (set! local1 (+ local1 \"a\"))" +
895
+ " (set! local2 (+ local2 \"b\"))" +
896
+ " (list local1 local2" +
897
+ " (let ((local1 \"CCC\"))" +
898
+ " (list global1 local1 local2))))))" ).should == '("Global1" "Local1" "Local2" ("Aa" "Bb" ("Global1" "CCC" "Bb")))'
899
+ end
900
+ end
901
+
902
+ describe Nendo, "when call replStr() with macroexpand-1 function" do
903
+ before do
904
+ @nendo = Nendo::Core.new()
905
+ end
906
+ it "should" do
907
+ @nendo.replStr( " (set! twice (macro (x) (list 'begin x x))) (macroexpand-1 '(twice (+ 1 1))) " ).should == "(begin (+ 1 1) (+ 1 1))"
908
+ @nendo.replStr( " (set! inc (macro (x) (list 'set! x (list '+ x 1)))) (macroexpand-1 '(inc a)) " ).should == "(set! a (+ a 1))"
909
+ @nendo.replStr( " (set! a 10) (inc a) " ).should == "11"
910
+ @nendo.replStr( " (set! a 10) (inc a) (inc a)" ).should == "12"
911
+ @nendo.replStr( " (macroexpand-1 '(twice (twice (inc a))))" ).should ==
912
+ "(begin (twice (inc a)) (twice (inc a)))"
913
+ @nendo.replStr( " (macroexpand-1 (macroexpand-1 '(twice (twice (inc a)))))" ).should ==
914
+ "(begin (begin (inc a) (inc a)) (begin (inc a) (inc a)))"
915
+ @nendo.replStr( " (macroexpand-1 (macroexpand-1 (macroexpand-1 '(twice (twice (inc a))))))" ).should ==
916
+ "(begin (begin (set! a (+ a 1)) (set! a (+ a 1))) (begin (inc a) (inc a)))"
917
+ @nendo.replStr( " (macroexpand-1 (macroexpand-1 (macroexpand-1 (macroexpand-1 '(twice (twice (inc a)))))))" ).should ==
918
+ "(begin (begin (set! a (+ a 1)) (set! a (+ a 1))) (begin (set! a (+ a 1)) (set! a (+ a 1))))"
919
+ @nendo.replStr( " (set! a 10) (twice (twice (inc a)))" ).should == "14"
920
+ end
921
+ end
922
+
923
+ describe Nendo, "when call functions in init.nnd " do
924
+ before do
925
+ @nendo = Nendo::Core.new()
926
+ @nendo.loadInitFile
927
+ @nendo.loadInitFile # to self optimizing. The init.nnd file will be loaded twice, so `map' can be optimized on second loading phase.
928
+ end
929
+ it "should" do
930
+ @nendo.replStr( " (cadr '(1 2 3 4)) " ).should == "2"
931
+ @nendo.replStr( " (caddr '(1 2 3 4)) " ).should == "3"
932
+ @nendo.replStr( " (cadddr '(1 2 3 4)) " ).should == "4"
933
+ @nendo.replStr( " (caar '((5 6 7 8))) " ).should == "5"
934
+ @nendo.replStr( " (cdar '((5 6 7 8))) " ).should == "(6 7 8)"
935
+ @nendo.replStr( " (cadar '((5 6 7 8))) " ).should == "6"
936
+ @nendo.replStr( " (cddar '((5 6 7 8))) " ).should == "(7 8)"
937
+ @nendo.replStr( " (iota 1) " ).should == "(0)"
938
+ @nendo.replStr( " (iota 3) " ).should == "(0 1 2)"
939
+ @nendo.replStr( " (append '() '()) " ).should == "()"
940
+ @nendo.replStr( " (append '(1) '()) " ).should == "(1)"
941
+ @nendo.replStr( " (append '() '(2)) " ).should == "(2)"
942
+ @nendo.replStr( " (append '(1) '(2)) " ).should == "(1 2)"
943
+ @nendo.replStr( " (append '(1 2) '(3 4)) " ).should == "(1 2 3 4)"
944
+ @nendo.replStr( " (pair? '()) " ).should == "#f"
945
+ @nendo.replStr( " (pair? '(1)) " ).should == "#t"
946
+ @nendo.replStr( " (pair? '(1 2)) " ).should == "#t"
947
+ @nendo.replStr( " (pair? '(1 2 3)) " ).should == "#t"
948
+ @nendo.replStr( " (pair? '(1 . 2)) " ).should == "#t"
949
+ @nendo.replStr( " (pair? '(())) " ).should == "#t"
950
+ @nendo.replStr( " (pair? 1) " ).should == "#f"
951
+ @nendo.replStr( " (pair? \"str\") " ).should == "#f"
952
+ @nendo.replStr( " (list? '()) " ).should == "#t"
953
+ @nendo.replStr( " (list? '(1)) " ).should == "#t"
954
+ @nendo.replStr( " (list? '(1 2)) " ).should == "#t"
955
+ @nendo.replStr( " (list? '(1 2 3)) " ).should == "#t"
956
+ @nendo.replStr( " (list? '(1 . 2)) " ).should == "#f"
957
+ @nendo.replStr( " (list? '(1 2 . 3)) " ).should == "#f"
958
+ @nendo.replStr( " (list? '(())) " ).should == "#t"
959
+ @nendo.replStr( " (list? 1) " ).should == "#f"
960
+ @nendo.replStr( " (list? \"str\") " ).should == "#f"
961
+ @nendo.replStr( " (even? 2) " ).should == "#t"
962
+ @nendo.replStr( " (even? 1) " ).should == "#f"
963
+ @nendo.replStr( " (even? 0) " ).should == "#t"
964
+ @nendo.replStr( " (even? -1) " ).should == "#f"
965
+ @nendo.replStr( " (even? -2) " ).should == "#t"
966
+ @nendo.replStr( " (odd? 2) " ).should == "#f"
967
+ @nendo.replStr( " (odd? 1) " ).should == "#t"
968
+ @nendo.replStr( " (odd? 0) " ).should == "#f"
969
+ @nendo.replStr( " (odd? -1) " ).should == "#t"
970
+ @nendo.replStr( " (odd? -2) " ).should == "#f"
971
+ @nendo.replStr( " (zero? 0) " ).should == "#t"
972
+ @nendo.replStr( " (zero? #f) " ).should == "#f"
973
+ @nendo.replStr( " (zero? #t) " ).should == "#f"
974
+ @nendo.replStr( " (zero? 1) " ).should == "#f"
975
+ @nendo.replStr( " (zero? 2) " ).should == "#f"
976
+ @nendo.replStr( " (zero? -1) " ).should == "#f"
977
+ @nendo.replStr( " (zero? \"str\") " ).should == "#f"
978
+ @nendo.replStr( " (zero? zero?) " ).should == "#f"
979
+ @nendo.replStr( " (positive? 1) " ).should == "#t"
980
+ @nendo.replStr( " (positive? 0) " ).should == "#f"
981
+ @nendo.replStr( " (positive? -1) " ).should == "#f"
982
+ @nendo.replStr( " (negative? 1) " ).should == "#f"
983
+ @nendo.replStr( " (negative? 0) " ).should == "#f"
984
+ @nendo.replStr( " (negative? -1) " ).should == "#t"
985
+ @nendo.replStr( " (abs -1) " ).should == "1"
986
+ @nendo.replStr( " (abs 1) " ).should == "1"
987
+ @nendo.replStr( " (abs -1000) " ).should == "1000"
988
+ @nendo.replStr( " (abs 1000) " ).should == "1000"
989
+ @nendo.replStr( " (max -2 1 0 1 2 3 4 5) " ).should == "5"
990
+ @nendo.replStr( " (max 5 4 3 2 1 0 -1 -2) " ).should == "5"
991
+ @nendo.replStr( " (max 1000000000 10 -10000) " ).should == "1000000000"
992
+ @nendo.replStr( " (min -2 1 0 1 2 3 4 5) " ).should == "-2"
993
+ @nendo.replStr( " (min 5 4 3 2 1 0 -1 -2) " ).should == "-2"
994
+ @nendo.replStr( " (min 1000000000 10 -10000) " ).should == "-10000"
995
+ @nendo.replStr( " (succ -1) " ).should == "0"
996
+ @nendo.replStr( " (succ 0) " ).should == "1"
997
+ @nendo.replStr( " (succ 1) " ).should == "2"
998
+ @nendo.replStr( " (pred -1) " ).should == "-2"
999
+ @nendo.replStr( " (pred 0) " ).should == "-1"
1000
+ @nendo.replStr( " (pred 1) " ).should == "0"
1001
+ @nendo.replStr( " (pred 2) " ).should == "1"
1002
+ @nendo.replStr( " (min 1000000000 10 -10000) " ).should == "-10000"
1003
+ @nendo.replStr( " (nth 0 '(100 200 300)) " ).should == "100"
1004
+ @nendo.replStr( " (nth 1 '(100 200 300)) " ).should == "200"
1005
+ @nendo.replStr( " (nth 2 '(100 200 300)) " ).should == "300"
1006
+ @nendo.replStr( " (nth 3 '(100 200 300)) " ).should == "()"
1007
+ @nendo.replStr( " (nth -1 '(100 200 300)) " ).should == "()"
1008
+ @nendo.replStr( " (first '(100 200 300 400 500 600 700 800 900 1000)) " ).should == "100"
1009
+ @nendo.replStr( " (second '(100 200 300 400 500 600 700 800 900 1000)) " ).should == "200"
1010
+ @nendo.replStr( " (third '(100 200 300 400 500 600 700 800 900 1000)) " ).should == "300"
1011
+ @nendo.replStr( " (fourth '(100 200 300 400 500 600 700 800 900 1000)) " ).should == "400"
1012
+ @nendo.replStr( " (fifth '(100 200 300 400 500 600 700 800 900 1000)) " ).should == "500"
1013
+ @nendo.replStr( " (sixth '(100 200 300 400 500 600 700 800 900 1000)) " ).should == "600"
1014
+ @nendo.replStr( " (seventh '(100 200 300 400 500 600 700 800 900 1000)) " ).should == "700"
1015
+ @nendo.replStr( " (eighth '(100 200 300 400 500 600 700 800 900 1000)) " ).should == "800"
1016
+ @nendo.replStr( " (ninth '(100 200 300 400 500 600 700 800 900 1000)) " ).should == "900"
1017
+ @nendo.replStr( " (tenth '(100 200 300 400 500 600 700 800 900 1000)) " ).should == "1000"
1018
+ @nendo.replStr( " (first '()) " ).should == "()"
1019
+ @nendo.replStr( " (tenth '()) " ).should == "()"
1020
+ @nendo.replStr( " (to-s 10) " ).should == '"10"'
1021
+ @nendo.replStr( " (to_s 10) " ).should == '"10"'
1022
+ @nendo.replStr( " (x->string 10) " ).should == '"10"'
1023
+ @nendo.replStr( " (to-s 2.1) " ).should == '"2.1"'
1024
+ @nendo.replStr( " (to_s 2.1) " ).should == '"2.1"'
1025
+ @nendo.replStr( " (x->string 2.1) " ).should == '"2.1"'
1026
+ @nendo.replStr( " (to_i \"22\") " ).should == '22'
1027
+ @nendo.replStr( " (to-i \"22\") " ).should == '22'
1028
+ @nendo.replStr( " (to_i \"10000\") " ).should == '10000'
1029
+ @nendo.replStr( " (to-i \"10000\") " ).should == '10000'
1030
+ @nendo.replStr( " (let1 aaa 111 aaa) " ).should == "111"
1031
+ @nendo.replStr( " (let1 aaa (+ 2 3) aaa) " ).should == "5"
1032
+ @nendo.replStr( " (let1 aaa 333 (let1 bbb 444 (+ aaa bbb))) " ).should == "777"
1033
+ @nendo.replStr( " (let1 aaa 333 (let1 bbb 444 (set! bbb 555) (+ aaa bbb))) " ).should == "888"
1034
+ @nendo.replStr( " (let1 v (map (lambda (x) x) '(1 2 3)) v) " ).should == "(1 2 3)"
1035
+ @nendo.replStr( " (let ((v (map (lambda (x) x) '(1 2 3)))) v) " ).should == "(1 2 3)"
1036
+ @nendo.replStr( " (cond (true 1) (false 2)) " ).should == "1"
1037
+ @nendo.replStr( " (cond (true ) (false )) " ).should == "#t"
1038
+ @nendo.replStr( " (cond (false 1) (true 2)) " ).should == "2"
1039
+ @nendo.replStr( " (cond (true 1) (true 2)) " ).should == "1"
1040
+ @nendo.replStr( " (cond (false 1) (false 2)) " ).should == "()"
1041
+ @nendo.replStr( " (cond (false 1) (false 2) (else 3)) " ).should == "3"
1042
+ @nendo.replStr( " (cond ((- 10 9) => (lambda (x) (+ \"<\" (to_s x) \">\"))) (else 2)) " ).should == '"<1>"'
1043
+ @nendo.replStr( " (cond (true 1) ((- 10 8) => (lambda (x) (+ \"<\" (to_s x) \">\"))) (else 3)) " ).should == "1"
1044
+ @nendo.replStr( " (or) " ).should == "#f"
1045
+ @nendo.replStr( " (or true) " ).should == "#t"
1046
+ @nendo.replStr( " (or false) " ).should == "#f"
1047
+ @nendo.replStr( " (or nil) " ).should == "#f"
1048
+ @nendo.replStr( " (or '(1)) " ).should == "(1)"
1049
+ @nendo.replStr( " (or '()) " ).should == "()"
1050
+ @nendo.replStr( " (or true true true) " ).should == "#t"
1051
+ @nendo.replStr( " (or 1 2 3) " ).should == "1"
1052
+ @nendo.replStr( " (or false 2) " ).should == "2"
1053
+ @nendo.replStr( " (or false false 3) " ).should == "3"
1054
+ @nendo.replStr( " (or false '(2) false) " ).should == "(2)"
1055
+ @nendo.replStr( " (and) " ).should == "#t"
1056
+ @nendo.replStr( " (and true) " ).should == "#t"
1057
+ @nendo.replStr( " (and false) " ).should == "#f"
1058
+ @nendo.replStr( " (and nil) " ).should == "nil"
1059
+ @nendo.replStr( " (and '(1)) " ).should == "(1)"
1060
+ @nendo.replStr( " (and '()) " ).should == "()"
1061
+ @nendo.replStr( " (and true true true) " ).should == "#t"
1062
+ @nendo.replStr( " (and 1 2 3) " ).should == "3"
1063
+ @nendo.replStr( " (and false 2) " ).should == "#f"
1064
+ @nendo.replStr( " (and false false 3) " ).should == "#f"
1065
+ @nendo.replStr( " (and true 2) " ).should == "2"
1066
+ @nendo.replStr( " (and true true 3) " ).should == "3"
1067
+ @nendo.replStr( " (and true true 3 false) " ).should == "#f"
1068
+ @nendo.replStr( " (and true '(2) true) " ).should == "#t"
1069
+ @nendo.replStr( " (and true true '(2)) " ).should == "(2)"
1070
+ @nendo.replStr( " (and true '(2) false) " ).should == "#f"
1071
+ @nendo.replStr( " (define total 0) (and 1 2 (set! total (+ total 1)) (set! total (+ total 2)) 5) total" ).should == "3"
1072
+ @nendo.replStr( " (define total 1) (and 1 2 false (set! total (+ total 2)) (set! total (+ total 3)) 5) total" ).should == "1"
1073
+ @nendo.replStr( " (apply + 100 '()) " ).should == "100"
1074
+ @nendo.replStr( " (apply + '(1 2)) " ).should == "3"
1075
+ @nendo.replStr( " (apply + 1 2 '(3)) " ).should == "6"
1076
+ @nendo.replStr( " (apply + 1 2 '(3 4)) " ).should == "10"
1077
+ @nendo.replStr( " (apply + 1 2 3 '(4)) " ).should == "10"
1078
+ @nendo.replStr( ' (apply + \'("a" "b" "c")) ' ).should == '"abc"'
1079
+ @nendo.replStr( " (range 5) " ).should == "(0 1 2 3 4)"
1080
+ @nendo.replStr( " (range 5 1) " ).should == "(1 2 3 4 5)"
1081
+ @nendo.replStr( " (range 5 2) " ).should == "(2 3 4 5 6)"
1082
+ @nendo.replStr( " (iota 5 2) " ).should == "(2 3 4 5 6)"
1083
+ @nendo.replStr( " (apply + (range 11)) " ).should == "55"
1084
+ @nendo.replStr( " (apply + (map (lambda (x) (+ x 1)) (range 10))) " ).should == "55"
1085
+ @nendo.replStr( " (apply + (append (range 11) '(100))) " ).should == "155"
1086
+ @nendo.replStr( " (map (lambda (x) (* x 2)) '(1 2 3)) " ).should == "(2 4 6)"
1087
+ @nendo.replStr( " (map (lambda (x) (+ x 1)) '(1 2 3)) " ).should == "(2 3 4)"
1088
+ @nendo.replStr( " (map (lambda (a b) (+ a b)) '(1 2 3) '(10 20 30)) " ).should == "(11 22 33)"
1089
+ @nendo.replStr( " (map (lambda (a b) (- b a)) '(1 2 3) '(10 20 30)) " ).should == "(9 18 27)"
1090
+ @nendo.replStr( " (map (lambda (a b c) (+ a b c)) '(1 2 3) '(10 20 30) '(100 200 300)) " ).should == "(111 222 333)"
1091
+ @nendo.replStr( " (define _result"+
1092
+ " (map"+
1093
+ " (lambda (x)"+
1094
+ " (* x 2))"+
1095
+ " (range 10000 1)))"+
1096
+ " (list"+
1097
+ " (first _result)"+
1098
+ " (second _result)"+
1099
+ " (last-pair _result))" ).should == "(2 4 (20000))"
1100
+ @nendo.replStr( " (define _lst '()) (for-each (lambda (x) (set! _lst (cons (* x 2) _lst))) '(1 2 3)) _lst" ).should == "(6 4 2)"
1101
+ @nendo.replStr( " (define _lst '()) (for-each (lambda (x) (set! _lst (cons (+ x 1) _lst))) '(1 2 3)) _lst" ).should == "(4 3 2)"
1102
+ @nendo.replStr( " (define _lst '()) (for-each (lambda (a b) (set! _lst (cons (cons a b) _lst))) '(1 2 3) '(10 20 30)) _lst" ).should ==
1103
+ "((3 . 30) (2 . 20) (1 . 10))"
1104
+ @nendo.replStr( " (define _cnt 0) (for-each (lambda (x) (set! _cnt (+ _cnt 1))) (range 10000)) _cnt" ).should == "10000"
1105
+ @nendo.replStr( " (filter (lambda (x) (= x 100)) '(1 2 3)) " ).should == "()"
1106
+ @nendo.replStr( " (filter (lambda (x) (= x 2)) '(1 2 3)) " ).should == "(2)"
1107
+ @nendo.replStr( " (filter (lambda (x) (not (= x 2))) '(1 2 3)) " ).should == "(1 3)"
1108
+ @nendo.replStr( " (filter (lambda (x) (if (= x 2) (* x 100) false)) '(1 2 3)) " ).should == "(2)"
1109
+ @nendo.replStr( " (find (lambda (x) (= x 100)) '(1 2 3)) " ).should == "#f"
1110
+ @nendo.replStr( " (find (lambda (x) (= x 2)) '(1 2 3)) " ).should == "2"
1111
+ @nendo.replStr( " (find (lambda (x) (not (= x 2))) '(1 2 3)) " ).should == "1"
1112
+ @nendo.replStr( " (find (lambda (x) (if (= x 2) (* x 100) false)) '(1 2 3)) " ).should == "2"
1113
+ @nendo.replStr( " (filter-map (lambda (x) (= x 100)) '(1 2 3)) " ).should == "()"
1114
+ @nendo.replStr( " (filter-map (lambda (x) (= x 2)) '(1 2 3)) " ).should == "(#t)"
1115
+ @nendo.replStr( " (filter-map (lambda (x) (not (= x 2))) '(1 2 3)) " ).should == "(#t #t)"
1116
+ @nendo.replStr( " (filter-map (lambda (x) (if (= x 2) (* x 10) false)) '(1 2 3)) " ).should == "(20)"
1117
+ @nendo.replStr( " (filter-map (lambda (x) (if (not (= x 2)) (* x 10) false)) '(1 2 3)) " ).should == "(10 30)"
1118
+ @nendo.replStr( " " +
1119
+ "(let1 result '()" +
1120
+ " (do" +
1121
+ " ((i 0 (+ i 1)))" +
1122
+ " ((< 10 i) #f)" +
1123
+ " (set! result (cons i result)))" +
1124
+ " (reverse result))" ).should == "(0 1 2 3 4 5 6 7 8 9 10)"
1125
+ @nendo.replStr( " " +
1126
+ "(let1 result '()" +
1127
+ " (do" +
1128
+ " ((i 0 (+ i 3)))" +
1129
+ " ((< (* 3 10) i) #f)" +
1130
+ " (set! result (cons i result)))" +
1131
+ " (reverse result))" ).should == "(0 3 6 9 12 15 18 21 24 27 30)"
1132
+ end
1133
+ end
1134
+
1135
+ describe Nendo, "when use values " do
1136
+ before do
1137
+ @nendo = Nendo::Core.new()
1138
+ @nendo.loadInitFile
1139
+ end
1140
+ it "should" do
1141
+ @nendo.replStr( " (values? (make-values '())) " ).should == "#t"
1142
+ lambda { @nendo.replStr( " (make-values '(1))) " ) }.should raise_error(ArgumentError)
1143
+ @nendo.replStr( " (values? (make-values '(1 2))) " ).should == "#t"
1144
+ @nendo.replStr( " (values? (make-values '(1 2 3))) " ).should == "#t"
1145
+ @nendo.replStr( " (values? (values)) " ).should == "#t"
1146
+ @nendo.replStr( " (values? (values 1)) " ).should == "#f"
1147
+ @nendo.replStr( " (values 1) " ).should == "1"
1148
+ @nendo.replStr( " (values? (values 1 2)) " ).should == "#t"
1149
+ @nendo.replStr( " (values? (values 1 2 3)) " ).should == "#t"
1150
+ @nendo.replStr( " (values? (values '(a) \"b\" '(\"C\"))) " ).should == "#t"
1151
+ @nendo.replStr( " (values-values (values)) " ).should == "()"
1152
+ lambda { @nendo.replStr( " (values-values (values 1)) " ) }.should raise_error(TypeError)
1153
+ @nendo.replStr( " (values-values (values 1 2)) " ).should == "(1 2)"
1154
+ @nendo.replStr( " (values-values (values 1 2 3)) " ).should == "(1 2 3)"
1155
+ @nendo.replStr( " (values-values (values '(a) \"b\" '(\"C\"))) " ).should == '((a) "b" ("C"))'
1156
+ @nendo.replStr( "" +
1157
+ " (call-with-values" +
1158
+ " (lambda () (values 4 5)) " +
1159
+ " (lambda (a b) b))" ).should == "5"
1160
+ @nendo.replStr( "" +
1161
+ " (call-with-values" +
1162
+ " (lambda () (values 1 2)) " +
1163
+ " cons)" ).should == "(1 . 2)"
1164
+ @nendo.replStr( "" +
1165
+ " (call-with-values" +
1166
+ " (lambda () (values 10)) " +
1167
+ " list)" ).should == "(10)"
1168
+ @nendo.replStr( "" +
1169
+ " (call-with-values" +
1170
+ " (lambda () (values)) " +
1171
+ " list)" ).should == "()"
1172
+ @nendo.replStr( " (call-with-values * -) " ).should == "-1"
1173
+ @nendo.replStr( " (receive (a) (values) (list a)) " ).should == "()"
1174
+ @nendo.replStr( " (receive (a) (values 10) (list a)) " ).should == "(10)"
1175
+ @nendo.replStr( " (receive (a b) (values 10 20) (list a b)) " ).should == "(10 20)"
1176
+ @nendo.replStr( " (receive (a b c) (values 10 20 30) (list a b c)) " ).should == "(10 20 30)"
1177
+ @nendo.replStr( " (receive (a . b) (values 10) (list a b)) " ).should == "(10 ())"
1178
+ @nendo.replStr( " (receive (a . b) (values 10 20) (list a b)) " ).should == "(10 (20))"
1179
+ @nendo.replStr( " (receive (a . b) (values 10 20 30) (list a b)) " ).should == "(10 (20 30))"
1180
+ @nendo.replStr( " (receive all (values) all) " ).should == "()"
1181
+ @nendo.replStr( " (receive all (values 10) all) " ).should == "(10)"
1182
+ @nendo.replStr( " (receive all (values 10 20) all) " ).should == "(10 20)"
1183
+ @nendo.replStr( " (receive all (values 10 20 30) all) " ).should == "(10 20 30)"
1184
+ @nendo.replStr( " (receive (a b) (values '(1) '(2)) (list a b)) " ).should == "((1) (2))"
1185
+ @nendo.replStr( " (receive (a b) (values '() '(2)) (list a b)) " ).should == "(() (2))"
1186
+ @nendo.replStr( " (receive (a b) (values '(1) '()) (list a b)) " ).should == "((1) ())"
1187
+ @nendo.replStr( " (receive (a b) (values #t #t) (cons a b)) " ).should == "(#t . #t)"
1188
+ @nendo.replStr( " (receive (a b) (values #t #f) (cons a b)) " ).should == "(#t . #f)"
1189
+ @nendo.replStr( " (receive (a b) (values nil #t) (cons a b)) " ).should == "(nil . #t)"
1190
+ @nendo.replStr( " (receive (a b) (values nil #f) (cons a b)) " ).should == "(nil . #f)"
1191
+ @nendo.replStr( " (receive (a b) (values nil nil) (cons a b)) " ).should == "(nil . nil)"
1192
+ end
1193
+ end
1194
+
1195
+ describe Nendo, "when toplevel variable was overwritten " do
1196
+ before do
1197
+ @nendo = Nendo::Core.new()
1198
+ @nendo.loadInitFile
1199
+ end
1200
+ it "should" do
1201
+ @nendo.replStr( " (define a 1) a" ).should == "1"
1202
+ lambda { @nendo.replStr( " (define (c-func) (+ a b)) (c-func)" ) }.should raise_error( NameError )
1203
+ @nendo.replStr( " (define b 2) b" ).should == "2"
1204
+ @nendo.replStr( " (c-func) " ).should == "3"
1205
+ @nendo.replStr( " (define b 20) " ).should == "20"
1206
+ @nendo.replStr( " (c-func) " ).should == "21"
1207
+
1208
+ @nendo.replStr( " (define (a-func) 10) (a-func)" ).should == "10"
1209
+ lambda { @nendo.replStr( " (define (c-func) (* (a-func) (b-func))) (c-func)" ) }.should raise_error( NameError )
1210
+ @nendo.replStr( " (define (b-func) 20) (b-func)" ).should == "20"
1211
+ @nendo.replStr( " (c-func) " ).should == "200"
1212
+ @nendo.replStr( " (define (b-func) 200) (b-func)" ).should == "200"
1213
+ @nendo.replStr( " (c-func) " ).should == "2000"
1214
+ end
1215
+ end
1216
+
1217
+ describe Nendo, "when use quasiquote macro " do
1218
+ before do
1219
+ @nendo = Nendo::Core.new()
1220
+ @nendo.loadInitFile
1221
+ end
1222
+ it "should" do
1223
+ @nendo.replStr( " '(a b c) " ).should == "(a b c)"
1224
+ @nendo.replStr( " `(a b c) " ).should == "(a b c)"
1225
+ @nendo.replStr( " `(1 2 3) " ).should == "(1 2 3)"
1226
+ @nendo.replStr( " (set! a 3) `(1 2 ,a) " ).should == "(1 2 3)"
1227
+ @nendo.replStr( " (set! a 3) `(1 2 ,@(list a)) " ).should == "(1 2 3)"
1228
+ @nendo.replStr( " (set! a 3) `(1 ,@(list 2 a)) " ).should == "(1 2 3)"
1229
+ @nendo.replStr( " (set! a 11) `,a " ).should == "11"
1230
+ @nendo.replStr( " (set! a 12) ``,a " ).should == "`,a"
1231
+ @nendo.replStr( " `(list ,(+ 1 2) 4) " ).should == "(list 3 4)"
1232
+ @nendo.replStr( " (let ((name 'a)) `(list ,name ',name)) " ).should == "(list a 'a)"
1233
+ @nendo.replStr( " `(a `(b ,(+ 1 2) ,(foo ,(+ 1 3) d) e) f) " ).should == "(a `(b ,(+ 1 2) ,(foo 4 d) e) f)"
1234
+ @nendo.replStr( " `(( foo ,(- 10 3)) ,@(cdr '(c)) . ,(car '(cons))) " ).should == "((foo 7) . cons)"
1235
+ end
1236
+ end
1237
+
1238
+ describe Nendo, "when use macros made by quasiquote. " do
1239
+ before do
1240
+ @nendo = Nendo::Core.new()
1241
+ @nendo.loadInitFile
1242
+ end
1243
+ it "should" do
1244
+ @nendo.replStr( " (case (length '(1 )) ((1) \"one\") ((2) \"two\") (else \"else\")) " ).should == '"one"'
1245
+ @nendo.replStr( " (case (length '(1 2 )) ((1) \"one\") ((2) \"two\") (else \"else\")) " ).should == '"two"'
1246
+ @nendo.replStr( " (case (length '(1 2 3 )) ((1) \"one\") ((2) \"two\") (else \"else\")) " ).should == '"else"'
1247
+ @nendo.replStr( " (case (length '(1 2 3 4)) ((1) \"one\") ((2) \"two\") (else \"else\")) " ).should == '"else"'
1248
+ @nendo.replStr( " (case 100 ((1) \"one\") ((2) \"two\") (else \"else\")) " ).should == '"else"'
1249
+ @nendo.replStr( " (let* ((a 1) (b (+ a 2))) (cons a b)) " ).should == "(1 . 3)"
1250
+ @nendo.replStr( " (let* ((a 10) (b (* a 2))) (cons a b)) " ).should == "(10 . 20)"
1251
+ @nendo.replStr( " (let* ((a 10) (b (* a 2)) (c (* b 3))) (list a b c)) " ).should == "(10 20 60)"
1252
+ @nendo.replStr( " (begin0 1 2 3) " ).should == "1"
1253
+ @nendo.replStr( " (define a 2) (begin0 (set! a (* a 2)) (set! a (* a 2)) (set! a (* a 2)) ) " ).should == "4"
1254
+ @nendo.replStr( " (begin0 100) " ).should == "100"
1255
+ @nendo.replStr( " (begin0) " ).should == "#f"
1256
+ @nendo.replStr( " " +
1257
+ "(receive (a b)" +
1258
+ " (begin0" +
1259
+ " (values 1 2)" +
1260
+ " (values 10 20)" +
1261
+ " (values 100 200))" +
1262
+ " (cons a b))" ).should == "(1 . 2)"
1263
+ end
1264
+ end
1265
+
1266
+ describe Nendo, "when use define and lambda macro " do
1267
+ before do
1268
+ @nendo = Nendo::Core.new()
1269
+ @nendo.loadInitFile
1270
+ end
1271
+ it "should" do
1272
+ @nendo.replStr( " (macroexpand '(define (main argv) (newline) 0)) " ).should == "(define main (lambda (argv) (newline) 0))"
1273
+ @nendo.replStr( " (macroexpand '(define (main argv) (define (foo x) x) (+ 10 20) 0 (foo 111))) " ).should == "(define main (lambda (argv) (letrec ((foo (lambda (x) x))) (+ 10 20) (foo 111))))"
1274
+ @nendo.replStr( " (define (main argv) (define (foo x) x) (+ 10 20) 0 (foo 111)) (main) " ).should == "111"
1275
+ @nendo.replStr( " (define (main argv) (define (foo1 x) (+ 1 x)) (define (foo2 x) (+ 2 x)) (* (foo1 20) (foo2 30))) (main '()) " ).should == "672"
1276
+ @nendo.replStr( " (macroexpand '(define (main argv) 0)) " ).should == "(define main (lambda (argv) 0))"
1277
+ end
1278
+ end
1279
+
1280
+ describe Nendo, "when use macros expands some syntax. " do
1281
+ before do
1282
+ @nendo = Nendo::Core.new()
1283
+ @nendo.loadInitFile
1284
+ end
1285
+ it "should" do
1286
+ @nendo.replStr( "" +
1287
+ " (let1 total 0" +
1288
+ " (let loop ((cnt 10))" +
1289
+ " (if (< 0 cnt)" +
1290
+ " (begin" +
1291
+ " (set! total (+ total cnt))" +
1292
+ " (loop (- cnt 1)))))" +
1293
+ " total)" +
1294
+ "" ).should == "55"
1295
+ @nendo.replStr( "" +
1296
+ "(let label ((a 2)" +
1297
+ " (b 3))" +
1298
+ " (if (<= 9 (+ a b))" +
1299
+ " (* a b)" +
1300
+ " (label 4 5)))" +
1301
+ "" ).should == "20"
1302
+ @nendo.replStr( "" +
1303
+ "(macroexpand '(let loop ((x 1)) "+
1304
+ " 1"+
1305
+ " 2"+
1306
+ " (loop 100)))" ).should == "(letrec ((loop (lambda (x) 1 2 (loop 100)))) (loop 1))"
1307
+ end
1308
+ end
1309
+
1310
+ describe Nendo, "when use dot-operator (.) macro " do
1311
+ before do
1312
+ @nendo = Nendo::Core.new()
1313
+ @nendo.loadInitFile
1314
+ end
1315
+ it "should" do
1316
+ @nendo.replStr( " (macroexpand '(. a b)) " ).should == "(a.b)"
1317
+ @nendo.replStr( " (macroexpand '(. a b c)) " ).should == "(a.b c)"
1318
+ @nendo.replStr( " (macroexpand '(. Kernel open)) " ).should == "(Kernel.open)"
1319
+ lambda { @nendo.replStr( " (macroexpand '(. open)) " ) }.should raise_error( ArgumentError )
1320
+ lambda { @nendo.replStr( " (macroexpand '(. open \"aaa\")) " ) }.should raise_error( TypeError )
1321
+ @nendo.replStr( " (macroexpand '(. a size)) " ).should == "(a.size)"
1322
+ @nendo.replStr( " (macroexpand '(. (. a size) to_s)) " ).gsub( /10[0-9][0-9][0-9]/, "10000" ).should == "(let ((__gensym__fb4e25e49e9fb4e46342224606faf2e3eabf1251_10000 (a.size))) (__gensym__fb4e25e49e9fb4e46342224606faf2e3eabf1251_10000.to_s))"
1323
+ @nendo.replStr( " (macroexpand '(. (. (. a size) to_s) to_i)) " ).gsub( /10[0-9][0-9][0-9]/, "10000" ).should == "(let ((__gensym__fb4e25e49e9fb4e46342224606faf2e3eabf1251_10000 (let ((__gensym__fb4e25e49e9fb4e46342224606faf2e3eabf1251_10000 (a.size))) (__gensym__fb4e25e49e9fb4e46342224606faf2e3eabf1251_10000.to_s)))) (__gensym__fb4e25e49e9fb4e46342224606faf2e3eabf1251_10000.to_i))"
1324
+ lambda { @nendo.replStr( " (macroexpand '(. (. a size))) " ) }.should raise_error( ArgumentError )
1325
+ @nendo.replStr( " (set! str \"str\") str.size " ).should == "3"
1326
+ @nendo.replStr( " (set! str \"str\") (. str size) " ).should == "3"
1327
+ @nendo.replStr( " (set! str \"str\") (+ 1 (. str size)) " ).should == "4"
1328
+ @nendo.replStr( " (set! str \"string\") (. (. str size) to_s) " ).should == '"6"'
1329
+ @nendo.replStr( " (to-s str.size) " ).should == '"6"'
1330
+ @nendo.replStr( " (to-s 'str.size) " ).should == '"str.size"'
1331
+ @nendo.replStr( " (require \"date\") " ).should == "#f"
1332
+ @nendo.replStr( " (. (Date.new 0) strftime \"%x\") " ).should == '"01/01/00"'
1333
+ @nendo.replStr( " (. (Date.new 0) strftime \"%s\") " ).should == '"-62167392000"'
1334
+ @nendo.replStr( " (require \"digest/md5\") " ).should == "#f"
1335
+ @nendo.replStr( " (Digest::MD5.hexdigest \"abc\") " ).should == '"900150983cd24fb0d6963f7d28e17f72"'
1336
+ @nendo.replStr( " (Digest::MD5.hexdigest \"source text\") " ).should == '"20f79a1416626eeacc0bd9a8db87faa2"'
1337
+ @nendo.replStr( " (define a 1) (a.is_a? Fixnum) " ).should == "#t"
1338
+ @nendo.replStr( " (define a 1) (a.is_a? Float) " ).should == "#f"
1339
+ @nendo.replStr( " (define a 1) (a.is_a? String) " ).should == "#f"
1340
+ @nendo.replStr( " (define a 1.1) (a.is_a? Fixnum) " ).should == "#f"
1341
+ @nendo.replStr( " (define a 1.1) (a.is_a? Float) " ).should == "#t"
1342
+ @nendo.replStr( " (define a 1.1) (a.is_a? String) " ).should == "#f"
1343
+ @nendo.replStr( " (define a \"s\") (a.is_a? Fixnum) " ).should == "#f"
1344
+ @nendo.replStr( " (define a \"s\") (a.is_a? Float) " ).should == "#f"
1345
+ @nendo.replStr( " (define a \"s\") (a.is_a? String) " ).should == "#t"
1346
+ end
1347
+ end
1348
+
1349
+ describe Nendo, "when use keyword feature " do
1350
+ before do
1351
+ @nendo = Nendo::Core.new()
1352
+ @nendo.loadInitFile
1353
+ end
1354
+ it "should" do
1355
+ @nendo.replStr( " (keyword? :a) " ).should == "#t"
1356
+ @nendo.replStr( ' (keyword? (intern ":a")) ' ).should == "#f"
1357
+ @nendo.replStr( ' (symbol? (intern ":a")) ' ).should == "#t"
1358
+ @nendo.replStr( " (keyword? ':a) " ).should == "#t"
1359
+ @nendo.replStr( " (symbol? ':a) " ).should == "#f"
1360
+ @nendo.replStr( " (eq? :a :a) " ).should == "#t"
1361
+ @nendo.replStr( " (eqv? :a :a) " ).should == "#t"
1362
+ @nendo.replStr( ' (eq? :a (intern ":a")) ' ).should == "#f"
1363
+ @nendo.replStr( ' (eqv? :a (intern ":a")) ' ).should == "#f"
1364
+ @nendo.replStr( ' (keyword? (make-keyword "a")) ' ).should == "#t"
1365
+ @nendo.replStr( " :a " ).should == ":a"
1366
+ @nendo.replStr( " ::a " ).should == "::a"
1367
+ @nendo.replStr( " :::key " ).should == ":::key"
1368
+ @nendo.replStr( ' (make-keyword "a") ' ).should == ":a"
1369
+ @nendo.replStr( ' (make-keyword ":a") ' ).should == "::a"
1370
+ @nendo.replStr( " (make-keyword 'a) " ).should == ":a"
1371
+ @nendo.replStr( " (keyword->string :a) " ).should == '"a"'
1372
+ @nendo.replStr( " (keyword->string :abcde) " ).should == '"abcde"'
1373
+ lambda { @nendo.replStr( " (keyword->string 'a) " ) }.should raise_error( TypeError )
1374
+ @nendo.replStr( " : " ).should == ':'
1375
+ @nendo.replStr( " (keyword->string :) " ).should == '""'
1376
+ @nendo.replStr( ' (make-keyword "") ' ).should == ":"
1377
+ @nendo.replStr( " (get-keyword :y '(:x 1 :y 2 :z 3)) " ).should == "2"
1378
+ @nendo.replStr( " (get-keyword 'z '(x 1 y 2 z 3)) " ).should == "3"
1379
+ lambda { @nendo.replStr( " (get-keyword 'z '(x 1 y 2 z)) " ) }.should raise_error( RuntimeError )
1380
+ lambda { @nendo.replStr( " (get-keyword :t '(:x 1 :y 2 :z 3)) " ) }.should raise_error( RuntimeError )
1381
+ @nendo.replStr( " (get-keyword :t '() #f) " ).should == "#f"
1382
+ @nendo.replStr( " (get-keyword :t '(:x) #f) " ).should == "#f"
1383
+ lambda { @nendo.replStr( " (get-keyword :t '()) " ) }.should raise_error( RuntimeError )
1384
+ lambda { @nendo.replStr( " (get-keyword :t '(:x)) " ) }.should raise_error( RuntimeError )
1385
+ lambda { @nendo.replStr( " (get-keyword :t 1) " ) }.should raise_error( RuntimeError )
1386
+ @nendo.replStr( " (get-keyword :t 1 #f) " ).should == "#f"
1387
+ end
1388
+ end
1389
+
1390
+ describe Nendo, "when use hash-table feature " do
1391
+ before do
1392
+ @nendo = Nendo::Core.new()
1393
+ @nendo.loadInitFile
1394
+ end
1395
+ it "should" do
1396
+ @nendo.replStr( " (define h (make-hash-table)) " ).should == "{}"
1397
+ @nendo.replStr( " (hash-table? 1) " ).should == "#f"
1398
+ @nendo.replStr( " (hash-table? '(1)) " ).should == "#f"
1399
+ @nendo.replStr( " (hash-table? (Array.new)) " ).should == "#f"
1400
+ @nendo.replStr( " (hash-table? (Hash.new)) " ).should == "#t"
1401
+ @nendo.replStr( " h " ).should == "{}"
1402
+ @nendo.replStr( " (hash-table-put! h 'k1 'v1) h" ).should == "{:k1=>:v1}"
1403
+ @nendo.replStr( " (hash-table-put! h 'k2 200) h" ).should == "{:k1=>:v1, :k2=>200}"
1404
+ @nendo.replStr( " (hash-table-get h 'k1)" ).should == "v1"
1405
+ @nendo.replStr( " (hash-table-get h 'k2)" ).should == "200"
1406
+ @nendo.replStr( " (hash-table-exist? h 'k1)" ).should == "#t"
1407
+ @nendo.replStr( " (hash-table-exist? h 'k2)" ).should == "#t"
1408
+ @nendo.replStr( " (hash-table-exist? h 'k3)" ).should == "#f"
1409
+ @nendo.replStr( " (hash-table-exist? h \"k1\")" ).should == "#f"
1410
+ @nendo.replStr( " (hash-table-num-entries h)" ).should == "2"
1411
+ @nendo.replStr( " (hash-table-delete! h 'k1)" ).should == "v1"
1412
+ lambda { @nendo.replStr( " (hash-table-get h 'k1)" ) }.should raise_error( RuntimeError )
1413
+ @nendo.replStr( " (hash-table-get h 'k1 #f) " ).should == "#f"
1414
+ @nendo.replStr( " (hash-table-num-entries h)" ).should == "1"
1415
+ @nendo.replStr( " (hash-table-clear! h)" ).should == "{}"
1416
+ @nendo.replStr( " (hash-table-num-entries h)" ).should == "0"
1417
+ @nendo.replStr( " (set! h (hash-table '(\"a\" \"AAA\") '(\"b\" \"BBB\") '(\"c\" \"CCC\"))) h" ).should == "{\"a\"=>\"AAA\", \"b\"=>\"BBB\", \"c\"=>\"CCC\"}"
1418
+ @nendo.replStr( " (set! h (hash-table '(\"a\" . \"AAA\") '(\"b\" . \"BBB\") '(\"c\" . \"CCC\"))) h" ).should == "{\"a\"=>\"AAA\", \"b\"=>\"BBB\", \"c\"=>\"CCC\"}"
1419
+ @nendo.replStr( " (hash-table-keys h)" ).should == '("a" "b" "c")'
1420
+ @nendo.replStr( " (hash-table-values h)" ).should == '("AAA" "BBB" "CCC")'
1421
+ @nendo.replStr( " (hash-table-map h (lambda (a b) (+ a b)))" ).should == '("aAAA" "bBBB" "cCCC")'
1422
+ @nendo.replStr( " (hash-table-for-each h (lambda (a b) (+ a b)))" ).should == '("aAAA" "bBBB" "cCCC")'
1423
+ @nendo.replStr( " (set! h (hash-table '(a AAA) '(b BBB))) h" ).should == "{:a=>:AAA, :b=>:BBB}"
1424
+ @nendo.replStr( " (set! h (hash-table '(a . AAA) '(b BBB))) h" ).should == "{:a=>:AAA, :b=>:BBB}"
1425
+ @nendo.replStr( " (set! h (hash-table '(a AAA) '(b . BBB))) h" ).should == "{:a=>:AAA, :b=>:BBB}"
1426
+ @nendo.replStr( " (define alist (hash-table->alist h)) alist" ).should == "((a . AAA) (b . BBB))"
1427
+ @nendo.replStr( " (define h2 (alist->hash-table alist)) h2" ).should == "{:a=>:AAA, :b=>:BBB}"
1428
+ @nendo.replStr( " (set! h (make-hash-table)) " ).should == "{}"
1429
+ @nendo.replStr( " (hash-table-push! h 'a :AAA_1) (hash-table->alist h)" ).should == "((a :AAA_1))"
1430
+ @nendo.replStr( " (hash-table-push! h 'a :AAA_2) (hash-table->alist h)" ).should == "((a :AAA_2 :AAA_1))"
1431
+ @nendo.replStr( " (hash-table-push! h 'b :BBB_1) (hash-table->alist h)" ).should == "((a :AAA_2 :AAA_1) (b :BBB_1))"
1432
+ @nendo.replStr( " (hash-table-push! h 'b :BBB_2) (hash-table->alist h)" ).should == "((a :AAA_2 :AAA_1) (b :BBB_2 :BBB_1))"
1433
+ end
1434
+ end
1435
+
1436
+ describe Nendo, "when use vector feature " do
1437
+ before do
1438
+ @nendo = Nendo::Core.new()
1439
+ @nendo.loadInitFile
1440
+ end
1441
+ it "should" do
1442
+ @nendo.replStr( " (define v (vector)) v" ).should == "#()"
1443
+ @nendo.replStr( " (vector? 1) " ).should == "#f"
1444
+ @nendo.replStr( " (vector? (vector)) " ).should == "#t"
1445
+ @nendo.replStr( " (vector? (vector 1)) " ).should == "#t"
1446
+ @nendo.replStr( " (vector? (vector 1 2)) " ).should == "#t"
1447
+ @nendo.replStr( " (vector? '#(1)) " ).should == "#t"
1448
+ @nendo.replStr( " (vector? '#(1 2)) " ).should == "#t"
1449
+ @nendo.replStr( " (vector? (Array.new)) " ).should == "#t"
1450
+ @nendo.replStr( " (vector? (Hash.new)) " ).should == "#f"
1451
+ @nendo.replStr( " (vector? 1.1) " ).should == "#f"
1452
+ @nendo.replStr( " (vector? \"str\") " ).should == "#f"
1453
+ @nendo.replStr( " (define v (make-vector 4))" ).should == "#(nil nil nil nil)"
1454
+ @nendo.replStr( " (vector-set! v 0 'v1) v" ).should == "#(v1 nil nil nil)"
1455
+ @nendo.replStr( " (vector-set! v 1 '100) v" ).should == "#(v1 100 nil nil)"
1456
+ @nendo.replStr( " (vector-set! v 2 '200) v" ).should == "#(v1 100 200 nil)"
1457
+ @nendo.replStr( " (vector-set! v 3 '(a b c)) v" ).should == "#(v1 100 200 (a b c))"
1458
+ @nendo.replStr( " (vector-length v)" ).should == "4"
1459
+ @nendo.replStr( " (vector-ref v 0) " ).should == "v1"
1460
+ @nendo.replStr( " (vector-ref v 1) " ).should == "100"
1461
+ @nendo.replStr( " (vector-ref v 2) " ).should == "200"
1462
+ @nendo.replStr( " (vector-ref v 3) " ).should == "(a b c)"
1463
+ lambda { @nendo.replStr( " (vector-ref v -1)" ) }.should raise_error( RuntimeError )
1464
+ lambda { @nendo.replStr( " (vector-ref v -2)" ) }.should raise_error( RuntimeError )
1465
+ lambda { @nendo.replStr( " (vector-ref v 5)" ) }.should raise_error( RuntimeError )
1466
+ lambda { @nendo.replStr( " (vector-ref v 6)" ) }.should raise_error( RuntimeError )
1467
+ @nendo.replStr( " (vector-ref v -1 1000)" ).should == "1000"
1468
+ @nendo.replStr( " (vector-ref v -2 2000)" ).should == "2000"
1469
+ @nendo.replStr( " (vector-ref v 5 3000)" ).should == "3000"
1470
+ @nendo.replStr( " (vector-ref v 6 4000)" ).should == "4000"
1471
+ @nendo.replStr( " (vector-ref v 7 #f)" ).should == "#f"
1472
+ @nendo.replStr( " (define v (make-vector 10)) v" ).should == "#(nil nil nil nil nil nil nil nil nil nil)"
1473
+ @nendo.replStr( " (vector->list v)" ).should == "(nil nil nil nil nil nil nil nil nil nil)"
1474
+ @nendo.replStr( " (define lst '(a b c (d)))" ).should == "(a b c (d))"
1475
+ @nendo.replStr( " (list->vector lst)" ).should == "#(a b c (d))"
1476
+ @nendo.replStr( " (list->vector (range 10 1))" ).should == "#(1 2 3 4 5 6 7 8 9 10)"
1477
+ end
1478
+ end
1479
+
1480
+ describe Nendo, "tail call optimization " do
1481
+ before do
1482
+ @nendo = Nendo::Core.new()
1483
+ @nendo.loadInitFile
1484
+ @nendo.loadInitFile # to self optimizing. The init.nnd file will be loaded twice, so `filter' can be optimized on second loading phase.
1485
+ end
1486
+ it "should" do
1487
+ @nendo.replStr( " (setup-tailcall-mark '(print \"abc\")) " ).should == "(%tailcall (print \"abc\"))"
1488
+ @nendo.replStr( " (setup-tailcall-mark '(begin (print \"abc\") 1 2 3)) " ).should == "(begin (print \"abc\") 1 2 3)"
1489
+ @nendo.replStr( " (setup-tailcall-mark '(begin 1 2 (print \"abc\") 3)) " ).should == "(begin 1 2 (print \"abc\") 3)"
1490
+ @nendo.replStr( " (setup-tailcall-mark '(begin 1 2 3 (print \"abc\"))) " ).should == "(begin 1 2 3 (%tailcall (print \"abc\")))"
1491
+ @nendo.replStr( "" +
1492
+ "(setup-tailcall-mark"+
1493
+ " '(lambda '(x)"+
1494
+ " 1"+
1495
+ " 2"+
1496
+ " (print \"abc\")))" ).should == "(lambda '(x) 1 2 (%tailcall (print \"abc\")))"
1497
+ @nendo.replStr( "" +
1498
+ "(setup-tailcall-mark"+
1499
+ " '(lambda (x)"+
1500
+ " 1"+
1501
+ " 2"+
1502
+ " (if #t"+
1503
+ " (begin 1 2 (print \"abc\"))"+
1504
+ " (begin 1 2 (print \"ABC\")))))" ).should == "(lambda (x) 1 2 (if #t (begin 1 2 (%tailcall (print \"abc\"))) (begin 1 2 (%tailcall (print \"ABC\")))))"
1505
+ @nendo.replStr( "(setup-tailcall-mark (macroexpand "+
1506
+ " '(define (foo) (foo))"+
1507
+ " ))" ).should == "(define foo (lambda () (%tailcall (foo))))"
1508
+ @nendo.replStr( "(setup-tailcall-mark (macroexpand "+
1509
+ " '(values? (make-values '()))"+
1510
+ " ))" ).should == "(%tailcall (values? (make-values '())))"
1511
+ @nendo.replStr( "(setup-tailcall-mark (macroexpand "+
1512
+ " '(cond (false 1) (false 2))"+
1513
+ " ))" ).should == "(if #f (begin 1) (if #f (begin 2) ()))"
1514
+ @nendo.replStr( "(setup-tailcall-mark (macroexpand "+
1515
+ " '(cond (false 1) (false 2) (else 3))"+
1516
+ " ))" ).should == "(if #f (begin 1) (if #f (begin 2) (if #t (begin 3) ())))"
1517
+ @nendo.replStr( "(setup-tailcall-mark (macroexpand "+
1518
+ " '(and (foo 1) (bar 2))"+
1519
+ " ))" ).should == "(if (not (eq? #f (foo 1))) (%tailcall (bar 2)) #f)"
1520
+ @nendo.replStr( "(setup-tailcall-mark (macroexpand "+
1521
+ " '(or (foo 1) (bar 2))"+
1522
+ " ))" ).gsub( /20[0-9][0-9][0-9]/, "20000" ).should == "(let ((__gensym__fb4e25e49e9fb4e46342224606faf2e3eabf1251_20000 (foo 1))) (if __gensym__fb4e25e49e9fb4e46342224606faf2e3eabf1251_20000 __gensym__fb4e25e49e9fb4e46342224606faf2e3eabf1251_20000 (let ((__gensym__fb4e25e49e9fb4e46342224606faf2e3eabf1251_20000 (bar 2))) (if __gensym__fb4e25e49e9fb4e46342224606faf2e3eabf1251_20000 __gensym__fb4e25e49e9fb4e46342224606faf2e3eabf1251_20000 #f))))"
1523
+ @nendo.replStr( "(setup-tailcall-mark (macroexpand "+
1524
+ " '(let loop ((x 1)) 1 2 (loop 100))"+
1525
+ " ))" ).should == "(letrec ((loop (lambda (x) 1 2 (%tailcall (loop 100))))) (%tailcall (loop 1)))"
1526
+ @nendo.replStr( "(setup-tailcall-mark (macroexpand "+
1527
+ " '(let1 aaa 111 aaa)"+
1528
+ " ))" ).should == "(let ((aaa 111)) aaa)"
1529
+ @nendo.replStr( "" +
1530
+ "(setup-tailcall-mark"+
1531
+ " '(letrec ((func1 "+
1532
+ " (lambda (x)"+
1533
+ " 1"+
1534
+ " (func2)))"+
1535
+ " (func2 "+
1536
+ " (lambda (x)"+
1537
+ " 2"+
1538
+ " (func1))))"+
1539
+ " (func1 100)))" ).should == "(letrec ((func1 (lambda (x) 1 (%tailcall (func2)))) (func2 (lambda (x) 2 (%tailcall (func1))))) (%tailcall (func1 100)))"
1540
+ @nendo.replStr( "(set-optimize-level 0) " ).should == "0"
1541
+ lambda { @nendo.replStr( "(filter (lambda (x) (< x 10)) (range 10000)) " ) }.should raise_error(SystemStackError)
1542
+ @nendo.replStr( "(set-optimize-level 1) " ).should == "1"
1543
+ @nendo.replStr( "(filter (lambda (x) (< x 10)) (range 10000)) " ).should == "(0 1 2 3 4 5 6 7 8 9)"
1544
+ @nendo.replStr( "(define str (if #t (car '(\"a\")) (car '(\"b\")))) (sprintf \"A%sZ\" str) " ).should == '"AaZ"'
1545
+ @nendo.replStr( "(letrec ((str (if #t (+ \"a\" \"A\") '()))) str.class) " ).should == 'String'
1546
+ @nendo.replStr( "(letrec ((str (if #t (+ \"a\" \"A\") '()))) (+ str \"...\")) " ).should == '"aA..."'
1547
+ end
1548
+ end
1549
+
1550
+
1551
+ describe Nendo, "optional argument parser " do
1552
+ before do
1553
+ @nendo = Nendo::Core.new()
1554
+ @nendo.loadInitFile
1555
+ end
1556
+ it "should" do
1557
+ @nendo.replStr( " (get-optional '() 100) " ).should == "100"
1558
+ @nendo.replStr( " (get-optional '(1) 100) " ).should == "1"
1559
+ @nendo.replStr( " (get-optional '(2) 100) " ).should == "2"
1560
+ @nendo.replStr( " (get-optional '(3 4) 100) " ).should == "3"
1561
+ @nendo.replStr( " (get-optional '() #t) " ).should == "#t"
1562
+ @nendo.replStr( " (get-optional '() #f) " ).should == "#f"
1563
+ end
1564
+ end
1565
+
1566
+