Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Word redefinition in Forth

Tags:

forth

In Forth, in case of re-definition of word, what is the expected behavior of another word that uses the re-defined one? For e.g. if x calls y:

: Y ." Old Y " CR ;
: X 10 0 DO Y LOOP ;
\ ... 
: Y ." New Y " ;

then after the re-definition of Y, what should be the output of X, Old Y or New Y ?

like image 521
ivand58 Avatar asked Feb 06 '23 09:02

ivand58


1 Answers

The short answer: X will output Old Y, see also your example in online test. At the time when Y is defined in the second time, the X is already compiled.

In Forth, redefinition is just shadowing: it is the case when the name of a new definition shadows some another name in the same word list, and the shadowed definition becomes inaccessible (unfindable) by this name.

Also Forth uses incremental compilation and static name resolution (that is performed in compile time). As the result, the new definitions don't affect any previous definitions (and already compiled code).

like image 101
ruvim Avatar answered Feb 11 '23 10:02

ruvim