51.upstream.scm 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. ;;; Copyright (C) Joo ChurlSoo (2004). All Rights Reserved.
  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. (define (rest-values rest . default)
  18. (let* ((caller (if (or (null? default)
  19. (boolean? (car default))
  20. (integer? (car default))
  21. (memq (car default) (list + -)))
  22. '()
  23. (if (string? rest) rest (list rest))))
  24. (rest-list (if (null? caller) rest (car default)))
  25. (rest-length (if (list? rest-list)
  26. (length rest-list)
  27. (if (string? caller)
  28. (error caller rest-list 'rest-list
  29. '(list? rest-list))
  30. (apply error "bad rest list" rest-list 'rest-list
  31. '(list? rest-list) caller))))
  32. (default (if (null? caller) default (cdr default)))
  33. (default-list (if (null? default) default (cdr default)))
  34. (default-length (length default-list))
  35. (number
  36. (and (not (null? default))
  37. (let ((option (car default)))
  38. (or (and (integer? option)
  39. (or (and (> rest-length (abs option))
  40. (if (string? caller)
  41. (error caller rest-list 'rest-list
  42. `(<= (length rest-list)
  43. ,(abs option)))
  44. (apply error "too many arguments"
  45. rest-list 'rest-list
  46. `(<= (length rest-list)
  47. ,(abs option))
  48. caller)))
  49. (and (> default-length (abs option))
  50. (if (string? caller)
  51. (error caller default-list
  52. 'default-list
  53. `(<= (length default-list)
  54. ,(abs option)))
  55. (apply error "too many defaults"
  56. default-list 'default-list
  57. `(<= (length default-list)
  58. ,(abs option))
  59. caller)))
  60. option))
  61. (eq? option #t)
  62. (and (not option) 'false)
  63. (and (eq? option +) +)
  64. (and (eq? option -) -)
  65. (if (string? caller)
  66. (error caller option 'option
  67. '(or (boolean? option)
  68. (integer? option)
  69. (memq option (list + -))))
  70. (apply error "bad optional argument" option 'option
  71. '(or (boolean? option)
  72. (integer? option)
  73. (memq option (list + -)))
  74. caller)))))))
  75. (cond
  76. ((or (eq? #t number) (eq? 'false number))
  77. (and (not (every pair? default-list))
  78. (if (string? caller)
  79. (error caller default-list 'default-list
  80. '(every pair? default-list))
  81. (apply error "bad default list" default-list 'default-list
  82. '(every pair? default-list) caller)))
  83. (let loop ((rest-list rest-list)
  84. (default-list default-list)
  85. (result '()))
  86. (if (null? default-list)
  87. (if (null? rest-list)
  88. (apply values (reverse result))
  89. (if (eq? #t number)
  90. (if (string? caller)
  91. (error caller rest-list 'rest-list '(null? rest-list))
  92. (apply error "bad argument" rest-list 'rest-list
  93. '(null? rest-list) caller))
  94. (apply values (append-reverse result rest-list))))
  95. (if (null? rest-list)
  96. (apply values (append-reverse result (map car default-list)))
  97. (let ((default (car default-list)))
  98. (let lp ((rest rest-list)
  99. (head '()))
  100. (if (null? rest)
  101. (loop (reverse head)
  102. (cdr default-list)
  103. (cons (car default) result))
  104. (if (list? default)
  105. (if (member (car rest) default)
  106. (loop (append-reverse head (cdr rest))
  107. (cdr default-list)
  108. (cons (car rest) result))
  109. (lp (cdr rest) (cons (car rest) head)))
  110. (if ((cdr default) (car rest))
  111. (loop (append-reverse head (cdr rest))
  112. (cdr default-list)
  113. (cons (car rest) result))
  114. (lp (cdr rest) (cons (car rest) head)))))))))))
  115. ((or (and (integer? number) (> number 0))
  116. (eq? number +))
  117. (and (not (every pair? default-list))
  118. (if (string? caller)
  119. (error caller default-list 'default-list
  120. '(every pair? default-list))
  121. (apply error "bad default list" default-list 'default-list
  122. '(every pair? default-list) caller)))
  123. (let loop ((rest rest-list)
  124. (default default-list))
  125. (if (or (null? rest) (null? default))
  126. (apply values
  127. (if (> default-length rest-length)
  128. (append rest-list
  129. (map car (list-tail default-list rest-length)))
  130. rest-list))
  131. (let ((arg (car rest))
  132. (par (car default)))
  133. (if (list? par)
  134. (if (member arg par)
  135. (loop (cdr rest) (cdr default))
  136. (if (string? caller)
  137. (error caller arg 'arg `(member arg ,par))
  138. (apply error "unmatched argument"
  139. arg 'arg `(member arg ,par) caller)))
  140. (if ((cdr par) arg)
  141. (loop (cdr rest) (cdr default))
  142. (if (string? caller)
  143. (error caller arg 'arg `(,(cdr par) arg))
  144. (apply error "incorrect argument"
  145. arg 'arg `(,(cdr par) arg) caller))))))))
  146. (else
  147. (apply values (if (> default-length rest-length)
  148. (append rest-list (list-tail default-list rest-length))
  149. rest-list))))))
  150. (define-syntax arg-and
  151. (syntax-rules()
  152. ((arg-and arg (a1 a2 ...) ...)
  153. (and (or (symbol? 'arg)
  154. (error "bad syntax" 'arg '(symbol? 'arg)
  155. '(arg-and arg (a1 a2 ...) ...)))
  156. (or (a1 a2 ...)
  157. (error "incorrect argument" arg 'arg '(a1 a2 ...)))
  158. ...))
  159. ((arg-and caller arg (a1 a2 ...) ...)
  160. (and (or (symbol? 'arg)
  161. (error "bad syntax" 'arg '(symbol? 'arg)
  162. '(arg-and caller arg (a1 a2 ...) ...)))
  163. (or (a1 a2 ...)
  164. (if (string? caller)
  165. (error caller arg 'arg '(a1 a2 ...))
  166. (error "incorrect argument" arg 'arg '(a1 a2 ...) caller)))
  167. ...))))
  168. ;; accessory macro for arg-ands
  169. (define-syntax caller-arg-and
  170. (syntax-rules()
  171. ((caller-arg-and caller arg (a1 a2 ...) ...)
  172. (and (or (symbol? 'arg)
  173. (error "bad syntax" 'arg '(symbol? 'arg)
  174. '(caller-arg-and caller arg (a1 a2 ...) ...)))
  175. (or (a1 a2 ...)
  176. (if (string? caller)
  177. (error caller arg 'arg '(a1 a2 ...))
  178. (error "incorrect argument" arg 'arg '(a1 a2 ...) caller)))
  179. ...))
  180. ((caller-arg-and null caller arg (a1 a2 ...) ...)
  181. (and (or (symbol? 'arg)
  182. (error "bad syntax" 'arg '(symbol? 'arg)
  183. '(caller-arg-and caller arg (a1 a2 ...) ...)))
  184. (or (a1 a2 ...)
  185. (if (string? caller)
  186. (error caller arg 'arg '(a1 a2 ...))
  187. (error "incorrect argument" arg 'arg '(a1 a2 ...) caller)))
  188. ...))))
  189. (define-syntax arg-ands
  190. (syntax-rules (common)
  191. ((arg-ands (a1 a2 ...) ...)
  192. (and (arg-and a1 a2 ...) ...))
  193. ((arg-ands common caller (a1 a2 ...) ...)
  194. (and (caller-arg-and caller a1 a2 ...) ...))))
  195. (define-syntax arg-or
  196. (syntax-rules()
  197. ((arg-or arg (a1 a2 ...) ...)
  198. (or (and (not (symbol? 'arg))
  199. (error "bad syntax" 'arg '(symbol? 'arg)
  200. '(arg-or arg (a1 a2 ...) ...)))
  201. (and (a1 a2 ...)
  202. (error "incorrect argument" arg 'arg '(a1 a2 ...)))
  203. ...))
  204. ((arg-or caller arg (a1 a2 ...) ...)
  205. (or (and (not (symbol? 'arg))
  206. (error "bad syntax" 'arg '(symbol? 'arg)
  207. '(arg-or caller arg (a1 a2 ...) ...)))
  208. (and (a1 a2 ...)
  209. (if (string? caller)
  210. (error caller arg 'arg '(a1 a2 ...))
  211. (error "incorrect argument" arg 'arg '(a1 a2 ...) caller)))
  212. ...))))
  213. ;; accessory macro for arg-ors
  214. (define-syntax caller-arg-or
  215. (syntax-rules()
  216. ((caller-arg-or caller arg (a1 a2 ...) ...)
  217. (or (and (not (symbol? 'arg))
  218. (error "bad syntax" 'arg '(symbol? 'arg)
  219. '(caller-arg-or caller arg (a1 a2 ...) ...)))
  220. (and (a1 a2 ...)
  221. (if (string? caller)
  222. (error caller arg 'arg '(a1 a2 ...))
  223. (error "incorrect argument" arg 'arg '(a1 a2 ...) caller)))
  224. ...))
  225. ((caller-arg-or null caller arg (a1 a2 ...) ...)
  226. (or (and (not (symbol? 'arg))
  227. (error "bad syntax" 'arg '(symbol? 'arg)
  228. '(caller-arg-or caller arg (a1 a2 ...) ...)))
  229. (and (a1 a2 ...)
  230. (if (string? caller)
  231. (error caller arg 'arg '(a1 a2 ...))
  232. (error "incorrect argument" arg 'arg '(a1 a2 ...) caller)))
  233. ...))))
  234. (define-syntax arg-ors
  235. (syntax-rules (common)
  236. ((arg-ors (a1 a2 ...) ...)
  237. (or (arg-or a1 a2 ...) ...))
  238. ((arg-ors common caller (a1 a2 ...) ...)
  239. (or (caller-arg-or caller a1 a2 ...) ...))))
  240. (define-syntax err-and
  241. (syntax-rules ()
  242. ((err-and err expression ...)
  243. (and (or expression
  244. (if (string? err)
  245. (error err 'expression)
  246. (error "false expression" 'expression err)))
  247. ...))))
  248. (define-syntax err-ands
  249. (syntax-rules ()
  250. ((err-ands (err expression ...) ...)
  251. (and (err-and err expression ...)
  252. ...))))
  253. (define-syntax err-or
  254. (syntax-rules ()
  255. ((err-or err expression ...)
  256. (or (and expression
  257. (if (string? err)
  258. (error err 'expression)
  259. (error "true expression" 'expression err)))
  260. ...))))
  261. (define-syntax err-ors
  262. (syntax-rules ()
  263. ((err-ors (err expression ...) ...)
  264. (or (err-or err expression ...)
  265. ...))))