* condition-case
@ 2009-11-20 11:09 Thien-Thi Nguyen
0 siblings, 0 replies; only message in thread
From: Thien-Thi Nguyen @ 2009-11-20 11:09 UTC (permalink / raw)
To: guile-user
[-- Attachment #1: Type: text/plain, Size: 96 bytes --]
Here is an implementation of the Emacs Lisp `condition-case',
along with a small test harness:
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: z.scm --]
[-- Type: text/x-scheme, Size: 1357 bytes --]
(define-macro (condition-case var bodyform . handlers)
`(catch
#t (lambda ()
,bodyform)
(lambda ,var
(case (car ,var)
,@(map (lambda (h)
;; Emacs Lisp `case' handles lone symbols,
;; which is pleasant.
`(,(let ((name (car h)))
(if (or (eq? 'else name) (pair? name))
name
(list name)))
,@(cdr h)))
handlers)
,@(if (assq 'else handlers)
'()
`((else
;; This copy is to workaround a (possibly misguided)
;; `nconc2last' used in the Guile 1.4.x implementation.
(let ((copy (list-copy ,var)))
(apply throw (car copy) (cdr copy))))))))))
;;; (put 'condition-case 'scheme-indent-function 2)
(define (read-string s)
(call-with-input-string s read))
(write-line
(condition-case c
(if (pair? (cdr (command-line)))
(apply throw (map read-string (cdr (command-line))))
'ok)
(bad
(simple-format #t "BAD: ~S !~%" (cdr c))
42)
((a b c)
(write c) (newline)
(cdr c))
(else
(let ((key (car c))
(args (cdr c)))
(write-line "ELSE!")
(write key) (newline)
(write args) (newline)
'else))))
[-- Attachment #3: Type: text/plain, Size: 313 bytes --]
To play, save to (say) /tmp/z.scm, and invoke from command line, e.g.:
guile -s /tmp/z.scm
guile -s /tmp/z.scm bad stuff happens
guile -s /tmp/z.scm no bad stuff happens
I wonder if there is a better way, where better means "more idiomatic"
or "standardardized", or "more elegant", or what have you.
thi
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2009-11-20 11:09 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-11-20 11:09 condition-case Thien-Thi Nguyen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).