home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / CLISP / CLISPSRC.TAR / clisp-1995-01-01 / src / defstruc.lsp < prev    next >
Encoding:
Lisp/Scheme  |  1994-06-18  |  33.8 KB  |  783 lines

  1. ; Sources fⁿr DEFSTRUCT Macro.
  2. ; Bruno Haible 13.04.1988, 22.08.1988
  3. ; umgeschrieben am 02.09.1989 von Bruno Haible
  4.  
  5. (in-package "SYSTEM")
  6.  
  7. (defsetf %structure-ref %structure-store)
  8.  
  9. #| ErklΣrung der auftretenden Datentypen:
  10.  
  11.    (get name 'DEFSTRUCT-DESCRIPTION) =
  12.      #(names type keyword-constructor slotlist defaultfun0 defaultfun1 ...)
  13.  
  14.    names ist eine Codierung der INCLUDE-Verschachtelung fⁿr Structure name:
  15.    names = (name_1 ... name_i-1 name_i) wobei name=name_1,
  16.      name_1 enthΣlt name_2, ..., name_i-1 enthΣlt name_i.
  17.  
  18.    type (wenn der Typ der ganzen Structure gemeint ist):
  19.       = T                      Abspeicherung als normale Structure
  20.       = LIST                   Abspeicherung als Liste
  21.       = VECTOR                 Abspeicherung als (simple-)Vector
  22.       = (VECTOR element-type)  Abspeicherung als Vector mit Element-Typ
  23.  
  24.    keyword-constructor = NIL oder der Name des Keyword-Constructor
  25.  
  26.    slotlist ist eine gepackte Beschreibung der einzelnen slots einer Structure:
  27.    slotlist = ({slot}*)
  28.    slot = (name offset default type readonly)
  29.    wobei name der Slotname ist,
  30.               (NIL fⁿr den Slot, in dem der Structure-Name steht)
  31.          default der Defaultwert ist:
  32.               entweder eine Konstante, die zum Defaultwert evaluiert,
  33.               oder eine Form (ein Symbol oder eine Liste (SVREF ...)), die
  34.               bei Auswertung in einem beliebigen Environment eine Funktion
  35.               liefert, die bei Aufruf den Defaultwert liefert.
  36.          type der deklarierte Type fⁿr diesen Slot ist,
  37.          readonly = NIL oder = T angibt, ob dieser Slot readonly ist, d.h.
  38.               nach dem Aufbau der Structure nicht mehr mit (setf ...)
  39.               verΣndert werden kann.
  40.    Bei type = T belegt der Structure-Name den Slot 0, wird aber nicht in der
  41.      slotlist aufgefⁿhrt, da zu seiner Initialisierung nichts zu tun ist.
  42.  
  43. |#
  44.  
  45.  
  46. #| (ds-symbol-or-error x) liefert eine Fehlermeldung, falls x kein Symbol ist.
  47. |#
  48. (defun ds-symbol-or-error (x)
  49.   (unless (symbolp x)
  50.     (error-of-type 'program-error
  51.       (DEUTSCH "~S: Das ist kein Symbol: ~S"
  52.        ENGLISH "~S: this is not a symbol: ~S"
  53.        FRANCAIS "~S : Ceci n'est pas un symbole: ~S")
  54.       'defstruct x
  55. ) ) )
  56.  
  57. #| Hilfsfunktion fⁿr beide Konstruktoren:
  58.    (ds-arg-default arg slot)
  59.    liefert zu einem Argument arg (Teil einer Argumentliste) den Teil der
  60.    Argumentliste, der dieses Argument mit dem Default fⁿr slot bindet.
  61. |#
  62.  
  63. (defun ds-arg-default (arg slot)
  64.   (let ((default (third slot)))
  65.     ; Default ist entweder Konstante oder Funktion oder Symbol
  66.     (if (constantp default)
  67.       (if (null default) arg `(,arg ,default))
  68.       `(,arg (SYS::%FUNCALL ,default))
  69. ) ) )
  70.  
  71. #| Hilfsfunktion fⁿr beide Konstruktoren:
  72.    (ds-make-constructor-body type name names size slotlist)
  73.    liefert den Ausdruck, der eine Structure vom vorgegebenen Typ
  74.    kreiert und fⁿllt.
  75. |#
  76. (defun ds-make-constructor-body (type name names size slotlist)
  77.   `(LET ((OBJECT
  78.            ,(cond ((eq type 'T) `(%MAKE-STRUCTURE ,names ,size))
  79.                   ((eq type 'LIST) `(MAKE-LIST ,size))
  80.                   ((consp type) `(MAKE-ARRAY ,size :ELEMENT-TYPE ',(second type)))
  81.                   (t `(MAKE-ARRAY ,size))
  82.             )
  83.         ))
  84.      ,@(mapcar
  85.          #'(lambda (slot &aux (offset (second slot)))
  86.              `(SETF
  87.                 ,(cond ((eq type 'T)
  88.                         `(%STRUCTURE-REF ',name OBJECT ,offset) )
  89.                        ((eq type 'LIST)
  90.                         `(NTH ,offset OBJECT) )
  91.                        ((eq type 'VECTOR)
  92.                         `(SVREF OBJECT ,offset) )
  93.                        (t `(AREF OBJECT ,offset) )
  94.                  )
  95.                 ,(if (first slot)
  96.                    `(THE ,(fourth slot) ,(first slot))
  97.                    `(QUOTE ,(third slot))
  98.               )  )
  99.            )
  100.          slotlist
  101.        )
  102.      OBJECT
  103. )  )
  104.  
  105. #| Hilfsfunktion fⁿr ds-make-boa-constructor:
  106.  
  107.    (ds-arg-with-default arg slotlist)
  108.    liefert zu einem Argument arg (Teil einer Argumentliste) den Teil der
  109.    Argumentliste, der dieses Argument mit dem richtigen Defaultwert bindet.
  110. |#
  111.  
  112. (defun ds-arg-with-default (arg slotlist)
  113.   (if (listp arg)
  114.     ; Defaultwert ist bereits mitgegeben
  115.     arg
  116.     ; nur ein Symbol
  117.     (let ((slot (find arg slotlist :key #'first :test #'eq)))
  118.       (if slot
  119.         ; Slot gefunden -> dessen Defaultwert nehmen
  120.         (ds-arg-default arg slot)
  121.         ; Slot nicht gefunden, kein Defaultwert
  122.         arg
  123. ) ) ) )
  124.  
  125. #| (ds-make-boa-constructor descriptor type name names size slotlist)
  126.    liefert die Form, die den BOA-Konstrukor definiert.
  127. |#
  128. (defun ds-make-boa-constructor (descriptor type name names size slotlist)
  129.   (let ((constructorname (first descriptor))
  130.         (arglist (second descriptor)))
  131.     ; auf &KEY und &ALLOW-OTHER-KEYS testen:
  132.     (let ((keying (or (member '&KEY arglist :test #'eq)
  133.                       (member '&ALLOW-OTHER-KEYS arglist :test #'eq)
  134.          ))       )
  135.       (when keying
  136.         (error-of-type 'program-error
  137.           (DEUTSCH "~S ~S: Die Argumentliste fⁿr eine keywordfreie Konstruktorfunktion ~S darf kein ~S enthalten: ~S"
  138.            ENGLISH "~S ~S: the argument list for the BOA contructor ~S must not contain ~S: ~S"
  139.            FRANCAIS "~S ~S : La liste d'arguments pour un constructeur ~S libre de mot-clΘs ne peux pas contenir ~S: ~S")
  140.           'defstruct name constructorname (car keying) arglist
  141.     ) ) )
  142.     ; angegebene Argumente sammeln:
  143.     (let* ((argnames
  144.              (let ((L nil))
  145.                (dolist (arg arglist)
  146.                  (unless (member arg lambda-list-keywords :test #'eq)
  147.                    (push (if (listp arg) (first arg) arg) L)
  148.                ) )
  149.                (nreverse L)
  150.            ) )
  151.            ; argnames ist die Liste aller bereits in der Paramterliste mit
  152.            ; Werten versehenen Argumente.
  153.            (new-arglist ; neue Argumentliste
  154.              `(; required args:
  155.                ,@(do ((arglistr arglist (cdr arglistr))
  156.                       (arg)
  157.                       (required-args nil))
  158.                      ((or (endp arglistr)
  159.                           (member (setq arg (car arglistr)) lambda-list-keywords :test #'eq)
  160.                       )
  161.                       (nreverse required-args)
  162.                      )
  163.                    (push arg required-args)
  164.                  )
  165.                ; optional args:
  166.                ,@(do ((arglistr (cdr (member '&optional arglist :test #'eq)) (cdr arglistr))
  167.                       (arg)
  168.                       (optionals nil))
  169.                      ((or (endp arglistr)
  170.                           (member (setq arg (car arglistr)) lambda-list-keywords :test #'eq)
  171.                       )
  172.                       (if (null optionals) nil (cons '&optional (nreverse optionals)))
  173.                      )
  174.                    (push (ds-arg-with-default arg slotlist) optionals)
  175.                  )
  176.                ; rest arg:
  177.                ,@(let ((arglistr (member '&rest arglist :test #'eq)))
  178.                    (if arglistr `(&rest ,(second arglistr)) '())
  179.                  )
  180.                ; aux args:
  181.                &aux
  182.                ,@(do ((aux-args-r (cdr (member '&aux arglist :test #'eq)) (cdr aux-args-r))
  183.                       (aux-arg)
  184.                       (new-aux-args nil))
  185.                      ((or (null aux-args-r)
  186.                           (member (setq aux-arg (car aux-args-r)) lambda-list-keywords :test #'eq)
  187.                       )
  188.                       (nreverse new-aux-args)
  189.                      )
  190.                    (push (ds-arg-with-default aux-arg slotlist) new-aux-args)
  191.                  )
  192.                ,@(let ((slotinitlist nil))
  193.                    (dolist (slot slotlist)
  194.                      (when (first slot)
  195.                        (unless (member (first slot) argnames :test #'eq)
  196.                          (push (ds-arg-with-default (first slot) slotlist) slotinitlist)
  197.                    ) ) )
  198.                    (nreverse slotinitlist)
  199.               )  )
  200.           ))
  201.       `(DEFUN ,constructorname ,new-arglist
  202.          ,(ds-make-constructor-body type name names size slotlist)
  203.        )
  204. ) ) )
  205.  
  206. #| (ds-make-keyword-constructor descriptor type name names size slotlist)
  207.    liefert die Form, die den Keyword-Konstruktor definiert.
  208. |#
  209. (defun ds-make-keyword-constructor (descriptor type name names size slotlist)
  210.   `(DEFUN ,descriptor
  211.      (&KEY
  212.       ,@(mapcap
  213.           #'(lambda (slot)
  214.               (if (first slot) (list (ds-arg-default (first slot) slot)) '())
  215.             )
  216.           slotlist
  217.      )  )
  218.      ,(ds-make-constructor-body type name names size slotlist)
  219. )  )
  220.  
  221. #| (ds-make-pred predname type name name-offset)
  222.    liefert die Form, die das TyptestprΣdikat fⁿr die Structure name kreiert.
  223.    Dabei ist:
  224.    type         der Typ der Structure,
  225.    name         der Name der Structure,
  226.    predname     der Name des TyptestprΣdikats,
  227.    name-offset  (nur bei type /= T ma▀geblich)
  228.                 die Stelle, an der der Name abgespeichert wird.
  229. |#
  230. (defun ds-make-pred (predname type name name-offset)
  231.   `(,@(if (eq type 'T) `((PROCLAIM '(INLINE ,predname))) '())
  232.     (DEFUN ,predname (OBJECT)
  233.       ,(if (eq type 'T)
  234.          `(%STRUCTURE-TYPE-P ',name OBJECT)
  235.          (if (eq type 'LIST)
  236.            `(AND (CONSP OBJECT)
  237.                  ,@(if (eql name-offset 0)
  238.                      `((EQ (CAR OBJECT) ',name))
  239.                      `((> (LENGTH OBJECT) ,name-offset)
  240.                        (EQ (NTH ,name-offset OBJECT) ',name)
  241.                       )
  242.             )      )
  243.            `(AND (SIMPLE-VECTOR-P OBJECT)
  244.                  (> (LENGTH OBJECT) ,name-offset)
  245.                  (EQ (SVREF OBJECT ,name-offset) ',name)
  246.             )
  247.        ) )
  248.    ))
  249. )
  250.  
  251. (defun ds-make-copier (copiername name type)
  252.   (declare (ignore name))
  253.   `(,@(if (or (eq type 'T) (eq type 'LIST))
  254.         `((PROCLAIM '(INLINE ,copiername)))
  255.         '()
  256.       )
  257.     (DEFUN ,copiername (STRUCTURE)
  258.       ,(if (eq type 'T)
  259.          '(%COPY-STRUCTURE STRUCTURE)
  260.          (if (eq type 'LIST)
  261.            '(COPY-LIST STRUCTURE)
  262.            (if (consp type)
  263.              `(LET* ((OBJ-LENGTH (ARRAY-TOTAL-SIZE STRUCTURE))
  264.                      (OBJECT (MAKE-ARRAY OBJ-LENGTH :ELEMENT-TYPE (QUOTE ,(second type))))
  265.                     )
  266.                 (DOTIMES (I OBJ-LENGTH OBJECT)
  267.                   (SETF (AREF OBJECT I) (AREF STRUCTURE I))
  268.               ) )
  269.              `(LET* ((OBJ-LENGTH (LENGTH STRUCTURE))
  270.                      (OBJECT (MAKE-ARRAY OBJ-LENGTH)))
  271.                 (DOTIMES (I OBJ-LENGTH OBJECT)
  272.                    (SETF (SVREF OBJECT I) (SVREF STRUCTURE I))
  273.               ) )
  274.        ) ) )
  275. )  ))
  276.  
  277. (defun ds-make-accessors (name type concname slotlist)
  278.   (mapcap
  279.     #'(lambda (slot)
  280.         (if (first slot)
  281.           (let ((accessorname (concat-pnames concname (first slot)))
  282.                 (offset (second slot))
  283.                 (slottype (fourth slot)))
  284.             `((PROCLAIM '(FUNCTION ,accessorname (,name) ,slottype))
  285.               (PROCLAIM '(INLINE ,accessorname))
  286.               (DEFUN ,accessorname (OBJECT)
  287.                 (THE ,slottype
  288.                   ,(if (eq type 'T)
  289.                      `(%STRUCTURE-REF ',name OBJECT ,offset)
  290.                      (if (eq type 'LIST)
  291.                        `(NTH ,offset OBJECT)
  292.                        (if (consp type)
  293.                          `(AREF OBJECT ,offset)
  294.                          `(SVREF OBJECT ,offset)
  295.              )) )  ) ) )
  296.           )
  297.           '()
  298.       ) )
  299.     slotlist
  300. ) )
  301.  
  302. (defun ds-make-defsetfs (name type concname slotlist)
  303.   (mapcap
  304.     #'(lambda (slot)
  305.         (if (and (first slot) (not (fifth slot))) ; not READ-ONLY
  306.           (let ((accessorname (concat-pnames concname (first slot)))
  307.                 (offset (second slot))
  308.                 (slottype (fourth slot)))
  309.             `((DEFSETF ,accessorname (STRUCT) (VALUE)
  310.                 ,(if (eq type 'T)
  311.                    `(LIST '%STRUCTURE-STORE '',name
  312.                       STRUCT
  313.                       ,offset
  314.                       ,(if (eq 'T slottype)
  315.                          `VALUE
  316.                          `(LIST 'THE ',slottype VALUE)
  317.                     )  )
  318.                    (if (eq type 'LIST)
  319.                      `(LIST 'SETF (LIST 'NTH ,offset STRUCT) VALUE)
  320.                      (if (consp type)
  321.                        `(LIST 'SETF (LIST 'AREF STRUCT ,offset) VALUE)
  322.                        `(LIST 'SETF (LIST 'SVREF STRUCT ,offset) VALUE)
  323.              ))  ) ) )
  324.       ) ) )
  325.     slotlist
  326. ) )
  327.  
  328. ; Ein Hook fⁿr CLOS
  329. (defun clos::define-structure-class (name) (declare (ignore name)) ) ; vorlΣufig
  330.  
  331. (defmacro defstruct (name-and-options . docstring-and-slotargs)
  332.   (let ((name                              name-and-options)
  333.         (options                           nil)
  334.         (conc-name-option                  t)
  335.         (constructor-option-list           nil)
  336.         (keyword-constructor               nil)
  337.         (copier-option                     t)
  338.         (predicate-option                  0)
  339.         (include-option                    nil)
  340.          names
  341.          namesform
  342.         (namesbinding                      nil)
  343.         (print-function-option             nil)
  344.         (type-option                       t)
  345.         (named-option                      0)
  346.         (initial-offset-option             0)
  347.         (initial-offset                    0)
  348.         (docstring                         nil)
  349.         (slotargs                          docstring-and-slotargs)
  350.          size
  351.         (include-skip                      0)
  352.         (inherited-slot-count              0)
  353.         (slotlist                          nil)
  354.         (slotdefaultvars                   nil)
  355.         (slotdefaultfuns                   nil)
  356.          constructor-forms                      )
  357.     ;; name-and-options ⁿberprⁿfen:
  358.     (when (listp name-and-options)
  359.       (setq name (first name-and-options))
  360.       (setq options (rest name-and-options))
  361.     ) ; andernfalls sind name und options schon korrekt.
  362.     (unless (and (symbolp name) (not (keywordp name)))
  363.       (error-of-type 'program-error
  364.         (DEUTSCH "~S: Falsche Syntax fⁿr Name und Optionen: ~S"
  365.          ENGLISH "~S: invalid syntax for name and options: ~S"
  366.          FRANCAIS "~S : Mauvaise syntaxe pour un nom et des options: ~S")
  367.         'defstruct name-and-options
  368.     ) )
  369.     ; name ist ein Symbol, options die Liste der Optionen.
  370.     ;; Abarbeitung der Optionen:
  371.     (dolist (option options)
  372.       (when (keywordp option) (setq option (list option))) ; Option ohne Argumente
  373.       (if (listp option)
  374.         (if (keywordp (car option))
  375.           (case (first option)
  376.             (:CONC-NAME
  377.                (setq conc-name-option (or (second option) ""))
  378.             )
  379.             (:CONSTRUCTOR
  380.                (if (atom (cdr option))
  381.                  ; Default-Keyword-Constructor
  382.                  (push (concat-pnames "MAKE-" name) constructor-option-list)
  383.                  (let ((arg (second option)))
  384.                    (ds-symbol-or-error arg)
  385.                    (push
  386.                      (if (atom (cddr option))
  387.                        arg ; Keyword-Constructor
  388.                        (if (not (listp (third option)))
  389.                          (error-of-type 'program-error
  390.                            (DEUTSCH "~S ~S: Argumentliste mu▀ eine Liste sein: ~S"
  391.                             ENGLISH "~S ~S: argument list should be a list: ~S"
  392.                             FRANCAIS "~S ~S : La liste d'arguments doit Ωtre une liste: ~S")
  393.                            'defstruct name (third option)
  394.                          )
  395.                          (rest option) ; BOA-Constructor
  396.                      ) )
  397.                      constructor-option-list
  398.             )  ) ) )
  399.             (:COPIER
  400.                (when (consp (cdr option))
  401.                  (let ((arg (second option)))
  402.                    (ds-symbol-or-error arg)
  403.                    (setq copier-option arg)
  404.             )  ) )
  405.             (:PREDICATE
  406.                (when (consp (cdr option))
  407.                  (let ((arg (second option)))
  408.                    (ds-symbol-or-error arg)
  409.                    (setq predicate-option arg)
  410.             )  ) )
  411.             ((:INCLUDE :INHERIT)
  412.                (if (null include-option)
  413.                  (setq include-option option)
  414.                  (error-of-type 'program-error
  415.                    (DEUTSCH "~S ~S: Es darf nur ein :INCLUDE-Argument geben: ~S"
  416.                     ENGLISH "~S ~S: At most one :INCLUDE argument may be specified: ~S"
  417.                     FRANCAIS "~S ~S : Il ne peut y avoir qu'un argument :INCLUDE: ~S")
  418.                    'defstruct name options
  419.             )  ) )
  420.             (:PRINT-FUNCTION
  421.                (let ((arg (second option)))
  422.                  (when (and (consp arg) (eq (first arg) 'FUNCTION))
  423.                    (warn (DEUTSCH "~S: Bei :PRINT-FUNCTION ist FUNCTION bereits implizit.~@
  424.                                    Verwende daher ~S statt ~S."
  425.                           ENGLISH "~S: Use of :PRINT-FUNCTION implicitly applies FUNCTION.~@
  426.                                    Therefore using ~S instead of ~S."
  427.                           FRANCAIS "~S : FUNCTION est dΘjα implicite avec :PRINT-FUNCTION.~@
  428.                                     C'est pourquoi ~S est utilisΘ au lieu de ~S.")
  429.                          'defstruct (second arg) arg
  430.                    )
  431.                    (setq arg (second arg))
  432.                  )
  433.                  (setq print-function-option
  434.                    (if (symbolp arg)
  435.                      ; ein Ausdruck, der eine eventuelle lokale Definition
  436.                      ; von arg mitberⁿcksichtigt, aber nicht erfordert:
  437.                      `(FUNCTION ,(concat-pnames name "-PRINT-FUNCTION")
  438.                         (LAMBDA (STRUCT STREAM DEPTH)
  439.                           (,arg STRUCT STREAM DEPTH)
  440.                       ) )
  441.                      `#',arg
  442.             )  ) ) )
  443.             (:TYPE (setq type-option (second option)))
  444.             (:NAMED (setq named-option t))
  445.             (:INITIAL-OFFSET (setq initial-offset-option (or (second option) 0)))
  446.             (T (error-of-type 'program-error
  447.                  (DEUTSCH "~S ~S: Die Option ~S gibt es nicht."
  448.                   ENGLISH "~S ~S: unknown option ~S"
  449.                   FRANCAIS "~S ~S : Option ~S non reconnue.")
  450.                  'defstruct name (first option)
  451.           ) )  )
  452.           (error-of-type 'program-error
  453.             (DEUTSCH "~S ~S: Falsche Syntax in ~S-Option: ~S"
  454.              ENGLISH "~S ~S: invalid syntax in ~S option: ~S"
  455.              FRANCAIS "~S ~S : Mauvaise syntaxe dans l'option ~S: ~S")
  456.             'defstruct name 'defstruct option
  457.         ) )
  458.         (error-of-type 'program-error
  459.           (DEUTSCH "~S ~S: Das ist keine ~S-Option: ~S"
  460.            ENGLISH "~S ~S: not a ~S option: ~S"
  461.            FRANCAIS "~S ~S : Ceci n'est pas une option ~S: ~S")
  462.           'defstruct name 'defstruct option
  463.     ) ) )
  464.     ; conc-name-option ist entweder T oder "" oder das :CONC-NAME-Argument.
  465.     ; constructor-option-list ist eine Liste aller :CONSTRUCTOR-Argumente,
  466.     ;   jeweils in der Form  symbol  oder  (symbol arglist . ...).
  467.     ; copier-option ist entweder T oder das :COPIER-Argument.
  468.     ; predicate-option ist entweder 0 oder das :PREDICATE-Argument.
  469.     ; include-option ist entweder NIL oder die gesamte
  470.     ;   :INCLUDE/:INHERIT-Option.
  471.     ; print-function-option ist NIL oder eine Form, die die Print-Function
  472.     ;   liefert.
  473.     ; type-option ist entweder T oder das :TYPE-Argument.
  474.     ; named-option ist entweder 0 oder T.
  475.     ; initial-offset-option ist entweder 0 oder das :INITIAL-OFFSET-Argument.
  476.     ;; ▄berprⁿfung der Optionen:
  477.     (setq named-option (or (eq type-option 'T) (eq named-option 'T)))
  478.     ; named-option (NIL oder T) gibt an, ob der Name in der Structure steckt.
  479.     (if named-option
  480.       (when (eql predicate-option 0)
  481.         (setq predicate-option (concat-pnames name "-P")) ; Defaultname
  482.       )
  483.       (unless (or (eql predicate-option 0) (eq predicate-option 'NIL))
  484.         (error-of-type 'program-error
  485.           (DEUTSCH "~S ~S: Bei unbenannten Structures kann es kein :PREDICATE geben."
  486.            ENGLISH "~S ~S: There is no :PREDICATE on unnamed structures."
  487.            FRANCAIS "~S ~S : Il ne peut pas y avoir de :PREDICATE avec des structures anonymes.")
  488.           'defstruct name
  489.     ) ) )
  490.     ; predicate-option ist
  491.     ;   bei named-option=T: entweder NIL oder der Name des TyptestprΣdikats,
  492.     ;   bei named-option=NIL bedeutungslos.
  493.     (if (eq conc-name-option 'T)
  494.       (setq conc-name-option (string-concat (string name) "-"))
  495.     )
  496.     ; conc-name-option ist der Namensprefix.
  497.     (if (null constructor-option-list)
  498.       (setq constructor-option-list (list (concat-pnames "MAKE-" name)))
  499.       (setq constructor-option-list (remove 'NIL constructor-option-list))
  500.     )
  501.     ; constructor-option-list ist eine Liste aller zu kreierenden Konstruktoren,
  502.     ;   jeweils in der Form  symbol  oder  (symbol arglist . ...).
  503.     (if (eq copier-option 'T)
  504.       (setq copier-option (concat-pnames "COPY-" name))
  505.     )
  506.     ; copier-option ist entweder NIL oder der Name der Kopierfunktion.
  507.     (unless (or (eq type-option 'T)
  508.                 (eq type-option 'VECTOR)
  509.                 (eq type-option 'LIST)
  510.                 (and (consp type-option) (eq (first type-option) 'VECTOR))
  511.             )
  512.       (error-of-type 'program-error
  513.         (DEUTSCH "~S ~S: UnzulΣssige :TYPE-Option ~S"
  514.          ENGLISH "~S ~S: invalid :TYPE option ~S"
  515.          FRANCAIS "~S ~S : Option :TYPE inadmissible: ~S")
  516.         'defstruct name type-option
  517.     ) )
  518.     ; type-option ist entweder T oder LIST oder VECTOR oder (VECTOR ...)
  519.     (unless (and (integerp initial-offset-option) (>= initial-offset-option 0))
  520.       (error-of-type 'program-error
  521.         (DEUTSCH "~S ~S: Der :INITIAL-OFFSET mu▀ ein Integer >=0 sein, nicht ~S"
  522.          ENGLISH "~S ~S: The :INITIAL-OFFSET must be a nonnegative integer, not ~S"
  523.          FRANCAIS "~S ~S : :INITIAL-OFFSET doit Ωtre un entier positif ou zΘro et non ~S")
  524.         'defstruct name initial-offset-option
  525.     ) )
  526.     ; initial-offset-option ist ein Integer >=0.
  527.     (when (and (plusp initial-offset-option) (eq type-option 'T))
  528.       (error-of-type 'program-error
  529.         (DEUTSCH "~S ~S: :INITIAL-OFFSET darf nur zusammen mit :TYPE angegeben werden: ~S"
  530.          ENGLISH "~S ~S: :INITIAL-OFFSET must not be specified without :TYPE : ~S"
  531.          FRANCAIS "~S ~S : :INITIAL-OFFSET ne peut Ωtre prΘcisΘ qu'ensemble avec :TYPE: ~S")
  532.         'defstruct name options
  533.     ) )
  534.     ; Bei type-option=T ist initial-offset-option=0.
  535.     (when (eq type-option 'T) (setq include-skip 1))
  536.     ; include-skip ist 1 bei type-option=T, 0 sonst.
  537.     (when (stringp (first docstring-and-slotargs))
  538.       (setq docstring (first docstring-and-slotargs))
  539.       (setq slotargs (rest docstring-and-slotargs))
  540.     ) ; sonst stimmen docstring und slotargs bereits.
  541.     ; docstring ist entweder NIL oder ein String.
  542.     ; slotargs sind die restlichen Argumente.
  543.     (if include-option
  544.       (let* ((option (rest include-option))
  545.              (subname (first option))
  546.              (incl-desc (get subname 'DEFSTRUCT-DESCRIPTION)))
  547.         (when (null incl-desc)
  548.           (error-of-type 'program-error
  549.             (DEUTSCH "~S ~S: Teilstruktur ~S ist nicht definiert."
  550.              ENGLISH "~S ~S: included structure ~S has not been defined."
  551.              FRANCAIS "~S ~S : La structure incluse ~S n'est pas dΘfinie.")
  552.             'defstruct name subname
  553.         ) )
  554.         (setq names (cons name (svref incl-desc 0)))
  555.         (setq namesbinding
  556.               (list
  557.                 (list
  558.                   (setq namesform (gensym))
  559.                   `(CONS ',name (LOAD-TIME-VALUE (SVREF (GET ',subname 'DEFSTRUCT-DESCRIPTION) 0)))
  560.         )     ) )
  561.         (unless (equalp (svref incl-desc 1) type-option)
  562.           (error-of-type 'program-error
  563.             (DEUTSCH "~S ~S: Teilstruktur ~S mu▀ vom selben Typ ~S sein."
  564.              ENGLISH "~S ~S: included structure ~S must be of the same type ~S."
  565.              FRANCAIS "~S ~S : La structure incluse ~S doit Ωtre du mΩme type ~S.")
  566.             'defstruct name subname type-option
  567.         ) )
  568.         (setq slotlist (nreverse (mapcar #'copy-list (svref incl-desc 3))))
  569.         ; slotlist ist die umgedrehte Liste der vererbten Slots
  570.         (when slotlist (setq include-skip (1+ (second (first slotlist)))))
  571.         ; include-skip >=0 ist die Anzahl der bereits von der Teilstruktur
  572.         ;   verbrauchten Slots, das "size" der Teilstruktur.
  573.         ; Weitere Argumente der :INCLUDE-Option abarbeiten:
  574.         (dolist (slotarg (rest option))
  575.           (let* ((slotname (if (atom slotarg) slotarg (first slotarg)))
  576.                  (slot (find slotname slotlist :key #'first :test #'eq)))
  577.             (when (null slot)
  578.               (error-of-type 'program-error
  579.                 (DEUTSCH "~S ~S: Teilstruktur ~S hat keine Komponente namens ~S."
  580.                  ENGLISH "~S ~S: included structure ~S has no component with name ~S."
  581.                  FRANCAIS "~S ~S : La structure incluse ~S n'a pas de composante de nom ~S.")
  582.                 'defstruct name subname slotname
  583.             ) )
  584.             (if (atom slotarg)
  585.               (setf (third slot) 'NIL) ; Default auf NIL ⁿberschreiben
  586.               (progn
  587.                 (let ((default (second slotarg)))
  588.                   (unless (constantp default)
  589.                     (push
  590.                       `(FUNCTION ,(concat-pnames "DEFAULT-" slotname)
  591.                          (LAMBDA () ,default)
  592.                        )
  593.                       slotdefaultfuns
  594.                     )
  595.                     (setq default (gensym))
  596.                     (push default slotdefaultvars)
  597.                   )
  598.                   (setf (third slot) default)
  599.                 )
  600.                 ; slot-options dieses Slot-Specifier abarbeiten:
  601.                 (do ((slot-arglistr (cddr slotarg) (cddr slot-arglistr)))
  602.                     ((endp slot-arglistr))
  603.                   (let ((slot-keyword (first slot-arglistr))
  604.                         (slot-key-value (second slot-arglistr)))
  605.                     (cond ((eq slot-keyword ':READ-ONLY)
  606.                            (if slot-key-value
  607.                              (setf (fifth slot) t)
  608.                              (if (fifth slot)
  609.                                (error-of-type 'program-error
  610.                                  (DEUTSCH "~S ~S: Der READ-ONLY-Slot ~S von Teilstruktur ~S mu▀ auch in ~S READ-ONLY bleiben."
  611.                                   ENGLISH "~S ~S: The READ-ONLY slot ~S of the included structure ~S must remain READ-ONLY in ~S."
  612.                                   FRANCAIS "~S ~S : Le composant READ-ONLY ~S de la structure incluse ~S doit rester READ-ONLY dans ~S.")
  613.                                  'defstruct name slotname subname name
  614.                                )
  615.                                (setf (fifth slot) nil)
  616.                           )) )
  617.                           ((eq slot-keyword ':TYPE)
  618.                            (unless (subtypep slot-key-value (fourth slot))
  619.                              (error-of-type 'program-error
  620.                                (DEUTSCH "~S ~S: Der Typ ~S von Slot ~S mu▀ ein Untertyp des in Teilstruktur ~S definierten Typs ~S sein."
  621.                                 ENGLISH "~S ~S: The type ~S of slot ~S should be a subtype of the type defined for the included strucure ~S, namely ~S."
  622.                                 FRANCAIS "~S ~S : Le type ~S du composant ~S doit Ωtre un sous-type du type dΘfini dans la structure incluse ~S, c'est-α-dire ~S.")
  623.                                'defstruct name slot-key-value slotname subname (fourth slot)
  624.                            ) )
  625.                            (setf (fourth slot) slot-key-value)
  626.                           )
  627.                           (t (error-of-type 'program-error
  628.                                (DEUTSCH "~S ~S: ~S ist keine Slot-Option."
  629.                                 ENGLISH "~S ~S: ~S is not a slot option."
  630.                                 FRANCAIS "~S ~S : ~S n'est pas un option de composant.")
  631.                                'defstruct name slot-keyword
  632.                           )  )
  633.                 ) ) )
  634.         ) ) ) )
  635.         (when (eq (first include-option) ':INHERIT)
  636.           (setq inherited-slot-count (length slotlist))
  637.       ) )
  638.       (progn
  639.         (setq names (list name))
  640.         (setq namesform `',names)
  641.     ) )
  642.     ; names ist die Include-Verschachtelung, namesform die Form dazu.
  643.     ; slotlist ist die bisherige Slotliste, umgedreht.
  644.     ; inherited-slot-count ist die Anzahl der Slots, die beim Bilden der
  645.     ; Accessoren zu ignorieren sind.
  646.     (when (and named-option ; benannte Structure
  647.                (consp type-option) ; vom Typ (VECTOR ...)
  648.                ; mu▀ den/die Namen enthalten k÷nnen:
  649.                (not (typep names (second type-option)))
  650.           )
  651.       (error-of-type 'program-error
  652.         (DEUTSCH "~S ~S: Structure vom Typ ~S kann den Namen nicht enthalten."
  653.          ENGLISH "~S ~S: structure of type ~S can't hold the name."
  654.          FRANCAIS "~S ~S : Une structure de type ~S ne peut pas contenir le nom.")
  655.         'defstruct name type-option
  656.     ) )
  657.     ; Aufbau der Structure:
  658.     ; names, evtl. include-Slots, initial-offset-option mal NIL, Slots.
  659.     ; Aufbau von Vektor oder Liste:
  660.     ; include-Anteil, initial-offset-option mal NIL, evtl. Name, Slots.
  661.     (setq initial-offset (+ include-skip initial-offset-option))
  662.     (unless (eq type-option 'T)
  663.       (when named-option
  664.         (push
  665.           (list nil ; Kennzeichen fⁿr Typerkennungs-Slot
  666.                 (setq initial-offset-option initial-offset)
  667.                 name ; "Defaultwert" = name
  668.                 'SYMBOL ; Typ = Symbol
  669.                 T) ; Read-Only
  670.           slotlist
  671.         )
  672.         (setq initial-offset (1+ initial-offset))
  673.     ) )
  674.     ; Die einzelnen Slots kommen ab initial-offset.
  675.     ; Bei type/=T (also Vektor oder Liste) und named-option sitzt
  676.     ;   der Name in Slot Nummer  initial-offset-option = (1- initial-offset).
  677.     ; Abarbeitung der einzelnen Slots:
  678.     (let ((offset initial-offset))
  679.       (dolist (slotarg slotargs)
  680.         (let (slotname
  681.               default)
  682.           (if (atom slotarg)
  683.             (setq slotname slotarg  default nil)
  684.             (setq slotname (first slotarg)  default (second slotarg))
  685.           )
  686.           (unless (constantp default)
  687.             (push
  688.               `(FUNCTION ,(concat-pnames "DEFAULT-" slotname)
  689.                  (LAMBDA () ,default)
  690.                )
  691.               slotdefaultfuns
  692.             )
  693.             (setq default (gensym))
  694.             (push default slotdefaultvars)
  695.           )
  696.           (when (find slotname slotlist :key #'first :test #'eq)
  697.             (error-of-type 'program-error
  698.               (DEUTSCH "~S ~S: Es kann nicht mehrere Slots mit demselben Namen ~S geben."
  699.                ENGLISH "~S ~S: There may be only one slot with the name ~S."
  700.                FRANCAIS "~S ~S : Il ne peut pas y avoir plusieurs composants avec le mΩme nom ~S.")
  701.               'defstruct name slotname
  702.           ) )
  703.           (let ((type t) (read-only nil))
  704.             (when (consp slotarg)
  705.               (do ((slot-arglistr (cddr slotarg) (cddr slot-arglistr)))
  706.                   ((endp slot-arglistr))
  707.                 (let ((slot-keyword (first slot-arglistr))
  708.                       (slot-key-value (second slot-arglistr)))
  709.                   (cond ((eq slot-keyword ':READ-ONLY)
  710.                          (setq read-only (if slot-key-value t nil))
  711.                         )
  712.                         ((eq slot-keyword ':TYPE) (setq type slot-key-value))
  713.                         (t (error-of-type 'program-error
  714.                              (DEUTSCH "~S ~S: ~S ist keine Slot-Option."
  715.                               ENGLISH "~S ~S: ~S is not a slot option."
  716.                               FRANCAIS "~S ~S : ~S n'est pas une option de composant.")
  717.                              'defstruct name slot-keyword
  718.                         )  )
  719.             ) ) ) )
  720.             (push (list slotname offset default type read-only) slotlist)
  721.         ) )
  722.         (incf offset)
  723.       )
  724.       (setq size offset)
  725.     )
  726.     ; size = GesamtlΣnge der Structure
  727.     (setq slotlist (nreverse slotlist))
  728.     (setq slotdefaultfuns (nreverse slotdefaultfuns))
  729.     (setq slotdefaultvars (nreverse slotdefaultvars))
  730.     ; Die slots in slotlist sind jetzt wieder aufsteigend geordnet.
  731.     (setq constructor-forms
  732.       (mapcar
  733.         #'(lambda (constructor-option)
  734.             (if (consp constructor-option)
  735.               (ds-make-boa-constructor
  736.                 constructor-option type-option name namesform size slotlist
  737.               )
  738.               (progn
  739.                 (if (null keyword-constructor)
  740.                   (setq keyword-constructor constructor-option)
  741.                 )
  742.                 (ds-make-keyword-constructor
  743.                   constructor-option type-option name namesform size slotlist
  744.           ) ) ) )
  745.         constructor-option-list
  746.     ) )
  747.     ; constructor-forms = Liste der Formen, die die Konstruktoren definieren.
  748.     (let ((index 4))
  749.       (dolist (defaultvar slotdefaultvars)
  750.         (setf (third (find defaultvar slotlist :key #'third :test #'eq))
  751.               `(SVREF (GET ',name 'DEFSTRUCT-DESCRIPTION) ,index)
  752.         )
  753.         (incf index)
  754.     ) )
  755.     ; slotlist enthΣlt nun keine der slotdefaultvars mehr.
  756.     `(EVAL-WHEN (LOAD COMPILE EVAL)
  757.        (LET ()
  758.          (LET ,(append namesbinding (mapcar #'list slotdefaultvars slotdefaultfuns))
  759.            ,@constructor-forms
  760.            (%PUT ',name 'DEFSTRUCT-DESCRIPTION
  761.                  (VECTOR ,namesform ',type-option ',keyword-constructor ',slotlist
  762.                          ,@slotdefaultvars
  763.          ) )     )
  764.          ,@(if (eq type-option 'T) `((CLOS::DEFINE-STRUCTURE-CLASS ',name)))
  765.          ,@(if (and named-option predicate-option)
  766.              (ds-make-pred predicate-option type-option name initial-offset-option)
  767.            )
  768.          ,@(if copier-option (ds-make-copier copier-option name type-option))
  769.          ,@(let ((directslotlist (nthcdr inherited-slot-count slotlist)))
  770.              `(,@(ds-make-accessors name type-option conc-name-option directslotlist)
  771.                ,@(ds-make-defsetfs name type-option conc-name-option directslotlist)
  772.               )
  773.            )
  774.          (SETF (DOCUMENTATION ',name 'STRUCTURE) ,docstring)
  775.          ,(if print-function-option
  776.             `(%PUT ',name 'STRUCTURE-PRINT ,print-function-option)
  777.             `(REMPROP ',name 'STRUCTURE-PRINT)
  778.           )
  779.          ',name
  780.      ) )
  781. ) )
  782.  
  783.