Files
lisplings/exercises/17_macros/macro5.lisp
T

27 lines
1.0 KiB
Common Lisp

;;; MACROS 5: READER MACROS, A TASTER
;;;
;;; The # you know from #' and #(...) is a dispatch character: the
;;; character after it picks a reader function, and SET-DISPATCH-
;;; MACRO-CHARACTER installs one. The function takes (STREAM
;;; SUB-CHAR NUM-ARG) and returns the object the text becomes -
;;; the reader never evaluates, it only reads.
;;;
;;; (set-dispatch-macro-character #\# #\? #'reader)
;; TODO: Make #? read the NEXT form and hand it back wrapped in
;; (maybe ...): #?red should read as (maybe red). MAYBE-READER
;; below needs the fix; the registration line is already right.
(defun maybe-reader (stream sub-char num-arg)
(declare (ignore sub-char num-arg))
(read stream))
(set-dispatch-macro-character #\# #\? #'maybe-reader)
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal '(maybe red) '#?red)
(check-equal '(maybe (1 2)) '#?(1 2))