1 IN: compiler.tree.recursive.tests
2 USING: compiler.tree.recursive tools.test
3 kernel combinators.short-circuit math sequences accessors
6 compiler.tree.combinators ;
8 [ { f f f f } ] [ f { f t f f } (tail-calls) ] unit-test
9 [ { f f f t } ] [ t { f t f f } (tail-calls) ] unit-test
10 [ { f t t t } ] [ t { f f t t } (tail-calls) ] unit-test
11 [ { f f f t } ] [ t { f f t f } (tail-calls) ] unit-test
13 \ analyze-recursive must-infer
15 : label-is-loop? ( nodes word -- ? )
19 [ drop label>> loop?>> ]
20 [ swap label>> word>> eq? ]
22 ] curry contains-node? ;
24 \ label-is-loop? must-infer
26 : label-is-not-loop? ( nodes word -- ? )
30 [ drop label>> loop?>> not ]
31 [ swap label>> word>> eq? ]
33 ] curry contains-node? ;
35 \ label-is-not-loop? must-infer
37 : loop-test-1 ( a -- )
38 dup [ 1+ loop-test-1 ] [ drop ] if ; inline recursive
41 [ loop-test-1 ] build-tree analyze-recursive
42 \ loop-test-1 label-is-loop?
46 [ loop-test-1 1 2 3 ] build-tree analyze-recursive
47 \ loop-test-1 label-is-loop?
51 [ [ loop-test-1 ] each ] build-tree analyze-recursive
52 \ loop-test-1 label-is-loop?
56 [ [ loop-test-1 ] each ] build-tree analyze-recursive
57 \ (each-integer) label-is-loop?
60 : loop-test-2 ( a -- )
61 dup [ 1+ loop-test-2 1- ] [ drop ] if ; inline recursive
64 [ loop-test-2 ] build-tree analyze-recursive
65 \ loop-test-2 label-is-not-loop?
68 : loop-test-3 ( a -- )
69 dup [ [ loop-test-3 ] each ] [ drop ] if ; inline recursive
72 [ loop-test-3 ] build-tree analyze-recursive
73 \ loop-test-3 label-is-not-loop?
76 : loop-test-4 ( a -- )
81 ] if ; inline recursive
84 [ [ [ ] map ] map ] build-tree analyze-recursive
86 dup #recursive? [ label>> loop?>> not ] [ drop f ] if
95 blah [ b ] [ a ] if ; inline recursive
98 blah [ b ] [ a ] if ; inline recursive
101 [ a ] build-tree analyze-recursive
106 [ a ] build-tree analyze-recursive
111 [ b ] build-tree analyze-recursive
116 [ a ] build-tree analyze-recursive
123 blah [ b' b' ] [ a' ] if ; inline recursive
126 blah [ b' ] [ a' ] if ; inline recursive
129 [ a' ] build-tree analyze-recursive
134 [ b' ] build-tree analyze-recursive
138 ! I used to think this should be f, but doing this on pen and
139 ! paper almost convinced me that a loop conversion here is
143 [ b' ] build-tree analyze-recursive
148 [ a' ] build-tree analyze-recursive
155 a'' ; inline recursive
158 b'' a'' ; inline recursive
161 [ a'' ] build-tree analyze-recursive
162 \ a'' label-is-not-loop?
165 : loop-in-non-loop ( x quot: ( i -- ) -- )
167 [ [ 1 - ] dip loop-in-non-loop ] [ call ] 2bi
168 ] [ 2drop ] if ; inline recursive
171 [ 10 [ [ drop ] each-integer ] loop-in-non-loop ]
172 build-tree analyze-recursive
173 \ (each-integer) label-is-loop?