Ruvim
2024-03-07 12:29:13 UTC
The accepted proposal for quotations [1] specifies only compilation
semantics for the words "[:" and ";]".
The expected interpretation semantics for "[: ... ;]" are that this
construct behaves like ":noname ... ;" (at least for the resulting stack
effect), but the system should correctly work regardless whether the
current definition exists (i.e., a definition that is being compiled).
It means, if a definition is being compiled, its compilation shall be
correctly suspended by "[:", and resumed after ";]" under the hood.
Bellow are the test cases for the standardized compilation semantics,
and for the expected interpretation semantics.
Interestingly, some Forth systems fail the t12 and t13 tests for the
standardized compilation semantics.
My implementation passes all these test cases. Its basic factors are as
follows.
The updated "current definition" term:
*current definition*: The definition whose compilation has been started
most recently but not yet ended.
CONCEIVE ( -- ) ( C: -- def-sys )
Start compilation of the new definition.
This word shall correctly work regardless whether the current definition
exists. It does not change STATE.
BIRTH ( -- xt ) ( C: def-sys -- )
End compilation of the current definition and return its xt.
GERM ( -- xt|0 )
If the current definition exists return its xt, otherwise return 0.
===== start of "quotation.test.fth"
\ Test cases for quotations "[: ... ;]"
\ Testing the compilation semantics
\ t11
t{ :noname [: 123 ;] ; execute execute -> 123 }t
: lit, postpone literal ;
: ([:) postpone [: ;
: (;]) postpone ;] ;
\ t12
t{ :noname [ ([:) (;]) ] ; 0<> -> -1 }t
\ t13
t{ :noname 1 [ ([:) 2 lit, (;]) ] 3 ; execute swap execute -> 1 3 2 }t
\ Testing the interpretation semantics
\ (the expected behavior)
\ t21
t{ depth [: ;] depth 1- = ?dup nip -> 0 }t
\ t22
t{ [: 123 ;] execute -> 123 }t
\ t23
t{ [: 1 [: 2 ;] 3 ;] execute swap execute -> 1 3 2 }t
\ Testing the interpretation semantics
\ doing compilation of another definition
\ (the expected behavior)
\ t31
t{ [: [ depth [: ;] depth 1- = ?dup nip ] literal ;] execute -> 0 }t
\ t32
t{ [: 1 [ [: 2 ;] ] literal 3 ;] execute swap execute -> 1 3 2 }t
===== end of "quotation.test.fth"
[1] http://www.forth200x.org/quotations-v4.txt
--
Ruvim
semantics for the words "[:" and ";]".
The expected interpretation semantics for "[: ... ;]" are that this
construct behaves like ":noname ... ;" (at least for the resulting stack
effect), but the system should correctly work regardless whether the
current definition exists (i.e., a definition that is being compiled).
It means, if a definition is being compiled, its compilation shall be
correctly suspended by "[:", and resumed after ";]" under the hood.
Bellow are the test cases for the standardized compilation semantics,
and for the expected interpretation semantics.
Interestingly, some Forth systems fail the t12 and t13 tests for the
standardized compilation semantics.
My implementation passes all these test cases. Its basic factors are as
follows.
The updated "current definition" term:
*current definition*: The definition whose compilation has been started
most recently but not yet ended.
CONCEIVE ( -- ) ( C: -- def-sys )
Start compilation of the new definition.
This word shall correctly work regardless whether the current definition
exists. It does not change STATE.
BIRTH ( -- xt ) ( C: def-sys -- )
End compilation of the current definition and return its xt.
GERM ( -- xt|0 )
If the current definition exists return its xt, otherwise return 0.
===== start of "quotation.test.fth"
\ Test cases for quotations "[: ... ;]"
\ Testing the compilation semantics
\ t11
t{ :noname [: 123 ;] ; execute execute -> 123 }t
: lit, postpone literal ;
: ([:) postpone [: ;
: (;]) postpone ;] ;
\ t12
t{ :noname [ ([:) (;]) ] ; 0<> -> -1 }t
\ t13
t{ :noname 1 [ ([:) 2 lit, (;]) ] 3 ; execute swap execute -> 1 3 2 }t
\ Testing the interpretation semantics
\ (the expected behavior)
\ t21
t{ depth [: ;] depth 1- = ?dup nip -> 0 }t
\ t22
t{ [: 123 ;] execute -> 123 }t
\ t23
t{ [: 1 [: 2 ;] 3 ;] execute swap execute -> 1 3 2 }t
\ Testing the interpretation semantics
\ doing compilation of another definition
\ (the expected behavior)
\ t31
t{ [: [ depth [: ;] depth 1- = ?dup nip ] literal ;] execute -> 0 }t
\ t32
t{ [: 1 [ [: 2 ;] ] literal 3 ;] execute swap execute -> 1 3 2 }t
===== end of "quotation.test.fth"
[1] http://www.forth200x.org/quotations-v4.txt
--
Ruvim