1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: assocs fry kernel accessors sequences compiler.utilities
4 arrays stack-checker.inlining namespaces compiler.tree
6 IN: compiler.tree.combinators
8 : each-node ( nodes quot: ( node -- ) -- )
12 children>> [ _ each-node ] each
19 ] each ; inline recursive
21 : map-nodes ( nodes quot: ( node -- node' ) -- nodes )
25 [ [ _ map-nodes ] map ] change-children
28 [ _ map-nodes ] change-child
31 ] map-flat ; inline recursive
33 : contains-node? ( nodes quot: ( node -- ? ) -- ? )
35 _ keep swap [ drop t ] [
37 children>> [ _ contains-node? ] any?
40 child>> _ contains-node?
44 ] any? ; inline recursive
46 : select-children ( seq flags -- seq' )
47 [ [ drop f ] unless ] 2map ;
49 : sift-children ( seq flags -- seq' )
50 zip [ nip ] assoc-filter keys ;
52 : until-fixed-point ( #recursive quot: ( node -- ) -- )
53 over label>> t >>fixed-point drop
55 over label>> fixed-point>> [ 2drop ] [ until-fixed-point ] if ;