2008-07-20 05:24:37 -04:00
|
|
|
! Copyright (C) 2008 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-08-19 22:48:08 -04:00
|
|
|
USING: assocs fry kernel accessors sequences sequences.deep arrays
|
2008-08-02 00:31:43 -04:00
|
|
|
stack-checker.inlining namespaces compiler.tree ;
|
2008-07-20 05:24:37 -04:00
|
|
|
IN: compiler.tree.combinators
|
|
|
|
|
2008-07-30 04:38:10 -04:00
|
|
|
: each-node ( nodes quot: ( node -- ) -- )
|
2008-07-27 21:25:42 -04:00
|
|
|
dup dup '[
|
2008-09-10 23:11:40 -04:00
|
|
|
_ [
|
2008-07-27 21:25:42 -04:00
|
|
|
dup #branch? [
|
2008-09-10 23:11:40 -04:00
|
|
|
children>> [ _ each-node ] each
|
2008-07-27 21:25:42 -04:00
|
|
|
] [
|
|
|
|
dup #recursive? [
|
2008-09-10 23:11:40 -04:00
|
|
|
child>> _ each-node
|
2008-07-27 21:25:42 -04:00
|
|
|
] [ drop ] if
|
|
|
|
] if
|
|
|
|
] bi
|
2008-07-30 04:38:10 -04:00
|
|
|
] each ; inline recursive
|
2008-07-27 23:47:40 -04:00
|
|
|
|
|
|
|
: map-nodes ( nodes quot: ( node -- node' ) -- nodes )
|
|
|
|
dup dup '[
|
|
|
|
@
|
|
|
|
dup #branch? [
|
2008-09-10 23:11:40 -04:00
|
|
|
[ [ _ map-nodes ] map ] change-children
|
2008-07-27 23:47:40 -04:00
|
|
|
] [
|
|
|
|
dup #recursive? [
|
2008-09-10 23:11:40 -04:00
|
|
|
[ _ map-nodes ] change-child
|
2008-07-27 23:47:40 -04:00
|
|
|
] when
|
|
|
|
] if
|
|
|
|
] map flatten ; inline recursive
|
2008-07-30 04:38:10 -04:00
|
|
|
|
|
|
|
: contains-node? ( nodes quot: ( node -- ? ) -- ? )
|
|
|
|
dup dup '[
|
2008-09-10 23:11:40 -04:00
|
|
|
_ keep swap [ drop t ] [
|
2008-07-30 04:38:10 -04:00
|
|
|
dup #branch? [
|
2008-09-10 23:11:40 -04:00
|
|
|
children>> [ _ contains-node? ] contains?
|
2008-07-30 04:38:10 -04:00
|
|
|
] [
|
|
|
|
dup #recursive? [
|
2008-09-10 23:11:40 -04:00
|
|
|
child>> _ contains-node?
|
2008-07-30 04:38:10 -04:00
|
|
|
] [ drop f ] if
|
|
|
|
] if
|
|
|
|
] if
|
|
|
|
] contains? ; inline recursive
|
|
|
|
|
|
|
|
: select-children ( seq flags -- seq' )
|
|
|
|
[ [ drop f ] unless ] 2map ;
|
2008-08-01 21:04:36 -04:00
|
|
|
|
2008-08-19 22:48:08 -04:00
|
|
|
: sift-children ( seq flags -- seq' )
|
|
|
|
zip [ nip ] assoc-filter keys ;
|
|
|
|
|
2008-10-20 02:56:28 -04:00
|
|
|
: (3each) [ 3array flip ] dip '[ first3 @ ] ; inline
|
2008-08-01 21:04:36 -04:00
|
|
|
|
|
|
|
: 3each ( seq1 seq2 seq3 quot -- seq ) (3each) each ; inline
|
|
|
|
|
|
|
|
: 3map ( seq1 seq2 seq3 quot -- seq ) (3each) map ; inline
|
2008-08-02 00:31:43 -04:00
|
|
|
|
2008-08-14 00:52:49 -04:00
|
|
|
: until-fixed-point ( #recursive quot: ( node -- ) -- )
|
2008-08-02 00:31:43 -04:00
|
|
|
over label>> t >>fixed-point drop
|
|
|
|
[ with-scope ] 2keep
|
2008-08-14 00:52:49 -04:00
|
|
|
over label>> fixed-point>> [ 2drop ] [ until-fixed-point ] if ;
|
|
|
|
inline recursive
|