2007-09-20 18:09:08 -04:00
|
|
|
! Copyright (C) 2004, 2007 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
|
|
|
USING: arrays assocs classes combinators combinators.private
|
|
|
|
continuations continuations.private generic hashtables io kernel
|
|
|
|
kernel.private math namespaces namespaces.private prettyprint
|
|
|
|
quotations sequences splitting strings threads vectors words ;
|
|
|
|
IN: tools.interpreter
|
|
|
|
|
2007-10-03 16:56:49 -04:00
|
|
|
SYMBOL: interpreter
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
SYMBOL: break-hook
|
|
|
|
|
|
|
|
: break ( -- )
|
2007-10-03 16:56:49 -04:00
|
|
|
continuation callstack
|
|
|
|
over set-continuation-call
|
|
|
|
walker-hook [ continue-with ] [ break-hook get call ] if* ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2007-10-03 16:56:49 -04:00
|
|
|
: with-interpreter-datastack ( quot -- )
|
|
|
|
interpreter get continuation-data
|
|
|
|
swap with-datastack
|
|
|
|
interpreter get set-continuation-data ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
GENERIC: restore ( obj -- )
|
|
|
|
|
|
|
|
M: continuation restore
|
2007-10-03 16:56:49 -04:00
|
|
|
clone interpreter set ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
M: pair restore
|
2007-10-03 20:49:17 -04:00
|
|
|
first2 clone interpreter set
|
|
|
|
[ nip f ] curry with-interpreter-datastack ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
M: f restore
|
2007-10-03 16:56:49 -04:00
|
|
|
drop interpreter off ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2007-10-03 16:56:49 -04:00
|
|
|
: (step-into-call) \ break add* call ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2007-10-03 16:56:49 -04:00
|
|
|
: (step-into-if) ? (step-into-call) ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2007-10-03 16:56:49 -04:00
|
|
|
: (step-into-dispatch)
|
|
|
|
nth (step-into-call) ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2007-10-03 16:56:49 -04:00
|
|
|
: (step-into-execute) ( word -- )
|
|
|
|
dup "step-into" word-prop [
|
|
|
|
call
|
|
|
|
] [
|
|
|
|
dup compound? [
|
|
|
|
word-def (step-into-call)
|
|
|
|
] [
|
|
|
|
execute break
|
|
|
|
] if
|
|
|
|
] ?if ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2007-10-03 20:49:17 -04:00
|
|
|
: (step-into-continuation)
|
|
|
|
continuation callstack over set-continuation-call break ;
|
|
|
|
|
2007-10-03 16:56:49 -04:00
|
|
|
{
|
|
|
|
{ call [ (step-into-call) ] }
|
|
|
|
{ (throw) [ (step-into-call) ] }
|
|
|
|
{ execute [ (step-into-execute) ] }
|
|
|
|
{ if [ (step-into-if) ] }
|
|
|
|
{ dispatch [ (step-into-dispatch) ] }
|
2007-10-03 20:49:17 -04:00
|
|
|
{ continuation [ (step-into-continuation) ] }
|
2007-10-03 16:56:49 -04:00
|
|
|
} [ "step-into" set-word-prop ] assoc-each
|
|
|
|
|
|
|
|
{
|
|
|
|
>n ndrop >c c>
|
2007-10-03 20:49:17 -04:00
|
|
|
continue continue-with
|
2007-10-03 16:56:49 -04:00
|
|
|
(continue-with) stop break
|
|
|
|
} [
|
|
|
|
dup [ execute break ] curry
|
|
|
|
"step-into" set-word-prop
|
|
|
|
] each
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2007-10-03 16:56:49 -04:00
|
|
|
! Time travel
|
|
|
|
SYMBOL: history
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2007-10-03 16:56:49 -04:00
|
|
|
: save-interpreter ( -- )
|
|
|
|
history get [ interpreter get clone swap push ] when* ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2007-10-03 20:49:17 -04:00
|
|
|
: restore-interpreter ( interp -- )
|
|
|
|
clone interpreter set ;
|
|
|
|
|
2007-10-03 16:56:49 -04:00
|
|
|
: step-back ( -- )
|
|
|
|
history get dup empty?
|
2007-10-03 20:49:17 -04:00
|
|
|
[ drop ] [ pop restore-interpreter ] if ;
|
2007-10-03 16:56:49 -04:00
|
|
|
|
|
|
|
: (continue) ( continuation -- )
|
|
|
|
>continuation<
|
|
|
|
set-catchstack
|
|
|
|
set-namestack
|
|
|
|
set-retainstack
|
|
|
|
>r set-datastack r>
|
|
|
|
set-callstack ;
|
|
|
|
|
|
|
|
! Stepping
|
|
|
|
: step-all ( -- )
|
|
|
|
[ interpreter get (continue) ] in-thread ;
|
|
|
|
|
|
|
|
: change-innermost-frame ( quot -- )
|
|
|
|
interpreter get continuation-call clone
|
|
|
|
[
|
|
|
|
dup innermost-frame-scan 1+
|
|
|
|
swap innermost-frame-quot
|
|
|
|
rot call
|
|
|
|
] keep
|
|
|
|
[ set-innermost-frame-quot ] keep
|
|
|
|
interpreter get set-continuation-call ; inline
|
|
|
|
|
|
|
|
: (step) ( quot -- )
|
|
|
|
save-interpreter
|
|
|
|
change-innermost-frame
|
|
|
|
[ set-walker-hook interpreter get (continue) ] callcc1
|
|
|
|
restore ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2007-10-03 16:56:49 -04:00
|
|
|
: step ( n -- )
|
|
|
|
[
|
|
|
|
2dup nth \ break = [
|
|
|
|
nip
|
2007-09-20 18:09:08 -04:00
|
|
|
] [
|
2007-10-03 16:56:49 -04:00
|
|
|
>r 1+ r> cut [ break ] swap 3append
|
2007-09-20 18:09:08 -04:00
|
|
|
] if
|
2007-10-03 16:56:49 -04:00
|
|
|
] (step) ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: step-out ( -- )
|
2007-10-03 16:56:49 -04:00
|
|
|
[ nip \ break add ] (step) ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2007-10-03 16:56:49 -04:00
|
|
|
GENERIC: (step-into) ( obj -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2007-10-03 16:56:49 -04:00
|
|
|
M: word (step-into) (step-into-execute) ;
|
|
|
|
M: wrapper (step-into) wrapped break ;
|
|
|
|
M: object (step-into) break ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2007-10-03 16:56:49 -04:00
|
|
|
: step-into ( -- )
|
|
|
|
[
|
|
|
|
cut [
|
|
|
|
swap % unclip literalize , \ (step-into) , %
|
|
|
|
] [ ] make
|
|
|
|
] (step) ;
|