[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

extending defstruct



I want to modify my suggestion for the syntax of byte fields in defstruct in
response to David Plummer's note.  First, in order to be compatible with
existing CLtL syntax (as opposed to Zetalisp), the first element of the slot
description should be a *list* of byte field descriptions; thus we follow
the convention that the car of a slot descriptor names the slot(s), and the
cdr contains default values and options.  Second, there is no need for a
:byte-field keyword, since the shape of every byte field *must* be specified.
So I propose the following:

	(defstruct thingie
	  (foo 'default-foo-value)
	  (((low-bar nil (byte 8 0))
	    (mid-bar #x00 (byte 8 8) :read-only t)
	    (high-bar #x1a (byte 8 16)))
	  (baz 'default-baz-value))

Some notes:  (1) A slot that is divided up into byte fields should be
restricted to type fixnum unless the user explicitly specifies that it
should be a bignum (or something else) via the :type keyword.  So byte
specifiers that exceed the dimensions of a fixnum (or whatever type is
specified) should generate an error.  (2) the list of byte field descriptors
may also contain as its first element a symbol, in which case the symbol is
taken to be the name of the entire slot.  (3) Per Plummer's suggestion, a
default value of NIL for a byte field means ``no default.''  (4) If the user
specifies incompatible defaults for overlapping byte fields, an error
should be generated.  (5) If overlapping byte fields are both filled by a
constructor, or if one is filled by a default and the other by a
constructor, the results of the operation are undefined.

Here's another example:

	(defstruct gadget
	  (common-name nil)
	  ((feature-bits
	    (input-size nil (byte 3 0))
	    (output-size nil (byte 3 3) :read-only t)
	    (clock-pins nil (byte 2 6))
	    (internal-store nil (byte 10 8)))
	   #x12345 :type fixnum)
	  (gadget-cost :type short-float))


The slot GADGET-FEATURE-BITS is of type fixnum and is divided into four
byte fields.  Note that in this example we gave the entire slot a default
value rather than giving the byte fields separate defaults.

-- Dave