In article <ulo1bt$351qd$
[email protected]>, dxf <
[email protected]> wrote:
On 16/12/2023 6:45 pm, Anton Ertl wrote:
...
And Chuck Moore and others who do not use locals do not practice what
Jeff Fox preached. They just use VARIABLEs instead of locals.
Moore has said:
"Don't use local variables [...] It's bad. It's wrong."
<https://www.ultratechnology.com/1xforth.htm>
If he's merely using VARIABLEs where locals would otherwise be used that's >worth investigating. Unfortunately I'm not aware of any substantive code
by Moore in the public domain to test the proposition.
As one of the "others who do not use locals" I offer the following sample. >Readers may decide whether such use of VARIABLEs and VALUEs amounts to locals >in the usual meaning of the word. ISTM the more factored the code, the more >difficult it is to apply the term 'local'.
variable #DIGIT \ #digits entered
: /FIELD ( n -- 0 )
drop #digit @ begin dup while
<bs> emit space <bs> emit 1-
repeat decimal #digit off ;
: .VIS ( c -- )
dup visible? if emit 1 #digit +! end drop ;
: ?$ ( n c -- n' c' )
[char] $ of /field hex [char] $ .vis key then ;
: +DIGIT ( u c -- u' err )
base @ >digit if swap base @ * + dup 255 u> end
drop true ;
: BUILD# ( c -- n c2 ) \ CR or BL exits
decimal #digit off
0 swap begin dup bl <> over <cr> <> and while
?$ upcase dup .vis +digit if beep /field then key
repeat decimal ;
: GET# ( -- n ) key build# drop ;
variable LEN 0 value MAXCHR
: ROOM? ( -- f ) len @ maxchr < ;
: +CHR ( c -- ) ibuf len @ + c! 1 len +! ;
: DO-NUM ( c -- ) \ CR or BL BL exits
begin room? while
build# swap #digit @ if +chr else drop then
<cr> = if end space key bl of <cr> then
repeat drop ;
: DO-ASC ( c -- )
begin dup <cr> - room? and while
dup .chr +chr key
repeat drop ;
: DIGIT? ( c -- f )
dup [char] $ = swap [char] 0 [char] 9 between or ;
: GET$ ( maxlen -- adr len )
to maxchr len off
key dup digit? if do-num else do-asc then
ibuf len @ ;
An excellent example.
Another excellent example is the <# # #> wordset.
They are superbly factored, by virtue of the "global"
HLD a (user) variable. It is actually more of a
"package-local" variable. Who are in the habit
of using LOCAL's as a first resort, are blocked to think
in this direction.
In the 2500 lines of the manx package (plays musical score
simultaneously on midi keyboard and mechanical instruments) you see
no LOCAL and merely half a dozen VARIABLE or VALUE.
Some of this are hard (or impossible) to replace by LOCAL,
e.g. SHIFTER and TICKS-PER-SECOND. They are set by
a calibration word in the initialisation, then are needed all
over the place, mostly by words that need not address them directly.
They convert the system dependant tick rate to real time.
Then you have the assembler/disassembler ciasdis.
This implements a "blackboard design pattern" for a
kind of state machine.
It is hard to imagine that some one scribbles something on a blackboard,
and then no one is ever to read it.
Groetjes Albert
--
Don't praise the day before the evening. One swallow doesn't make spring.
You must not say "hey" before you have crossed the bridge. Don't sell the
hide of the bear until you shot it. Better one bird in the hand than ten in
the air. First gain is a cat spinning. - the Wise from Antrim -
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)