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