2010-01-27 03:00:10 -05:00
|
|
|
! Copyright (C) 2007, 2010 Slava Pestov.
|
2007-09-20 18:09:08 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-11-13 01:09:47 -05:00
|
|
|
USING: parser kernel sequences words effects combinators assocs
|
2010-02-01 00:15:24 -05:00
|
|
|
definitions quotations namespaces memoize accessors arrays
|
2009-11-13 04:52:14 -05:00
|
|
|
compiler.units ;
|
2007-09-20 18:09:08 -04:00
|
|
|
IN: macros
|
|
|
|
|
2009-02-06 11:22:09 -05:00
|
|
|
<PRIVATE
|
|
|
|
|
2010-02-01 00:15:24 -05:00
|
|
|
! 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.
|
|
|
|
|
2009-03-21 04:17:35 -04:00
|
|
|
: real-macro-effect ( effect -- effect' )
|
2009-03-23 01:34:02 -04:00
|
|
|
in>> { "quot" } <effect> ;
|
2008-01-09 17:52:37 -05:00
|
|
|
|
2010-02-01 00:15:24 -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>
|
|
|
|
|
2010-02-01 00:15:24 -05:00
|
|
|
: 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
|
|
|
|
2009-03-21 02:27:50 -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
|
|
|
|
2010-01-27 03:00:10 -05:00
|
|
|
M: macro make-inline cannot-be-inline ;
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
M: macro definer drop \ MACRO: \ ; ;
|
|
|
|
|
2010-02-01 00:15:24 -05:00
|
|
|
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
|
|
|
|
2010-02-01 00:15:24 -05:00
|
|
|
M: macro forget* [ call-next-method ] [ reset-macro ] bi ;
|
2009-11-13 04:52:14 -05:00
|
|
|
|
2010-01-29 08:58:39 -05:00
|
|
|
M: macro always-bump-effect-counter? drop t ;
|