Регистрация | Войти
Lisp — программируемый язык программирования
Ужасный макрос
Автор: shamaz.mazum - 2011-12-03T19:47:29.000000+04:00
(defmacro defcstruct-and-class (name translate-finally &rest args)
  "Defines cstruct, lisp class and method for conversion from cstruct to class"
  (let ((slot-value-sym (gensym)))
    `(progn
       (defcstruct ,name
         ,@(loop for i in args collect
                 (let ((position (position :translate-func i)))
                   (if position
                       (append (subseq i 0 position)
                               (subseq i (+ 2 position))
)

                     i
)
)
)
)

       (defclass ,name ()
         ,(loop for i in args collect (first i))
)

       
       (defmethod translate-to-lisp-class (cstruct (,name ,name))
         ,@(loop for i in args
                 for slot = (first i)
                 for translator = (getf (subseq i 2) :translate-func) collect
                 `(let ((,slot-value-sym (foreign-slot-value cstruct ',name ',slot)))
                    ,(if (not translator)
                         `(setf (slot-value ,name ',slot) ,slot-value-sym)
                       `(setf (slot-value ,name ',slot) (funcall ,translator ,slot-value-sym))
)
)
)

         ,(if translate-finally `(funcall ,translate-finally ,name))
)

       
       (export ',name)
       ,@(loop for i in args for slot = (first i) collect
               `(export ',slot)
)
)
)
)

@2009-2013 lisper.ru