From: David Bakhash
Subject: read macros (help)
Date: 
Message-ID: <cxju34htwl7.fsf@engc.bu.edu>
hey,

I'd like to know, if it's possible, how to do the following.

A shorthand for making a simple bit-array (without  using
"make-array") is to do something like
this:

#*00000000

that makes a byte's worth of "0" bits.  A shorthand even more is to do 
this:

#8*0

where the "8" tells the reader the size of the bit-array.  What I want 
to do with the reader (again, without using "make-array") is to do
something like this:

(defconstant +array-size+ 8)

##+array-size+#*0

and then let the reader look up the value of the variable `array-size' 
and then insert that.  Of course, it's possible that the reader
doesn't even have the value of that variable yet, so I guess the
second half of that question is: in what cases will the reader know
the values of the variables?

dave
From: Barry Margolin
Subject: Re: read macros (help)
Date: 
Message-ID: <XJwr1.29$P34.438337@cam-news-reader1.bbnplanet.com>
In article <···············@engc.bu.edu>, David Bakhash  <·····@bu.edu> wrote:
>  What I want 
>to do with the reader (again, without using "make-array") is to do
>something like this:
>
>(defconstant +array-size+ 8)
>
>##+array-size+#*0
>
>and then let the reader look up the value of the variable `array-size' 
>and then insert that.  Of course, it's possible that the reader
>doesn't even have the value of that variable yet, so I guess the
>second half of that question is: in what cases will the reader know
>the values of the variables?

(defun read-variable-sized-bit-vector (stream macro-char param)
  (declare (ignore param))
  (let* ((size-expr (car (read-delimited-list macro-char stream t)))
         (size (eval size-expr)))
    (if (> size 0)
        (let* ((*read-base* 2)
	       (bits (read stream t nil t)))
          ;; Taking the easy way out here...
	  (read-from-string (format "#~D*~B")))
        (make-array 0 :element-type 'bit))))

The way this is designed, you bind it to a macro character or dispatch
character, and it expects a second occurence of that character to indicate
the end of the size expression.  E.g. if you bind it to #$, you would write

#$+array-size+$01010101

Note that if you try to use this in a file being compiled that the size
expression has to have a value in the compilater read-time environment.
This means that you probably can't have the DEFCONSTANT in the same source
file as the use.

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Cambridge, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.