factor/extra/tools/interpreter/interpreter.factor

115 lines
2.7 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
2007-10-04 18:45:19 -04:00
TUPLE: interpreter continuation ;
: <interpreter> interpreter construct-empty ;
2007-09-20 18:09:08 -04:00
2007-10-04 18:45:19 -04:00
GENERIC# restore 1 ( obj interpreter -- )
2007-09-20 18:09:08 -04:00
2007-10-04 18:45:19 -04:00
M: f restore
set-interpreter-continuation ;
2007-09-20 18:09:08 -04:00
M: continuation restore
2007-10-04 18:45:19 -04:00
>r clone r> set-interpreter-continuation ;
: with-interpreter-datastack ( quot interpreter -- )
interpreter-continuation [
continuation-data
swap with-datastack
] keep set-continuation-data ; inline
2007-09-20 18:09:08 -04:00
M: pair restore
2007-10-04 18:45:19 -04:00
>r first2 r> [ restore ] keep
>r [ nip f ] curry r> with-interpreter-datastack ;
2007-09-20 18:09:08 -04:00
2007-10-04 18:45:19 -04:00
<PRIVATE
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
2007-10-03 20:49:17 -04:00
: (step-into-continuation)
continuation callstack over set-continuation-call break ;
M: word (step-into) (step-into-execute) ;
{
{ 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) ] }
} [ "step-into" set-word-prop ] assoc-each
{
>n ndrop >c c>
2007-10-03 20:49:17 -04:00
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
! Stepping
2007-10-04 18:45:19 -04:00
: change-innermost-frame ( quot interpreter -- )
interpreter-continuation [
continuation-call clone
[
dup innermost-frame-scan 1+
swap innermost-frame-quot
rot call
] keep
[ set-innermost-frame-quot ] keep
] keep set-continuation-call ; inline
: (step) ( interpreter quot -- )
swap
[ change-innermost-frame ] keep
[ interpreter-continuation with-walker-hook ] keep
restore ;
2007-10-04 18:45:19 -04:00
PRIVATE>
: step ( interpreter -- )
[
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
2007-10-04 18:45:19 -04:00
: step-out ( interpreter -- )
[ nip \ break add ] (step) ;
2007-09-20 18:09:08 -04:00
2007-10-04 18:45:19 -04:00
: step-into ( interpreter -- )
[
cut [
swap % unclip literalize , \ (step-into) , %
] [ ] make
] (step) ;
2007-10-04 18:45:19 -04:00
: step-all ( interpreter -- )
interpreter-continuation [ (continue) ] curry in-thread ;