| 1 |
#!r6rs |
| 2 |
(library (srfi :197 pipeline-operators) |
| 3 |
(export chain |
| 4 |
chain-and |
| 5 |
chain-when |
| 6 |
chain-lambda |
| 7 |
nest |
| 8 |
nest-reverse) |
| 9 |
(import (chezscheme)) |
| 10 |
|
| 11 |
(meta define (gentemp) (car (generate-temporaries '(x)))) |
| 12 |
|
| 13 |
(meta define (id=? x y) (and (identifier? x) (free-identifier=? x y))) |
| 14 |
|
| 15 |
(define-syntax chain |
| 16 |
(lambda (x) |
| 17 |
(syntax-case x () |
| 18 |
((_ initial-value (step ...) ...) |
| 19 |
#'(chain initial-value _ (... ...) (step ...) ...)) |
| 20 |
((_ initial-value placeholder (step ...) ...) |
| 21 |
#'(chain initial-value placeholder (... ...) (step ...) ...)) |
| 22 |
((_ initial-value placeholder ellipsis) (and (identifier? #'placeholder) |
| 23 |
(identifier? #'ellipsis)) |
| 24 |
#'initial-value) |
| 25 |
((_ initial-value placeholder ellipsis (step ...) rest ...) |
| 26 |
(let loop ((vars '()) (out '()) (in #'(step ...))) |
| 27 |
(syntax-case in () |
| 28 |
((u …) (and (id=? #'u #'placeholder) (id=? #'… #'ellipsis)) |
| 29 |
(let ((chain-rest-var (gentemp))) |
| 30 |
#`(chain (let-values ((#,(if (null? vars) |
| 31 |
chain-rest-var |
| 32 |
#`(#,@(reverse vars) . #,chain-rest-var)) |
| 33 |
initial-value)) |
| 34 |
(apply #,@(reverse out) #,chain-rest-var)) |
| 35 |
placeholder |
| 36 |
ellipsis |
| 37 |
rest ...))) |
| 38 |
((u … . _) (and (id=? #'u #'placeholder) (id=? #'… #'ellipsis)) |
| 39 |
(syntax-violation 'chain "_ ... only allowed at end" #'(step ...))) |
| 40 |
((u . step-rest) (id=? #'u #'placeholder) |
| 41 |
(let ((chain-var (gentemp))) |
| 42 |
(loop (cons chain-var vars) (cons chain-var out) #'step-rest))) |
| 43 |
((… . _) (id=? #'… #'ellipsis) |
| 44 |
(syntax-violation 'chain "misplaced ..." #'(step ...))) |
| 45 |
((x . step-rest) |
| 46 |
(loop vars (cons #'x out) #'step-rest)) |
| 47 |
(() |
| 48 |
(with-syntax ((result (reverse out))) |
| 49 |
#`(chain |
| 50 |
#,(cond |
| 51 |
((null? vars) |
| 52 |
#'(begin initial-value result)) |
| 53 |
((null? (cdr vars)) |
| 54 |
#`(let ((#,(car vars) initial-value)) result)) |
| 55 |
(else |
| 56 |
#`(let-values ((#,(reverse vars) initial-value)) result))) |
| 57 |
placeholder |
| 58 |
ellipsis |
| 59 |
rest ...))))))))) |
| 60 |
|
| 61 |
(define-syntax chain-and |
| 62 |
(lambda (x) |
| 63 |
(syntax-case x () |
| 64 |
((_ initial-value (step ...) ...) |
| 65 |
#'(chain-and initial-value _ (step ...) ...)) |
| 66 |
((_ initial-value placeholder) (identifier? #'placeholder) |
| 67 |
#'initial-value) |
| 68 |
((_ initial-value placeholder (step ...) rest ...) |
| 69 |
(let loop ((var #f) (out '()) (in #'(step ...))) |
| 70 |
(syntax-case in () |
| 71 |
((u . step-rest) (id=? #'u #'placeholder) |
| 72 |
(if var |
| 73 |
(syntax-violation 'chain-and "only one _ allowed per step" #'(step ...)) |
| 74 |
(let ((chain-var (gentemp))) |
| 75 |
(loop chain-var (cons chain-var out) #'step-rest)))) |
| 76 |
((x . step-rest) |
| 77 |
(loop var (cons #'x out) #'step-rest)) |
| 78 |
(() |
| 79 |
(with-syntax ((result (reverse out))) |
| 80 |
#`(chain-and |
| 81 |
#,(if var |
| 82 |
#`(let ((#,var initial-value)) |
| 83 |
(and #,var result)) |
| 84 |
#'(and initial-value result)) |
| 85 |
placeholder |
| 86 |
rest ...))))))))) |
| 87 |
|
| 88 |
(define-syntax chain-when |
| 89 |
(lambda (x) |
| 90 |
(syntax-case x () |
| 91 |
((_ initial-value (guard? (step ...)) ...) |
| 92 |
#'(chain-when initial-value _ (guard? (step ...)) ...)) |
| 93 |
((_ initial-value placeholder) (identifier? #'placeholder) |
| 94 |
#'initial-value) |
| 95 |
((_ initial-value placeholder (guard? (step ...)) rest ...) |
| 96 |
(let loop ((var #f) (out '()) (in #'(step ...))) |
| 97 |
(syntax-case in () |
| 98 |
((u . step-rest) (id=? #'u #'placeholder) |
| 99 |
(if var |
| 100 |
(syntax-violation 'chain-when "only one _ allowed per step" #'(step ...)) |
| 101 |
(let ((chain-var (gentemp))) |
| 102 |
(loop chain-var (cons chain-var out) #'step-rest)))) |
| 103 |
((x . step-rest) |
| 104 |
(loop var (cons #'x out) #'step-rest)) |
| 105 |
(() |
| 106 |
(with-syntax ((result (reverse out))) |
| 107 |
#`(chain-when |
| 108 |
#,(if var |
| 109 |
#`(let ((#,var initial-value)) |
| 110 |
(if guard? result #,var)) |
| 111 |
#'(let ((chain-var initial-value)) |
| 112 |
(if guard? result chain-var))) |
| 113 |
placeholder |
| 114 |
rest ...))))))))) |
| 115 |
|
| 116 |
(define-syntax chain-lambda |
| 117 |
(lambda (x) |
| 118 |
(syntax-case x () |
| 119 |
((_ (step ...) ...) |
| 120 |
#'(chain-lambda _ (... ...) (step ...) ...)) |
| 121 |
((_ placeholder (step ...) ...) |
| 122 |
#'(chain-lambda placeholder (... ...) (step ...) ...)) |
| 123 |
((_ placeholder ellipsis (first-step ...) rest ...) |
| 124 |
(let loop ((vars '()) (out '()) (in #'(first-step ...))) |
| 125 |
(syntax-case in () |
| 126 |
((u …) (and (id=? #'u #'placeholder) (id=? #'… #'ellipsis)) |
| 127 |
(let ((chain-rest-var (gentemp))) |
| 128 |
#`(lambda #,(if (null? vars) |
| 129 |
chain-rest-var |
| 130 |
#`(#,@vars . #,chain-rest-var)) |
| 131 |
(chain (apply #,@(reverse out) #,chain-rest-var) placeholder ellipsis rest ...)))) |
| 132 |
((u … . _) (and (id=? #'u #'placeholder) (id=? #'… #'ellipsis)) |
| 133 |
(syntax-violation 'chain-lambda "_ ... only allowed at end" #'(first-step ...))) |
| 134 |
((u . step-rest) (id=? #'u #'placeholder) |
| 135 |
(let ((chain-var (gentemp))) |
| 136 |
(loop (cons chain-var vars) (cons chain-var out) #'step-rest))) |
| 137 |
((… . _) (id=? #'… #'ellipsis) |
| 138 |
(syntax-violation 'chain-lambda "misplaced ..." #'(first-step ...))) |
| 139 |
((x . step-rest) |
| 140 |
(loop vars (cons #'x out) #'step-rest)) |
| 141 |
(() |
| 142 |
#`(lambda #,(reverse vars) (chain #,(reverse out) placeholder ellipsis rest ...))))))))) |
| 143 |
|
| 144 |
(define-syntax nest |
| 145 |
(lambda (x) |
| 146 |
(syntax-case x () |
| 147 |
((_ last) #'last) |
| 148 |
((_ (step ...) ... last) #'(nest _ (step ...) ... last)) |
| 149 |
((_ placeholder (extra-step ...) ... (step ...) last) |
| 150 |
(let loop ((arg #'last) (out '()) (in #'(step ...))) |
| 151 |
(syntax-case in () |
| 152 |
((u . step-rest) (id=? #'u #'placeholder) |
| 153 |
(if (eof-object? arg) |
| 154 |
(syntax-violation 'nest "only one _ allowed per step" #'(step ...)) |
| 155 |
(loop (eof-object) (cons arg out) #'step-rest))) |
| 156 |
((x . step-rest) |
| 157 |
(loop arg (cons #'x out) #'step-rest)) |
| 158 |
(() |
| 159 |
(if (eof-object? arg) |
| 160 |
#`(nest placeholder (extra-step ...) ... #,(reverse out)) |
| 161 |
(syntax-violation 'nest "step must contain _" #'(step ...))))))) |
| 162 |
((_ placeholder last) #'last)))) |
| 163 |
|
| 164 |
(define-syntax nest-reverse |
| 165 |
(lambda (x) |
| 166 |
(syntax-case x () |
| 167 |
((_ first) #'first) |
| 168 |
((_ first (step ...) ...) #'(nest-reverse first _ (step ...) ...)) |
| 169 |
((_ first placeholder (step ...) (extra-step ...) ...) |
| 170 |
(let loop ((arg #'first) (out '()) (in #'(step ...))) |
| 171 |
(syntax-case in () |
| 172 |
((u . step-rest) (id=? #'u #'placeholder) |
| 173 |
(if (eof-object? arg) |
| 174 |
(syntax-violation 'nest-reverse "only one _ allowed per step" #'(step ...)) |
| 175 |
(loop (eof-object) (cons arg out) #'step-rest))) |
| 176 |
((x . step-rest) |
| 177 |
(loop arg (cons #'x out) #'step-rest)) |
| 178 |
(() |
| 179 |
(if (eof-object? arg) |
| 180 |
#`(nest-reverse #,(reverse out) placeholder (extra-step ...) ...) |
| 181 |
(syntax-violation 'nest-reverse "step must contain _" #'(step ...))))))) |
| 182 |
((_ first placeholder) #'first))))) |