1 ! Copyright (C) 2007, 2008 Slava Pestov, Eduardo Cavazos.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays combinators fry.private kernel
4 locals.backend locals.errors locals.types make math quotations
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 [ 2nip ] [ 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-reader localize dupd read-local-quot [ local-value ] append ;
26 M: local-writer localize
27 dupd "local-reader" word-prop
28 read-local-quot [ set-local-value ] append ;
33 [ [ [ local-reader? ] dip '[ [ 1array ] _ [ndip] ] [ [ ] ] if ] map-index concat ]
34 [ length dup 1 > [ [ load-locals ] curry ] [ drop [ load-local ] ] if ]
37 M: object localize 1quotation ;
39 : drop-locals-quot ( args -- )
40 [ length , [ drop-locals ] % ] unless-empty ;
42 : point-free ( quot -- newquot )
43 [ { } swap [ localize % ] each drop-locals-quot ] [ ] make ;