Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Forth (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Examples== === βHello, World!β === <syntaxhighlight lang="forth"> : HELLO ( -- ) CR ." Hello, World!" ; </syntaxhighlight> HELLO <cr> Hello, World! The word <code>CR</code> (Carriage Return) causes the output following <code>CR</code> to be displayed on a new line. The parsing word <code>."</code> (dot-quote) reads a double-quote delimited string and appends code to the current definition so that the parsed string will be displayed upon execution. The space character separating the word <code>."</code> from the string <code>Hello, World!</code> is not included as part of the string. It is needed so that the parser recognizes <code>."</code> as a Forth word. A standard Forth system is also an interpreter, and the same output can be obtained by typing the following code fragment into the Forth console: <syntaxhighlight lang="forth"> CR .( Hello, World!) </syntaxhighlight> <code>.(</code> (dot-paren) is an immediate word that parses a parenthesis-delimited string and displays it. As with the word <code>."</code> the space character separating <code>.(</code> from <code>Hello, World!</code> is not part of the string. The word <code>CR</code> comes before the text to print. By convention, the Forth interpreter does not start output on a new line. Also by convention, the interpreter waits for input at the end of the previous line, after an <code>ok</code> prompt. There is no implied "flush-buffer" action in Forth's <code>CR</code>, as sometimes is in other programming languages. === Mixing states of compiling and interpreting === Here is the definition of a word <code>EMIT-Q</code> which when executed emits the single character <code>Q</code>: <syntaxhighlight lang="forth"> : EMIT-Q 81 ( the ASCII value for the character 'Q' ) EMIT ; </syntaxhighlight> This definition was written to use the [[ASCII]] value of the <code>Q</code> character (81) directly. The text between the parentheses is a comment and is ignored by the compiler. The word <code>EMIT</code> takes a value from the data stack and displays the corresponding character. The following redefinition of <code>EMIT-Q</code> uses the words <code><nowiki>[</nowiki></code> (left-bracket), <code><nowiki>]</nowiki></code> (right-bracket), <code>CHAR</code> and <code>LITERAL</code> to temporarily switch to interpreter state, calculate the ASCII value of the <code>Q</code> character, return to compilation state and append the calculated value to the current colon definition: <syntaxhighlight lang="forth"> : EMIT-Q [ CHAR Q ] LITERAL EMIT ; </syntaxhighlight> The parsing word <code>CHAR</code> takes a space-delimited word as parameter and places the value of its first character on the data stack. The word <code><nowiki>[CHAR]</nowiki></code> is an immediate version of <code>CHAR</code>. Using <code><nowiki>[CHAR]</nowiki></code>, the example definition for <code>EMIT-Q</code> could be rewritten like this: <syntaxhighlight lang="forth"> : EMIT-Q [CHAR] Q EMIT ; \ Emit the single character 'Q' </syntaxhighlight> This definition used <code>\</code> (backslash) for the describing comment. Both <code>CHAR</code> and <code><nowiki>[CHAR]</nowiki></code> are predefined in {{Not a typo|ANS}} <!-- not a misspelling --> Forth. Using <code>IMMEDIATE</code> and <code>POSTPONE</code>, <code><nowiki>[CHAR]</nowiki></code> could have been defined like this: <syntaxhighlight lang="forth"> : [CHAR] CHAR POSTPONE LITERAL ; IMMEDIATE </syntaxhighlight> === RC4 cipher program === In 1987, [[Ron Rivest]] developed the [[RC4]] cipher-system for RSA Data Security, Inc. Its description follows: {{Blockquote|We have an array of 256 bytes, all different. Every time the array is used it changes by swapping two bytes. The swaps are controlled by counters ''i'' and ''j'', each initially 0. To get a new ''i'', add 1. To get a new ''j'', add the array byte at the new ''i''. Exchange the array bytes at ''i'' and ''j''. The code is the array byte at the sum of the array bytes at ''i'' and ''j''. This is XORed with a byte of the plaintext to encrypt, or the ciphertext to decrypt. The array is initialized by first setting it to 0 through 255. Then step through it using ''i'' and ''j'', getting the new ''j'' by adding to it the array byte at ''i'' and a key byte, and swapping the array bytes at ''i'' and ''j''. Finally, ''i'' and ''j'' are set to 0. All additions are modulo 256.}} The following Standard Forth version uses Core and Core Extension words only. <syntaxhighlight lang="forth"> 0 value ii 0 value jj 0 value KeyAddr 0 value KeyLen create SArray 256 allot \ state array of 256 bytes : KeyArray KeyLen mod KeyAddr ; : get_byte + c@ ; : set_byte + c! ; : as_byte 255 and ; : reset_ij 0 TO ii 0 TO jj ; : i_update 1 + as_byte TO ii ; : j_update ii SArray get_byte + as_byte TO jj ; : swap_s_ij jj SArray get_byte ii SArray get_byte jj SArray set_byte ii SArray set_byte ; : rc4_init ( KeyAddr KeyLen -- ) 256 min TO KeyLen TO KeyAddr 256 0 DO i i SArray set_byte LOOP reset_ij BEGIN ii KeyArray get_byte jj + j_update swap_s_ij ii 255 < WHILE ii i_update REPEAT reset_ij ; : rc4_byte ii i_update jj j_update swap_s_ij ii SArray get_byte jj SArray get_byte + as_byte SArray get_byte xor ; </syntaxhighlight> This is one way to test the code: <syntaxhighlight lang="forth"> hex create AKey 61 c, 8A c, 63 c, D2 c, FB c, : test cr 0 DO rc4_byte . LOOP cr ; AKey 5 rc4_init 2C F9 4C EE DC 5 test \ output should be: F1 38 29 C9 DE </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)