scsh-users
[Top] [All Lists]

expect

To: scsh@martigny.ai.mit.edu
Subject: expect
From: "Jin S. Choi" <jsc@atype.com>
Date: Mon, 1 Jul 1996 18:11:30 -0400
Reply-to: jin@atype.com
Has anyone written a nice "expect"-like macro?
Here's a very basic one I just cobbled up after learning r4rs macros;
any suggestions, improvements, or a better macro would be appreciated.

(define-syntax expect
  (syntax-rules (default)
                ((expect string match-binding (default body1 body2 ...))
                 (begin body1 body2 ...))
                ((expect string match-binding (regexp body1 body2 ...))
                 (let ((match-binding (string-match regexp string)))
                   (if match-binding
                       (begin body1 body2 ...)
                       #f)))
                ((expect string match-binding (regexp body1 body2 ...) clause1 
clause2 ...)
                 (let ((match-binding (string-match regexp string)))
                   (if match-binding
                       (begin body1 body2 ...)
                       (expect string match-binding clause1 clause2 ...))))))

(define (do-test str)
  (expect str the-match
          ("foo(.*)" (match:substring the-match 1))
          ("^(.*) (.*)$" (list (match:substring the-match 1) (match:substring 
the-match 2)))
          (default #t)))

(do-test "foobar") -> "bar"
(do-test "hello world") -> '("hello" "world")
(do-test "baz") -> #t


<Prev in Thread] Current Thread [Next in Thread>
  • expect, Jin S. Choi <=