optargs.scm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. ;;;; optargs.scm -- support for optional arguments
  2. ;;;;
  3. ;;;; Copyright (C) 1997, 1998, 1999, 2001, 2002, 2004, 2006, 2009, 2010, 2011 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 3 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. ;;;;
  19. ;;;; Contributed by Maciej Stachowiak <mstachow@alum.mit.edu>
  20. ;;; Commentary:
  21. ;;; {Optional Arguments}
  22. ;;;
  23. ;;; The C interface for creating Guile procedures has a very handy
  24. ;;; "optional argument" feature. This module attempts to provide
  25. ;;; similar functionality for procedures defined in Scheme with
  26. ;;; a convenient and attractive syntax.
  27. ;;;
  28. ;;; exported macros are:
  29. ;;; let-optional
  30. ;;; let-optional*
  31. ;;; let-keywords
  32. ;;; let-keywords*
  33. ;;; lambda*
  34. ;;; define*
  35. ;;; define*-public
  36. ;;; defmacro*
  37. ;;; defmacro*-public
  38. ;;;
  39. ;;;
  40. ;;; Summary of the lambda* extended parameter list syntax (brackets
  41. ;;; are used to indicate grouping only):
  42. ;;;
  43. ;;; ext-param-list ::= [identifier]* [#:optional [ext-var-decl]+]?
  44. ;;; [#:key [ext-var-decl]+ [#:allow-other-keys]?]?
  45. ;;; [[#:rest identifier]|[. identifier]]?
  46. ;;;
  47. ;;; ext-var-decl ::= identifier | ( identifier expression )
  48. ;;;
  49. ;;; The characters `*', `+' and `?' are not to be taken literally; they
  50. ;;; mean respectively, zero or more occurences, one or more occurences,
  51. ;;; and one or zero occurences.
  52. ;;;
  53. ;;; Code:
  54. (define-module (ice-9 optargs)
  55. #:use-module (system base pmatch)
  56. #:re-export (lambda* define*)
  57. #:export (let-optional
  58. let-optional*
  59. let-keywords
  60. let-keywords*
  61. define*-public
  62. defmacro*
  63. defmacro*-public))
  64. ;; let-optional rest-arg (binding ...) . body
  65. ;; let-optional* rest-arg (binding ...) . body
  66. ;; macros used to bind optional arguments
  67. ;;
  68. ;; These two macros give you an optional argument interface that is
  69. ;; very "Schemey" and introduces no fancy syntax. They are compatible
  70. ;; with the scsh macros of the same name, but are slightly
  71. ;; extended. Each of binding may be of one of the forms <var> or
  72. ;; (<var> <default-value>). rest-arg should be the rest-argument of
  73. ;; the procedures these are used from. The items in rest-arg are
  74. ;; sequentially bound to the variable namess are given. When rest-arg
  75. ;; runs out, the remaining vars are bound either to the default values
  76. ;; or to `#f' if no default value was specified. rest-arg remains
  77. ;; bound to whatever may have been left of rest-arg.
  78. ;;
  79. (define (vars&inits bindings)
  80. (let lp ((bindings bindings) (vars '()) (inits '()))
  81. (syntax-case bindings ()
  82. (()
  83. (values (reverse vars) (reverse inits)))
  84. (((v init) . rest) (identifier? #'v)
  85. (lp #'rest (cons #'v vars) (cons #'init inits)))
  86. ((v . rest) (identifier? #'v)
  87. (lp #'rest (cons #'v vars) (cons #'#f inits))))))
  88. (define-syntax let-optional
  89. (lambda (x)
  90. (syntax-case x ()
  91. ((_ rest-arg (binding ...) b0 b1 ...) (identifier? #'rest-arg)
  92. (call-with-values (lambda () (vars&inits #'(binding ...)))
  93. (lambda (vars inits)
  94. (with-syntax ((n (length vars))
  95. (n+1 (1+ (length vars)))
  96. (vars (append vars (list #'rest-arg)))
  97. ((t ...) (generate-temporaries vars))
  98. ((i ...) inits))
  99. #'(let ((t (lambda vars i))
  100. ...)
  101. (apply (lambda vars b0 b1 ...)
  102. (or (parse-lambda-case '(0 n n n+1 #f '())
  103. (list t ...)
  104. rest-arg)
  105. (error "sth" rest-arg)))))))))))
  106. (define-syntax let-optional*
  107. (lambda (x)
  108. (syntax-case x ()
  109. ((_ rest-arg (binding ...) b0 b1 ...) (identifier? #'rest-arg)
  110. (call-with-values (lambda () (vars&inits #'(binding ...)))
  111. (lambda (vars inits)
  112. (with-syntax ((n (length vars))
  113. (n+1 (1+ (length vars)))
  114. (vars (append vars (list #'rest-arg)))
  115. ((i ...) inits))
  116. #'(apply (lambda vars b0 b1 ...)
  117. (or (parse-lambda-case '(0 n n n+1 #f '())
  118. (list (lambda vars i) ...)
  119. rest-arg)
  120. (error "sth" rest-arg))))))))))
  121. ;; let-keywords rest-arg allow-other-keys? (binding ...) . body
  122. ;; let-keywords* rest-arg allow-other-keys? (binding ...) . body
  123. ;; macros used to bind keyword arguments
  124. ;;
  125. ;; These macros pick out keyword arguments from rest-arg, but do not
  126. ;; modify it. This is consistent at least with Common Lisp, which
  127. ;; duplicates keyword args in the rest arg. More explanation of what
  128. ;; keyword arguments in a lambda list look like can be found below in
  129. ;; the documentation for lambda*. Bindings can have the same form as
  130. ;; for let-optional. If allow-other-keys? is false, an error will be
  131. ;; thrown if anything that looks like a keyword argument but does not
  132. ;; match a known keyword parameter will result in an error.
  133. ;;
  134. (define-syntax let-keywords
  135. (lambda (x)
  136. (syntax-case x ()
  137. ((_ rest-arg aok (binding ...) b0 b1 ...) (identifier? #'rest-arg)
  138. (call-with-values (lambda () (vars&inits #'(binding ...)))
  139. (lambda (vars inits)
  140. (with-syntax ((n (length vars))
  141. (vars vars)
  142. (ivars (generate-temporaries vars))
  143. ((kw ...) (map symbol->keyword
  144. (map syntax->datum vars)))
  145. ((idx ...) (iota (length vars)))
  146. ((t ...) (generate-temporaries vars))
  147. ((i ...) inits))
  148. #'(let ((t (lambda ivars i))
  149. ...)
  150. (apply (lambda vars b0 b1 ...)
  151. (or (parse-lambda-case '(0 0 #f n aok ((kw . idx) ...))
  152. (list t ...)
  153. rest-arg)
  154. (error "sth" rest-arg))))))))
  155. ((_ rest-arg aok (binding ...) b0 b1 ...)
  156. #'(let ((r rest-arg))
  157. (let-keywords r aok (binding ...) b0 b1 ...))))))
  158. (define-syntax let-keywords*
  159. (lambda (x)
  160. (syntax-case x ()
  161. ((_ rest-arg aok (binding ...) b0 b1 ...) (identifier? #'rest-arg)
  162. (call-with-values (lambda () (vars&inits #'(binding ...)))
  163. (lambda (vars inits)
  164. (with-syntax ((n (length vars))
  165. (vars vars)
  166. ((kw ...) (map symbol->keyword
  167. (map syntax->datum vars)))
  168. ((idx ...) (iota (length vars)))
  169. ((i ...) inits))
  170. #'(apply (lambda vars b0 b1 ...)
  171. (or (parse-lambda-case '(0 0 #f n aok ((kw . idx) ...))
  172. (list (lambda vars i) ...)
  173. rest-arg)
  174. (error "sth" rest-arg)))))))
  175. ((_ rest-arg aok (binding ...) b0 b1 ...)
  176. #'(let ((r rest-arg))
  177. (let-keywords* r aok (binding ...) b0 b1 ...))))))
  178. ;; lambda* args . body
  179. ;; lambda extended for optional and keyword arguments
  180. ;;
  181. ;; lambda* creates a procedure that takes optional arguments. These
  182. ;; are specified by putting them inside brackets at the end of the
  183. ;; paramater list, but before any dotted rest argument. For example,
  184. ;; (lambda* (a b #:optional c d . e) '())
  185. ;; creates a procedure with fixed arguments a and b, optional arguments c
  186. ;; and d, and rest argument e. If the optional arguments are omitted
  187. ;; in a call, the variables for them are bound to `#f'.
  188. ;;
  189. ;; lambda* can also take keyword arguments. For example, a procedure
  190. ;; defined like this:
  191. ;; (lambda* (#:key xyzzy larch) '())
  192. ;; can be called with any of the argument lists (#:xyzzy 11)
  193. ;; (#:larch 13) (#:larch 42 #:xyzzy 19) (). Whichever arguments
  194. ;; are given as keywords are bound to values.
  195. ;;
  196. ;; Optional and keyword arguments can also be given default values
  197. ;; which they take on when they are not present in a call, by giving a
  198. ;; two-item list in place of an optional argument, for example in:
  199. ;; (lambda* (foo #:optional (bar 42) #:key (baz 73)) (list foo bar baz))
  200. ;; foo is a fixed argument, bar is an optional argument with default
  201. ;; value 42, and baz is a keyword argument with default value 73.
  202. ;; Default value expressions are not evaluated unless they are needed
  203. ;; and until the procedure is called.
  204. ;;
  205. ;; lambda* now supports two more special parameter list keywords.
  206. ;;
  207. ;; lambda*-defined procedures now throw an error by default if a
  208. ;; keyword other than one of those specified is found in the actual
  209. ;; passed arguments. However, specifying #:allow-other-keys
  210. ;; immediately after the keyword argument declarations restores the
  211. ;; previous behavior of ignoring unknown keywords. lambda* also now
  212. ;; guarantees that if the same keyword is passed more than once, the
  213. ;; last one passed is the one that takes effect. For example,
  214. ;; ((lambda* (#:key (heads 0) (tails 0)) (display (list heads tails)))
  215. ;; #:heads 37 #:tails 42 #:heads 99)
  216. ;; would result in (99 47) being displayed.
  217. ;;
  218. ;; #:rest is also now provided as a synonym for the dotted syntax rest
  219. ;; argument. The argument lists (a . b) and (a #:rest b) are equivalent in
  220. ;; all respects to lambda*. This is provided for more similarity to DSSSL,
  221. ;; MIT-Scheme and Kawa among others, as well as for refugees from other
  222. ;; Lisp dialects.
  223. ;; define* args . body
  224. ;; define*-public args . body
  225. ;; define and define-public extended for optional and keyword arguments
  226. ;;
  227. ;; define* and define*-public support optional arguments with
  228. ;; a similar syntax to lambda*. Some examples:
  229. ;; (define* (x y #:optional a (z 3) #:key w . u) (display (list y z u)))
  230. ;; defines a procedure x with a fixed argument y, an optional agument
  231. ;; a, another optional argument z with default value 3, a keyword argument w,
  232. ;; and a rest argument u.
  233. ;;
  234. ;; Of course, define*[-public] also supports #:rest and #:allow-other-keys
  235. ;; in the same way as lambda*.
  236. (define-syntax define*-public
  237. (lambda (x)
  238. (syntax-case x ()
  239. ((_ (id . args) b0 b1 ...)
  240. #'(define-public id (lambda* args b0 b1 ...)))
  241. ((_ id val) (identifier? #'id)
  242. #'(define-public id val)))))
  243. ;; defmacro* name args . body
  244. ;; defmacro*-public args . body
  245. ;; defmacro and defmacro-public extended for optional and keyword arguments
  246. ;;
  247. ;; These are just like defmacro and defmacro-public except that they
  248. ;; take lambda*-style extended paramter lists, where #:optional,
  249. ;; #:key, #:allow-other-keys and #:rest are allowed with the usual
  250. ;; semantics. Here is an example of a macro with an optional argument:
  251. ;; (defmacro* transmogrify (a #:optional b)
  252. (define-syntax defmacro*
  253. (lambda (x)
  254. (syntax-case x ()
  255. ((_ id args doc b0 b1 ...) (string? (syntax->datum #'doc))
  256. #'(define-macro id doc (lambda* args b0 b1 ...)))
  257. ((_ id args b0 b1 ...)
  258. #'(define-macro id #f (lambda* args b0 b1 ...))))))
  259. (define-syntax-rule (defmacro*-public id args b0 b1 ...)
  260. (begin
  261. (defmacro* id args b0 b1 ...)
  262. (export-syntax id)))
  263. ;;; Support for optional & keyword args with the interpreter.
  264. (define *uninitialized* (list 'uninitialized))
  265. (define (parse-lambda-case spec inits args)
  266. (pmatch spec
  267. ((,nreq ,nopt ,rest-idx ,nargs ,allow-other-keys? ,kw-indices)
  268. (define (req args prev tail n)
  269. (cond
  270. ((zero? n)
  271. (if prev (set-cdr! prev '()))
  272. (let ((slots-tail (make-list (- nargs nreq) *uninitialized*)))
  273. (opt (if prev (append! args slots-tail) slots-tail)
  274. slots-tail tail nopt inits)))
  275. ((null? tail)
  276. #f) ;; fail
  277. (else
  278. (req args tail (cdr tail) (1- n)))))
  279. (define (opt slots slots-tail args-tail n inits)
  280. (cond
  281. ((zero? n)
  282. (rest-or-key slots slots-tail args-tail inits rest-idx))
  283. ((null? args-tail)
  284. (set-car! slots-tail (apply (car inits) slots))
  285. (opt slots (cdr slots-tail) '() (1- n) (cdr inits)))
  286. (else
  287. (set-car! slots-tail (car args-tail))
  288. (opt slots (cdr slots-tail) (cdr args-tail) (1- n) (cdr inits)))))
  289. (define (rest-or-key slots slots-tail args-tail inits rest-idx)
  290. (cond
  291. (rest-idx
  292. ;; it has to be this way, vars are allocated in this order
  293. (set-car! slots-tail args-tail)
  294. (if (pair? kw-indices)
  295. (permissive-keys slots (cdr slots-tail) args-tail inits)
  296. (rest-or-key slots (cdr slots-tail) '() inits #f)))
  297. ((pair? kw-indices)
  298. ;; fail early here, because once we're in keyword land we throw
  299. ;; errors instead of failing
  300. (and (or (null? args-tail) rest-idx (keyword? (car args-tail)))
  301. (key slots slots-tail args-tail inits)))
  302. ((pair? args-tail)
  303. #f) ;; fail
  304. (else
  305. slots)))
  306. (define (permissive-keys slots slots-tail args-tail inits)
  307. (cond
  308. ((null? args-tail)
  309. (if (null? inits)
  310. slots
  311. (begin
  312. (if (eq? (car slots-tail) *uninitialized*)
  313. (set-car! slots-tail (apply (car inits) slots)))
  314. (permissive-keys slots (cdr slots-tail) '() (cdr inits)))))
  315. ((not (keyword? (car args-tail)))
  316. (permissive-keys slots slots-tail (cdr args-tail) inits))
  317. ((and (keyword? (car args-tail))
  318. (pair? (cdr args-tail))
  319. (assq-ref kw-indices (car args-tail)))
  320. => (lambda (i)
  321. (list-set! slots i (cadr args-tail))
  322. (permissive-keys slots slots-tail (cddr args-tail) inits)))
  323. ((and (keyword? (car args-tail))
  324. (pair? (cdr args-tail))
  325. allow-other-keys?)
  326. (permissive-keys slots slots-tail (cddr args-tail) inits))
  327. (else (scm-error 'keyword-argument-error #f "Unrecognized keyword"
  328. '() args-tail))))
  329. (define (key slots slots-tail args-tail inits)
  330. (cond
  331. ((null? args-tail)
  332. (if (null? inits)
  333. slots
  334. (begin
  335. (if (eq? (car slots-tail) *uninitialized*)
  336. (set-car! slots-tail (apply (car inits) slots)))
  337. (key slots (cdr slots-tail) '() (cdr inits)))))
  338. ((not (keyword? (car args-tail)))
  339. (if rest-idx
  340. ;; no error checking, everything goes to the rest..
  341. (key slots slots-tail '() inits)
  342. (scm-error 'keyword-argument-error #f "Invalid keyword"
  343. '() args-tail)))
  344. ((and (keyword? (car args-tail))
  345. (pair? (cdr args-tail))
  346. (assq-ref kw-indices (car args-tail)))
  347. => (lambda (i)
  348. (list-set! slots i (cadr args-tail))
  349. (key slots slots-tail (cddr args-tail) inits)))
  350. ((and (keyword? (car args-tail))
  351. (pair? (cdr args-tail))
  352. allow-other-keys?)
  353. (key slots slots-tail (cddr args-tail) inits))
  354. (else (scm-error 'keyword-argument-error #f "Unrecognized keyword"
  355. '() args-tail))))
  356. (let ((args (list-copy args)))
  357. (req args #f args nreq)))
  358. (else (error "unexpected spec" spec))))