71.upstream.scm 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. ;;; Copyright (c) 2005 Sebastian Egner.
  2. ;;; Permission is hereby granted, free of charge, to any person obtaining a copy
  3. ;;; of this software and associated documentation files (the ``Software''), to
  4. ;;; deal in the Software without restriction, including without limitation the
  5. ;;; rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  6. ;;; sell copies of the Software, and to permit persons to whom the Software is
  7. ;;; furnished to do so, subject to the following conditions:
  8. ;;; The above copyright notice and this permission notice shall be included in
  9. ;;; all copies or substantial portions of the Software.
  10. ;;; THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. ;;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. ;;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. ;;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  14. ;;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  15. ;;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  16. ;;; IN THE SOFTWARE.
  17. ; Reference implementation of SRFI-71 (generic part)
  18. ; Sebastian.Egner@philips.com, 20-May-2005, PLT 208
  19. ;
  20. ; In order to avoid conflicts with the existing let etc.
  21. ; the macros defined here are called srfi-let etc.,
  22. ; and they are defined in terms of r5rs-let etc.
  23. ; It is up to the actual implementation to save let/*/rec
  24. ; in r5rs-let/*/rec first and redefine let/*/rec
  25. ; by srfi-let/*/rec then.
  26. ;
  27. ; There is also a srfi-letrec* being defined (in view of R6RS.)
  28. ;
  29. ; Macros used internally are named i:<something>.
  30. ;
  31. ; Abbreviations for macro arguments:
  32. ; bs - <binding spec>
  33. ; b - component of a binding spec (values, <variable>, or <expression>)
  34. ; v - <variable>
  35. ; vr - <variable> for rest list
  36. ; x - <expression>
  37. ; t - newly introduced temporary variable
  38. ; vx - (<variable> <expression>)
  39. ; rec - flag if letrec is produced (and not let)
  40. ; cwv - call-with-value skeleton of the form (x formals)
  41. ; (call-with-values (lambda () x) (lambda formals /payload/))
  42. ; where /payload/ is of the form (let (vx ...) body1 body ...).
  43. ;
  44. ; Remark (*):
  45. ; We bind the variables of a letrec to i:undefined since there is
  46. ; no portable (R5RS) way of binding a variable to a values that
  47. ; raises an error when read uninitialized.
  48. (define i:undefined 'undefined)
  49. (define-syntax srfi-letrec* ; -> srfi-letrec
  50. (syntax-rules ()
  51. ((srfi-letrec* () body1 body ...)
  52. (srfi-letrec () body1 body ...))
  53. ((srfi-letrec* (bs) body1 body ...)
  54. (srfi-letrec (bs) body1 body ...))
  55. ((srfi-letrec* (bs1 bs2 bs ...) body1 body ...)
  56. (srfi-letrec (bs1) (srfi-letrec* (bs2 bs ...) body1 body ...)))))
  57. (define-syntax srfi-letrec ; -> i:let
  58. (syntax-rules ()
  59. ((srfi-letrec ((b1 b2 b ...) ...) body1 body ...)
  60. (i:let "bs" #t () () (body1 body ...) ((b1 b2 b ...) ...)))))
  61. (define-syntax srfi-let* ; -> srfi-let
  62. (syntax-rules ()
  63. ((srfi-let* () body1 body ...)
  64. (srfi-let () body1 body ...))
  65. ((srfi-let* (bs) body1 body ...)
  66. (srfi-let (bs) body1 body ...))
  67. ((srfi-let* (bs1 bs2 bs ...) body1 body ...)
  68. (srfi-let (bs1) (srfi-let* (bs2 bs ...) body1 body ...)))))
  69. (define-syntax srfi-let ; -> i:let or i:named-let
  70. (syntax-rules ()
  71. ((srfi-let ((b1 b2 b ...) ...) body1 body ...)
  72. (i:let "bs" #f () () (body1 body ...) ((b1 b2 b ...) ...)))
  73. ((srfi-let tag ((b1 b2 b ...) ...) body1 body ...)
  74. (i:named-let tag () (body1 body ...) ((b1 b2 b ...) ...)))))
  75. (define-syntax i:let
  76. (syntax-rules (values)
  77. ; (i:let "bs" rec (cwv ...) (vx ...) body (bs ...))
  78. ; processes the binding specs bs ... by adding call-with-values
  79. ; skeletons to cwv ... and bindings to vx ..., and afterwards
  80. ; wrapping the skeletons around the payload (let (vx ...) . body).
  81. ; no more bs to process -> wrap call-with-values skeletons
  82. ((i:let "bs" rec (cwv ...) vxs body ())
  83. (i:let "wrap" rec vxs body cwv ...))
  84. ; recognize form1 without variable -> dummy binding for side-effects
  85. ((i:let "bs" rec cwvs (vx ...) body (((values) x) bs ...))
  86. (i:let "bs" rec cwvs (vx ... (dummy (begin x #f))) body (bs ...)))
  87. ; recognize form1 with single variable -> just extend vx ...
  88. ((i:let "bs" rec cwvs (vx ...) body (((values v) x) bs ...))
  89. (i:let "bs" rec cwvs (vx ... (v x)) body (bs ...)))
  90. ; recognize form1 without rest arg -> generate cwv
  91. ((i:let "bs" rec cwvs vxs body (((values v ...) x) bs ...))
  92. (i:let "form1" rec cwvs vxs body (bs ...) (x ()) (values v ...)))
  93. ; recognize form1 with rest arg -> generate cwv
  94. ((i:let "bs" rec cwvs vxs body (((values . vs) x) bs ...))
  95. (i:let "form1+" rec cwvs vxs body (bs ...) (x ()) (values . vs)))
  96. ; recognize form2 with single variable -> just extend vx ...
  97. ((i:let "bs" rec cwvs (vx ...) body ((v x) bs ...))
  98. (i:let "bs" rec cwvs (vx ... (v x)) body (bs ...)))
  99. ; recognize form2 with >=2 variables -> transform to form1
  100. ((i:let "bs" rec cwvs vxs body ((b1 b2 b3 b ...) bs ...))
  101. (i:let "form2" rec cwvs vxs body (bs ...) (b1 b2) (b3 b ...)))
  102. ; (i:let "form1" rec cwvs vxs body bss (x (t ...)) (values v1 v2 v ...))
  103. ; processes the variables in v1 v2 v ... adding them to (t ...)
  104. ; and producing a cwv when finished. There is not rest argument.
  105. ((i:let "form1" rec (cwv ...) vxs body bss (x ts) (values))
  106. (i:let "bs" rec (cwv ... (x ts)) vxs body bss))
  107. ((i:let "form1" rec cwvs (vx ...) body bss (x (t ...)) (values v1 v ...))
  108. (i:let "form1" rec cwvs (vx ... (v1 t1)) body bss (x (t ... t1)) (values v ...)))
  109. ; (i:let "form1+" rec cwvs vxs body bss (x (t ...)) (values v ... . vr))
  110. ; processes the variables in v ... . vr adding them to (t ...)
  111. ; and producing a cwv when finished. The rest arg is vr.
  112. ((i:let "form1+" rec cwvs (vx ...) body bss (x (t ...)) (values v1 v2 . vs))
  113. (i:let "form1+" rec cwvs (vx ... (v1 t1)) body bss (x (t ... t1)) (values v2 . vs)))
  114. ((i:let "form1+" rec (cwv ...) (vx ...) body bss (x (t ...)) (values v1 . vr))
  115. (i:let "bs" rec (cwv ... (x (t ... t1 . tr))) (vx ... (v1 t1) (vr tr)) body bss))
  116. ((i:let "form1+" rec (cwv ...) (vx ...) body bss (x ()) (values . vr))
  117. (i:let "bs" rec (cwv ... (x tr)) (vx ... (vr tr)) body bss))
  118. ; (i:let "form2" rec cwvs vxs body bss (v ...) (b ... x))
  119. ; processes the binding items (b ... x) from form2 as in
  120. ; (v ... b ... x) into ((values v ... b ...) x), i.e. form1.
  121. ; Then call "bs" recursively.
  122. ((i:let "form2" rec cwvs vxs body (bs ...) (v ...) (x))
  123. (i:let "bs" rec cwvs vxs body (((values v ...) x) bs ...)))
  124. ((i:let "form2" rec cwvs vxs body bss (v ...) (b1 b2 b ...))
  125. (i:let "form2" rec cwvs vxs body bss (v ... b1) (b2 b ...)))
  126. ; (i:let "wrap" rec ((v x) ...) (body ...) cwv ...)
  127. ; wraps cwv ... around the payload generating the actual code.
  128. ; For letrec this is of course different than for let.
  129. ((i:let "wrap" #f vxs body)
  130. (r5rs-let vxs . body))
  131. ((i:let "wrap" #f vxs body (x formals) cwv ...)
  132. (call-with-values
  133. (lambda () x)
  134. (lambda formals (i:let "wrap" #f vxs body cwv ...))))
  135. ((i:let "wrap" #t vxs body)
  136. (r5rs-letrec vxs . body))
  137. ((i:let "wrap" #t ((v t) ...) body cwv ...)
  138. (r5rs-let ((v i:undefined) ...) ; (*)
  139. (i:let "wraprec" ((v t) ...) body cwv ...)))
  140. ; (i:let "wraprec" ((v t) ...) body cwv ...)
  141. ; generate the inner code for a letrec. The variables v ...
  142. ; are the user-visible variables (bound outside), and t ...
  143. ; are the temporary variables bound by the cwv consumers.
  144. ((i:let "wraprec" ((v t) ...) (body ...))
  145. (begin (set! v t) ... (r5rs-let () body ...)))
  146. ((i:let "wraprec" vxs body (x formals) cwv ...)
  147. (call-with-values
  148. (lambda () x)
  149. (lambda formals (i:let "wraprec" vxs body cwv ...))))
  150. ))
  151. (define-syntax i:named-let
  152. (syntax-rules (values)
  153. ; (i:named-let tag (vx ...) body (bs ...))
  154. ; processes the binding specs bs ... by extracting the variable
  155. ; and expression, adding them to vx and turning the result into
  156. ; an ordinary named let.
  157. ((i:named-let tag vxs body ())
  158. (r5rs-let tag vxs . body))
  159. ((i:named-let tag (vx ...) body (((values v) x) bs ...))
  160. (i:named-let tag (vx ... (v x)) body (bs ...)))
  161. ((i:named-let tag (vx ...) body ((v x) bs ...))
  162. (i:named-let tag (vx ... (v x)) body (bs ...)))))
  163. ; --- standard procedures ---
  164. (define (uncons pair)
  165. (values (car pair) (cdr pair)))
  166. (define (uncons-2 list)
  167. (values (car list) (cadr list) (cddr list)))
  168. (define (uncons-3 list)
  169. (values (car list) (cadr list) (caddr list) (cdddr list)))
  170. (define (uncons-4 list)
  171. (values (car list) (cadr list) (caddr list) (cadddr list) (cddddr list)))
  172. (define (uncons-cons alist)
  173. (values (caar alist) (cdar alist) (cdr alist)))
  174. (define (unlist list)
  175. (apply values list))
  176. (define (unvector vector)
  177. (apply values (vector->list vector)))
  178. ; --- standard macros ---
  179. (define-syntax values->list
  180. (syntax-rules ()
  181. ((values->list x)
  182. (call-with-values (lambda () x) list))))
  183. (define-syntax values->vector
  184. (syntax-rules ()
  185. ((values->vector x)
  186. (call-with-values (lambda () x) vector))))