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 ]
36 { [ dup 1 > ] [ [ load-locals ] curry ] }
37 { [ dup 1 = ] [ drop [ load-local ] ] }
42 M: object localize 1quotation ;
44 : drop-locals-quot ( args -- )
45 [ length , [ drop-locals ] % ] unless-empty ;
47 : point-free ( quot -- newquot )
48 [ { } swap [ localize % ] each drop-locals-quot ] [ ] make ;