factor/basis/tools/trace/trace.factor

72 lines
1.8 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: accessors classes combinators.short-circuit effects
generic.math io io.styles kernel kernel.private make math.parser
namespaces prettyprint quotations sequences strings summary
tools.continuations words ;
2009-04-08 07:23:07 -04:00
IN: tools.trace
2009-05-07 00:47:17 -04:00
<PRIVATE
: callstack-depth ( callstack -- n )
2011-10-15 22:19:44 -04:00
callstack>array midpoint@ ;
2009-05-07 00:47:17 -04:00
SYMBOL: end
2009-04-08 07:23:07 -04:00
: into? ( obj -- ? )
2009-04-15 23:16:52 -04:00
{
[ word? ]
[ predicate? not ]
[ math-generic? not ]
[
[ inline? ]
[ vocabulary>> { "math" "accessors" } member? not ] bi or
2009-04-15 23:16:52 -04:00
]
} 1&& ;
2009-04-08 07:23:07 -04:00
2013-03-24 13:04:44 -04:00
TUPLE: trace-step-state word inputs ;
2009-04-08 07:23:07 -04:00
2013-03-24 13:04:44 -04:00
M: trace-step-state summary
2009-04-08 07:23:07 -04:00
[
[ "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
2013-03-24 13:04:44 -04:00
\ trace-step-state boa ;
2009-04-08 07:23:07 -04:00
: 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
2011-10-15 22:19:44 -04:00
<< \ trace t "no-compile" set-word-prop >>