Changeset 4817


Ignore:
Timestamp:
11/18/03 00:50:48 (18 years ago)
Author:
piso
Message:

Work in progress.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/j/src/org/armedbear/lisp/defstruct.lisp

    r4204 r4817  
    22;;;
    33;;; Copyright (C) 2003 Peter Graves
    4 ;;; $Id: defstruct.lisp,v 1.27 2003-10-05 18:37:59 piso Exp $
     4;;; $Id: defstruct.lisp,v 1.28 2003-11-18 00:50:48 piso Exp $
    55;;;
    66;;; This program is free software; you can redistribute it and/or
     
    2424(defvar *ds-constructors*)
    2525(defvar *ds-copier*)
     26(defvar *ds-type*)
    2627(defvar *ds-named*)
    2728(defvar *ds-predicate*)
     
    3435         (slot-descriptions (mapcar #'(lambda (x y) (list x y)) slot-names inits))
    3536         (keys (cons '&key slot-descriptions)))
    36     `((defun ,constructor-name ,keys
    37         (%make-structure ',*ds-name* (list ,@slot-names))))))
     37    (case *ds-type*
     38      (LIST
     39       (if *ds-named*
     40           `((defun ,constructor-name ,keys
     41               (list ',*ds-name* ,@slot-names)))
     42           `((defun ,constructor-name ,keys
     43               (list ,@slot-names)))))
     44      (t
     45       `((defun ,constructor-name ,keys
     46           (%make-structure ',*ds-name* (list ,@slot-names))))))))
    3847
    3948(defun default-constructor-name ()
     
    4554        (dolist (constructor *ds-constructors*)
    4655          (when (car constructor)
    47             (setf results (append results (define-constructor constructor slots)))))
     56            (setf results (nconc results (define-constructor constructor slots)))))
    4857        results)
    4958      (define-constructor (cons (default-constructor-name) nil) slots)))
    5059
    5160(defun define-predicate ()
    52   (when *ds-predicate*
     61  (when (and *ds-predicate*
     62             (or *ds-named* (null *ds-type*)))
    5363    (let ((pred (intern *ds-predicate*)))
    5464      `((defun ,pred (object)
     
    5666
    5767
    58 (defmacro get-slot-accessor (slot)
    59   (case slot
    60     (0 #'%structure-ref-0)
    61     (1 #'%structure-ref-1)
    62     (2 #'%structure-ref-2)
     68(defmacro get-slot-accessor (slot type)
     69  (case type
     70    (LIST
     71     `(lambda (instance) (elt instance ,slot)))
    6372    (t
    64      `(lambda (instance) (%structure-ref instance ,slot)))))
     73     (case slot
     74       (0 #'%structure-ref-0)
     75       (1 #'%structure-ref-1)
     76       (2 #'%structure-ref-2)
     77       (t
     78        `(lambda (instance) (%structure-ref instance ,slot)))))))
    6579
    66 (defmacro get-slot-mutator (slot)
    67   (case slot
    68     (0 #'%structure-set-0)
    69     (1 #'%structure-set-1)
    70     (2 #'%structure-set-2)
     80(defmacro get-slot-mutator (slot type)
     81  (case type
     82    (LIST
     83     `(lambda (instance value) (%set-elt instance ,slot value)))
    7184    (t
    72      `(lambda (instance value) (%structure-set instance ,slot value)))))
     85     (case slot
     86       (0 #'%structure-set-0)
     87       (1 #'%structure-set-1)
     88       (2 #'%structure-set-2)
     89       (t
     90        `(lambda (instance value) (%structure-set instance ,slot value)))))))
    7391
    7492(defun define-access-function (slot-name index)
     
    7795             (intern (concatenate 'string (symbol-name *ds-conc-name*) (symbol-name slot-name)))
    7896             slot-name)))
    79     `((setf (symbol-function ',accessor) (get-slot-accessor ,index))
    80       (%put ',accessor 'setf-inverse (get-slot-mutator ,index)))))
     97    `((setf (symbol-function ',accessor) (get-slot-accessor ,index ,*ds-type*))
     98      (%put ',accessor 'setf-inverse (get-slot-mutator ,index ,*ds-type*)))))
    8199
    82100(defun define-access-functions (slots)
     
    85103    (dolist (slot slots)
    86104      (let ((slot-name (if (atom slot) slot (car slot))))
    87         (setf result (append result (define-access-function slot-name index))))
     105        (setf result (nconc result (define-access-function slot-name index))))
    88106      (incf index))
    89107    result))
     
    91109(defun define-copier ()
    92110  (when *ds-copier*
    93     `((setf (fdefinition ',*ds-copier*) #'copy-structure))))
     111    (case *ds-type*
     112      (LIST
     113       `((setf (fdefinition ',*ds-copier*) #'copy-list)))
     114      (t
     115       `((setf (fdefinition ',*ds-copier*) #'copy-structure))))))
    94116
    95117(defun parse-1-option (option)
     
    124146       (if (null (cadr option))
    125147           (setf *ds-predicate* nil)
    126            (setf *ds-predicate* (symbol-name (cadr option))))))))
     148           (setf *ds-predicate* (symbol-name (cadr option))))))
     149    (:type
     150     (setf *ds-type* (cadr option)))
     151    (t
     152     (format t "unrecognized DEFSTRUCT option: ~S~%" (car option)))))
    127153
    128154(defun parse-name-and-options (name-and-options)
     
    148174        (*ds-constructors* nil)
    149175        (*ds-copier* nil)
     176        (*ds-type* nil)
     177        (*ds-named* nil)
    150178        (*ds-predicate* nil)
    151179        (*ds-print-function* nil))
     
    162190       ,@(define-copier)
    163191       ',*ds-name*)))
    164 
    165 (provide 'defstruct)
Note: See TracChangeset for help on using the changeset viewer.