factor/basis/tools/trace/trace.factor

90 lines
2.3 KiB
Factor
Raw Normal View History

2009-04-08 07:23:07 -04:00
! Copyright (C) 2009 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: concurrency.promises models tools.continuations kernel
2009-04-15 23:16:52 -04:00
sequences concurrency.messaging locals continuations threads
namespaces namespaces.private make assocs accessors io strings
prettyprint math math.parser words effects summary io.styles classes
2009-05-07 00:47:17 -04:00
generic.math combinators.short-circuit kernel.private quotations ;
2009-04-08 07:23:07 -04:00
IN: tools.trace
SYMBOL: exclude-vocabs
SYMBOL: include-vocabs
2009-04-15 23:16:52 -04:00
exclude-vocabs { "math" "accessors" } swap set-global
2009-04-08 07:23:07 -04:00
2009-05-07 00:47:17 -04:00
<PRIVATE
: callstack-depth ( callstack -- n )
callstack>array length 2/ ;
SYMBOL: end
2009-04-08 07:23:07 -04:00
: include? ( vocab -- ? )
include-vocabs get dup [ member? ] [ 2drop t ] if ;
: exclude? ( vocab -- ? )
exclude-vocabs get dup [ member? ] [ 2drop f ] if ;
: into? ( obj -- ? )
2009-04-15 23:16:52 -04:00
{
[ word? ]
[ predicate? not ]
[ math-generic? not ]
[
{
[ inline? ]
[
{
[ vocabulary>> include? ]
[ vocabulary>> exclude? not ]
} 1&&
]
} 1||
]
} 1&& ;
2009-04-08 07:23:07 -04:00
TUPLE: trace-step word inputs ;
M: trace-step summary
[
[ "Word: " % word>> name>> % ]
[ " -- inputs: " % inputs>> unparse-short % ] bi
] "" make ;
: <trace-step> ( continuation word -- trace-step )
[ nip ] [ [ data>> ] [ stack-effect in>> length ] bi* short tail* ] 2bi
\ trace-step boa ;
: print-step ( continuation -- )
dup continuation-current dup word? [
[ nip name>> ] [ <trace-step> ] 2bi write-object nl
] [
nip short.
] if ;
2009-04-15 23:16:52 -04:00
: print-depth ( continuation -- )
call>> callstack-depth
[ CHAR: \s <string> write ]
[ number>string write ": " write ] bi ;
2009-05-07 00:47:17 -04:00
: trace-into? ( continuation -- ? )
continuation-current into? ;
2009-04-08 07:23:07 -04:00
: trace-step ( continuation -- continuation' )
2009-05-07 00:47:17 -04:00
dup call>> innermost-frame-executing quotation? [
dup continuation-current end eq? [
[ print-depth ]
[ print-step ]
[ dup trace-into? [ continuation-step-into ] [ continuation-step ] if ]
tri
] unless
] when ;
PRIVATE>
2009-04-08 07:23:07 -04:00
: trace ( quot -- data )
[ [ trace-step ] break-hook ] dip
[ break ] [ end drop ] surround
with-variable ;
2009-04-17 00:14:26 -04:00
<< \ trace t "no-compile" set-word-prop >>