For experimental programming I find it easiest to manipulate state in
global variables and refer to them with a $ prefix - as $line.
Typically this state the state would be put into a struct "context"
object to avoid the global environment. Here is a defmacro helper to do
just that
(defun make-def-forms (name vars)
"Return forms that: define a struct named NAME with slots VARS. Define
a global variable of the form *NAME* and initialize it with the
default constructor for the structure. Define symbol macros of the
form $VAR to access each slot VARS in the global variable."
(destructuring-bind (var-name constructor-name acc-names)
(read-from-string
(format nil "(*~(~A~)* make-~(~:*~A~) (~{$~(~A~)~^ ~}))"
name vars))
`(progn
(defstruct ,name ,@vars)
(defvar ,var-name (,constructor-name))
,@(loop for acc-name in acc-names for var in vars
collect `(define-symbol-macro ,acc-name
(slot-value ,var-name ',var))))))
e.g.
(eval (make-def-forms 'ctx '(buffer index)))
(setq $buffer (make-array 10) $index 0)
(setf (elt $buffer (setf $index 1)) 10)
(elt $buffer $index)
(macroexpand-1 '$index)
The question is there a better name than make-def-forms for this
particular "general" functionality, and the associated macro?
also I prefer vars to to be passed in from a list object, and a defmacro
does not help there (unless it is in a global variable).
* Madhu <m3fsrhydic.fsf@leonis4.robolove.meer.net> :
For experimental programming I find it easiest to manipulate state in
global variables and refer to them with a $ prefix - as $line.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 293 |
Nodes: | 16 (2 / 14) |
Uptime: | 219:16:15 |
Calls: | 6,621 |
Calls today: | 3 |
Files: | 12,171 |
Messages: | 5,317,854 |