File:  [gforth] / gforth / assert.fs
Revision 1.14: download - view: text, annotated - select for diffs
Mon Dec 31 18:40:23 2007 UTC (11 years, 9 months ago) by anton
Branches: MAIN
CVS tags: HEAD
updated copyright notices for GPL v3

    1: \ assertions
    2: 
    3: \ Copyright (C) 1995,1996,1997,1999,2002,2003 Free Software Foundation, Inc.
    4: 
    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: \ as published by the Free Software Foundation, either version 3
   10: \ 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: \ along with this program. If not, see http://www.gnu.org/licenses/.
   19: 
   20: require source.fs
   21: 
   22: variable assert-level ( -- a-addr ) \ gforth
   23: \G All assertions above this level are turned off.
   24: 1 assert-level !
   25: 
   26: : assertn ( n -- ) \ gforth assert-n
   27:     \ this is internal (it is not immediate)
   28:     assert-level @ >
   29:     if
   30: 	POSTPONE (
   31:     then ;
   32: 
   33: : assert0( ( -- ) \ gforth assert-zero
   34:     \G Important assertions that should always be turned on.
   35:     0 assertn ; immediate
   36: : assert1( ( -- ) \ gforth assert-one
   37:     \G Normal assertions; turned on by default.
   38:     1 assertn ; immediate
   39: : assert2( ( -- ) \ gforth assert-two
   40:     \G Debugging assertions.
   41:     2 assertn ; immediate
   42: : assert3( ( -- ) \ gforth assert-three
   43:     \G Slow assertions that you may not want to turn on in normal debugging;
   44:     \G you would turn them on mainly for thorough checking.
   45:     3 assertn ; immediate
   46: : assert( ( -- ) \ gforth
   47:     \G Equivalent to @code{assert1(}
   48:     POSTPONE assert1( ; immediate
   49: 
   50: : (end-assert) ( flag nfile nline -- ) \ gforth-internal
   51:     rot if
   52: 	2drop
   53:     else
   54: 	.sourcepos ." : failed assertion"
   55: 	true abort" assertion failed" \ !! or use a new throw code?
   56:     then ;
   57: 
   58: : ) ( -- ) \ gforth	close-paren
   59: \G End an assertion.
   60:     compile-sourcepos POSTPONE (end-assert) ; immediate

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>