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!
=== 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>
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)