ps-defenum.scm 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ; DEFINE-ENUMERATION macro hacked to use external (C enumeration) names
  3. ; instead of integers.
  4. ;(define-external-enumeration bing
  5. ; ((foo "BAR")
  6. ; baz))
  7. ; =>
  8. ;(define-syntax bing ...)
  9. ;(define bing/foo (make-external-constant 'bing 'foo "BAR"))
  10. ;(define bing/baz (make-external-constant 'bing 'baz "BAZ"))
  11. ;
  12. ;(enum bing foo) => bing/foo
  13. (define-record-type external-constant :external-constant
  14. (make-external-constant enum-name name c-string)
  15. external-constant?
  16. (enum-name external-constant-enum-name)
  17. (name external-constant-name)
  18. (c-string external-constant-c-string))
  19. (define-record-discloser :external-constant
  20. (lambda (external-const)
  21. (list 'enum
  22. (external-constant-enum-name external-const)
  23. (external-constant-name external-const))))
  24. (define-syntax define-external-enumeration
  25. (lambda (form rename compare)
  26. (let* ((name (cadr form))
  27. (symbol->upcase-string
  28. (lambda (s)
  29. (list->string (map (lambda (c)
  30. (if (char=? c #\-)
  31. #\_
  32. (char-upcase c)))
  33. (string->list (symbol->string s))))))
  34. (constant
  35. (lambda (sym string)
  36. `(,(rename 'make-external-constant) ',name ',sym ,string)))
  37. (conc (lambda things
  38. (string->symbol (apply string-append
  39. (map (lambda (thing)
  40. (if (symbol? thing)
  41. (symbol->string thing)
  42. thing))
  43. things)))))
  44. (var-name
  45. (lambda (sym)
  46. (conc name "/" sym)))
  47. (components
  48. (list->vector
  49. (map (lambda (stuff)
  50. (if (pair? stuff)
  51. (cons (car stuff)
  52. (var-name (car stuff)))
  53. (cons stuff
  54. (var-name stuff))))
  55. (caddr form))))
  56. (%define (rename 'define))
  57. (%define-syntax (rename 'define-syntax))
  58. (%begin (rename 'begin))
  59. (%quote (rename 'quote))
  60. (%make-external-constant (rename 'make-external-constant))
  61. (e-name (conc name '- 'enumeration))
  62. (count (vector-length components)))
  63. `(,%begin
  64. (,%define-syntax ,name
  65. (let ((components ',components))
  66. (lambda (e r c)
  67. (let ((key (cadr e)))
  68. (cond ((c key 'enum)
  69. (let ((which (caddr e)))
  70. (let loop ((i 0)) ;vector-posq
  71. (if (< i ,count)
  72. (if (c which (car (vector-ref components i)))
  73. (r (cdr (vector-ref components i)))
  74. (loop (+ i 1)))
  75. ;; (syntax-error "unknown enumerand name"
  76. ;; `(,(cadr e) ,(car e) ,(caddr e)))
  77. e))))
  78. (else e))))))
  79. (,%define ,(conc name '- 'count) ,count)
  80. . ,(map (lambda (stuff)
  81. (if (pair? stuff)
  82. `(,%define ,(var-name (car stuff))
  83. (,%make-external-constant ',name
  84. ',(car stuff)
  85. ,(cadr stuff)))
  86. `(,%define ,(var-name stuff)
  87. (,%make-external-constant ',name
  88. ',stuff
  89. ,(symbol->upcase-string stuff)))))
  90. (caddr form)))))
  91. (begin define define-syntax quote external make-external-constant))