Added a simple implementation of structs in lisp, using just a macro and a few functions

This commit is contained in:
Emin Arslan 2025-01-21 17:06:58 +03:00
parent 06ef04aede
commit 2506c37def

31
asd.lisp Normal file
View File

@ -0,0 +1,31 @@
(progn
(defun add (a b) (+ a b))
(defvar whatever 16))
;; struct
(progn
(defun constructor-name (sym)
(intern (concatenate 'string "MAKE-" (string sym))))
(defun accessor-name (name sym)
(intern (concatenate 'string (string name) "-" (string sym))))
(defun setter-name (name sym)
(intern (concatenate 'string "SET-" (string name) "-" (string sym))))
(defmacro mydefstruct (name &rest slots)
`(progn
(defun ,(constructor-name name)
,slots
(list ,@slots))
,@(loop for i from 0 to (1- (length slots)) collect
`(defun ,(accessor-name name (nth i slots))
(instance)
(nth ,i instance)))
,@ (loop for i from 0 to (1- (length slots)) collect
`(defun ,(setter-name name (nth i slots))
(instance val)
(setf (nth ,i instance) val)))
,@(loop for i from 0 to (1- (length slots)) collect
`(defsetf ,(accessor-name name (nth i slots))
,(setter-name name (nth i slots)))))))