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 : label-is-loop? ( nodes word -- ? )
17 [ drop label>> loop?>> ]
18 [ swap label>> word>> eq? ]
20 ] curry contains-node? ;
22 : label-is-not-loop? ( nodes word -- ? )
26 [ drop label>> loop?>> not ]
27 [ swap label>> word>> eq? ]
29 ] curry contains-node? ;
31 : loop-test-1 ( a -- )
32 dup [ 1+ loop-test-1 ] [ drop ] if ; inline recursive
35 [ loop-test-1 ] build-tree analyze-recursive
36 \ loop-test-1 label-is-loop?
40 [ loop-test-1 1 2 3 ] build-tree analyze-recursive
41 \ loop-test-1 label-is-loop?
45 [ [ loop-test-1 ] each ] build-tree analyze-recursive
46 \ loop-test-1 label-is-loop?
50 [ [ loop-test-1 ] each ] build-tree analyze-recursive
51 \ (each-integer) label-is-loop?
54 : loop-test-2 ( a b -- a' )
55 dup [ 1+ loop-test-2 1- ] [ drop ] if ; inline recursive
58 [ loop-test-2 ] build-tree analyze-recursive
59 \ loop-test-2 label-is-not-loop?
62 : loop-test-3 ( a -- )
63 dup [ [ loop-test-3 ] each ] [ drop ] if ; inline recursive
66 [ loop-test-3 ] build-tree analyze-recursive
67 \ loop-test-3 label-is-not-loop?
70 : loop-test-4 ( a -- )
75 ] if ; inline recursive
78 [ [ [ ] map ] map ] build-tree analyze-recursive
80 dup #recursive? [ label>> loop?>> not ] [ drop f ] if
84 : blah ( -- value ) f ;
89 blah [ b ] [ a ] if ; inline recursive
92 blah [ b ] [ a ] if ; inline recursive
95 [ a ] build-tree analyze-recursive
100 [ a ] build-tree analyze-recursive
105 [ b ] build-tree analyze-recursive
110 [ a ] build-tree analyze-recursive
117 blah [ b' b' ] [ a' ] if ; inline recursive
120 blah [ b' ] [ a' ] if ; inline recursive
123 [ a' ] build-tree analyze-recursive
128 [ b' ] build-tree analyze-recursive
132 ! I used to think this should be f, but doing this on pen and
133 ! paper almost convinced me that a loop conversion here is
137 [ b' ] build-tree analyze-recursive
142 [ a' ] build-tree analyze-recursive
149 a'' ; inline recursive
152 b'' a'' ; inline recursive
155 [ a'' ] build-tree analyze-recursive
156 \ a'' label-is-not-loop?
159 : loop-in-non-loop ( x quot: ( i -- ) -- )
161 [ [ 1 - ] dip loop-in-non-loop ] [ call ] 2bi
162 ] [ 2drop ] if ; inline recursive
165 [ 10 [ [ drop ] each-integer ] loop-in-non-loop ]
166 build-tree analyze-recursive
167 \ (each-integer) label-is-loop?