1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel sequences namespaces quotations accessors words
4 continuations vectors effects math stack-checker.transforms ;
7 GENERIC: expand-macros ( quot -- quot' )
13 : begin ( -- ) V{ } clone stack set ;
17 [ [ literalize , ] each ]
21 : literal ( obj -- ) stack get push ;
23 GENERIC: expand-macros* ( obj -- )
25 : (expand-macros) ( quot -- )
26 [ expand-macros* ] each ;
28 M: wrapper expand-macros* wrapped>> literal ;
30 : expand-macro ( quot -- )
31 stack [ swap with-datastack >vector ] change
32 stack get pop >quotation end (expand-macros) ;
34 : expand-macro? ( word -- quot ? )
35 dup [ "macro" word-prop ] [ "transform-quot" word-prop ] bi or dup [
36 swap [ stack-effect in>> length ] [ "transform-n" word-prop ] bi or
40 M: word expand-macros*
41 dup expand-macro? [ nip expand-macro ] [ drop end , ] if ;
43 M: object expand-macros* literal ;
45 M: callable expand-macros*
46 expand-macros literal ;
48 M: callable expand-macros ( quot -- quot' )
49 [ begin (expand-macros) end ] [ ] make ;