11.20Assertions. 4tH 4tH Forth compiler
إعلان
إعلان
CHAPTER 11. ADVANCED PROGRAMMING
11.20
Assertions
158
You have probably seen this before: you’ve made a program, compiled it and it doesn’t work. Then you start putting code at strategic places, trying to pinpoint the error. And when you’re finally done, you’ve got to revisit all of these places to remove that code. And you probably forget a few..
4tH has a built-in facility which allows to put that code there, debug your program and remove the debugging code from your program by changing a single line.
It is called "assertion" and those of you who have ever worked with C probably know what we’re talking about.
An assertion is a line of code that will evaluate an expression. If the expression evaluates to false, it will exit the program with an error message. Let’s take a look at this simple colon-definition:
: add
+
;
\ expects two numbers on the stack
If we call add by writing:
1 add it will fail. Now we add this assertion: assert( depth 2 >= )
It will evaluate to false when there are less than two items on the stack. The program will be terminated and the appropriate error message will be issued. You may think that this is nice, but you still have to remove all assertion manually.
Not true! If you tried this out already you will see that you won’t find an assertion anywhere. It’s gone! True, if you want to use assertions you have to enable them. You do that with the word ’[ASSERT]’:
[assert]
: add assert( depth 2 >= )
+
;
1 add
Now assertions will compile and work. If you remove the word ’[ASSERT]’ all assertions will disappear like they were comment. ’[ASSERT]’ works just like ’[DECIMAL]’,
’[HEX]’, etc. They work linear and do not follow the program flow. If you put ’[ASSERT]’ halfway your source-file you will notice that assertions work from that point:
: add assert( depth 2 >= )
+
;
[assert]
: print-hex base @ >r hex assert( depth 1 >= )
. cr r> base !
;
\ assertions disabled
\ enable assertions
\ assertions enabled
تنزيل
إعلان