2005-02-17 16:10:35 -05:00
|
|
|
! Copyright (C) 2004, 2005 Slava Pestov.
|
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
2004-11-03 23:35:36 -05:00
|
|
|
IN: interpreter
|
2005-06-12 21:52:36 -04:00
|
|
|
USING: errors kernel kernel-internals lists math namespaces
|
2005-06-19 17:50:35 -04:00
|
|
|
prettyprint sequences io strings vectors words ;
|
2004-11-03 23:35:36 -05:00
|
|
|
|
|
|
|
! A Factor interpreter written in Factor. Used by compiler for
|
2005-03-10 17:57:22 -05:00
|
|
|
! partial evaluation, also by the walker.
|
2004-11-03 23:35:36 -05:00
|
|
|
|
|
|
|
! Meta-stacks
|
|
|
|
SYMBOL: meta-r
|
2005-04-06 21:41:49 -04:00
|
|
|
: push-r meta-r get push ;
|
|
|
|
: pop-r meta-r get pop ;
|
2004-11-03 23:35:36 -05:00
|
|
|
SYMBOL: meta-d
|
2005-04-06 21:41:49 -04:00
|
|
|
: push-d meta-d get push ;
|
|
|
|
: pop-d meta-d get pop ;
|
|
|
|
: peek-d meta-d get peek ;
|
|
|
|
: peek-next-d meta-d get [ length 2 - ] keep nth ;
|
2004-11-03 23:35:36 -05:00
|
|
|
SYMBOL: meta-n
|
|
|
|
SYMBOL: meta-c
|
|
|
|
|
|
|
|
! Call frame
|
|
|
|
SYMBOL: meta-cf
|
|
|
|
|
2005-03-20 19:05:57 -05:00
|
|
|
! Currently executing word.
|
|
|
|
SYMBOL: meta-executing
|
|
|
|
|
2004-11-03 23:35:36 -05:00
|
|
|
: init-interpreter ( -- )
|
|
|
|
10 <vector> meta-r set
|
|
|
|
10 <vector> meta-d set
|
2004-11-21 19:27:18 -05:00
|
|
|
namestack meta-n set
|
2005-06-12 21:20:00 -04:00
|
|
|
catchstack meta-c set
|
2005-03-20 19:05:57 -05:00
|
|
|
f meta-cf set
|
|
|
|
f meta-executing set ;
|
2004-11-03 23:35:36 -05:00
|
|
|
|
|
|
|
: copy-interpreter ( -- )
|
|
|
|
#! Copy interpreter state from containing namespaces.
|
2005-01-28 23:55:22 -05:00
|
|
|
meta-r [ clone ] change
|
|
|
|
meta-d [ clone ] change
|
2004-12-04 15:10:46 -05:00
|
|
|
meta-n [ ] change
|
|
|
|
meta-c [ ] change ;
|
2004-11-03 23:35:36 -05:00
|
|
|
|
|
|
|
! Callframe.
|
2005-03-20 19:05:57 -05:00
|
|
|
: up ( -- ) pop-r meta-cf set pop-r drop ;
|
2004-11-03 23:35:36 -05:00
|
|
|
|
|
|
|
: next ( -- obj )
|
2004-12-10 21:39:27 -05:00
|
|
|
meta-cf get [ meta-cf [ uncons ] change ] [ up next ] ifte ;
|
2004-11-03 23:35:36 -05:00
|
|
|
|
2005-06-15 23:27:28 -04:00
|
|
|
: meta-interp ( -- interp )
|
|
|
|
meta-d get meta-r get meta-n get meta-c get <interp> ;
|
|
|
|
|
|
|
|
: set-meta-interp ( interp -- )
|
|
|
|
>interp< meta-c set meta-n set meta-r set meta-d set ;
|
|
|
|
|
2004-11-03 23:35:36 -05:00
|
|
|
: host-word ( word -- )
|
2005-06-15 23:27:28 -04:00
|
|
|
[
|
|
|
|
\ call push-r interp [
|
|
|
|
interp over interp-data push
|
2005-07-24 20:17:51 -04:00
|
|
|
[ ] set-interp
|
|
|
|
] cons cons push-r meta-interp [ ] set-interp
|
2005-06-15 23:27:28 -04:00
|
|
|
] call set-meta-interp pop-d 2drop ;
|
2004-11-03 23:35:36 -05:00
|
|
|
|
|
|
|
: meta-call ( quot -- )
|
|
|
|
#! Note we do tail call optimization here.
|
2005-03-20 19:05:57 -05:00
|
|
|
meta-cf [
|
|
|
|
[ meta-executing get push-r push-r ] when*
|
|
|
|
] change ;
|
2004-11-03 23:35:36 -05:00
|
|
|
|
|
|
|
: meta-word ( word -- )
|
2005-03-05 14:45:23 -05:00
|
|
|
dup "meta-word" word-prop [
|
2005-01-02 23:57:54 -05:00
|
|
|
call
|
2004-11-03 23:35:36 -05:00
|
|
|
] [
|
2005-03-20 19:05:57 -05:00
|
|
|
dup compound? [
|
|
|
|
dup word-def meta-call meta-executing set
|
|
|
|
] [
|
|
|
|
host-word
|
|
|
|
] ifte
|
2005-01-02 23:57:54 -05:00
|
|
|
] ?ifte ;
|
2004-11-03 23:35:36 -05:00
|
|
|
|
2005-03-10 17:57:22 -05:00
|
|
|
: do ( obj -- ) dup word? [ meta-word ] [ push-d ] ifte ;
|
2004-11-03 23:35:36 -05:00
|
|
|
|
2004-12-17 21:46:19 -05:00
|
|
|
: meta-word-1 ( word -- )
|
2005-03-05 14:45:23 -05:00
|
|
|
dup "meta-word" word-prop [ call ] [ host-word ] ?ifte ;
|
2004-12-17 21:46:19 -05:00
|
|
|
|
2005-03-10 17:57:22 -05:00
|
|
|
: do-1 ( obj -- ) dup word? [ meta-word-1 ] [ push-d ] ifte ;
|
2004-11-03 23:35:36 -05:00
|
|
|
|
2005-03-10 17:57:22 -05:00
|
|
|
: set-meta-word ( word quot -- ) "meta-word" set-word-prop ;
|
2004-11-03 23:35:36 -05:00
|
|
|
|
2005-01-28 23:55:22 -05:00
|
|
|
\ datastack [ meta-d get clone push-d ] set-meta-word
|
|
|
|
\ set-datastack [ pop-d clone meta-d set ] set-meta-word
|
2005-06-12 21:20:00 -04:00
|
|
|
\ >r [ pop-d push-r ] set-meta-word
|
|
|
|
\ r> [ pop-r push-d ] set-meta-word
|
2005-01-28 23:55:22 -05:00
|
|
|
\ callstack [ meta-r get clone push-d ] set-meta-word
|
|
|
|
\ set-callstack [ pop-d clone meta-r set ] set-meta-word
|
2004-11-03 23:35:36 -05:00
|
|
|
\ call [ pop-d meta-call ] set-meta-word
|
|
|
|
\ execute [ pop-d meta-word ] set-meta-word
|
|
|
|
\ ifte [ pop-d pop-d pop-d [ nip ] [ drop ] ifte meta-call ] set-meta-word
|
2005-06-12 21:52:36 -04:00
|
|
|
\ dispatch [ pop-d pop-d swap nth meta-call ] set-meta-word
|
2005-03-10 17:57:22 -05:00
|
|
|
|
2005-05-03 23:50:04 -04:00
|
|
|
\ set-meta-word forget
|