5

我想通过一个测试语法异常的宏来扩展 srfi-78。我想要这样的东西:

#! /usr/bin/env scheme-script
#!r6rs

(import (rnrs) (srfi :78 lightweight-testing))

; the macros I want to test
(define-syntax some-macros
  (syntax-rules ()
    [(_) 'ok]))

; the extension to srfi-78
(define-syntax check-exception
  (syntax-rules ()
        ; ... some code ...
        ))

; tests

; prints "correct" or someting like that
(check (some-macros) => 'ok)

; should print "correct" (i. e. the test passed)
(check-exception (some-macros 'arg)) 

; should print "error"
; (i. e. the exception was not thrown as expected)
(check-exception (some-macros)) 

有可能吗?如果不是,您将如何为宏编写测试?

test-read-eval-string从 srfi-64 得知。它接受一个字符串,将其转换为一个表单并在初始环境中评估该表单。我想要一个在当前环境中评估给定形式并捕获异常的宏。

4

1 回答 1

3

唯一可移植的方法是通过 eval 调用代码,并将其包装在防护中。

例如:

(define (safe-eval code env)
  (guard [e [(syntax-violation? e) (display e)]]
    (eval code env)))

用法:

> (safe-eval '(let a v) (environment '(rnrs)))
&who: let
&message: "invalid syntax"
&syntax:
  form: (let a v)
  subform: #f
于 2012-12-05T09:26:54.587 回答