On Mon, Apr 22, 2019 at 08:57:32PM +0200, Zelphir Kaltstahl wrote:
> Hello Guile users,
> 
> I was looking for an assert facility in Guile and found the following:
> 
> https://www.gnu.org/software/guile/manual/html_node/rnrs-base.html#rnrs-base
> 
> (Search for assert there to find it on the page.)
> 
> However, while searching, I also found something that looks even better:
> 
> http://okmij.org/ftp/Scheme/assert-syntax-rule.txt

This is my assert macro.  I'm sure there are dozens of other versions.

---
(define-module (mlg assert)
  #:export (assert
            assert-type))

(define-syntax __FILE__
   (syntax-rules ()
     ((_)
      (or (assv-ref (current-source-location) 'filename)
          "(unknown file)"))))

(define-syntax __LINE__
   (syntax-rules ()
     ((_)
      (or (assv-ref (current-source-location) 'line)
          "(unknown line)"))))

(define-syntax assert
  (lambda (x)
    (syntax-case x ()
      ((_ expression)
       #'(let ((ret expression))
           (unless ret
             (error
              (format #f "~a:~a: assertion failed: ~a = ~s"
                      (__FILE__) (__LINE__) 'expression expression))))))))

(define-syntax assert-type
  (lambda (x)
    (syntax-case x ()
      [(_ type var)
       #`(if (not (#,(datum->syntax #'var
                                    (string->symbol (string-append 
                                                     (symbol->string 
(syntax->datum #`type)) 
                                                     "?")))
                   var))
             (scm-error 'wrong-type-arg
                        #f
                        (string-append "not type '" 
                                       #,(symbol->string (syntax->datum #`type))
                                       "': ~s")
                        (list var)
                        (list var)))])))

Reply via email to