Skip to content

Instantly share code, notes, and snippets.

@Bike
Last active February 5, 2021 15:22
Show Gist options
  • Save Bike/aadca846dee67e6fd16db4e69f54be2e to your computer and use it in GitHub Desktop.
Save Bike/aadca846dee67e6fd16db4e69f54be2e to your computer and use it in GitHub Desktop.
;;;; Now the earth was formless and empty, darkness was over the surface of the deep,
#|
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
|#
(defclass twb-class (standard-class) ())
(defun slotd-spec (slotd)
`(:name ,(sb-mop:slot-definition-name slotd)
:initform ,(sb-mop:slot-definition-initform slotd)
:initfunction ,(sb-mop:slot-definition-initfunction slotd)
:initargs ,(sb-mop:slot-definition-initargs slotd)
:readers ,(sb-mop:slot-definition-readers slotd)
:writers ,(sb-mop:slot-definition-writers slotd)))
(defun new-slotd (name)
`(:name ,name
:allocation :instance
:initform nil :initfunction ,(lambda () nil)))
(defun add-slotd (class new-dsd)
(reinitialize-instance
class
:direct-slots (append (mapcar #'slotd-spec (sb-mop:class-direct-slots class))
(list new-dsd))))
(defmethod sb-mop:validate-superclass ((class twb-class) (super standard-class))
t)
(defmethod slot-missing ((class twb-class) object slot-name operation
&optional new-value)
(add-slotd class (new-slotd slot-name))
(ecase operation
((slot-value) nil)
((slot-boundp) t)
((setf) (setf (slot-value object slot-name) new-value))
((slot-makunbound) (slot-makunbound object slot-name))))
@Bike
Copy link
Author

Bike commented Feb 5, 2021

#|
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org/>
|#

(defclass twb-class (standard-class) ())

(defun slotd-spec (slotd)
  `(:name ,(sb-mop:slot-definition-name slotd)
    :initform ,(sb-mop:slot-definition-initform slotd)
    :initfunction ,(sb-mop:slot-definition-initfunction slotd)
    :initargs ,(sb-mop:slot-definition-initargs slotd)
    :readers ,(sb-mop:slot-definition-readers slotd)
    :writers ,(sb-mop:slot-definition-writers slotd)))

(defun add-slotd (class new-dsd)
  (reinitialize-instance
   class
   :direct-slots (append (mapcar #'slotd-spec (sb-mop:class-direct-slots class))
                         (list new-dsd))))

(defmethod sb-mop:validate-superclass ((class twb-class) (super standard-class))
  t)

(defun process-slot-spec (slot-spec expected-name)
  (unless (and (consp slot-spec)
               (eq (first slot-spec) expected-name))
    (error "wrong"))
  (loop for (key value) on (rest slot-spec) by #'cddr
        when (eq key :reader)
          collect value into readers
        else when (eq key :writer)
               collect value into writers
        else when (eq key :accessor)
               collect value into readers
               and collect `(setf ,value) into writers
        else when (eq key :initarg)
               collect value into initargs
        else when (eq key :initform)
               collect key into result
               and collect value into result
               and collect :initfunction into result
               and collect (coerce `(lambda () ,value) 'function) into result
        else
          collect key into result
          and collect value into result
        finally (return (list* :name expected-name
                               :readers readers
                               :writers writers
                               :initargs initargs
                               result))))

(defmethod slot-missing ((class twb-class) object slot-name operation
                         &optional new-value)
  (restart-case
      (error "slot ~a missing from object ~a during operation ~a"
             slot-name object operation)
    (define-new-slot (new-slot-spec)
      :interactive (lambda ()
                     (princ "New slot-definition: " *query-io*)
                     (list (read *query-io*)))
      :report "Define a new slot"
      (add-slotd class (process-slot-spec new-slot-spec slot-name))))
  (ecase operation
    ((slot-value) (slot-value object slot-name))
    ((slot-boundp) (slot-boundp object slot-name))
    ((setf) (setf (slot-value object slot-name) new-value))
    ((slot-makunbound) (slot-makunbound object slot-name))))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment