factor/basis/compiler/tree/combinators/combinators.factor

62 lines
1.7 KiB
Factor
Raw Normal View History

2008-07-20 05:24:37 -04:00
! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
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
: each-node ( nodes quot: ( node -- ) -- )
dup dup '[
2008-09-10 23:11:40 -04:00
_ [
dup #branch? [
2008-09-10 23:11:40 -04:00
children>> [ _ each-node ] each
] [
dup #recursive? [
2008-09-10 23:11:40 -04:00
child>> _ each-node
] [ drop ] if
] if
] bi
] each ; inline recursive
: map-nodes ( nodes quot: ( node -- node' ) -- nodes )
dup dup '[
@
dup #branch? [
2008-09-10 23:11:40 -04:00
[ [ _ map-nodes ] map ] change-children
] [
dup #recursive? [
2008-09-10 23:11:40 -04:00
[ _ map-nodes ] change-child
] when
] if
] map flatten ; inline recursive
: contains-node? ( nodes quot: ( node -- ? ) -- ? )
dup dup '[
2008-09-10 23:11:40 -04:00
_ keep swap [ drop t ] [
dup #branch? [
2008-09-10 23:11:40 -04:00
children>> [ _ contains-node? ] contains?
] [
dup #recursive? [
2008-09-10 23:11:40 -04:00
child>> _ contains-node?
] [ drop f ] if
] if
] if
] contains? ; inline recursive
: select-children ( seq flags -- seq' )
[ [ drop f ] unless ] 2map ;
: sift-children ( seq flags -- seq' )
zip [ nip ] assoc-filter keys ;
2008-10-20 02:56:28 -04:00
: (3each) [ 3array flip ] dip '[ first3 @ ] ; inline
: 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