ps-syntax.scm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Richard Kelsey
  3. ; Redefine CASE so that it doesn't call MEMV
  4. (define-syntax case
  5. (lambda (e r c)
  6. (let ((x (r 'x))
  7. (xlet (r 'let))
  8. (xcond (r 'cond))
  9. (xif (r 'if))
  10. (xeq? (r 'eq?))
  11. (xquote (r 'quote)))
  12. (let ((test (lambda (y)
  13. `(,xeq? ,x (,xquote ,y)))))
  14. `(,xlet ((,x ,(cadr e)))
  15. (,xcond . ,(map (lambda (clause)
  16. (if (c (car clause) 'else)
  17. clause
  18. `(,(let label ((xs (car clause)))
  19. (cond ((null? xs) #f)
  20. ((null? (cdr xs))
  21. (test (car xs)))
  22. (else
  23. `(,xif ,(test (car xs))
  24. #t
  25. ,(label (cdr xs))))))
  26. . ,(cdr clause))))
  27. (cddr e))))))))
  28. ; RECEIVE (from big-scheme)
  29. (define-syntax receive
  30. (syntax-rules ()
  31. ((receive ?vars ?producer . ?body)
  32. (call-with-values (lambda () ?producer)
  33. (lambda ?vars . ?body)))))
  34. (define-syntax external
  35. (lambda (e r c)
  36. (let ((l (length e)))
  37. (if (and (or (= l 3) (= l 4))
  38. (string? (cadr e)))
  39. `(,(r 'real-external) ,(cadr e) ',(caddr e))
  40. e))))
  41. ; DEFINE-EXTERNAL-ENUMERATION (from prescheme)
  42. (define-syntax define-external-enumeration
  43. (lambda (form rename compare)
  44. (let* ((name (cadr form))
  45. (symbol->upcase-string
  46. (lambda (s)
  47. (list->string (map (lambda (c)
  48. (if (char=? c #\-)
  49. #\_
  50. (char-upcase c)))
  51. (string->list (symbol->string s))))))
  52. (constant
  53. (lambda (sym string)
  54. `(,(rename 'make-external-constant) ',name ',sym ,string)))
  55. (conc (lambda things
  56. (string->symbol (apply string-append
  57. (map (lambda (thing)
  58. (if (symbol? thing)
  59. (symbol->string thing)
  60. thing))
  61. things)))))
  62. (var-name
  63. (lambda (sym)
  64. (conc name "/" sym)))
  65. (components
  66. (list->vector
  67. (map (lambda (stuff)
  68. (if (pair? stuff)
  69. (cons (car stuff)
  70. (var-name (car stuff)))
  71. (cons stuff
  72. (var-name stuff))))
  73. (caddr form))))
  74. (%define (rename 'define))
  75. (%define-syntax (rename 'define-syntax))
  76. (%begin (rename 'begin))
  77. (%quote (rename 'quote))
  78. (%make-external-constant (rename 'make-external-constant))
  79. (e-name (conc name '- 'enumeration))
  80. (count (vector-length components)))
  81. `(,%begin
  82. (,%define-syntax ,name
  83. (let ((components ',components))
  84. (lambda (e r c)
  85. (let ((key (cadr e)))
  86. (cond ((c key 'enum)
  87. (let ((which (caddr e)))
  88. (let loop ((i 0)) ;vector-posq
  89. (if (< i ,count)
  90. (if (c which (car (vector-ref components i)))
  91. (r (cdr (vector-ref components i)))
  92. (loop (+ i 1)))
  93. ;; (syntax-error "unknown enumerand name"
  94. ;; `(,(cadr e) ,(car e) ,(caddr e)))
  95. e))))
  96. (else e))))))
  97. (,%define ,(conc name '- 'count) ,count)
  98. . ,(map (lambda (stuff)
  99. (if (pair? stuff)
  100. `(,%define ,(var-name (car stuff))
  101. (,%make-external-constant ',name
  102. ',(car stuff)
  103. ,(cadr stuff)))
  104. `(,%define ,(var-name stuff)
  105. (,%make-external-constant ',name
  106. ',stuff
  107. ,(symbol->upcase-string stuff)))))
  108. (caddr form)))))
  109. (begin define define-syntax quote external make-external-constant))