[gforth] / gforth / assert.fs  

gforth: gforth/assert.fs


1 : anton 1.1 \ assertions
2 :    
3 : anton 1.17 \ Copyright (C) 1995,1996,1997,1999,2002,2003,2007,2010 Free Software Foundation, Inc.
4 : anton 1.5
5 :     \ This file is part of Gforth.
6 :    
7 :     \ Gforth is free software; you can redistribute it and/or
8 :     \ modify it under the terms of the GNU General Public License
9 : anton 1.14 \ as published by the Free Software Foundation, either version 3
10 : anton 1.5 \ of the License, or (at your option) any later version.
11 :    
12 :     \ This program is distributed in the hope that it will be useful,
13 :     \ but WITHOUT ANY WARRANTY; without even the implied warranty of
14 :     \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 :     \ GNU General Public License for more details.
16 :    
17 :     \ You should have received a copy of the GNU General Public License
18 : anton 1.14 \ along with this program. If not, see http://www.gnu.org/licenses/.
19 : anton 1.5
20 :     require source.fs
21 : anton 1.1
22 : anton 1.4 variable assert-level ( -- a-addr ) \ gforth
23 : crook 1.8 \G All assertions above this level are turned off.
24 : anton 1.1 1 assert-level !
25 :    
26 : pazsan 1.18 : (end-assert) ( flag nfile nline -- ) \ gforth-internal
27 :     rot if
28 :     2drop
29 :     else
30 :     .sourcepos ." : failed assertion"
31 :     true abort" assertion failed" \ !! or use a new throw code?
32 :     then ;
33 :    
34 :     : assert) ( -- )
35 :     compile-sourcepos POSTPONE (end-assert) ;
36 :    
37 :     6 Constant assert-canary
38 :    
39 : anton 1.4 : assertn ( n -- ) \ gforth assert-n
40 : anton 1.2 \ this is internal (it is not immediate)
41 : anton 1.1 assert-level @ >
42 :     if
43 :     POSTPONE (
44 : pazsan 1.18 else
45 :     ['] assert) assert-canary
46 : anton 1.1 then ;
47 :    
48 : pazsan 1.18 : ) ( -- ) \ gforth close-paren
49 :     \G End an assertion. Generic end, can be used for other similar purposes
50 :     assert-canary <> abort" unmatched assertion"
51 :     execute ; immediate
52 :    
53 : anton 1.4 : assert0( ( -- ) \ gforth assert-zero
54 : crook 1.8 \G Important assertions that should always be turned on.
55 : anton 1.16 0 assertn ; immediate compile-only
56 : anton 1.4 : assert1( ( -- ) \ gforth assert-one
57 : crook 1.8 \G Normal assertions; turned on by default.
58 : anton 1.16 1 assertn ; immediate compile-only
59 : anton 1.4 : assert2( ( -- ) \ gforth assert-two
60 : crook 1.8 \G Debugging assertions.
61 : anton 1.16 2 assertn ; immediate compile-only
62 : anton 1.4 : assert3( ( -- ) \ gforth assert-three
63 : crook 1.8 \G Slow assertions that you may not want to turn on in normal debugging;
64 :     \G you would turn them on mainly for thorough checking.
65 : anton 1.16 3 assertn ; immediate compile-only
66 : anton 1.4 : assert( ( -- ) \ gforth
67 : crook 1.8 \G Equivalent to @code{assert1(}
68 : anton 1.16 POSTPONE assert1( ; immediate compile-only
69 : anton 1.1

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help