1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel math sorting words parser io summary
4 quotations sequences prettyprint continuations effects
5 definitions compiler.units namespaces assocs tools.walker
6 tools.time generic inspector fry tools.continuations ;
9 GENERIC: reset ( word -- )
12 subwords [ reset ] each ;
15 dup "unannotated-def" word-prop [
17 dup dup "unannotated-def" word-prop define
18 ] with-compilation-unit
19 f "unannotated-def" set-word-prop
22 ERROR: cannot-annotate-twice word ;
24 M: cannot-annotate-twice summary drop "Cannot annotate a word twice" ;
28 : check-annotate-twice ( word -- word )
29 dup "unannotated-def" word-prop [
35 GENERIC# annotate 1 ( word quot -- )
38 [ "methods" word-prop values ] dip '[ _ annotate ] each ;
41 [ check-annotate-twice ] dip
43 [ dup def>> 2dup "unannotated-def" set-word-prop ] dip
44 call( old -- new ) define
45 ] with-compilation-unit ;
49 : stack-values ( names -- alist )
50 [ datastack ] dip [ nip ] [ length tail* ] 2bi zip ;
52 : trace-message ( word quot str -- )
53 "--- " write write bl over .
54 [ stack-effect ] dip '[ @ stack-values ] [ f ] if*
55 [ simple-table. ] unless-empty flush ; inline
57 : entering ( str -- ) [ in>> ] "Entering" trace-message ;
59 : leaving ( str -- ) [ out>> ] "Leaving" trace-message ;
61 : (watch) ( word def -- def )
62 over '[ _ entering @ _ leaving ] ;
67 dup '[ [ _ ] dip (watch) ] annotate ;
71 : (watch-vars) ( word vars quot -- newquot )
73 "--- Entering: " write _ .
74 "--- Variable values:" print _ [ dup get ] H{ } map>assoc describe
80 : watch-vars ( word vars -- )
81 dupd '[ [ _ _ ] dip (watch-vars) ] annotate ;
83 : breakpoint ( word -- )
84 [ add-breakpoint ] annotate ;
86 : breakpoint-if ( word quot -- )
87 '[ [ _ [ [ break ] when ] ] dip 3append ] annotate ;
91 word-timing [ H{ } clone ] initialize
93 : reset-word-timing ( -- )
94 word-timing get clear-assoc ;
98 : (add-timing) ( def word -- def' )
99 '[ _ benchmark _ word-timing get at+ ] ;
103 : add-timing ( word -- )
104 dup '[ _ (add-timing) ] annotate ;
106 : word-timing. ( -- )
108 >alist [ 1000000 /f ] assoc-map sort-values