1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel continuations assocs namespaces sequences words
4 vocabs definitions hashtables init sets ;
7 SYMBOL: old-definitions
8 SYMBOL: new-definitions
10 TUPLE: redefine-error def ;
12 : redefine-error ( definition -- )
14 { { "Continue" t } } throw-restarts drop ;
16 : add-once ( key assoc -- )
17 2dup key? [ over redefine-error ] when conjoin ;
19 : (remember-definition) ( definition loc assoc -- )
20 >r over set-where r> add-once ;
22 : remember-definition ( definition loc -- )
23 new-definitions get first (remember-definition) ;
25 : remember-class ( class loc -- )
26 over new-definitions get first key? [ dup redefine-error ] when
27 new-definitions get second (remember-definition) ;
29 : forward-reference? ( word -- ? )
30 dup old-definitions get assoc-stack
31 [ new-definitions get assoc-stack not ]
34 SYMBOL: recompile-hook
36 : <definitions> ( -- pair ) { H{ } H{ } } [ clone ] map ;
38 SYMBOL: definition-observers
40 GENERIC: definitions-changed ( assoc obj -- )
42 [ V{ } clone definition-observers set-global ]
43 "compiler.units" add-init-hook
45 : add-definition-observer ( obj -- )
46 definition-observers get push ;
48 : remove-definition-observer ( obj -- )
49 definition-observers get delete ;
51 : notify-definition-observers ( assoc -- )
52 definition-observers get
53 [ definitions-changed ] with each ;
55 : changed-vocabs ( assoc -- vocabs )
56 [ drop word? ] assoc-filter
57 [ drop word-vocabulary dup [ vocab ] when dup ] assoc-map ;
59 : updated-definitions ( -- assoc )
61 dup forgotten-definitions get update
62 dup new-definitions get first update
63 dup new-definitions get second update
64 dup changed-definitions get update
65 dup dup changed-vocabs update ;
67 : compile ( words -- )
68 recompile-hook get call
69 dup [ drop crossref? ] assoc-contains?
72 SYMBOL: outdated-tuples
73 SYMBOL: update-tuples-hook
75 : call-recompile-hook ( -- )
76 changed-definitions get [ drop word? ] assoc-filter
77 compiled-usages recompile-hook get call ;
79 : call-update-tuples-hook ( -- )
80 update-tuples-hook get call ;
82 : finish-compilation-unit ( -- )
84 call-update-tuples-hook
85 dup [ drop crossref? ] assoc-contains? modify-code-heap ;
87 : with-nested-compilation-unit ( quot -- )
89 H{ } clone changed-definitions set
90 H{ } clone outdated-tuples set
91 [ finish-compilation-unit ] [ ] cleanup
94 : with-compilation-unit ( quot -- )
96 H{ } clone changed-definitions set
97 H{ } clone forgotten-definitions set
98 H{ } clone outdated-tuples set
99 H{ } clone new-classes set
100 <definitions> new-definitions set
101 <definitions> old-definitions set
103 finish-compilation-unit
105 notify-definition-observers
107 ] with-scope ; inline
109 : compile-call ( quot -- )
110 [ define-temp ] with-compilation-unit execute ;
112 : default-recompile-hook ( words -- alist )
113 [ f ] { } map>assoc ;
115 recompile-hook global
116 [ [ default-recompile-hook ] or ]