nendo 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/test/nendo_spec.rb CHANGED
@@ -2,24 +2,24 @@
2
2
  # -*- encoding: utf-8 -*-
3
3
  #
4
4
  # nendo_spec.rb - "RSpec file for nendo language"
5
- #
5
+ #
6
6
  # Copyright (c) 2009-2010 Kiyoka Nishiyama <kiyoka@sumibi.org>
7
- #
7
+ #
8
8
  # Redistribution and use in source and binary forms, with or without
9
9
  # modification, are permitted provided that the following conditions
10
10
  # are met:
11
- #
11
+ #
12
12
  # 1. Redistributions of source code must retain the above copyright
13
13
  # notice, this list of conditions and the following disclaimer.
14
- #
14
+ #
15
15
  # 2. Redistributions in binary form must reproduce the above copyright
16
16
  # notice, this list of conditions and the following disclaimer in the
17
17
  # documentation and/or other materials provided with the distribution.
18
- #
18
+ #
19
19
  # 3. Neither the name of the authors nor the names of its contributors
20
20
  # may be used to endorse or promote products derived from this
21
21
  # software without specific prior written permission.
22
- #
22
+ #
23
23
  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
24
  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
25
  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@@ -31,7 +31,7 @@
31
31
  # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32
32
  # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33
33
  # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
- #
34
+ #
35
35
  require 'nendo'
36
36
  include Nendo
37
37
 
@@ -56,7 +56,7 @@ describe Cell, "when initialized as '(100)" do
56
56
  before do
57
57
  @cell = Cell.new( 100 )
58
58
  end
59
-
59
+
60
60
  it "should" do
61
61
  @cell.isNull.should_not be_true
62
62
  @cell.length.should == 1
@@ -75,7 +75,7 @@ describe Cell, "when initialized as '(100 . 200)" do
75
75
  before do
76
76
  @cell = Cell.new( 100, 200 )
77
77
  end
78
-
78
+
79
79
  it "should" do
80
80
  @cell.isNull.should_not be_true
81
81
  @cell.isDotted.should be_true
@@ -95,7 +95,7 @@ describe Cell, "when initialized as '(cons 100 (cons 200 300)) " do
95
95
  before do
96
96
  @cell = Cell.new( 100, Cell.new( 200, 300 ))
97
97
  end
98
-
98
+
99
99
  it "should" do
100
100
  @cell.isNull.should_not be_true
101
101
  @cell.isDotted.should_not be_true
@@ -534,15 +534,15 @@ describe Nendo, "when use quote and syntax-quote " do
534
534
  @nendo.evalStr( " (quote (1 . 2)) " ).should == "(1 . 2)"
535
535
  @nendo.evalStr( " (syntax-quote ())" ).should == "()"
536
536
  @nendo.evalStr( " (syntax-quote 1)" ).should == "1"
537
- @nendo.evalStr( " (syntax-quote \"str\") " ).should == '"str"'
537
+ @nendo.evalStr( ' (syntax-quote "str") ' ).should == '"str"'
538
538
  @nendo.evalStr( " (syntax-quote (1 . 2)) " ).should == "(1 . 2)"
539
- @nendo.evalStr( " (quote quote) " ).should == "'"
540
- @nendo.evalStr( " 'quote " ).should == "'"
541
- @nendo.evalStr( " ''1 " ).should == "'1"
542
- @nendo.evalStr( " (quote syntax-quote) " ).should == "syntax-quote"
543
- @nendo.evalStr( " (syntax-quote '1) " ).should == "'1"
544
- @nendo.evalStr( " (syntax-quote (quote 1)) " ).should == "'1"
545
- @nendo.evalStr( " (quote (syntax-quote 1)) " ).should == "(syntax-quote 1)"
539
+ @nendo.evalStr( " (quote quote) " ).should == "quote"
540
+ @nendo.evalStr( " 'quote " ).should == "quote"
541
+ @nendo.evalStr( " ''1 " ).should == "(quote 1)"
542
+ @nendo.evalStr( " (quote syntax-quote) " ).should == "quote"
543
+ @nendo.evalStr( " (syntax-quote '1) " ).should == "(quote 1)"
544
+ @nendo.evalStr( " (syntax-quote (quote 1)) " ).should == "(quote 1)"
545
+ @nendo.evalStr( " (quote (syntax-quote 1)) " ).should == "(quote 1)"
546
546
  end
547
547
  end
548
548
 
@@ -565,9 +565,9 @@ describe Nendo, "when read various list expressions" do
565
565
  @nendo.evalStr( " '[a (b) ((c)) (((d)))] " ).should == "(a (b) ((c)) (((d))))"
566
566
  @nendo.evalStr( " '(a [b] ([c]) (([d]))) " ).should == "(a (b) ((c)) (((d))))"
567
567
  @nendo.evalStr( " '[a [b] [[c]] [[[d]]]] " ).should == "(a (b) ((c)) (((d))))"
568
- @nendo.evalStr( " '('a)" ).should == "('a)"
569
- @nendo.evalStr( " '(''a)" ).should == "(''a)"
570
- @nendo.evalStr( " '('a 'b 'c)" ).should == "('a 'b 'c)"
568
+ @nendo.evalStr( " '('a)" ).should == "((quote a))"
569
+ @nendo.evalStr( " '(''a)" ).should == "((quote (quote a)))"
570
+ @nendo.evalStr( " '('a 'b 'c)" ).should == "((quote a) (quote b) (quote c))"
571
571
  @nendo.evalStr( ' \'("str") ' ).should == '("str")'
572
572
  @nendo.evalStr( ' \'("str" . 1) ' ).should == '("str" . 1)'
573
573
  @nendo.evalStr( ' \'(1 . "str") ' ).should == '(1 . "str")'
@@ -577,13 +577,13 @@ describe Nendo, "when read various list expressions" do
577
577
  @nendo.evalStr( " 'symbol " ).should == "symbol"
578
578
  @nendo.evalStr( " 'SYMBOL " ).should == "SYMBOL"
579
579
  @nendo.evalStr( " 'SyMbOl " ).should == "SyMbOl"
580
- @nendo.evalStr( " ''a " ).should == "'a"
580
+ @nendo.evalStr( " ''a " ).should == "(quote a)"
581
581
  @nendo.evalStr( " '1 " ).should == "1"
582
- @nendo.evalStr( " ''1 " ).should == "'1"
583
- @nendo.evalStr( " '''1 " ).should == "''1"
582
+ @nendo.evalStr( " ''1 " ).should == "(quote 1)"
583
+ @nendo.evalStr( " '''1 " ).should == "(quote (quote 1))"
584
584
  @nendo.evalStr( " '1.1 " ).should == "1.1"
585
- @nendo.evalStr( " ''1.1 " ).should == "'1.1"
586
- @nendo.evalStr( " '''1.1 " ).should == "''1.1"
585
+ @nendo.evalStr( " ''1.1 " ).should == "(quote 1.1)"
586
+ @nendo.evalStr( " '''1.1 " ).should == "(quote (quote 1.1))"
587
587
  @nendo.evalStr( " '() " ).should == "()"
588
588
  @nendo.evalStr( " '(()) " ).should == "(())"
589
589
  @nendo.evalStr( " '((())) " ).should == "((()))"
@@ -594,7 +594,7 @@ describe Nendo, "when read various list expressions" do
594
594
  @nendo.evalStr( " '(a . #f) " ).should == "(a . #f)"
595
595
  @nendo.evalStr( " '(a . nil) " ).should == "(a . nil)"
596
596
  @nendo.evalStr( " '(a b c d e . ()) " ).should == "(a b c d e)"
597
- @nendo.evalStr( " '(#t #t #f #f nil nil '() '()) " ).should == "(#t #t #f #f nil nil '() '())"
597
+ @nendo.evalStr( " '(#t #t #f #f nil nil '() '()) " ).should == "(#t #t #f #f nil nil (quote ()) (quote ()))"
598
598
  end
599
599
  end
600
600
 
@@ -686,6 +686,10 @@ describe Nendo, "when call evalStr() with built-in functions" do
686
686
  @nendo.evalStr( " (list '(a) '((b c))) " ).should == "((a) ((b c)))"
687
687
  @nendo.evalStr( " (list) " ).should == "()"
688
688
  @nendo.evalStr( " (list 1) " ).should == "(1)"
689
+ @nendo.evalStr( " (reverse '(1)) " ).should == "(1)"
690
+ @nendo.evalStr( " (reverse '(1 2 3)) " ).should == "(3 2 1)"
691
+ @nendo.evalStr( " (reverse '(1 2 ())) " ).should == "(() 2 1)"
692
+ @nendo.evalStr( " (reverse (list 1 2 (cdr '(100)))) " ).should == "(() 2 1)"
689
693
  @nendo.evalStr( " (define !a 10) !a" ).should == "10"
690
694
  @nendo.evalStr( " (define $a 11) $a" ).should == "11"
691
695
  @nendo.evalStr( " (define %a 12) %a" ).should == "12"
@@ -865,21 +869,40 @@ describe Nendo, "when call evalStr() with built-in special forms" do
865
869
  @nendo.evalStr( " (begin 1) " ).should == "1"
866
870
  @nendo.evalStr( " (begin 1 2) " ).should == "2"
867
871
  @nendo.evalStr( " (begin 1 2 3) " ).should == "3"
868
- @nendo.evalStr( " (set! x 2) (set! y (begin (set! x (* x 2)) (set! x (* x 2)) (set! x (* x 2)) 100)) (+ x y)" ).should == "116"
869
- @nendo.evalStr( " (let () 100) " ).should == "100"
870
- @nendo.evalStr( " (let ((a 11)) a) " ).should == "11"
871
- @nendo.evalStr( " (let ((a 11) (b 22)) (+ a b)) " ).should == "33"
872
- @nendo.evalStr( " (let ((a 22)) (let ((b 33)) (+ a b))) " ).should == "55"
873
- @nendo.evalStr( " (let ((a 22)(b 33)) (let ((c 44) (d 55)) (+ a b c d))) " ).should == "154"
874
- @nendo.evalStr( " (let ((a (let ((b 2)) (+ 100 b)))) a) " ).should == "102"
872
+ @nendo.evalStr( <<EOS
873
+ (set! x 2)
874
+ (set! y (begin
875
+ (set! x (* x 2))
876
+ (set! x (* x 2))
877
+ (set! x (* x 2))
878
+ 100))
879
+ (+ x y)
880
+ EOS
881
+ ).should == "116"
882
+ @nendo.evalStr( " (%let () 100) " ).should == "100"
883
+ @nendo.evalStr( " (%let ((a 11)) a) " ).should == "11"
884
+ @nendo.evalStr( " (%let ((a 11) (b 22)) (+ a b)) " ).should == "33"
885
+ @nendo.evalStr( " (%let ((a 22)) (%let ((b 33)) (+ a b))) " ).should == "55"
886
+ @nendo.evalStr( " (%let ((a 22)(b 33)) (%let ((c 44) (d 55)) (+ a b c d))) " ).should == "154"
887
+ @nendo.evalStr( " (%let ((a (%let ((b 2)) (+ 100 b)))) a) " ).should == "102"
875
888
  @nendo.evalStr( " (letrec () 100) " ).should == "100"
876
889
  @nendo.evalStr( " (letrec ((a 11)) a) " ).should == "11"
877
890
  @nendo.evalStr( " (letrec ((a 11) (b 22)) (+ a b)) " ).should == "33"
878
- @nendo.evalStr( " (letrec ((a 22)) (let ((b 33)) (+ a b))) " ).should == "55"
879
- @nendo.evalStr( " (letrec ((a 22)(b 33)) (let ((c 44) (d 55)) (+ a b c d))) " ).should == "154"
880
- @nendo.evalStr( " (letrec ((a (let ((b 2)) (+ 100 b)))) a) " ).should == "102"
881
- @nendo.evalStr( " (letrec ( (func1 (lambda (x) 13)) (func2 (lambda (x) (* 2 (func1)))) ) (list (func2) (func1))) " ).should == "(26 13)"
882
- @nendo.evalStr( " (letrec ( (func2 (lambda (x) (* 2 (func1)))) (func1 (lambda (x) 7)) ) (list (func2) (func1))) " ).should == "(14 7)"
891
+ @nendo.evalStr( " (letrec ((a 22)) (%let ((b 33)) (+ a b))) " ).should == "55"
892
+ @nendo.evalStr( " (letrec ((a 22)(b 33)) (%let ((c 44) (d 55)) (+ a b c d))) " ).should == "154"
893
+ @nendo.evalStr( " (letrec ((a (%let ((b 2)) (+ 100 b)))) a) " ).should == "102"
894
+ @nendo.evalStr( <<EOS
895
+ (letrec ((func1 (lambda (x) 13))
896
+ (func2 (lambda (x) (* 2 (func1)))))
897
+ (list (func2) (func1)))
898
+ EOS
899
+ ).should == "(26 13)"
900
+ @nendo.evalStr( <<EOS
901
+ (letrec ((func2 (lambda (x) (* 2 (func1))))
902
+ (func1 (lambda (x) 7)))
903
+ (list (func2) (func1)))
904
+ EOS
905
+ ).should == "(14 7)"
883
906
  @nendo.evalStr( " (if true 't 'f)" ).should == "t"
884
907
  @nendo.evalStr( " (if true '(1) '(2))" ).should == "(1)"
885
908
  @nendo.evalStr( " (if false 't 'f)" ).should == "f"
@@ -887,11 +910,18 @@ describe Nendo, "when call evalStr() with built-in special forms" do
887
910
  @nendo.evalStr( " (set! x 0) (if true (set! x 1) (set! x 2)) x" ).should == "1"
888
911
  @nendo.evalStr( " (set! x 0) (if false (set! x 1) (set! x 2)) x" ).should == "2"
889
912
  @nendo.evalStr( " (set! func (lambda (arg1) arg1)) (list (func 1) (func 2))" ).should == "(1 2)"
890
- @nendo.evalStr( " ((lambda (arg1) arg1) 3)" ).should == "3"
891
- @nendo.evalStr( " ((lambda (arg1) arg1) (+ 1 2 3))" ).should == "6"
892
- @nendo.evalStr( " ((if #t + *) 3 4)" ).should == "7"
893
- @nendo.evalStr( " ((if #f + *) 3 4)" ).should == "12"
894
- @nendo.evalStr( " (apply1 + '(1 2))" ).should == "3"
913
+ @nendo.evalStr( " ((lambda (arg1) arg1) 3)" ).should == "3"
914
+ @nendo.evalStr( " ((lambda (arg1) arg1) (+ 1 2 3))" ).should == "6"
915
+ @nendo.evalStr( " ((lambda (arg1 . arg2) arg1) 1 '(2))" ).should == "1"
916
+ @nendo.evalStr( " ((lambda (arg1 . arg2) arg2) 1 '(2))" ).should == "((2))"
917
+ @nendo.evalStr( " ((lambda (arg1 . arg2) arg2) 1 '(2 3))" ).should == "((2 3))"
918
+ @nendo.evalStr( " ((lambda (arg1 . arg2) arg1) '() '())" ).should == "()"
919
+ @nendo.evalStr( " ((lambda (arg1 . arg2) arg2) '() '())" ).should == "(())"
920
+ @nendo.evalStr( " ((lambda (arg1 . arg2) arg1) (cdr '(100)) (cdr '(200)))" ).should == "()"
921
+ @nendo.evalStr( " ((lambda (arg1 . arg2) arg2) (cdr '(100)) (cdr '(200)))" ).should == "(())"
922
+ @nendo.evalStr( " ((if #t + *) 3 4)" ).should == "7"
923
+ @nendo.evalStr( " ((if #f + *) 3 4)" ).should == "12"
924
+ @nendo.evalStr( " (apply1 + '(1 2))" ).should == "3"
895
925
  @nendo.evalStr( " (apply1 + (range 10 1))" ).should == "55"
896
926
  @nendo.evalStr( " (apply1 cons '(1 2))" ).should == "(1 . 2)"
897
927
  @nendo.evalStr( " (apply1 list '(1 2 3))" ).should == "(1 2 3)"
@@ -909,32 +939,55 @@ describe Nendo, "when call evalStr() with built-in special forms (renamed symbol
909
939
  @nendo.evalStr( " (/nendo/core/begin 1) " ).should == "1"
910
940
  @nendo.evalStr( " (/nendo/core/begin 1 2) " ).should == "2"
911
941
  @nendo.evalStr( " (/nendo/core/begin 1 2 3) " ).should == "3"
912
- @nendo.evalStr( " (/nendo/core/set! x 2) (/nendo/core/set! y (/nendo/core/begin (/nendo/core/set! x (* x 2)) (/nendo/core/set! x (* x 2)) (/nendo/core/set! x (* x 2)) 100)) (+ x y)" ).should == "116"
913
- @nendo.evalStr( " (/nendo/core/let () 100) " ).should == "100"
914
- @nendo.evalStr( " (/nendo/core/let ((a 11)) a) " ).should == "11"
915
- @nendo.evalStr( " (/nendo/core/let ((a 11) (b 22)) (+ a b)) " ).should == "33"
916
- @nendo.evalStr( " (/nendo/core/let ((a 22)) (let ((b 33)) (+ a b))) " ).should == "55"
917
- @nendo.evalStr( " (/nendo/core/let ((a 22)(b 33)) (let ((c 44) (d 55)) (+ a b c d))) " ).should == "154"
918
- @nendo.evalStr( " (/nendo/core/let ((a (let ((b 2)) (+ 100 b)))) a) " ).should == "102"
942
+ @nendo.evalStr( <<EOS
943
+ (/nendo/core/set! x 2)
944
+ (/nendo/core/set! y (/nendo/core/begin
945
+ (/nendo/core/set! x (* x 2))
946
+ (/nendo/core/set! x (* x 2))
947
+ (/nendo/core/set! x (* x 2))
948
+ 100))
949
+ (+ x y)
950
+ EOS
951
+ ).should == "116"
952
+ @nendo.evalStr( " (/nendo/core/%let () 100) " ).should == "100"
953
+ @nendo.evalStr( " (/nendo/core/%let ((a 11)) a) " ).should == "11"
954
+ @nendo.evalStr( " (/nendo/core/%let ((a 11) (b 22)) (+ a b)) " ).should == "33"
955
+ @nendo.evalStr( " (/nendo/core/%let ((a 22)) (let ((b 33)) (+ a b))) " ).should == "55"
956
+ @nendo.evalStr( " (/nendo/core/%let ((a 22)(b 33)) (let ((c 44) (d 55)) (+ a b c d))) " ).should == "154"
957
+ @nendo.evalStr( " (/nendo/core/%let ((a (let ((b 2)) (+ 100 b)))) a) " ).should == "102"
919
958
  @nendo.evalStr( " (/nendo/core/letrec () 100) " ).should == "100"
920
959
  @nendo.evalStr( " (/nendo/core/letrec ((a 11)) a) " ).should == "11"
921
960
  @nendo.evalStr( " (/nendo/core/letrec ((a 11) (b 22)) (+ a b)) " ).should == "33"
922
961
  @nendo.evalStr( " (/nendo/core/letrec ((a 22)) (let ((b 33)) (+ a b))) " ).should == "55"
923
962
  @nendo.evalStr( " (/nendo/core/letrec ((a 22)(b 33)) (let ((c 44) (d 55)) (+ a b c d))) " ).should == "154"
924
963
  @nendo.evalStr( " (/nendo/core/letrec ((a (let ((b 2)) (+ 100 b)))) a) " ).should == "102"
925
- @nendo.evalStr( " (/nendo/core/letrec ( (func1 (/nendo/core/lambda (x) 13)) (func2 (/nendo/core/lambda (x) (* 2 (func1)))) ) (list (func2) (func1))) " ).should == "(26 13)"
926
- @nendo.evalStr( " (/nendo/core/letrec ( (func2 (/nendo/core/lambda (x) (* 2 (func1)))) (func1 (/nendo/core/lambda (x) 7)) ) (list (func2) (func1))) " ).should == "(14 7)"
964
+ @nendo.evalStr( <<EOS
965
+ (/nendo/core/letrec ((func1 (/nendo/core/lambda (x) 13))
966
+ (func2 (/nendo/core/lambda (x) (* 2 (func1)))))
967
+ (list (func2) (func1)))
968
+ EOS
969
+ ).should == "(26 13)"
970
+ @nendo.evalStr( <<EOS
971
+ (/nendo/core/letrec ((func2 (/nendo/core/lambda (x) (* 2 (func1))))
972
+ (func1 (/nendo/core/lambda (x) 7)))
973
+ (list (func2) (func1)))
974
+ EOS
975
+ ).should == "(14 7)"
927
976
  @nendo.evalStr( " (/nendo/core/if true 't 'f)" ).should == "t"
928
977
  @nendo.evalStr( " (/nendo/core/if true '(1) '(2))" ).should == "(1)"
929
978
  @nendo.evalStr( " (/nendo/core/if false 't 'f)" ).should == "f"
930
979
  @nendo.evalStr( " (/nendo/core/if false '(1) '(2))" ).should == "(2)"
931
980
  @nendo.evalStr( " (/nendo/core/set! x 0) (/nendo/core/if true (set! x 1) (set! x 2)) x" ).should == "1"
932
981
  @nendo.evalStr( " (/nendo/core/set! x 0) (/nendo/core/if false (set! x 1) (set! x 2)) x" ).should == "2"
933
- @nendo.evalStr( " (/nendo/core/set! func (/nendo/core/lambda (arg1) arg1)) (list (func 1) (func 2))" ).should == "(1 2)"
934
- @nendo.evalStr( " ((/nendo/core/lambda (arg1) arg1) 3)" ).should == "3"
935
- @nendo.evalStr( " ((/nendo/core/lambda (arg1) arg1) (+ 1 2 3))" ).should == "6"
936
- @nendo.evalStr( " ((/nendo/core/if #t + *) 3 4)" ).should == "7"
937
- @nendo.evalStr( " ((/nendo/core/if #f + *) 3 4)" ).should == "12"
982
+ @nendo.evalStr( <<EOS
983
+ (/nendo/core/set! func (/nendo/core/lambda (arg1) arg1))
984
+ (list (func 1) (func 2))
985
+ EOS
986
+ ).should == "(1 2)"
987
+ @nendo.evalStr( " ((/nendo/core/lambda (arg1) arg1) 3)" ).should == "3"
988
+ @nendo.evalStr( " ((/nendo/core/lambda (arg1) arg1) (+ 1 2 3))" ).should == "6"
989
+ @nendo.evalStr( " ((/nendo/core/if #t + *) 3 4)" ).should == "7"
990
+ @nendo.evalStr( " ((/nendo/core/if #f + *) 3 4)" ).should == "12"
938
991
  lambda { @nendo.evalStr( " (/nendo/core/error \"My Runtime Error\") " ) }.should raise_error( RuntimeError )
939
992
  end
940
993
  end
@@ -946,26 +999,28 @@ describe Nendo, "when call evalStr() with global and lexical scope variable" do
946
999
  end
947
1000
  it "should" do
948
1001
  @nendo.evalStr( " (define var 111) " ).should == "111"
949
- @nendo.evalStr( " (let ((var 222)) var) " ).should == "222"
950
- @nendo.evalStr( " (let ((var 222)) (set! var 333) var) " ).should == "333"
951
- @nendo.evalStr( " (let ((var 222)) (set! var 333)) var " ).should == "111"
1002
+ @nendo.evalStr( " (%let ((var 222)) var) " ).should == "222"
1003
+ @nendo.evalStr( " (%let ((var 222)) (set! var 333) var) " ).should == "333"
1004
+ @nendo.evalStr( " (%let ((var 222)) (set! var 333)) var " ).should == "111"
952
1005
  @nendo.evalStr( " (define global1 \"G\") " ).should == '"G"'
953
- @nendo.evalStr( " " +
954
- "(let ((local1 \"L\")" +
955
- " (local2 \"L\"))" +
956
- " (set! global1 (+ global1 \"lobal1\"))" +
957
- " (set! local1 (+ local1 \"ocal1\"))" +
958
- " (set! local2 (+ local2 \"ocal2\"))" +
959
- " (list global1" +
960
- " local1" +
961
- " local2" +
962
- " (let ((local1 \"A\")" +
963
- " (local2 \"B\"))" +
964
- " (set! local1 (+ local1 \"a\"))" +
965
- " (set! local2 (+ local2 \"b\"))" +
966
- " (list local1 local2" +
967
- " (let ((local1 \"CCC\"))" +
968
- " (list global1 local1 local2))))))" ).should == '("Global1" "Local1" "Local2" ("Aa" "Bb" ("Global1" "CCC" "Bb")))'
1006
+ @nendo.evalStr( <<EOS
1007
+ (%let ((local1 \"L\")
1008
+ (local2 \"L\"))
1009
+ (set! global1 (+ global1 \"lobal1\"))
1010
+ (set! local1 (+ local1 \"ocal1\"))
1011
+ (set! local2 (+ local2 \"ocal2\"))
1012
+ (list global1
1013
+ local1
1014
+ local2
1015
+ (%let ((local1 \"A\")
1016
+ (local2 \"B\"))
1017
+ (set! local1 (+ local1 \"a\"))
1018
+ (set! local2 (+ local2 \"b\"))
1019
+ (list local1 local2
1020
+ (%let ((local1 \"CCC\"))
1021
+ (list global1 local1 local2))))))
1022
+ EOS
1023
+ ).should == '("Global1" "Local1" "Local2" ("Aa" "Bb" ("Global1" "CCC" "Bb")))'
969
1024
  end
970
1025
  end
971
1026
 
@@ -974,18 +1029,48 @@ describe Nendo, "when call evalStr() with macroexpand-1 function" do
974
1029
  @nendo = Nendo::Core.new()
975
1030
  end
976
1031
  it "should" do
977
- @nendo.evalStr( " (set! twice (macro (x) (list 'begin x x))) (macroexpand-1 '(twice (+ 1 1))) " ).should == "(begin (+ 1 1) (+ 1 1))"
978
- @nendo.evalStr( " (set! inc (macro (x) (list 'set! x (list '+ x 1)))) (macroexpand-1 '(inc a)) " ).should == "(set! a (+ a 1))"
1032
+ @nendo.evalStr( <<EOS
1033
+ (set! twice (macro (x) (list 'begin x x)))
1034
+ (macroexpand-1 '(twice (+ 1 1)))
1035
+ EOS
1036
+ ).should == "(begin (+ 1 1) (+ 1 1))"
1037
+ @nendo.evalStr( <<EOS
1038
+ (set! inc (macro (x) (list 'set! x (list '+ x 1))))
1039
+ (macroexpand-1 '(inc a))
1040
+ EOS
1041
+ ).should == "(set! a (+ a 1))"
979
1042
  @nendo.evalStr( " (set! a 10) (inc a) " ).should == "11"
980
1043
  @nendo.evalStr( " (set! a 10) (inc a) (inc a)" ).should == "12"
981
- @nendo.evalStr( " (macroexpand-1 '(twice (twice (inc a))))" ).should ==
1044
+ @nendo.evalStr( <<EOS
1045
+ (macroexpand-1
1046
+ '(twice (twice (inc a))))
1047
+ EOS
1048
+ ).should ==
982
1049
  "(begin (twice (inc a)) (twice (inc a)))"
983
- @nendo.evalStr( " (macroexpand-1 (macroexpand-1 '(twice (twice (inc a)))))" ).should ==
984
- "(begin (begin (inc a) (inc a)) (begin (inc a) (inc a)))"
985
- @nendo.evalStr( " (macroexpand-1 (macroexpand-1 (macroexpand-1 '(twice (twice (inc a))))))" ).should ==
986
- "(begin (begin (set! a (+ a 1)) (set! a (+ a 1))) (begin (inc a) (inc a)))"
987
- @nendo.evalStr( " (macroexpand-1 (macroexpand-1 (macroexpand-1 (macroexpand-1 '(twice (twice (inc a)))))))" ).should ==
988
- "(begin (begin (set! a (+ a 1)) (set! a (+ a 1))) (begin (set! a (+ a 1)) (set! a (+ a 1))))"
1050
+ @nendo.evalStr( <<EOS
1051
+ (macroexpand-1
1052
+ (macroexpand-1
1053
+ '(twice (twice (inc a)))))
1054
+ EOS
1055
+ ).should ==
1056
+ "(begin (begin (inc a) (inc a)) (twice (inc a)))"
1057
+ @nendo.evalStr( <<EOS
1058
+ (macroexpand-1
1059
+ (macroexpand-1
1060
+ (macroexpand-1
1061
+ '(twice (twice (inc a))))))
1062
+ EOS
1063
+ ).should ==
1064
+ "(begin (begin (set! a (+ a 1)) (inc a)) (twice (inc a)))"
1065
+ @nendo.evalStr( <<EOS
1066
+ (macroexpand-1
1067
+ (macroexpand-1
1068
+ (macroexpand-1
1069
+ (macroexpand-1
1070
+ '(twice (twice (inc a)))))))
1071
+ EOS
1072
+ ).should ==
1073
+ "(begin (begin (set! a (+ a 1)) (set! a (+ a 1))) (twice (inc a)))"
989
1074
  @nendo.evalStr( " (set! a 10) (twice (twice (inc a)))" ).should == "14"
990
1075
  end
991
1076
  end
@@ -1004,7 +1089,17 @@ describe Nendo, "when use #xxxx syntax " do
1004
1089
  @nendo.evalStr( " #! \n #t" ).should == "#t"
1005
1090
  @nendo.evalStr( " #! \n 100" ).should == "100"
1006
1091
  @nendo.evalStr( " #! 123 \n 100" ).should == "100"
1007
- @nendo.evalStr( " '#?=1" ).should == "(debug-print 1 \"(string)\" 1 '1)"
1092
+ @nendo.evalStr( " '#?=1" ).should == "(debug-print 1 \"(string)\" 1 (quote 1))"
1093
+ @nendo.evalStr( " '#?." ).should == '"(string):1"'
1094
+ @nendo.evalStr( " '#?." ).should == '"(string):1"'
1095
+ @nendo.evalStr( " (begin #?. (+ 1 1))" ).should == "2"
1096
+ @nendo.evalStr( <<EOS
1097
+ (begin
1098
+ #?.
1099
+ (+ 1 1)
1100
+ #?. )
1101
+ EOS
1102
+ ).should == '"(string):4"'
1008
1103
  @nendo.evalStr( " #b0 " ).should == Integer("0b0").to_s
1009
1104
  @nendo.evalStr( " #b01 " ).should == Integer("0b01").to_s
1010
1105
  @nendo.evalStr( " #b10 " ).should == Integer("0b10").to_s
@@ -1080,7 +1175,7 @@ describe Nendo, "when use regexp litteral and library functions " do
1080
1175
  @nendo.evalStr( " #/[a-z]/i " ).should == "#/[a-z]/i"
1081
1176
  lambda { @nendo.evalStr( " #/[a-z]/I " ) }.should raise_error(NameError)
1082
1177
  lambda { @nendo.evalStr( " #/[a-z]/a " ) }.should raise_error(NameError)
1083
-
1178
+
1084
1179
  @nendo.evalStr( ' (string->regexp "abc") ' ).should == '#/abc/'
1085
1180
  @nendo.evalStr( ' (string->regexp "[a-z]") ' ).should == '#/[a-z]/'
1086
1181
  @nendo.evalStr( ' (string->regexp "[a-zA-Z0-9]+" ) ' ).should == '#/[a-zA-Z0-9]+/'
@@ -1136,7 +1231,12 @@ describe Nendo, "when use regexp litteral and library functions " do
1136
1231
  @nendo.evalStr( ' (rxmatch->string #/XXX/ "xxx")' ).should == '#f'
1137
1232
 
1138
1233
  pending( "JRuby can't compute correctly" ) if defined? JRUBY_VERSION
1139
- @nendo.evalStr( ' (define matchdata (rxmatch #/([あ-ん])([あ-ん])([あ-ん])([あ-ん])([あ-ん])/ "ABC漢字あいうえお漢字ABC")) ' ).should == 'あいうえお'
1234
+ @nendo.evalStr( <<EOS
1235
+ (define matchdata
1236
+ (rxmatch #/([あ-ん])([あ-ん])([あ-ん])([あ-ん])([あ-ん])/
1237
+ "ABC漢字あいうえお漢字ABC"))
1238
+ EOS
1239
+ ).should == 'あいうえお'
1140
1240
  @nendo.evalStr( ' (rxmatch-start matchdata) ' ).should == '5'
1141
1241
  @nendo.evalStr( ' (rxmatch-end matchdata) ' ).should == '10'
1142
1242
  @nendo.evalStr( ' (rxmatch-substring matchdata) ' ).should == '"あいうえお"'
@@ -1168,6 +1268,17 @@ describe Nendo, "when call functions in init.nnd " do
1168
1268
  @nendo.evalStr( " (append '() '(2)) " ).should == "(2)"
1169
1269
  @nendo.evalStr( " (append '(1) '(2)) " ).should == "(1 2)"
1170
1270
  @nendo.evalStr( " (append '(1 2) '(3 4)) " ).should == "(1 2 3 4)"
1271
+ @nendo.evalStr( " (append '(1 2) (cdr '(200))) " ).should == "(1 2)"
1272
+ @nendo.evalStr( " (append (cdr '(100)) '(10 20)) " ).should == "(10 20)"
1273
+ @nendo.evalStr( " (define lst '()) " ).should == "()"
1274
+ @nendo.evalStr( " (push! lst 1) " ).should == "(1)"
1275
+ @nendo.evalStr( " lst " ).should == "(1)"
1276
+ @nendo.evalStr( " (push! lst 2) " ).should == "(2 1)"
1277
+ @nendo.evalStr( " lst " ).should == "(2 1)"
1278
+ @nendo.evalStr( " (push! lst 3) " ).should == "(3 2 1)"
1279
+ @nendo.evalStr( " lst " ).should == "(3 2 1)"
1280
+ @nendo.evalStr( ' (push! lst "str") ' ).should == '("str" 3 2 1)'
1281
+ @nendo.evalStr( " lst " ).should == '("str" 3 2 1)'
1171
1282
  @nendo.evalStr( " (pair? '()) " ).should == "#f"
1172
1283
  @nendo.evalStr( " (pair? '(1)) " ).should == "#t"
1173
1284
  @nendo.evalStr( " (pair? '(1 2)) " ).should == "#t"
@@ -1256,6 +1367,11 @@ describe Nendo, "when call functions in init.nnd " do
1256
1367
  @nendo.evalStr( ' (string-append "a") ' ).should == '"a"'
1257
1368
  @nendo.evalStr( ' (string-append "a" "b") ' ).should == '"ab"'
1258
1369
  @nendo.evalStr( ' (string-append "a" "B" "c" "D" "e") ' ).should == '"aBcDe"'
1370
+ @nendo.evalStr( ' (string=? "ABC" "ABC") ' ).should == '#t'
1371
+ @nendo.evalStr( ' (string=? "ABC" "ABc") ' ).should == '#f'
1372
+ @nendo.evalStr( ' (string=? "ABC" "abc") ' ).should == '#f'
1373
+ @nendo.evalStr( ' (string=? 100 100) ' ).should == '#t'
1374
+ @nendo.evalStr( ' (string=? (+ "A" "B") "AB") ' ).should == '#t'
1259
1375
  @nendo.evalStr( " (to_i \"22\") " ).should == '22'
1260
1376
  @nendo.evalStr( " (to-i \"22\") " ).should == '22'
1261
1377
  @nendo.evalStr( " (to_i \"10000\") " ).should == '10000'
@@ -1289,8 +1405,27 @@ describe Nendo, "when call functions in init.nnd " do
1289
1405
  @nendo.evalStr( " (cond (true 1) (true 2)) " ).should == "1"
1290
1406
  @nendo.evalStr( " (cond (false 1) (false 2)) " ).should == "()"
1291
1407
  @nendo.evalStr( " (cond (false 1) (false 2) (else 3)) " ).should == "3"
1292
- @nendo.evalStr( " (cond ((- 10 9) => (lambda (x) (+ \"<\" (to_s x) \">\"))) (else 2)) " ).should == '"<1>"'
1293
- @nendo.evalStr( " (cond (true 1) ((- 10 8) => (lambda (x) (+ \"<\" (to_s x) \">\"))) (else 3)) " ).should == "1"
1408
+ @nendo.evalStr( <<EOS
1409
+ (cond
1410
+ ((- 10 9)
1411
+ =>
1412
+ (lambda (x)
1413
+ (+ \"<\" (to_s x) \">\")))
1414
+ (else
1415
+ 2))
1416
+ EOS
1417
+ ).should == '"<1>"'
1418
+ @nendo.evalStr( <<EOS
1419
+ (cond
1420
+ (true 1)
1421
+ ((- 10 8)
1422
+ =>
1423
+ (lambda (x)
1424
+ (+ \"<\" (to_s x) \">\")))
1425
+ (else
1426
+ 3))
1427
+ EOS
1428
+ ).should == "1"
1294
1429
  @nendo.evalStr( " (or) " ).should == "#f"
1295
1430
  @nendo.evalStr( " (or true) " ).should == "#t"
1296
1431
  @nendo.evalStr( " (or false) " ).should == "#f"
@@ -1318,8 +1453,27 @@ describe Nendo, "when call functions in init.nnd " do
1318
1453
  @nendo.evalStr( " (and true '(2) true) " ).should == "#t"
1319
1454
  @nendo.evalStr( " (and true true '(2)) " ).should == "(2)"
1320
1455
  @nendo.evalStr( " (and true '(2) false) " ).should == "#f"
1321
- @nendo.evalStr( " (define total 0) (and 1 2 (set! total (+ total 1)) (set! total (+ total 2)) 5) total" ).should == "3"
1322
- @nendo.evalStr( " (define total 1) (and 1 2 false (set! total (+ total 2)) (set! total (+ total 3)) 5) total" ).should == "1"
1456
+ @nendo.evalStr( <<EOS
1457
+ (define total 0)
1458
+ (and 1
1459
+ 2
1460
+ (set! total (+ total 1))
1461
+ (set! total (+ total 2))
1462
+ 5)
1463
+ total
1464
+ EOS
1465
+ ).should == "3"
1466
+ @nendo.evalStr( <<EOS
1467
+ (define total 1)
1468
+ (and 1
1469
+ 2
1470
+ false
1471
+ (set! total (+ total 2))
1472
+ (set! total (+ total 3))
1473
+ 5)
1474
+ total
1475
+ EOS
1476
+ ).should == "1"
1323
1477
  @nendo.evalStr( " (apply + 100 '()) " ).should == "100"
1324
1478
  @nendo.evalStr( " (apply + '(1 2)) " ).should == "3"
1325
1479
  @nendo.evalStr( " (apply + 1 2 '(3)) " ).should == "6"
@@ -1333,28 +1487,134 @@ describe Nendo, "when call functions in init.nnd " do
1333
1487
  @nendo.evalStr( " (apply + (range 11)) " ).should == "55"
1334
1488
  @nendo.evalStr( " (apply + (map (lambda (x) (+ x 1)) (range 10))) " ).should == "55"
1335
1489
  @nendo.evalStr( " (apply + (append (range 11) '(100))) " ).should == "155"
1490
+ @nendo.evalStr( " (apply cons '(1 2)) " ).should == "(1 . 2)"
1491
+ @nendo.evalStr( " (apply list '(1 2)) " ).should == "(1 2)"
1492
+ @nendo.evalStr( " (apply list (list 1 '())) " ).should == "(1 ())"
1493
+ @nendo.evalStr( " (apply list (list 1 (cdr '(100)))) " ).should == "(1 ())"
1336
1494
  @nendo.evalStr( " (map (lambda (x) 1) '()) " ).should == "()"
1337
1495
  @nendo.evalStr( " (map (lambda (x) 1) (to-list '#())) " ).should == "()"
1338
1496
  @nendo.evalStr( " (map (lambda (x) (* x 2)) '(1 2 3)) " ).should == "(2 4 6)"
1339
1497
  @nendo.evalStr( " (map (lambda (x) (+ x 1)) '(1 2 3)) " ).should == "(2 3 4)"
1340
- @nendo.evalStr( " (map (lambda (a b) (+ a b)) '(1 2 3) '(10 20 30)) " ).should == "(11 22 33)"
1341
- @nendo.evalStr( " (map (lambda (a b) (- b a)) '(1 2 3) '(10 20 30)) " ).should == "(9 18 27)"
1342
- @nendo.evalStr( " (map (lambda (a b c) (+ a b c)) '(1 2 3) '(10 20 30) '(100 200 300)) " ).should == "(111 222 333)"
1343
- @nendo.evalStr( " (define _result"+
1344
- " (map"+
1345
- " (lambda (x)"+
1346
- " (* x 2))"+
1347
- " (range 10000 1)))"+
1348
- " (list"+
1349
- " (first _result)"+
1350
- " (second _result)"+
1351
- " (last-pair _result))" ).should == "(2 4 (20000))"
1352
- @nendo.evalStr( " (define _lst '()) (for-each (lambda (x) (set! _lst (cons 1 _lst))) '()) _lst" ).should == "()"
1353
- @nendo.evalStr( " (define _lst '()) (for-each (lambda (x) (set! _lst (cons (* x 2) _lst))) '(1 2 3)) _lst" ).should == "(6 4 2)"
1354
- @nendo.evalStr( " (define _lst '()) (for-each (lambda (x) (set! _lst (cons (+ x 1) _lst))) '(1 2 3)) _lst" ).should == "(4 3 2)"
1355
- @nendo.evalStr( " (define _lst '()) (for-each (lambda (a b) (set! _lst (cons (cons a b) _lst))) '(1 2 3) '(10 20 30)) _lst" ).should ==
1498
+ @nendo.evalStr( <<EOS
1499
+ (map
1500
+ (lambda (a b) (+ a b))
1501
+ '(1 2 3)
1502
+ '(10 20 30))
1503
+ EOS
1504
+ ).should == "(11 22 33)"
1505
+ @nendo.evalStr( <<EOS
1506
+ (map
1507
+ (lambda (a b)
1508
+ (- b a))
1509
+ '(1 2 3)
1510
+ '(10 20 30))
1511
+ EOS
1512
+ ).should == "(9 18 27)"
1513
+ @nendo.evalStr( <<EOS
1514
+ (map
1515
+ (lambda (a b c)
1516
+ (+ a b c))
1517
+ '(1 2 3)
1518
+ '(10 20 30)
1519
+ '(100 200 300))
1520
+ EOS
1521
+ ).should == "(111 222 333)"
1522
+ @nendo.evalStr( <<EOS
1523
+ (define _result
1524
+ (map
1525
+ (lambda (x)
1526
+ (* x 2))
1527
+ (range 10000 1)))
1528
+ (list
1529
+ (first _result)
1530
+ (second _result)
1531
+ (last-pair _result))
1532
+ EOS
1533
+ ).should == "(2 4 (20000))"
1534
+
1535
+ @nendo.evalStr( <<EOS
1536
+ (list
1537
+ (list* 1)
1538
+ (list* 1 2)
1539
+ (list* 1 2 3)
1540
+ (list* 1 2 3 4))
1541
+ EOS
1542
+ ).should == "(1 (1 . 2) (1 2 . 3) (1 2 3 . 4))"
1543
+
1544
+ @nendo.evalStr( <<EOS
1545
+ (list
1546
+ (list* '())
1547
+ (list* 1 '())
1548
+ (list* 1 2 '())
1549
+ (list* 1 2 3 '()))
1550
+ EOS
1551
+ ).should == "(() (1) (1 2) (1 2 3))"
1552
+
1553
+ @nendo.evalStr( <<EOS
1554
+ (list
1555
+ (list* (cdr '(100)))
1556
+ (list* 1 (cdr '(100)))
1557
+ (list* 1 2 (cdr '(100)))
1558
+ (list* 1 2 3 (cdr '(100))))
1559
+ EOS
1560
+ ).should == "(() (1) (1 2) (1 2 3))"
1561
+
1562
+ @nendo.evalStr( <<EOS
1563
+ (list
1564
+ (list* '())
1565
+ (list* '() '())
1566
+ (list* '() '() '())
1567
+ (list* '() '() '() '()))
1568
+ EOS
1569
+ ).should == "(() (()) (() ()) (() () ()))"
1570
+
1571
+ @nendo.evalStr( <<EOS
1572
+ (define _lst '())
1573
+ (for-each
1574
+ (lambda (x)
1575
+ (set! _lst (cons 1 _lst)))
1576
+ '())
1577
+ _lst
1578
+ EOS
1579
+ ).should == "()"
1580
+ @nendo.evalStr( <<EOS
1581
+ (define _lst '())
1582
+ (for-each
1583
+ (lambda (x)
1584
+ (set! _lst (cons (* x 2) _lst)))
1585
+ '(1 2 3))
1586
+ _lst
1587
+ EOS
1588
+ ).should == "(6 4 2)"
1589
+ @nendo.evalStr( <<EOS
1590
+ (define _lst '())
1591
+ (for-each
1592
+ (lambda (x)
1593
+ (set! _lst (cons (+ x 1) _lst)))
1594
+ '(1 2 3))
1595
+ _lst
1596
+ EOS
1597
+ ).should == "(4 3 2)"
1598
+ @nendo.evalStr( <<EOS
1599
+ (define _lst '())
1600
+ (for-each
1601
+ (lambda (a b)
1602
+ (set! _lst (cons (cons a b) _lst)))
1603
+ '(1 2 3)
1604
+ '(10 20 30))
1605
+ _lst
1606
+ EOS
1607
+ ).should ==
1356
1608
  "((3 . 30) (2 . 20) (1 . 10))"
1357
- @nendo.evalStr( " (define _cnt 0) (for-each (lambda (x) (set! _cnt (+ _cnt 1))) (range 10000)) _cnt" ).should == "10000"
1609
+ @nendo.evalStr( <<EOS
1610
+ (define _cnt 0)
1611
+ (for-each
1612
+ (lambda (x)
1613
+ (set! _cnt (+ _cnt 1)))
1614
+ (range 10000))
1615
+ _cnt
1616
+ EOS
1617
+ ).should == "10000"
1358
1618
  @nendo.evalStr( " (filter (lambda (x) x) '()) " ).should == "()"
1359
1619
  @nendo.evalStr( " (filter (lambda (x) (= x 100)) '(1 2 3)) " ).should == "()"
1360
1620
  @nendo.evalStr( " (filter (lambda (x) (= x 2)) '(1 2 3)) " ).should == "(2)"
@@ -1370,26 +1630,42 @@ describe Nendo, "when call functions in init.nnd " do
1370
1630
  @nendo.evalStr( " (filter-map (lambda (x) (= x 100)) '(1 2 3)) " ).should == "()"
1371
1631
  @nendo.evalStr( " (filter-map (lambda (x) (= x 2)) '(1 2 3)) " ).should == "(#t)"
1372
1632
  @nendo.evalStr( " (filter-map (lambda (x) (not (= x 2))) '(1 2 3)) " ).should == "(#t #t)"
1373
- @nendo.evalStr( " (filter-map (lambda (x) (if (= x 2) (* x 10) false)) '(1 2 3)) " ).should == "(20)"
1374
- @nendo.evalStr( " (filter-map (lambda (x) (if (not (= x 2)) (* x 10) false)) '(1 2 3)) " ).should == "(10 30)"
1633
+ @nendo.evalStr( <<EOS
1634
+ (filter-map
1635
+ (lambda (x)
1636
+ (if (= x 2) (* x 10) false))
1637
+ '(1 2 3))
1638
+ EOS
1639
+ ).should == "(20)"
1640
+ @nendo.evalStr( <<EOS
1641
+ (filter-map
1642
+ (lambda (x)
1643
+ (if (not (= x 2)) (* x 10) false))
1644
+ '(1 2 3))
1645
+ EOS
1646
+ ).should == "(10 30)"
1375
1647
  @nendo.evalStr( " (any even? '(1 2 3)) " ).should == "#t"
1376
1648
  @nendo.evalStr( " (any even? '(1 3)) " ).should == "#f"
1377
1649
  @nendo.evalStr( " (any odd? '(1 3)) " ).should == "#t"
1378
1650
  @nendo.evalStr( " (any (lambda (x) x) '(1 2 3)) " ).should == "1"
1379
- @nendo.evalStr( " " +
1380
- "(let1 result '()" +
1381
- " (do" +
1382
- " ((i 0 (+ i 1)))" +
1383
- " ((< 10 i) #f)" +
1384
- " (set! result (cons i result)))" +
1385
- " (reverse result))" ).should == "(0 1 2 3 4 5 6 7 8 9 10)"
1386
- @nendo.evalStr( " " +
1387
- "(let1 result '()" +
1388
- " (do" +
1389
- " ((i 0 (+ i 3)))" +
1390
- " ((< (* 3 10) i) #f)" +
1391
- " (set! result (cons i result)))" +
1392
- " (reverse result))" ).should == "(0 3 6 9 12 15 18 21 24 27 30)"
1651
+ @nendo.evalStr( <<EOS
1652
+ (let1 result '()
1653
+ (do
1654
+ ((i 0 (+ i 1)))
1655
+ ((< 10 i) #f)
1656
+ (set! result (cons i result)))
1657
+ (reverse result))
1658
+ EOS
1659
+ ).should == "(0 1 2 3 4 5 6 7 8 9 10)"
1660
+ @nendo.evalStr( <<EOS
1661
+ (let1 result '()
1662
+ (do
1663
+ ((i 0 (+ i 3)))
1664
+ ((< (* 3 10) i) #f)
1665
+ (set! result (cons i result)))
1666
+ (reverse result))
1667
+ EOS
1668
+ ).should == "(0 3 6 9 12 15 18 21 24 27 30)"
1393
1669
  end
1394
1670
  end
1395
1671
 
@@ -1414,22 +1690,30 @@ describe Nendo, "when use values " do
1414
1690
  @nendo.evalStr( " (values-values (values 1 2)) " ).should == "(1 2)"
1415
1691
  @nendo.evalStr( " (values-values (values 1 2 3)) " ).should == "(1 2 3)"
1416
1692
  @nendo.evalStr( " (values-values (values '(a) \"b\" '(\"C\"))) " ).should == '((a) "b" ("C"))'
1417
- @nendo.evalStr( "" +
1418
- " (call-with-values" +
1419
- " (lambda () (values 4 5)) " +
1420
- " (lambda (a b) b))" ).should == "5"
1421
- @nendo.evalStr( "" +
1422
- " (call-with-values" +
1423
- " (lambda () (values 1 2)) " +
1424
- " cons)" ).should == "(1 . 2)"
1425
- @nendo.evalStr( "" +
1426
- " (call-with-values" +
1427
- " (lambda () (values 10)) " +
1428
- " list)" ).should == "(10)"
1429
- @nendo.evalStr( "" +
1430
- " (call-with-values" +
1431
- " (lambda () (values)) " +
1432
- " list)" ).should == "()"
1693
+ @nendo.evalStr( <<EOS
1694
+ (call-with-values
1695
+ (lambda () (values 4 5))
1696
+ (lambda (a b) b))
1697
+ EOS
1698
+ ).should == "5"
1699
+ @nendo.evalStr( <<EOS
1700
+ (call-with-values
1701
+ (lambda () (values 1 2))
1702
+ cons)
1703
+ EOS
1704
+ ).should == "(1 . 2)"
1705
+ @nendo.evalStr( <<EOS
1706
+ (call-with-values
1707
+ (lambda () (values 10))
1708
+ list)
1709
+ EOS
1710
+ ).should == "(10)"
1711
+ @nendo.evalStr( <<EOS
1712
+ (call-with-values
1713
+ (lambda () (values))
1714
+ list)
1715
+ EOS
1716
+ ).should == "()"
1433
1717
  @nendo.evalStr( " (call-with-values * -) " ).should == "-1"
1434
1718
  @nendo.evalStr( " (receive (a) (values) (list a)) " ).should == "()"
1435
1719
  @nendo.evalStr( " (receive (a) (values 10) (list a)) " ).should == "(10)"
@@ -1488,7 +1772,7 @@ describe Nendo, "when use quasiquote macro " do
1488
1772
  @nendo.evalStr( " (set! a 3) `(1 2 ,@(list a)) " ).should == "(1 2 3)"
1489
1773
  @nendo.evalStr( " (set! a 3) `(1 ,@(list 2 a)) " ).should == "(1 2 3)"
1490
1774
  @nendo.evalStr( " (set! a 11) `,a " ).should == "11"
1491
- @nendo.evalStr( " (set! a 12) ``,a " ).should == "`,a"
1775
+ @nendo.evalStr( " (set! a 12) ``,a " ).should == "(quasiquote (unquote a))"
1492
1776
  @nendo.evalStr( ' (define str "string") str ' ).should == '"string"'
1493
1777
  @nendo.evalStr( ' `(,str) ' ).should == '("string")'
1494
1778
  @nendo.evalStr( ' `("STRING") ' ).should == '("STRING")'
@@ -1499,8 +1783,8 @@ describe Nendo, "when use quasiquote macro " do
1499
1783
  @nendo.evalStr( ' (caar `(("STRING" ,str))) ' ).should == '"STRING"'
1500
1784
  @nendo.evalStr( ' (string-join `("A" "B" "C" "D")) ' ).should == '"ABCD"'
1501
1785
  @nendo.evalStr( " `(list ,(+ 1 2) 4) " ).should == "(list 3 4)"
1502
- @nendo.evalStr( " (let ((name 'a)) `(list ,name ',name)) " ).should == "(list a 'a)"
1503
- @nendo.evalStr( " `(a `(b ,(+ 1 2) ,(foo ,(+ 1 3) d) e) f) " ).should == "(a `(b ,(+ 1 2) ,(foo 4 d) e) f)"
1786
+ @nendo.evalStr( " (let ((name 'a)) `(list ,name ',name)) " ).should == "(list a (quote a))"
1787
+ @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)"
1504
1788
  @nendo.evalStr( " `(( foo ,(- 10 3)) ,@(cdr '(c)) . ,(car '(cons))) " ).should == "((foo 7) . cons)"
1505
1789
  end
1506
1790
  end
@@ -1524,24 +1808,34 @@ describe Nendo, "when use macros made by quasiquote. " do
1524
1808
  @nendo.evalStr( " (let* ((a 10) (b (* a 2))) (cons a b)) " ).should == "(10 . 20)"
1525
1809
  @nendo.evalStr( " (let* ((a 10) (b (* a 2)) (c (* b 3))) (list a b c)) " ).should == "(10 20 60)"
1526
1810
  @nendo.evalStr( " (begin0 1 2 3) " ).should == "1"
1527
- @nendo.evalStr( " (define a 2) (begin0 (set! a (* a 2)) (set! a (* a 2)) (set! a (* a 2)) ) " ).should == "4"
1811
+ @nendo.evalStr( <<EOS
1812
+ (define a 2)
1813
+ (begin0 (set! a (* a 2))
1814
+ (set! a (* a 2))
1815
+ (set! a (* a 2)))
1816
+ EOS
1817
+ ).should == "4"
1528
1818
  @nendo.evalStr( " (begin0 100) " ).should == "100"
1529
1819
  @nendo.evalStr( " (begin0) " ).should == "#f"
1530
- @nendo.evalStr( " " +
1531
- "(receive (a b)" +
1532
- " (begin0" +
1533
- " (values 1 2)" +
1534
- " (values 10 20)" +
1535
- " (values 100 200))" +
1536
- " (cons a b))" ).should == "(1 . 2)"
1820
+ @nendo.evalStr( <<EOS
1821
+ (receive (a b)
1822
+ (begin0
1823
+ (values 1 2)
1824
+ (values 10 20)
1825
+ (values 100 200))
1826
+ (cons a b))
1827
+ EOS
1828
+ ).should == "(1 . 2)"
1537
1829
  @nendo.evalStr( " (macroexpand '(when #t (print \"1\") (print \"2\"))) " ).should == '(if #t (begin (print "1") (print "2")))'
1538
1830
  @nendo.evalStr( " (macroexpand '(unless #t (print \"1\") (print \"2\"))) " ).should == '(if (not #t) (begin (print "1") (print "2")))'
1539
- @nendo.evalStr( " (macroexpand '(if-let1 a #t (print \"T\") (print \"F\"))) " ).should == '(let ((a #t)) (if a (print "T") (print "F")))'
1831
+ @nendo.evalStr( " (macroexpand '(if-let1 a #t (print \"T\") (print \"F\"))) " ).should == '(%let ((a #t)) (if a (print "T") (print "F")))'
1540
1832
  @nendo.evalStr( " (let1 count 0 (when #t (set! count (+ count 1)) (set! count (+ count 1))) count) " ).should == "2"
1541
1833
  @nendo.evalStr( " (let1 count 0 (when #f (set! count (+ count 1)) (set! count (+ count 1))) count) " ).should == "0"
1542
1834
  @nendo.evalStr( " (let1 count 0 (unless #t (set! count (+ count 1)) (set! count (+ count 1))) count) " ).should == "0"
1543
1835
  @nendo.evalStr( " (let1 count 0 (unless #f (set! count (+ count 1)) (set! count (+ count 1))) count) " ).should == "2"
1544
1836
  @nendo.evalStr( " (if-let1 m (rxmatch #/([0-9]+)/ \"abc100abc\") (rxmatch-substring m 1)) " ).should == '"100"'
1837
+ @nendo.evalStr( " (macroexpand '#?.) " ).should == '"(string):1"'
1838
+ @nendo.evalStr( " (macroexpand '(begin 1 2 #?. 4 5)) " ).should == '(begin 1 2 "(string):1" 4 5)'
1545
1839
  end
1546
1840
  end
1547
1841
 
@@ -1551,11 +1845,51 @@ describe Nendo, "when use define and lambda macro " do
1551
1845
  @nendo.loadInitFile
1552
1846
  end
1553
1847
  it "should" do
1554
- @nendo.evalStr( " (macroexpand '(define (main argv) (newline) 0)) " ).should == "(define main (lambda (argv) (newline) 0))"
1555
- @nendo.evalStr( " (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) 0 (foo 111))))"
1556
- @nendo.evalStr( " (macroexpand '(define (main argv) (define result '()) (define (foo x) x) (define val 0) (define (bar x) (+ val 10)) )) " ).should == "(define main (lambda (argv) (letrec ((result '()) (foo (lambda (x) x)) (val 0) (bar (lambda (x) (+ val 10)))))))"
1557
- @nendo.evalStr( " (define (main argv) (define (foo x) x) (+ 10 20) 0 (foo 111)) (main) " ).should == "111"
1558
- @nendo.evalStr( " (define (main argv) (define (foo1 x) (+ 1 x)) (define (foo2 x) (+ 2 x)) (* (foo1 20) (foo2 30))) (main '()) " ).should == "672"
1848
+ @nendo.evalStr( <<EOS
1849
+ (macroexpand
1850
+ '(define (main argv)
1851
+ (newline)
1852
+ 0))
1853
+ EOS
1854
+ ).should == "(define main (lambda (argv) (newline) 0))"
1855
+ @nendo.evalStr( <<EOS
1856
+ (macroexpand
1857
+ '(define (main argv)
1858
+ (define (foo x) x)
1859
+ (+ 10 20)
1860
+ 0
1861
+ (foo 111)))
1862
+ EOS
1863
+ ).should == "(define main (lambda (argv) (letrec ((foo (lambda (x) x))) (+ 10 20) 0 (foo 111))))"
1864
+ @nendo.evalStr( <<EOS
1865
+ (macroexpand
1866
+ '(define (main argv)
1867
+ (define result '())
1868
+ (define (foo x) x)
1869
+ (define val 0)
1870
+ (define (bar x)
1871
+ (+ val 10))))
1872
+ EOS
1873
+ ).should == "(define main (lambda (argv) (letrec ((result (quote ())) (foo (lambda (x) x)) (val 0) (bar (lambda (x) (+ val 10)))))))"
1874
+ @nendo.evalStr( <<EOS
1875
+ (define (main argv)
1876
+ (define (foo x) x)
1877
+ (+ 10 20)
1878
+ 0
1879
+ (foo 111))
1880
+ (main)
1881
+ EOS
1882
+ ).should == "111"
1883
+ @nendo.evalStr( <<EOS
1884
+ (define (main argv)
1885
+ (define (foo1 x) (+ 1 x))
1886
+ (define (foo2 x) (+ 2 x))
1887
+ (*
1888
+ (foo1 20)
1889
+ (foo2 30)))
1890
+ (main '())
1891
+ EOS
1892
+ ).should == "672"
1559
1893
  @nendo.evalStr( " (macroexpand '(define (main argv) 0)) " ).should == "(define main (lambda (argv) 0))"
1560
1894
  end
1561
1895
  end
@@ -1566,15 +1900,27 @@ describe Nendo, "when use macro macro " do
1566
1900
  @nendo.loadInitFile
1567
1901
  end
1568
1902
  it "should" do
1569
- @nendo.evalStr( " (define inc!-macro (macro (x) (+ x 1))) #t" ).should == "#t"
1903
+ @nendo.evalStr( <<EOS
1904
+ (define inc!-macro
1905
+ (macro (x)
1906
+ (+ x 1)))
1907
+ EOS
1908
+ ).should match( /Nendo::LispMacro/ )
1570
1909
  @nendo.evalStr( " (inc!-macro 10) " ).should == "11"
1571
- @nendo.evalStr( " (define dec!-macro (macro (x) (define (dec! v) (- v 1)) (dec! x))) #t" ).should == "#t"
1910
+ @nendo.evalStr( <<EOS
1911
+ (define dec!-macro
1912
+ (macro (x)
1913
+ (define (dec! v)
1914
+ (- v 1))
1915
+ (dec! x)))
1916
+ EOS
1917
+ ).should match( /Nendo::LispMacro/ )
1572
1918
  @nendo.evalStr( " (dec!-macro 10) " ).should == "9"
1573
1919
  @nendo.evalStr( " (. (dec!-macro 10) class) " ).should == 'Fixnum'
1574
1920
  end
1575
1921
  end
1576
1922
 
1577
- describe Nendo, "when use macros expands some syntax. " do
1923
+ describe Nendo, "when use macros expand some syntax. " do
1578
1924
  before do
1579
1925
  @nendo = Nendo::Core.new()
1580
1926
  @nendo.loadInitFile
@@ -1616,8 +1962,8 @@ describe Nendo, "when use dot-operator (.) macro " do
1616
1962
  lambda { @nendo.evalStr( " (macroexpand '(. open)) " ) }.should raise_error( ArgumentError )
1617
1963
  lambda { @nendo.evalStr( " (macroexpand '(. open \"aaa\")) " ) }.should raise_error( TypeError )
1618
1964
  @nendo.evalStr( " (macroexpand '(. a size)) " ).should == "(a.size)"
1619
- @nendo.evalStr( " (macroexpand '(. (. a size) to_s)) " ).gsub( /[12]0[0-9][0-9][0-9]/, "X0000" ).should == "(let ((__gensym__fb4e25e49e9fb4e46342224606faf2e3eabf1251_X0000 (a.size))) (__gensym__fb4e25e49e9fb4e46342224606faf2e3eabf1251_X0000.to_s))"
1620
- @nendo.evalStr( " (macroexpand '(. (. (. a size) to_s) to_i)) " ).gsub( /[12]0[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))"
1965
+ @nendo.evalStr( " (macroexpand '(. (. a size) to_s)) " ).gsub( /[12]0[0-9][0-9][0-9]/, "X0000" ).should == "(%let ((__gensym__fb4e25e49e9fb4e46342224606faf2e3eabf1251_X0000 (a.size))) (__gensym__fb4e25e49e9fb4e46342224606faf2e3eabf1251_X0000.to_s))"
1966
+ @nendo.evalStr( " (macroexpand '(. (. (. a size) to_s) to_i)) " ).gsub( /[12]0[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))"
1621
1967
  lambda { @nendo.evalStr( " (macroexpand '(. (. a size))) " ) }.should raise_error( ArgumentError )
1622
1968
  @nendo.evalStr( " (set! str \"str\") str.size " ).should == "3"
1623
1969
  @nendo.evalStr( " (set! str \"str\") (. str size) " ).should == "3"
@@ -1792,17 +2138,52 @@ describe Nendo, "when use hash-table feature " do
1792
2138
  @nendo.evalStr( " (hash-table-num-entries h)" ).should == "1"
1793
2139
  @nendo.evalStr( " (hash-table-clear! h)" ).should == "{}"
1794
2140
  @nendo.evalStr( " (hash-table-num-entries h)" ).should == "0"
1795
- @nendo.evalStr( " (set! h (hash-table eq? '(\"a\" \"AAA\") '(\"b\" \"BBB\") '(\"c\" \"CCC\" 100))) (hash-table-keys h)" ).should == "(\"a\" \"b\" \"c\")"
1796
- @nendo.evalStr( " (set! h (hash-table eq? '(\"a\" \"AAA\") '(\"b\" \"BBB\") '(\"c\" \"CCC\" 100))) (hash-table-values h)" ).should == "((\"AAA\") (\"BBB\") (\"CCC\" 100))"
1797
- @nendo.evalStr( " (set! h (hash-table eq? '(\"a\" . \"AAA\") '(\"b\" . \"BBB\") '(\"c\" . \"CCC\"))) h" ).should == "{\"a\"=>\"AAA\", \"b\"=>\"BBB\", \"c\"=>\"CCC\"}"
2141
+ @nendo.evalStr( <<EOS
2142
+ (set! h (hash-table eq?
2143
+ '("a" "AAA")
2144
+ '("b" "BBB")
2145
+ '("c" "CCC" 100)))
2146
+ (hash-table-keys h)
2147
+ EOS
2148
+ ).should == "(\"a\" \"b\" \"c\")"
2149
+ @nendo.evalStr( <<EOS
2150
+ (set! h (hash-table eq?
2151
+ '("a" "AAA")
2152
+ '("b" "BBB")
2153
+ '("c" "CCC" 100)))
2154
+ (hash-table-values h)
2155
+ EOS
2156
+ ).should == "((\"AAA\") (\"BBB\") (\"CCC\" 100))"
2157
+ @nendo.evalStr( <<EOS
2158
+ (set! h (hash-table eq?
2159
+ '("a" . "AAA")
2160
+ '("b" . "BBB")
2161
+ '("c" . "CCC")))
2162
+ h
2163
+ EOS
2164
+ ).should == "{\"a\"=>\"AAA\", \"b\"=>\"BBB\", \"c\"=>\"CCC\"}"
1798
2165
  @nendo.evalStr( " (hash-table-keys h)" ).should == '("a" "b" "c")'
1799
2166
  @nendo.evalStr( " (hash-table-values h)" ).should == '("AAA" "BBB" "CCC")'
1800
2167
  @nendo.evalStr( " (hash-table-map h (lambda (a b) (+ a b)))" ).should == '("aAAA" "bBBB" "cCCC")'
1801
2168
  @nendo.evalStr( " (hash-table-for-each h (lambda (a b) (+ a b)))" ).should == '("aAAA" "bBBB" "cCCC")'
1802
2169
  @nendo.evalStr( " (set! h (make-hash-table)) " ).should == "{}"
1803
- @nendo.evalStr( " (set! h (hash-table eq? '(true . 1) '(false . 2) '(nil . 3))) h" ).should == "{true=>1, false=>2, nil=>3}"
2170
+ @nendo.evalStr( <<EOS
2171
+ (set! h (hash-table eq?
2172
+ '(true . 1)
2173
+ '(false . 2)
2174
+ '(nil . 3)))
2175
+ h
2176
+ EOS
2177
+ ).should == "{true=>1, false=>2, nil=>3}"
1804
2178
  @nendo.evalStr( " (hash-table-keys h) " ).should == "(#t #f nil)"
1805
- @nendo.evalStr( " (set! h (hash-table eq? '(1 . true) '(2 . false) '(3 . nil))) h" ).should == "{1=>true, 2=>false, 3=>nil}"
2179
+ @nendo.evalStr( <<EOS
2180
+ (set! h (hash-table eq?
2181
+ '(1 . true)
2182
+ '(2 . false)
2183
+ '(3 . nil)))
2184
+ h
2185
+ EOS
2186
+ ).should == "{1=>true, 2=>false, 3=>nil}"
1806
2187
  @nendo.evalStr( " (hash-table-keys h) " ).should == "(1 2 3)"
1807
2188
  end
1808
2189
  end
@@ -1858,75 +2239,156 @@ describe Nendo, "tail call optimization " do
1858
2239
  @nendo.loadInitFile # to self optimizing. The init.nnd file will be loaded twice, so `filter' can be optimized on second loading phase.
1859
2240
  end
1860
2241
  it "should" do
1861
- @nendo.evalStr( " (setup-tailcall-mark '(print \"abc\")) " ).should == "(%tailcall (print \"abc\"))"
1862
- @nendo.evalStr( " (setup-tailcall-mark '(begin (print \"abc\") 1 2 3)) " ).should == "(begin (print \"abc\") 1 2 3)"
1863
- @nendo.evalStr( " (setup-tailcall-mark '(begin 1 2 (print \"abc\") 3)) " ).should == "(begin 1 2 (print \"abc\") 3)"
1864
- @nendo.evalStr( " (setup-tailcall-mark '(begin 1 2 3 (print \"abc\"))) " ).should == "(begin 1 2 3 (%tailcall (print \"abc\")))"
1865
- @nendo.evalStr( " (setup-tailcall-mark '(lambda (x) x)) " ).should == "(lambda (x) x)"
1866
- @nendo.evalStr( " (setup-tailcall-mark '(macro (x) x)) " ).should == "(macro (x) x)"
1867
- @nendo.evalStr( " (setup-tailcall-mark '(%syntax (x) x)) " ).should == "(%syntax (x) x)"
1868
- @nendo.evalStr( " (setup-tailcall-mark '(%syntax (a b c) (begin a b c))) " ).should == "(%syntax (a b c) (begin a b c))"
1869
- @nendo.evalStr( " (setup-tailcall-mark '(lambda (x) (%syntax (y) x))) " ).should == "(lambda (x) (%syntax (y) x))"
1870
- @nendo.evalStr( " (setup-tailcall-mark '(lambda (x) (syntax-quote x))) " ).should == "(lambda (x) (syntax-quote x))"
1871
- @nendo.evalStr( " (setup-tailcall-mark '(lambda (x) (quote x))) " ).should == "(lambda (x) 'x)"
1872
- @nendo.evalStr( "" +
1873
- "(setup-tailcall-mark"+
1874
- " '(lambda '(x)"+
1875
- " 1"+
1876
- " 2"+
1877
- " (print \"abc\")))" ).should == "(lambda '(x) 1 2 (%tailcall (print \"abc\")))"
1878
- @nendo.evalStr( "" +
1879
- "(setup-tailcall-mark"+
1880
- " '(lambda (x)"+
1881
- " 1"+
1882
- " 2"+
1883
- " (if #t"+
1884
- " (begin 1 2 (print \"abc\"))"+
1885
- " (begin 1 2 (print \"ABC\")))))" ).should == "(lambda (x) 1 2 (if #t (begin 1 2 (%tailcall (print \"abc\"))) (begin 1 2 (%tailcall (print \"ABC\")))))"
1886
- @nendo.evalStr( "(setup-tailcall-mark (macroexpand "+
1887
- " '(define (foo) (foo))"+
1888
- " ))" ).should == "(define foo (lambda () (%tailcall (foo))))"
1889
- @nendo.evalStr( "(setup-tailcall-mark (macroexpand "+
1890
- " '(values? (make-values '()))"+
1891
- " ))" ).should == "(%tailcall (values? (make-values '())))"
1892
- @nendo.evalStr( "(setup-tailcall-mark (macroexpand "+
1893
- " '(cond (false 1) (false 2))"+
1894
- " ))" ).should == "(if #f (begin 1) (if #f (begin 2) ()))"
1895
- @nendo.evalStr( "(setup-tailcall-mark (macroexpand "+
1896
- " '(cond (false 1) (false 2) (else 3))"+
1897
- " ))" ).should == "(if #f (begin 1) (if #f (begin 2) (if #t (begin 3) ())))"
1898
- @nendo.evalStr( "(setup-tailcall-mark (macroexpand "+
1899
- " '(and (foo 1) (bar 2))"+
1900
- " ))" ).should == "(if (not (eq? #f (foo 1))) (%tailcall (bar 2)) #f)"
1901
- @nendo.evalStr( "(setup-tailcall-mark (macroexpand "+
1902
- " '(or (foo 1) (bar 2))"+
1903
- " ))" ).gsub( /[24]0[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))))"
1904
- @nendo.evalStr( "(setup-tailcall-mark (macroexpand "+
1905
- " '(let loop ((x 1)) 1 2 (loop 100))"+
1906
- " ))" ).should == "(letrec ((loop (lambda (x) 1 2 (%tailcall (loop 100))))) (%tailcall (loop 1)))"
1907
- @nendo.evalStr( "(setup-tailcall-mark (macroexpand "+
2242
+ @nendo.evalStr( " (%setup-%tailcall-mark '(print \"abc\")) " ).should == "(%tailcall (print \"abc\"))"
2243
+ @nendo.evalStr( " (%setup-%tailcall-mark '(begin (print \"abc\") 1 2 3)) " ).should == "(begin (print \"abc\") 1 2 3)"
2244
+ @nendo.evalStr( " (%setup-%tailcall-mark '(begin 1 2 (print \"abc\") 3)) " ).should == "(begin 1 2 (print \"abc\") 3)"
2245
+ @nendo.evalStr( " (%setup-%tailcall-mark '(begin 1 2 3 (print \"abc\"))) " ).should == "(begin 1 2 3 (%tailcall (print \"abc\")))"
2246
+ @nendo.evalStr( " (%setup-%tailcall-mark '(lambda (x) x)) " ).should == "(lambda (x) x)"
2247
+ @nendo.evalStr( " (%setup-%tailcall-mark '(macro (x) x)) " ).should == "(macro (x) x)"
2248
+ @nendo.evalStr( " (%setup-%tailcall-mark '(%syntax (x) x)) " ).should == "(%syntax (x) x)"
2249
+ @nendo.evalStr( " (%setup-%tailcall-mark '(%syntax (a b c) (begin a b c))) " ).should == "(%syntax (a b c) (begin a b c))"
2250
+ @nendo.evalStr( " (%setup-%tailcall-mark '(lambda (x) (%syntax (y) x))) " ).should == "(lambda (x) (%syntax (y) x))"
2251
+ @nendo.evalStr( " (%setup-%tailcall-mark '(lambda (x) (syntax-quote x))) " ).should == "(lambda (x) (quote x))"
2252
+ @nendo.evalStr( " (%setup-%tailcall-mark '(lambda (x) (quote x))) " ).should == "(lambda (x) (quote x))"
2253
+ @nendo.evalStr( <<EOS
2254
+ (%setup-%tailcall-mark
2255
+ '(lambda '(x)
2256
+ 1
2257
+ 2
2258
+ (print \"abc\")))
2259
+ EOS
2260
+ ).should == "(lambda (quote (x)) 1 2 (%tailcall (print \"abc\")))"
2261
+ @nendo.evalStr( <<EOS
2262
+ (%setup-%tailcall-mark
2263
+ '(lambda (x)
2264
+ 1
2265
+ 2
2266
+ (if #t
2267
+ (begin 1 2 (print "abc"))
2268
+ (begin 1 2 (print "ABC")))))
2269
+ EOS
2270
+ ).should == "(lambda (x) 1 2 (if #t (begin 1 2 (%tailcall (print \"abc\"))) (begin 1 2 (%tailcall (print \"ABC\")))))"
2271
+ @nendo.evalStr( <<EOS
2272
+ (%setup-%tailcall-mark (macroexpand
2273
+ '(define (foo) (foo))
2274
+ ))
2275
+ EOS
2276
+ ).should == "(define foo (lambda () (%tailcall (foo))))"
2277
+ @nendo.evalStr( <<EOS
2278
+ (%setup-%tailcall-mark (macroexpand
2279
+ '(values? (make-values '()))
2280
+ ))
2281
+ EOS
2282
+ ).should == "(%tailcall (values? (make-values (quote ()))))"
2283
+ @nendo.evalStr( <<EOS
2284
+ (%setup-%tailcall-mark (macroexpand
2285
+ '(cond
2286
+ (false 1)
2287
+ (false 2))
2288
+ ))
2289
+ EOS
2290
+ ).should == "(if #f (begin 1) (if #f (begin 2) ()))"
2291
+ @nendo.evalStr( <<EOS
2292
+ (%setup-%tailcall-mark (macroexpand
2293
+ '(cond
2294
+ (false 1)
2295
+ (false 2)
2296
+ (else 3))
2297
+ ))
2298
+ EOS
2299
+ ).should == "(if #f (begin 1) (if #f (begin 2) (if #t (begin 3) ())))"
2300
+ @nendo.evalStr( <<EOS
2301
+ (%setup-%tailcall-mark (macroexpand
2302
+ '(and
2303
+ (foo 1)
2304
+ (bar 2))
2305
+ ))
2306
+ EOS
2307
+ ).should == "(if (not (eq? #f (foo 1))) (%tailcall (bar 2)) #f)"
2308
+ @nendo.evalStr( <<EOS
2309
+ (%setup-%tailcall-mark (macroexpand
2310
+ '(or
2311
+ (foo 1)
2312
+ (bar 2))
2313
+ ))
2314
+ EOS
2315
+ ).gsub( /[24]0[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))))"
2316
+ @nendo.evalStr( <<EOS
2317
+ (%setup-%tailcall-mark (macroexpand
2318
+ '(let loop ((x 1))
2319
+ 1
2320
+ 2
2321
+ (loop 100))
2322
+ ))
2323
+ EOS
2324
+ ).should == "(letrec ((loop (lambda (x) 1 2 (%tailcall (loop 100))))) (%tailcall (loop 1)))"
2325
+ @nendo.evalStr( "(%setup-%tailcall-mark (macroexpand "+
1908
2326
  " '(let1 aaa 111 aaa)"+
1909
- " ))" ).should == "(let ((aaa 111)) aaa)"
1910
- @nendo.evalStr( "" +
1911
- "(setup-tailcall-mark"+
1912
- " '(letrec ((func1 "+
1913
- " (lambda (x)"+
1914
- " 1"+
1915
- " (func2)))"+
1916
- " (func2 "+
1917
- " (lambda (x)"+
1918
- " 2"+
1919
- " (func1))))"+
1920
- " (func1 100)))" ).should == "(letrec ((func1 (lambda (x) 1 (%tailcall (func2)))) (func2 (lambda (x) 2 (%tailcall (func1))))) (%tailcall (func1 100)))"
2327
+ " ))" ).should == "(%let ((aaa 111)) aaa)"
2328
+ @nendo.evalStr( <<EOS
2329
+ (%setup-%tailcall-mark
2330
+ '(letrec ((func1
2331
+ (lambda (x)
2332
+ 1
2333
+ (func2)))
2334
+ (func2
2335
+ (lambda (x)
2336
+ 2
2337
+ (func1))))
2338
+ (func1 100)))
2339
+ EOS
2340
+ ).should == "(letrec ((func1 (lambda (x) 1 (%tailcall (func2)))) (func2 (lambda (x) 2 (%tailcall (func1))))) (%tailcall (func1 100)))"
1921
2341
  @nendo.evalStr( "(set-optimize-level 0) " ).should == "0"
1922
- lambda { @nendo.evalStr( "(filter (lambda (x) (< x 10)) (range 10000)) " ) }.should raise_error(SystemStackError)
2342
+ lambda { @nendo.evalStr( <<EOS
2343
+ (filter
2344
+ (lambda (x) (< x 10))
2345
+ (range 10000))
2346
+ EOS
2347
+ ) }.should raise_error(SystemStackError)
1923
2348
  @nendo.evalStr( "(set-optimize-level 1) " ).should == "1"
1924
- @nendo.evalStr( "(apply + (map (lambda (x) (* x 2)) (range 10000))) " ).should == "99990000"
1925
- @nendo.evalStr( "(filter (lambda (x) (< x 10)) (range 1000)) " ).should == "(0 1 2 3 4 5 6 7 8 9)"
1926
- @nendo.evalStr( "(filter-map (lambda (x) (when (< x 10) (- x))) (range 1000)) " ).should == "(0 -1 -2 -3 -4 -5 -6 -7 -8 -9)"
1927
- @nendo.evalStr( "(define str (if #t (car '(\"a\")) (car '(\"b\")))) (sprintf \"A%sZ\" str) " ).should == '"AaZ"'
1928
- @nendo.evalStr( "(letrec ((str (if #t (+ \"a\" \"A\") '()))) str.class) " ).should == 'String'
1929
- @nendo.evalStr( "(letrec ((str (if #t (+ \"a\" \"A\") '()))) (+ str \"...\")) " ).should == '"aA..."'
2349
+ @nendo.evalStr( <<EOS
2350
+ (apply +
2351
+ (map
2352
+ (lambda (x) (* x 2))
2353
+ (range 10000)))
2354
+ EOS
2355
+ ).should == "99990000"
2356
+ @nendo.evalStr( <<EOS
2357
+ (filter
2358
+ (lambda (x) (< x 10))
2359
+ (range 1000))
2360
+ EOS
2361
+ ).should == "(0 1 2 3 4 5 6 7 8 9)"
2362
+ @nendo.evalStr( <<EOS
2363
+ (filter-map
2364
+ (lambda (x)
2365
+ (when (< x 10) (- x)))
2366
+ (range 1000))
2367
+ EOS
2368
+ ).should == "(0 -1 -2 -3 -4 -5 -6 -7 -8 -9)"
2369
+ @nendo.evalStr( <<EOS
2370
+ (define str
2371
+ (if #t
2372
+ (car '("a"))
2373
+ (car '("b"))))
2374
+ (sprintf "A%sZ" str)
2375
+ EOS
2376
+ ).should == '"AaZ"'
2377
+ @nendo.evalStr( <<EOS
2378
+ (letrec ((str (if #t
2379
+ (+ "a" "A")
2380
+ '())))
2381
+ str.class)
2382
+ EOS
2383
+ ).should == 'String'
2384
+ @nendo.evalStr( <<EOS
2385
+ (letrec ((str
2386
+ (if #t
2387
+ (+ \"a\" \"A\")
2388
+ '())))
2389
+ (+ str \"...\"))
2390
+ EOS
2391
+ ).should == '"aA..."'
1930
2392
  end
1931
2393
  end
1932
2394