From 2506c37def70fb6fde3a4d7c98e58db32684c3a0 Mon Sep 17 00:00:00 2001 From: Emin Arslan Date: Tue, 21 Jan 2025 17:06:58 +0300 Subject: [PATCH] Added a simple implementation of structs in lisp, using just a macro and a few functions --- asd.lisp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 asd.lisp diff --git a/asd.lisp b/asd.lisp new file mode 100644 index 0000000..a11cc38 --- /dev/null +++ b/asd.lisp @@ -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)))))))