CHAPTER 9. CHARACTER SEGMENT
9.10
Aborting a program
103
Some conditions are so grave you can consider them to be fatal errors. In such cases the only thing you can do is abort the program as soon as possible. Of course, there is a way in 4tH to do just that. You can use either ’ABORT’ or ’QUIT’. Same thing. Both will terminate your program immediately. This small program prints nothing: abort
." This will never be printed." cr
But there is more. Let’s say you only want to exit a program when a certain condition is met, e.g. a word left a non-zero value on the stack. In that case you would have to write something like this: if then
." We have an error condition!" cr quit
You can write that much shorter by using the word ’ABORT"’: abort" We have an error condition!"
’ABORT"’ will print the message following it and abort, but only when there is a non-zero value on the stack. So this program does not abort: false abort" This will not be printed!"
." This will be printed!"
You will find that ’ABORT"’ is a very handy tool when processing error conditions.
9.11
Opening a file
You probably don’t want to write programs that only write to the screen and read from the keyboard. So 4tH has a few words that allow you to work with files. Since 4tH is a scripting language, its capabilities are limited. But you will find that you can perform most common operations.
One of the limitations is that you can have a limited number of open files, but it will do in most situations.
Opening a output-file is pretty simple. Just throw the address and length of a filename and a file access mode on the stack and execute the word ’OPEN’. The value ’OPEN’ returns is a simple number which bears little significance. However, you have to save it to a variable or value, for you will need it later. We’d like to use values for storing file pointers, so we created the word ’FILE’. ’FILE’ simply creates a value and initializes it, so if you use it prematurely 4tH will issue an error message.
file myfile s" outfile.dat" output open error?
abort" File could not be opened" to myfile
( a1 n1 fam -- h f)
( h)
( --)