String prepend: Difference between revisions

Line 283:
 
=={{header|Forth}}==
Forth has no string prepend word, but adding it is not difficult. This demonstration code starts from the low level operations that Forth gives us and quickly builds a simple set of "WORDS" (sub-routines) that let us move strings from address to address. The memory format here is a simple count+text. Strings inare Forthjust return theiran address, or their address and length ontoon the data stack andso we can reference them as many times as we need to. Our prepend word makes use of the Return stack as a temporary storage for the address of the string we want to prepend. Standard Forth also provides a named general purpose buffer called PAD, so we make use of that too. With this PREPEND becomes part of the language.
 
<lang>\ the following functions are commonly native to a Forth system. Shown for completeness
Line 296:
 
\ Example begins here
256 buffer: string1 \ buffer: is Forth 2012 word
 
s" needs no introducton!" STRING1 PLACE \ initialize our string
 
: PREPEND ( addr len addr2 -- addr2)
>R \ push addr2 to return stack
Line 310 ⟶ 306:
 
Test our language extensions interactively at the console
<pre>256 buffer: string1 ok \ buffer: is Forth 2012 word
<pre>
s" needs no introductonintroduction!" STRING1 PLACE ok \ initialize our string
string1 writeln
needs no introduction! ok
Anonymous user