1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel sequences sequences.private namespaces make
4 quotations accessors words continuations vectors effects math
5 generalizations fry arrays combinators ;
8 GENERIC: expand-macros ( quot -- quot' )
12 : begin ( -- ) V{ } clone stack set ;
16 [ [ literalize , ] each ]
20 GENERIC: condomize? ( obj -- ? )
22 M: array condomize? [ condomize? ] any? ;
24 M: callable condomize? [ condomize? ] any? ;
26 M: object condomize? drop f ;
28 GENERIC: condomize ( obj -- obj' )
30 M: array condomize [ condomize ] map ;
32 M: callable condomize [ condomize ] map ;
36 : literal ( obj -- ) dup condomize? [ condomize ] when stack get push ;
38 GENERIC: expand-macros* ( obj -- )
40 : (expand-macros) ( quot -- )
41 [ expand-macros* ] each ;
43 M: wrapper expand-macros* wrapped>> literal ;
45 : expand-dispatch? ( word -- ? )
46 \ dispatch eq? stack get length 1 >= and ;
48 : expand-dispatch ( -- )
50 [ [ expand-macros ] [ ] map-as '[ _ dip ] % ]
52 length iota [ <reversed> ] keep
53 [ '[ _ ndrop _ nnip call ] [ ] like ] 2map , \ dispatch ,
56 : word, ( word -- ) end , ;
58 : expand-transform ( word quot -- )
61 stack [ _ with-datastack >vector ] change
62 stack get pop >quotation end (expand-macros)
68 : expand-transform? ( word -- ? )
69 dup "transform-quot" word-prop [
70 "transform-n" word-prop
74 : expand-macro? ( word -- ? )
75 dup "macro" word-prop [
76 stack-effect in>> length
80 M: word expand-macros*
82 { [ dup expand-dispatch? ] [ drop expand-dispatch ] }
83 { [ dup expand-macro? ] [ dup "macro" word-prop '[ _ execute ] expand-transform ] }
84 { [ dup expand-transform? ] [ dup "transform-quot" word-prop expand-transform ] }
88 M: object expand-macros* literal ;
90 M: callable expand-macros*
91 expand-macros literal ;
93 M: callable expand-macros ( quot -- quot' )
94 [ begin (expand-macros) end ] [ ] make ;