1 ! Copyright (C) 2008, 2009 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 ;
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-macro ( word quot -- )
61 stack [ _ with-datastack >vector ] change
62 stack get pop >quotation end (expand-macros)
68 : expand-macro? ( word -- quot ? )
69 dup [ "transform-quot" word-prop ] [ "macro" word-prop ] bi or dup [
70 swap [ "transform-n" word-prop ] [ stack-effect in>> length ] bi or
74 M: word expand-macros*
75 dup expand-dispatch? [ drop expand-dispatch ] [
76 dup expand-macro? [ expand-macro ] [
81 M: object expand-macros* literal ;
83 M: callable expand-macros*
84 expand-macros literal ;
86 M: callable expand-macros ( quot -- quot' )
87 [ begin (expand-macros) end ] [ ] make ;