factor/basis/macros/macros.factor

63 lines
1.7 KiB
Factor
Raw Normal View History

! Copyright (C) 2007, 2010 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: parser kernel sequences words effects combinators assocs
definitions quotations namespaces memoize accessors arrays
compiler.units ;
2007-09-20 18:09:08 -04:00
IN: macros
2009-02-06 11:22:09 -05:00
<PRIVATE
! The macro expander is split off into its own word. This allows
! the optimizing compiler to optimize and check the stack effect
! of the expander, even though the actual macro word does not
! infer.
: real-macro-effect ( effect -- effect' )
2009-03-23 01:34:02 -04:00
in>> { "quot" } <effect> ;
2008-01-09 17:52:37 -05:00
PREDICATE: macro-body < memoized "macro-owner" word-prop >boolean ;
: <macro-body> ( word quot effect -- macro-body )
real-macro-effect
[ name>> "( macro body: " " )" surround <uninterned-word> dup ] 2dip
define-memoized ;
M: macro-body crossref? "forgotten" word-prop not ;
M: macro-body reset-word
[ call-next-method ] [ "macro-body" remove-word-prop ] bi ;
M: macro-body where "macro-owner" word-prop where ;
: reset-macro ( word -- )
[ "macro" word-prop forget ] [ f "macro" set-word-prop ] bi ;
2009-02-06 11:22:09 -05:00
PRIVATE>
: define-macro ( word quot effect -- )
[ 2drop ] [ <macro-body> ] 3bi
{
[ "macro" set-word-prop ]
[ swap "macro-owner" set-word-prop ]
[ [ \ call [ ] 2sequence ] [ stack-effect ] bi define-declared ]
[ drop changed-effect ]
} 2cleave ;
2007-09-20 18:09:08 -04:00
SYNTAX: MACRO: (:) define-macro ;
2007-09-20 18:09:08 -04:00
2008-03-26 19:23:19 -04:00
PREDICATE: macro < word "macro" word-prop >boolean ;
2007-09-20 18:09:08 -04:00
M: macro make-inline cannot-be-inline ;
2007-09-20 18:09:08 -04:00
M: macro definer drop \ MACRO: \ ; ;
M: macro definition "macro" word-prop definition ;
M: macro subwords "macro" word-prop 1array ;
M: macro reset-word [ call-next-method ] [ reset-macro ] bi ;
2007-09-20 18:09:08 -04:00
M: macro forget* [ call-next-method ] [ reset-macro ] bi ;
M: macro always-bump-effect-counter? drop t ;