Added a simple implementation of structs in lisp, using just a macro and a few functions
This commit is contained in:
		
							
								
								
									
										31
									
								
								asd.lisp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								asd.lisp
									
									
									
									
									
										Normal 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)))))))
 | 
				
			||||||
		Reference in New Issue
	
	Block a user