| 1 | ; REFERENCE IMPLEMENTATION FOR SRFI-26 "CUT" |
| 2 | ; ========================================== |
| 3 | ; |
| 4 | ; [email protected], 5-Jun-2002. |
| 5 | ; adapted from the posting by Al Petrofsky <[email protected]> |
| 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 | |
| 33 | ; (srfi-26-internal-cut slot-names combination . se) |
| 34 | ; transformer used internally |
| 35 | ; slot-names : the internal names of the slots |
| 36 | ; combination : procedure being specialized, followed by its arguments |
| 37 | ; se : slots-or-exprs, the qualifiers of the macro |
| 38 | |
| 39 | (define-syntax srfi-26-internal-cut |
| 40 | (syntax-rules (<> <...>) |
| 41 | |
| 42 | ;; construct fixed- or variable-arity procedure: |
| 43 | ;; (begin proc) throws an error if proc is not an <expression> |
| 44 | ((srfi-26-internal-cut (slot-name ...) (proc arg ...)) |
| 45 | (lambda (slot-name ...) ((begin proc) arg ...))) |
| 46 | ((srfi-26-internal-cut (slot-name ...) (proc arg ...) <...>) |
| 47 | (lambda (slot-name ... . rest-slot) (apply proc arg ... rest-slot))) |
| 48 | |
| 49 | ;; process one slot-or-expr |
| 50 | ((srfi-26-internal-cut (slot-name ...) (position ...) <> . se) |
| 51 | (srfi-26-internal-cut (slot-name ... x) (position ... x) . se)) |
| 52 | ((srfi-26-internal-cut (slot-name ...) (position ...) nse . se) |
| 53 | (srfi-26-internal-cut (slot-name ...) (position ... nse) . se)))) |
| 54 | |
| 55 | ; (srfi-26-internal-cute slot-names nse-bindings combination . se) |
| 56 | ; transformer used internally |
| 57 | ; slot-names : the internal names of the slots |
| 58 | ; nse-bindings : let-style bindings for the non-slot expressions. |
| 59 | ; combination : procedure being specialized, followed by its arguments |
| 60 | ; se : slots-or-exprs, the qualifiers of the macro |
| 61 | |
| 62 | (define-syntax srfi-26-internal-cute |
| 63 | (syntax-rules (<> <...>) |
| 64 | |
| 65 | ;; If there are no slot-or-exprs to process, then: |
| 66 | ;; construct a fixed-arity procedure, |
| 67 | ((srfi-26-internal-cute |
| 68 | (slot-name ...) nse-bindings (proc arg ...)) |
| 69 | (let nse-bindings (lambda (slot-name ...) (proc arg ...)))) |
| 70 | ;; or a variable-arity procedure |
| 71 | ((srfi-26-internal-cute |
| 72 | (slot-name ...) nse-bindings (proc arg ...) <...>) |
| 73 | (let nse-bindings (lambda (slot-name ... . x) (apply proc arg ... x)))) |
| 74 | |
| 75 | ;; otherwise, process one slot: |
| 76 | ((srfi-26-internal-cute |
| 77 | (slot-name ...) nse-bindings (position ...) <> . se) |
| 78 | (srfi-26-internal-cute |
| 79 | (slot-name ... x) nse-bindings (position ... x) . se)) |
| 80 | ;; or one non-slot expression |
| 81 | ((srfi-26-internal-cute |
| 82 | slot-names nse-bindings (position ...) nse . se) |
| 83 | (srfi-26-internal-cute |
| 84 | slot-names ((x nse) . nse-bindings) (position ... x) . se)))) |
| 85 | |
| 86 | ; exported syntax |
| 87 | |
| 88 | (define-syntax <> |
| 89 | (identifier-syntax |
| 90 | (error #f "misplaced aux keyword <>"))) |
| 91 | (define-syntax <...> |
| 92 | (identifier-syntax |
| 93 | (error #f "misplaced aux keyword <...>"))) |
| 94 | |
| 95 | (define-syntax cut |
| 96 | (syntax-rules () |
| 97 | ((cut . slots-or-exprs) |
| 98 | (srfi-26-internal-cut () () . slots-or-exprs)))) |
| 99 | |
| 100 | (define-syntax cute |
| 101 | (syntax-rules () |
| 102 | ((cute . slots-or-exprs) |
| 103 | (srfi-26-internal-cute () () () . slots-or-exprs)))) |