syntax.scm 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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, Mike Sperber
  6. ;;;
  7. ;;; scheme48-1.9.2/ps-compiler/util/syntax.scm
  8. ;;;
  9. ;;; Syntax used by the compiler
  10. ;;;
  11. ;;; Subrecords
  12. ;;;
  13. ;;; SUPER is the name of the existing record
  14. ;;; SUB is the name of the subrecord
  15. ;;; SLOT is the name of the slot to use in the existing sturcture
  16. ;;; STUFF is the usual stuff from DEFINE-RECORD-TYPE
  17. (define-module (ps-compiler util syntax)
  18. #:use-module (srfi srfi-1)
  19. #:use-module (prescheme s48-defrecord)
  20. #:use-module (prescheme syntax-utils)
  21. #:export (define-subrecord
  22. define-subrecord-type
  23. define-local-syntax))
  24. (define-syntax define-subrecord
  25. (lambda (x)
  26. (syntax-case x ()
  27. ((_ super sub slot (arg-defs ...) (other-defs ...))
  28. (let* ((field-names (map (lambda (def)
  29. (syntax-case def ()
  30. ((fname _ ...) #'fname)
  31. (fname #'fname)))
  32. #'(arg-defs ... other-defs ...)))
  33. (field-setter? (append (map (lambda (def)
  34. (syntax-case def ()
  35. ((fname) #t)
  36. (_ #f)))
  37. #'(arg-defs ...))
  38. (make-list
  39. (length #'(other-defs ...)) #t))))
  40. #`(begin
  41. (define-record-type sub
  42. (arg-defs ...)
  43. (other-defs ...))
  44. #,@(map (lambda (fname)
  45. (let ((super-get (syntax-conc #'super '- fname))
  46. (sub-get (syntax-conc #'sub '- fname)))
  47. #`(define (#,super-get v)
  48. (#,sub-get (slot v)))))
  49. field-names)
  50. #,@(filter-map (lambda (fname setter?)
  51. (and setter?
  52. (let ((super-set (syntax-conc 'set- #'super '- fname '!))
  53. (sub-set (syntax-conc 'set- #'sub '- fname '!)))
  54. #`(define (#,super-set v n)
  55. (#,sub-set (slot v) n)))))
  56. field-names field-setter?)
  57. ))))))
  58. ;; Subrecords, version for JAR/SRFI-9 records
  59. ;; This should eventually replace the above.
  60. ;;
  61. ;; (define-subrecord-type id type-name super-slot
  62. ;; (maker ...)
  63. ;; predicate?
  64. ;; (slot accessor [modifier])
  65. ;; ...)
  66. ;;
  67. ;; SUPER-SLOT is the name of the slot to use in the existing record.
  68. #|
  69. (define-syntax define-subrecord-type
  70. (lambda (form rename compare)
  71. (let ((id (cadr form))
  72. (type (caddr form))
  73. (slot (cadddr form))
  74. (rest (cddddr form))
  75. (%define-record-type (rename 'define-record-type))
  76. (%define (rename 'define))
  77. (%x (rename 'v))
  78. (%v (rename 'x)))
  79. (let ((maker (car rest))
  80. (pred (cadr rest))
  81. (slots (cddr rest))
  82. (gensym (lambda (s i)
  83. (rename (string->symbol
  84. (string-append (symbol->string s)
  85. "%"
  86. (number->string i)))))))
  87. `(begin
  88. (,%define-record-type ,id ,type
  89. ,maker
  90. ,pred
  91. ,@(do ((slots slots (cdr slots))
  92. (i 0 (+ i 1))
  93. (new '() `((,(caar slots)
  94. ,(gensym 'subrecord-ref i)
  95. ,@(if (null? (cddar slots))
  96. '()
  97. `(,(gensym 'subrecord-set i))))
  98. . ,new)))
  99. ((null? slots)
  100. (reverse new))))
  101. ,@(do ((slots slots (cdr slots))
  102. (i 0 (+ i 1))
  103. (new '() `(,@(if (null? (cddar slots))
  104. '()
  105. `((,%define (,(caddar slots) ,%x ,%v)
  106. (,(gensym 'subrecord-set i)
  107. (,slot ,%x)
  108. ,%v))))
  109. (,%define (,(cadar slots) ,%x)
  110. (,(gensym 'subrecord-ref i)
  111. (,slot ,%x)))
  112. . ,new)))
  113. ((null? slots)
  114. (reverse new))))))))
  115. |#
  116. ;;(define-syntax define-simple-record-type
  117. ;; (lambda (form rename compare)
  118. ;; (let ((name (cadr form))
  119. ;; (slots (cddr form)))
  120. ;; `(begin (define-record-type ,name ,slots ())
  121. ;; (define ,(concatenate-symbol 'make- name)
  122. ;; ,(concatenate-symbol name '- 'maker))))))
  123. ;; Nothing actually local about it...
  124. #|
  125. (define-syntax define-local-syntax
  126. (lambda (form rename compare)
  127. (let ((pattern (cadr form))
  128. (body (cddr form)))
  129. `(,(rename 'define-syntax) ,(car pattern)
  130. (,(rename 'lambda) (form rename compare)
  131. (,(rename 'destructure) ((,(cdr pattern)
  132. (,(rename 'cdr) form)))
  133. . ,body))))))
  134. |#