Post by PMFPost by RuvimDo you know a Forth system in which the following definition for "const"
is compiled but does not work as expected?
: const ( x "<spaces>name" -- )
depth >r ( x ) ( R: n.depth )
: ( x colon-sys ) ( R: n.depth )
depth r> - ( x colon-sys n.size ) ( R: )
n>r ( x ) ( R: i*x n.size )
postpone literal ( ) ( R: i*x n.size )
nr> ( colon-sys n.size ) ( R: )
drop ( colon-sys )
postpone ; ( )
;
t{ 123 const foo -> }t
t{ foo -> 3 }t
Note 3.1.5.1 System-compilation types
<https://forth-standard.org/standard/usage#subsubsection.3.1.5.1>
--
Ruvim
Yes if fails on both my systems lxf and lxf64!
the reason if fails is that : stores the current depth and ; later
compares it with the actual
at that point. This is to catch unmatched loops and conditionals.
The usual use of >r r> works as intended
: const >r : r> postpone literal postpone ; ;
Yes, my definition can be implemented simpler, but it is given only for
illustration and test.
Another use case is to get xt that `:noname` leaves under colon-sys
(published many times in comp.lang.forth since 2000 [1])
For example:
: rec: ( "<spaces>name" -- colon-sys )
defer depth >r :noname depth r> - 1- roll
latest-name name> defer!
;
\ Usage example
rec: fib ( u.index -- u.value )
dup 2 u< if exit then
dup 2 - fib swap 1- fib +
;
t{ 0 fib 1 fib 2 fib 3 fib 4 fib -> 0 1 1 2 3 }t
Another real-life example in [2]. Regarding `latest-name` see [3].
Another use case is to factor out a definition builder using a helper
method like this:
: build-noname-with ( i*x xt.builder -- j*x xt.new )
\ xt.builder ( i*x -- j*x )
depth >r :noname depth r> - n>r
execute
nr> drop postpone ;
;
\ Usage example
: compose-before ( xt2 xt1 -- xt3 )
[: compile, compile, ;] build-noname-with
;
: compose ( xt1 xt2 -- xt3 )
swap compose-before
;
t{ [: 1 ;] [: 2 ;] compose execute -> 1 2 }t
All such techniques are not available if the Forth system does not allow
the stack depth to be changed between `:` and `;`.
Would you like to see these techniques available to standard programs?
Post by PMFI think !csp and ?csp are common also in other systems. I certainly did
not invent them.
Yes, it's a simple method to check the control-flow structures balance
from the FIG-Forth model.
I would suggest avoiding this method in Forth implementations.
[1] subject: colon-sys and ANS, 2000
<https://groups.google.com/g/comp.lang.forth/c/BU0sVOhxHQo/m/xJ7xXmNjV9YJ>
[2] minos2/md-viewer.fs
<https://github.com/forthy42/gforth/blob/3008f604af74aafd/minos2/md-viewer.fs#L356>
[3] [Proposal] New words: latest-name and latest-name-in
<https://forth-standard.org/proposals/new-words-latest-name-and-latest-name-in?hideDiff#reply-1249>
--
Ruvim