factor/extra/tools/interpreter/interpreter.factor

131 lines
2.9 KiB
Factor
Raw Normal View History

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
SYMBOL: interpreter
2007-09-20 18:09:08 -04:00
SYMBOL: break-hook
: break ( -- )
continuation callstack
over set-continuation-call
walker-hook [ continue-with ] [ break-hook get call ] if* ;
2007-09-20 18:09:08 -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
clone interpreter set ;
2007-09-20 18:09:08 -04:00
M: pair restore
2007-10-03 18:33:19 -04:00
first2 restore [ nip f ] curry with-interpreter-datastack ;
2007-09-20 18:09:08 -04:00
M: f restore
drop interpreter off ;
2007-09-20 18:09:08 -04:00
: (step-into-call) \ break add* call ;
2007-09-20 18:09:08 -04:00
: (step-into-if) ? (step-into-call) ;
2007-09-20 18:09:08 -04:00
: (step-into-dispatch)
nth (step-into-call) ;
2007-09-20 18:09:08 -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
{
{ call [ (step-into-call) ] }
{ (throw) [ (step-into-call) ] }
{ execute [ (step-into-execute) ] }
{ if [ (step-into-if) ] }
{ dispatch [ (step-into-dispatch) ] }
} [ "step-into" set-word-prop ] assoc-each
{
>n ndrop >c c>
continuation continue continue-with
(continue-with) stop break
} [
dup [ execute break ] curry
"step-into" set-word-prop
] each
2007-09-20 18:09:08 -04:00
! Time travel
SYMBOL: history
2007-09-20 18:09:08 -04:00
: save-interpreter ( -- )
history get [ interpreter get clone swap push ] when* ;
2007-09-20 18:09:08 -04:00
: step-back ( -- )
history get dup empty?
2007-10-03 18:33:19 -04:00
[ drop ] [ pop restore ] if ;
: (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
: step ( n -- )
[
2dup nth \ break = [
nip
2007-09-20 18:09:08 -04:00
] [
>r 1+ r> cut [ break ] swap 3append
2007-09-20 18:09:08 -04:00
] if
] (step) ;
2007-09-20 18:09:08 -04:00
: step-out ( -- )
[ nip \ break add ] (step) ;
2007-09-20 18:09:08 -04:00
GENERIC: (step-into) ( obj -- )
2007-09-20 18:09:08 -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
: step-into ( -- )
[
cut [
swap % unclip literalize , \ (step-into) , %
] [ ] make
] (step) ;