home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xscm105.zip / xscm / assert.scm next >
Text File  |  1992-08-29  |  1KB  |  48 lines

  1. ; $Header: /home/campbell/Languages/Scheme/scm/x-scm/RCS/assert.scm,v 1.1 1992/07/03 03:06:52 campbell Beta $
  2. ;
  3. ; Assertion checking macro
  4. ;
  5. ; Usage:
  6. ;
  7. ;    #.(assert 'expr . args)
  8. ;
  9. ; Prints a message and aborts if expr evalutes to #f.  Optional args
  10. ; are also printed.
  11. ;
  12. ; If you want to disable (most of) this for performance after things
  13. ; are debugged, just define assert:disabled to #t.
  14. ;
  15. ;  Author: Larry Campbell (campbell@redsox.bsw.com)
  16. ;  Copyright 1992 by The Boston Software Works, Inc.
  17. ;  Permission to use for any purpose whatsoever granted, as long
  18. ;  as this copyright notice remains intact.  Please send bug fixes
  19. ;  or enhancements to the above email address.
  20.  
  21. (require 'format)
  22.  
  23. (define assert:disabled #f)
  24.  
  25. (define (assert condition . args)
  26.   (let ((e (format #f "Assertion failed: ~A~%" condition)))
  27.     (if assert:disabled
  28.     #t
  29.     `(if (not ,condition)
  30.          (let ((msg ,e))
  31.            ,(if (not (null? args))
  32.             `(set!
  33.               msg
  34.               (string-append
  35.                msg
  36.                ,@(map
  37.               (lambda (a)
  38.                 `(format #f "    ~A=~A~%" ',a ,a))
  39.               args))))
  40.            (if ,*load-pathname*
  41.            (set!
  42.             msg
  43.             (string-append
  44.              msg (format #f "  In file ~A, line ~A"
  45.                  ,*load-pathname* ,(line-number)))))
  46.            (error msg))))))
  47.