27.14Adding new error messages. 4tH 4tH Forth compiler
تبلیغات
تبلیغات
CHAPTER 27. MODIFYING 4TH
27.14
Adding new error messages
529
Adding new messages is quite simple. It requires not much more than adding a #define and adding a string. You might have noticed that every 4tH message has a mnemonic. Although this is not required, it makes it much easier to read and thus maintain your code.
This mnemonic is no longer than eight characters, all uppercase and begins with "M4"
(which stands for Message 4tH). Let’s say you’ve added the ANS-Forth floating point wordset and you want to add the error message "Floating point exception". We’ll do it the easy way and just append the message at the end of the table.
First we have to come up with a mnemonic. We decide to use "M4FLOATE". Now we start up our favorite editor and load 4th.h. Then we look for the table with error mnemonics:
#define M4NOSTR 24
#define M4NULSTR 25
#define M4DUPNAM 26
#define M4CABORT 27
Now we simply add "M4FLOATE" to the end of the table. Since the last message had code
27, we give our message code 28:
#define M4NOSTR 24
#define M4NULSTR 25
#define M4DUPNAM 26
#define M4CABORT 27
#define M4FLOATE 28
We can now save 4th.h. Now we have to add the message itself. That is done by adding it to errs_4th.c. That file just contains an array of messages. Note that the messages are listed in order of their codes:
"Unterminated string",
"Null string",
};
"Duplicate name",
"Compilation aborted"
If you change that order, your compiler might display the right errorcode, but the wrong error message. Since our mnemonic comes last, our message comes last:
"Unterminated string",
"Null string",
"Duplicate name",
"Compilation aborted"
};
"Floating point exception"
Don’t forget adding a comma after the last message! If you don’t your compiler will certainly complain about that. Are we done now. No, not quite yet. The ’THROW’ routine wants to know which codes are exceptions generated by the system and which one are generated by the user. Why? Because user exceptions do not have messages attached to them!
We can change that in cmds_4th.h.
/* ranges */
#define LastWord4th
#define LastMsg4th
FTELL
M4CABORT
دانلود
تبلیغات