1 ! Copyright (C) 2008, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays combinators combinators.short-circuit
4 continuations effects generalizations kernel make math
5 namespaces quotations sequences sequences.private vectors words ;
8 GENERIC: expand-macros ( quot -- quot' )
13 V{ } clone stack set ;
16 stack get [ [ literalize , ] each ] [ delete-all ] bi ;
18 GENERIC: condomize? ( obj -- ? )
20 M: array condomize? [ condomize? ] any? ;
21 M: callable condomize? [ condomize? ] any? ;
22 M: object condomize? drop f ;
24 GENERIC: condomize ( obj -- obj' )
26 M: array condomize [ condomize ] map ;
27 M: callable condomize [ condomize ] map ;
31 dup condomize? [ condomize ] when stack get push ;
33 GENERIC: expand-macros* ( obj -- )
35 M: wrapper expand-macros* wrapped>> literal ;
37 : expand-dispatch? ( word -- ? )
38 \ dispatch eq? stack get length 1 >= and ;
40 : expand-dispatch ( -- )
42 [ [ expand-macros ] [ ] map-as '[ _ dip ] % ]
44 length <iota> [ <reversed> ] keep
45 [ '[ _ ndrop _ nnip call ] [ ] like ] 2map , \ dispatch ,
48 : word, ( word -- ) end , ;
50 : expand-macro ( word quot -- )
53 stack [ _ with-datastack >vector ] change
54 stack get pop >quotation end
55 [ expand-macros* ] each
61 : macro-quot ( word -- quot/f )
63 [ "transform-quot" word-prop ]
67 : macro-effect ( word -- n )
69 [ "transform-n" word-prop ]
70 [ stack-effect in>> length ]
73 : expand-macro? ( word -- quot ? )
75 swap macro-effect stack get length <=
80 M: word expand-macros*
82 { [ dup expand-dispatch? ] [ drop expand-dispatch ] }
83 { [ dup expand-macro? ] [ expand-macro ] }
87 M: object expand-macros* literal ;
89 M: callable expand-macros*
90 expand-macros literal ;
92 M: callable expand-macros
93 [ begin [ expand-macros* ] each end ] [ ] make ;