Creating variables. 4tH 4tH Forth compiler
Reklama
Reklama
CHAPTER 7. 4TH FUNDAMENTALS 70
." Hello world!"
Compile this and it works. Yes, that’s it! No declaration that this is the main function and it is beginning here and ending there. May be you think it looks funny on the display. Well, you can add a carriage return by adding the word ’CR’. So now it looks like:
." Hello world!" cr
Still pretty simple, huh?
7.9
Creating variables
One time or another you’re going to need variables. Declaring a variable is easy.
variable one
The same rules for declaring words apply for variables. You can’t use a name that already has been taken. A variable is a word too! And whitespace characters are not allowed. Note that 4tH is not case-sensitive!
7.10
Using variables
Of course variables are of little use when you could not assign values to them. This assigns the number 6 to variable ’ONE’:
6 one !
We don’t call ’!’ bang or something like that, we call it ’store’. Of course you don’t have to put a number on the stack to use it, you can use a number that is already on the stack. To retrieve the value stored in ’ONE’ we use: one @
The word ’@’ is called ’fetch’ and it puts the number stored in ’one’ on the stack. To display it you use ’.’: one @ .
There is a shortcut for that, the word ’?’, which will fetch the number stored in ’ONE’ and displays it: one ?
7.11
Built-in variables
4tH has only three built-in variables. They are called ’BASE’, ’>IN’ and ’OUT’. ’BASE’ controls the radix at run-time, ’>IN’ is used by ’PARSE’ and ’OUT’ returns a value to the host program.
Stiahnuť
Reklama