26.upstream.scm 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. ; REFERENCE IMPLEMENTATION FOR SRFI-26 "CUT"
  2. ; ==========================================
  3. ;
  4. ; Sebastian.Egner@philips.com, 5-Jun-2002.
  5. ; adapted from the posting by Al Petrofsky <al@petrofsky.org>
  6. ; placed in the public domain
  7. ;
  8. ; The code to handle the variable argument case was originally
  9. ; proposed by Michael Sperber and has been adapted to the new
  10. ; syntax of the macro using an explicit rest-slot symbol. The
  11. ; code to evaluate the non-slots for cute has been proposed by
  12. ; Dale Jordan. The code to allow a slot for the procedure position
  13. ; and to process the macro using an internal macro is based on
  14. ; a suggestion by Al Petrofsky. The code found below is, with
  15. ; exception of this header and some changes in variable names,
  16. ; entirely written by Al Petrofsky.
  17. ;
  18. ; compliance:
  19. ; Scheme R5RS (including macros).
  20. ;
  21. ; loading this file into Scheme 48 0.57:
  22. ; ,load cut.scm
  23. ;
  24. ; history of this file:
  25. ; SE, 6-Feb-2002: initial version as 'curry' with ". <>" notation
  26. ; SE, 14-Feb-2002: revised for <...>
  27. ; SE, 27-Feb-2002: revised for 'cut'
  28. ; SE, 03-Jun-2002: revised for proc-slot, cute
  29. ; SE, 04-Jun-2002: rewritten with internal transformer (no "loop" pattern)
  30. ; SE, 05-Jun-2002: replace my code by Al's; substituted "constant" etc.
  31. ; to match the convention in the SRFI-document
  32. ; (srfi-26-internal-cut slot-names combination . se)
  33. ; transformer used internally
  34. ; slot-names : the internal names of the slots
  35. ; combination : procedure being specialized, followed by its arguments
  36. ; se : slots-or-exprs, the qualifiers of the macro
  37. (define-syntax srfi-26-internal-cut
  38. (syntax-rules (<> <...>)
  39. ;; construct fixed- or variable-arity procedure:
  40. ;; (begin proc) throws an error if proc is not an <expression>
  41. ((srfi-26-internal-cut (slot-name ...) (proc arg ...))
  42. (lambda (slot-name ...) ((begin proc) arg ...)))
  43. ((srfi-26-internal-cut (slot-name ...) (proc arg ...) <...>)
  44. (lambda (slot-name ... . rest-slot) (apply proc arg ... rest-slot)))
  45. ;; process one slot-or-expr
  46. ((srfi-26-internal-cut (slot-name ...) (position ...) <> . se)
  47. (srfi-26-internal-cut (slot-name ... x) (position ... x) . se))
  48. ((srfi-26-internal-cut (slot-name ...) (position ...) nse . se)
  49. (srfi-26-internal-cut (slot-name ...) (position ... nse) . se))))
  50. ; (srfi-26-internal-cute slot-names nse-bindings combination . se)
  51. ; transformer used internally
  52. ; slot-names : the internal names of the slots
  53. ; nse-bindings : let-style bindings for the non-slot expressions.
  54. ; combination : procedure being specialized, followed by its arguments
  55. ; se : slots-or-exprs, the qualifiers of the macro
  56. (define-syntax srfi-26-internal-cute
  57. (syntax-rules (<> <...>)
  58. ;; If there are no slot-or-exprs to process, then:
  59. ;; construct a fixed-arity procedure,
  60. ((srfi-26-internal-cute
  61. (slot-name ...) nse-bindings (proc arg ...))
  62. (let nse-bindings (lambda (slot-name ...) (proc arg ...))))
  63. ;; or a variable-arity procedure
  64. ((srfi-26-internal-cute
  65. (slot-name ...) nse-bindings (proc arg ...) <...>)
  66. (let nse-bindings (lambda (slot-name ... . x) (apply proc arg ... x))))
  67. ;; otherwise, process one slot:
  68. ((srfi-26-internal-cute
  69. (slot-name ...) nse-bindings (position ...) <> . se)
  70. (srfi-26-internal-cute
  71. (slot-name ... x) nse-bindings (position ... x) . se))
  72. ;; or one non-slot expression
  73. ((srfi-26-internal-cute
  74. slot-names nse-bindings (position ...) nse . se)
  75. (srfi-26-internal-cute
  76. slot-names ((x nse) . nse-bindings) (position ... x) . se))))
  77. ; exported syntax
  78. (define-syntax cut
  79. (syntax-rules ()
  80. ((cut . slots-or-exprs)
  81. (srfi-26-internal-cut () () . slots-or-exprs))))
  82. (define-syntax cute
  83. (syntax-rules ()
  84. ((cute . slots-or-exprs)
  85. (srfi-26-internal-cute () () () . slots-or-exprs))))