1 ! Copyright (C) 2007, 2008 Slava Pestov, Eduardo Cavazos.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays fry kernel math quotations sequences
4 words combinators make locals.backend locals.types
6 IN: locals.rewrite.point-free
8 ! Step 3: rewrite locals usage within a single quotation into
9 ! retain stack manipulation
11 : local-index ( args obj -- n )
12 2dup '[ unquote _ eq? ] find drop
13 dup [ 2nip ] [ drop bad-local ] if ;
15 : read-local-quot ( args obj -- quot )
16 local-index neg [ get-local ] curry ;
18 GENERIC: localize ( args obj -- args quot )
20 M: local localize dupd read-local-quot ;
22 M: quote localize dupd local>> read-local-quot ;
24 M: local-word localize dupd read-local-quot [ call ] append ;
26 M: local-reader localize dupd read-local-quot [ local-value ] append ;
28 M: local-writer localize
29 dupd "local-reader" word-prop
30 read-local-quot [ set-local-value ] append ;
35 [ local-reader? [ 1array load-local ] [ load-local ] ? ]
38 M: object localize 1quotation ;
40 ! We special-case all the :> at the start of a quotation
41 : load-locals-quot ( args -- quot )
43 dup [ local-reader? ] any? [
44 dup [ local-reader? [ 1array ] [ ] ? ] map
46 ] [ [ ] ] if swap length [ load-locals ] curry append
49 : load-locals-index ( quot -- n )
50 [ [ dup def? [ local>> local-reader? ] [ drop t ] if ] find drop ]
53 : point-free-start ( quot -- args rest )
55 cut [ [ local>> ] map dup <reversed> load-locals-quot % ] dip ;
57 : point-free-body ( args quot -- args )
60 : drop-locals-quot ( args -- )
61 [ length , [ drop-locals ] % ] unless-empty ;
63 : point-free-end ( args obj -- )
65 [ localize % drop-locals-quot ]
66 [ [ drop-locals-quot ] [ , ] bi* ]
69 : point-free ( quot -- newquot )
72 [ drop-locals-quot ] [