On Apr 13, 2012, at 11:53 AM, Mark Cave-Ayland wrote:

On 22/03/12 12:16, Stephen Ehring wrote:

On 2012-Mar-22 00:09 , Programmingkid wrote:
Is there a way to add words to the end of a definition? If my word is declared like this:

: myword
firstWord
secondWord
;

is there a way to append a word to the end of this word so it looks like this:

: myword
firstWord
secondWord
thirdWord
;

The usual way I'd do something like that if I had to patch a live system would be as follows:

ok : anotherWord secondWord thirdWord ;
ok patch anotherWord secondWord myword
ok


You can also use (patch) with the XT's if the words are not externally visible.

Steve

I'm fairly sure that OpenBIOS don't implement either patch or (patch) at the moment. If it's just a case of replacing one XT with another within a word definition, then it shouldn't be too hard - or does it do something more clever?


I'm surprised this isn't implemented. From the IEEE 1275 spec:

patch ( "new-name< >old-name< >word-to-patch< >" -- ) Change contents of word-to-patch.
In the compiled definition of word-to-patch, change the first occurrence of old-name to new-name. Works properly even if old-name and/or new-name are numbers.
Used as: ok patch 555 test patch-me to edit the definition of patch-me, replacing the command test with the literal value 555. Implementation note:
When replacing a command with a number, an implementation might need to automatically create a named constant value for the replacement number. (The reason is that Forth commands often compile into a smaller memory space than literal numbers, so patching a number in place of an existing command is a problem.) A suggested name format is h#---, i.e., the number 555 (hex) would be named “h#555” . A name containing only digits (i.e., 555 constant 555) is not recommended, since changing base would cause incorrect evaluation of subsequent uses of that named value.

(patch) ( new-n1 num1? old-n2 num2? xt -- ) Change contents of command indicated by xt.
In the compiled definition of the command indicated by xt, change the first occurrence of old-n2 to new-n1. n1 and n2 can each be either an execution token or a literal number. The flag num1?, if true, indicates that new-n1 is a literal number. If false, it indicates that new-n1 is an execution token. The flag num2? is interpreted similarly.
Used as: ['] new-name false 555 true ['] patch-me (patch) to edit the definition of patch-me, replacing the value 555 with the command new-name. See: patch for more information.