s48-defrecord.scm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. ;;; Ported from Scheme 48 1.9. See file COPYING for notices and license.
  2. ;;;
  3. ;;; Port Author: Andrew Whatson
  4. ;;;
  5. ;;; Original Authors: Richard Kelsey, Jonathan Rees
  6. ;;;
  7. ;;; scheme48-1.9.2/scheme/big/defrecord.scm
  8. ;;;
  9. ;;; Syntax for defining record types
  10. ;;;
  11. ;;; This knows about the implementation of records and creates the various
  12. ;;; accessors, mutators, etc. directly instead of calling the procedures
  13. ;;; from the record structure. This is done to allow the optional auto-inlining
  14. ;;; optimizer to inline the accessors, mutators, etc.
  15. ;;;
  16. ;;; LOOPHOLE is used to get a little compile-time type checking (in addition to
  17. ;;; the usual complete run-time checking).
  18. ;;;
  19. ;;; (define-record-type name constructor-fields other-fields)
  20. ;;;
  21. ;;; Constructor-arguments fields are either <name> or (<name>), the second
  22. ;;; indicating a field whose value can be modified.
  23. ;;; Other-fields are one of:
  24. ;;; (<name> <expression>) = modifiable field with the given value.
  25. ;;; <name> = modifiable field with no initial value.
  26. ;;;
  27. ;;;(define-record-type job
  28. ;;; ((thunk)
  29. ;;; (dynamic-env)
  30. ;;; number
  31. ;;; inferior-lock
  32. ;;; )
  33. ;;; ((on-queue #f)
  34. ;;; (superior #f)
  35. ;;; (inferiors '())
  36. ;;; (condition #f)
  37. ;;; ))
  38. (define-module (prescheme s48-defrecord)
  39. #:use-module ((srfi srfi-9) #:prefix srfi-9:)
  40. #:use-module (prescheme scheme48)
  41. #:use-module (prescheme syntax-utils)
  42. #:export (define-record-type))
  43. (define-syntax define-record-type
  44. (lambda (x)
  45. (syntax-case x ()
  46. ((_ name (arg-defs ...) (other-defs ...))
  47. (with-syntax ((type-name (syntax-conc 'type/ #'name))
  48. (pred-name (syntax-conc #'name '?))
  49. (cons-name (syntax-conc '%make- #'name))
  50. (maker-name (syntax-conc #'name '-maker))
  51. ((arg-names ...) (map (lambda (def)
  52. (syntax-case def ()
  53. ((fname) #'fname)
  54. (fname #'fname)))
  55. #'(arg-defs ...)))
  56. ((other-names ...) (map (lambda (def)
  57. (syntax-case def ()
  58. ((fname _) #'fname)
  59. (fname #'fname)))
  60. #'(other-defs ...)))
  61. ((other-values ...) (map (lambda (def)
  62. (syntax-case def ()
  63. ((_ value) #'value)
  64. (_ #'unspecific)))
  65. #'(other-defs ...))))
  66. (let* ((field-setter? (append (map (lambda (def)
  67. (syntax-case def ()
  68. ((fname) #t)
  69. (_ #f)))
  70. #'(arg-defs ...))
  71. (make-list
  72. (length #'(other-defs ...)) #t)))
  73. (field-names #'(arg-names ... other-names ...))
  74. (field-getters (map (lambda (fname)
  75. (syntax-conc #'name '- fname))
  76. field-names))
  77. (field-setters (map (lambda (fname setter?)
  78. (if setter?
  79. (syntax-conc 'set- #'name '- fname '!)
  80. #f))
  81. field-names field-setter?)))
  82. #`(begin
  83. (srfi-9:define-record-type type-name
  84. (cons-name arg-names ... other-names ...)
  85. pred-name
  86. #,@(map (lambda (fname getter setter)
  87. (if setter
  88. #`(#,fname #,getter #,setter)
  89. #`(#,fname #,getter)))
  90. field-names field-getters field-setters))
  91. (define (maker-name arg-names ...)
  92. (cons-name arg-names ... other-values ...))
  93. )))))))