]> gitweb.factorcode.org Git - factor.git/blob - extra/lsys/strings/rewrite/rewrite.factor
622a86c4251c0370fd09c06b260db0855695510c
[factor.git] / extra / lsys / strings / rewrite / rewrite.factor
1
2 USING: kernel sbufs strings sequences assocs math
3        combinators.lib vars lsys.strings combinators.short-circuit ;
4
5 IN: lsys.strings.rewrite
6
7 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
8
9 VAR: rules
10
11 : lookup ( str -- str ) [ 1 head rules> at ] [ ] bi or ;
12
13 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
14
15 VAR: accum
16
17 : push-next ( next -- ) lookup accum> push-all ;
18
19 : (rewrite) ( slice -- )
20   { { [ empty? ]     [ drop ] }
21     { [ has-param? ] [ next+rest* [ push-next ] [ (rewrite) ] bi* ] }
22     { [ t ]          [ next+rest  [ push-next ] [ (rewrite) ] bi* ] } }
23   switch ;
24
25 : rewrite ( string -- string )
26   dup length 10 * <sbuf> >accum
27   <flat-slice> (rewrite)
28   accum> >string ;
29
30 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
31
32 VAR: result
33
34 : iterate ( -- ) result> rewrite >result ;
35
36 : iterations ( n -- ) [ iterate ] times ;