11.21Breakpoints. 4tH 4tH Forth compiler
Annonce
Annonce
CHAPTER 11. ADVANCED PROGRAMMING 159
Assertions are only enabled in "PRINT-HEX". The assertion inside "ADD" will be removed and thus be disabled. But there is more to ’[ASSERT]’ than the eye meets. It doesn’t enable assertions, it toggles them. When the 4tH compiler starts, assertions are disabled. The first ’[ASSERT]’ enables them. A second ’[ASSERT]’ will disable them again:
\ enable assertions [assert]
: add assert( depth 2 >= )
+
;
[assert]
: print-hex base @ >r hex assert( depth 1 >= )
. cr r> base !
;
\ assertions enabled
\ disable assertions
\ assertions disabled
There are many possibilities:
• You can start testing low level colon-definitions and move your way up to the high level definitions by moving ’[ASSERT]’ down.
• You can enable assertions on certain parts of your code by enclosing them with an
’[ASSERT]’ pair.
• You can switch the entire context of ’[ASSERT]’s by adding a single ’[ASSERT]’ to the top of your source.
You are not limited to range-checking when using ’ASSERT(’. Any expression that evaluates to TRUE is allowed:
[assert]
: add assert( ." ADD starts at " here . cr true ) assert( depth 2 >= ) assert( ." Values: " over over . . cr true )
+
;
We’re sure you can come up with more useful ideas. We did too.
11.21
Breakpoints
4tH also offers you the possibility to set breakpoints. It’s quite easy to enable this facility.
Just add this to the very beginning of your source:
[needs lib/debug.4th]
Setting a breakpoint is quite easy too, e.g. this piece of code malfunctions:
32 string argument
1 args argument place
Download
Annonce