Writing your first 4tH program. 4tH 4tH Forth compiler
Reklāma
Reklāma
CHAPTER 4. A GUIDED TOUR
Screen 1
Workspace flush list
Screen 2 write
(editor)
Screen n
File
44
Figure 4.1: Editor architecture
4.5
Writing your first 4tH program
We start our program by giving it a name. Press ”s” and enter ”hello.scr”. Now we’re going to enter the source text, so we start up the editor by pressing ”e” (you know by now you have to press the enter key afterwards). Then we select screen 0 for editing by entering:
0 clear
If you want to know what you’ve entered so far you can list the editing screen by entering: l
The editor will now show you a full listing:
12
13
14
15
7
8
9
10
11
Scr # 0
0
1
2
3
4
5
6
^ 0 OK
The first line will tell you which screen you’re working on, which is screen 0. Then all sixteen lines are listed, all blank of course. Finally it will show you the current line, which is line 0. The ”^” is the cursor, which is at the beginning of the line. You can move the cursor around with the ”m” command. Try:
CHAPTER 4. A GUIDED TOUR
10 m
The editor will respond with:
45
^ 0 OK
And shows you this way that the cursor has moved 10 positions. If you want to move the cursor backwards, you can do that too. Just enter a negative value, like:
-5 m
And the cursor will move back five positions:
^
If you enter a larger value, that is perfectly acceptable too:
128 m
0 OK
Note that every line is 64 characters long, so the editor will tell you you’ve just moved to line 2:
^ 2 OK
Don’t be afraid that you’ll do something wrong and lose your source. Note that this is 4tH, not Forth. If you try something funny like entering a very large value, the editor will just issue an error message:
1024 m
Off screen OK
You just tried to go beyond the workspace and the editor won’t allow you to do that. Okay, we’ve moved around enough. How about writing that program? You can enter text with the ”p” command, which stands for ”PUT”. Just provide the editor with the appropriate linenumber and the text:
0 p ." Hello world!" cr
Let’s list our screen: l
Scr # 0
0 ." Hello world!" cr
1
2
3
4
5
6
7
CHAPTER 4. A GUIDED TOUR 46
8
9
10
11
12
13
14
15
^." Hello world!" cr 0 OK
That’s it. That’s it? What about all that red tape like ”Program Hello” or ”int main()”, opening parenthesis or closing braces? Hey, this is Forth
2
, not C or something. You’ve just told the compiler it has to print the text ”Hello world!” and write a newline. Isn’t that what you wanted?
According to the figure in section 3, we first have to save the workspace in the RAM disk by entering ”flush”, then save it to disk by entering ”write” and subsequently leave the editor by entering ”q”. Although perfectly correct, it is a lot of typing for just saving and exiting.
You can do that a lot faster by just entering ”wq”, which stands for ”Write and Quit”.
Now we’re back in the main menu and we want to see our program run. Just hit ”R” and press enter. Don’t we have to compile it first? Sure, but 4tH will notice your program hasn’t been compiled yet and will compile it automatically for you. If you get an error message like this:
Compiling; Word 0: Undefined name
Then you know you’ve just made a classical beginners error: there is a space between .” and the text. You’ll have to go back to the editor to correct it. Reload screen 0 by entering:
0 list
11
12
13
14
15
OK
5
6
7
8
9
10
Scr # 0
0 ."Hello world!" cr
1
2
3
4
Now let’s see where our cursor is:
0 m
^."Hello world!" cr 0 OK
2
If you are not familiar with Forth and want to learn it, please read the primer. Everything you want to know is explained there in detail.
Lejupielādēt
Reklāma