This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

How can I change this defmacro so I can setf the return

5 posts · 4850 views

Here is the macro, a macro for cffi::mem-aref. I was trying to make it so I can setf it like you can with mem-aref, but can't figure out how to do it. Any help is appreciated.

(defmacro ? (ptr type &optional (index 0))
  `(cond ((pointerp ,ptr)
	  (return-from ? (mem-aref ,ptr ,type ,index)))
	 ((not (pointerp ,ptr))
	  (return-from ? mem-aref (c-pointer ,ptr) ,type  ,index)))
          (t 0)))

Re: How can I change this defmacro so I can setf the return

It's a question, how (setf (mem-aref ...) ...) is implemented so probably you can't write:
(defun resolve-pointer (ptr)
  (if (pointerp ptr) ptr (c-pointer ptr)))

(defmacro ? (ptr type &optional (index 0))
  `(mem-aref (resolve-pointer ,ptr) ,type ,index))
which result in:
(setf (? *ptr* *type* *index*) ...) ; => (setf (mem-aref (resolve-pointer *ptr*) *type* *index*) ...)
You don't want to put a comma before the whole first argument of mem-aref, because it will determine the type of ptr in compile-time, but the way I presented it won't probably work, because the setf machinery doesn't evaluate its first argument, whence it doesn't evaluate resolve-pointer. In that case you must teach the setf what to do by telling via defsetf or define-setf-expander, which isn't so simple, but it's good to tackle.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Re: How can I change this defmacro so I can setf the return

Can you show me how I would use desetf in this case, not sure I understood the documentation

Re: How can I change this defmacro so I can setf the return

Ok, I've tested the code above with:
(defun resolve-pointer (ptr) ptr)
and it works, no more labour is needed.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Re: How can I change this defmacro so I can setf the return

Thank you very much, it didn't even occur to me to write an external function. Its blazing fast too! Thanks:)