accessors.scm 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. ;;;; Copyright (C) 1999, 2000, 2005, 2006 Free Software Foundation, Inc.
  2. ;;;;
  3. ;;;; This program is free software; you can redistribute it and/or modify
  4. ;;;; it under the terms of the GNU General Public License as published by
  5. ;;;; the Free Software Foundation; either version 2, or (at your option)
  6. ;;;; any later version.
  7. ;;;;
  8. ;;;; This program is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;;;; GNU General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU General Public License
  14. ;;;; along with this software; see the file COPYING. If not, write to
  15. ;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  16. ;;;; Boston, MA 02110-1301 USA
  17. ;;;;
  18. (define-module (oop goops accessors)
  19. :use-module (oop goops)
  20. :re-export (standard-define-class)
  21. :export (define-class-with-accessors
  22. define-class-with-accessors-keywords))
  23. (define define-class-with-accessors
  24. (procedure->memoizing-macro
  25. (lambda (exp env)
  26. (let ((name (cadr exp))
  27. (supers (caddr exp))
  28. (slots (cdddr exp))
  29. (eat? #f))
  30. `(standard-define-class ,name ,supers
  31. ,@(map-in-order
  32. (lambda (slot)
  33. (cond (eat?
  34. (set! eat? #f)
  35. slot)
  36. ((keyword? slot)
  37. (set! eat? #t)
  38. slot)
  39. ((pair? slot)
  40. (if (get-keyword #:accessor (cdr slot) #f)
  41. slot
  42. (let ((name (car slot)))
  43. `(,name #:accessor ,name ,@(cdr slot)))))
  44. (else
  45. `(,slot #:accessor ,slot))))
  46. slots))))))
  47. (define define-class-with-accessors-keywords
  48. (procedure->memoizing-macro
  49. (lambda (exp env)
  50. (let ((name (cadr exp))
  51. (supers (caddr exp))
  52. (slots (cdddr exp))
  53. (eat? #f))
  54. `(standard-define-class ,name ,supers
  55. ,@(map-in-order
  56. (lambda (slot)
  57. (cond (eat?
  58. (set! eat? #f)
  59. slot)
  60. ((keyword? slot)
  61. (set! eat? #t)
  62. slot)
  63. ((pair? slot)
  64. (let ((slot
  65. (if (get-keyword #:accessor (cdr slot) #f)
  66. slot
  67. (let ((name (car slot)))
  68. `(,name #:accessor ,name ,@(cdr slot))))))
  69. (if (get-keyword #:init-keyword (cdr slot) #f)
  70. slot
  71. (let* ((name (car slot))
  72. (keyword (symbol->keyword name)))
  73. `(,name #:init-keyword ,keyword ,@(cdr slot))))))
  74. (else
  75. `(,slot #:accessor ,slot
  76. #:init-keyword ,(symbol->keyword slot)))))
  77. slots))))))