Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Forth (programming language)
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Forth (programming language)
(section)
Add topic