How does one implement pointers in Lisp? There is no implicit memory, so one have to explicitly specify the chunk of memory to work on. Which potentially could be optimized by loading array pointers into segment registers (getting bounds checks and protected memory at no cost). Also, there is no malloc. For now I devised following simple macro: (defmacro with-ptr ((name base &optional (offset 0)) &body xs)
(let ((o (gensym "p"))
(b (gensym "a")))
`(let ((,o ,offset)
(,b ,base))
(macrolet ((,name (op &rest as)
(let ((o ',o)
(b ',b)
(v (gensym "v")))
(case op
(++ `(let ((,v (aref ,b ,o)))
(incf ,o ,@as)
,v))
(-- `(let ((,v (aref ,b ,o)))
(decf ,o ,@as)
,v))
(base b)
(offset o)
(start? `(= ,o 0))
(end? `(= ,o (length ,b)))
(avail? `(< ,o (length ,b)))
(move-to-head `(setf ,o 0))
(move-to-last `(setf ,o (- (length ,b) 1)))
(otherwise `(aref ,b (+ ,o ,@as)))))))
(symbol-macrolet ((,name (aref ,b ,o)))
,@xs)))))