73fd22dc73fd9dcee6391320cc208f07015a5dd1
[bpt/guile.git] / srfi / srfi-16.scm
1 ;;;; srfi-16.scm --- `case-lambda' for Guile
2
3 ;;; Copyright (C) 2001 Free Software Foundation, Inc.
4 ;;;
5 ;;; This program is free software; you can redistribute it and/or
6 ;;; modify it under the terms of the GNU General Public License as
7 ;;; published by the Free Software Foundation; either version 2, or
8 ;;; (at your option) any later version.
9 ;;;
10 ;;; This program 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 ;;; General Public License for more details.
14 ;;;
15 ;;; You should have received a copy of the GNU General Public License
16 ;;; along with this software; see the file COPYING. If not, write to
17 ;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 ;;; Boston, MA 02111-1307 USA
19 ;;;
20 ;;; As a special exception, the Free Software Foundation gives permission
21 ;;; for additional uses of the text contained in its release of GUILE.
22 ;;;
23 ;;; The exception is that, if you link the GUILE library with other files
24 ;;; to produce an executable, this does not by itself cause the
25 ;;; resulting executable to be covered by the GNU General Public License.
26 ;;; Your use of that executable is in no way restricted on account of
27 ;;; linking the GUILE library code into it.
28 ;;;
29 ;;; This exception does not however invalidate any other reasons why
30 ;;; the executable file might be covered by the GNU General Public License.
31 ;;;
32 ;;; This exception applies only to the code released by the
33 ;;; Free Software Foundation under the name GUILE. If you copy
34 ;;; code from other Free Software Foundation releases into a copy of
35 ;;; GUILE, as the General Public License permits, the exception does
36 ;;; not apply to the code that you add in this way. To avoid misleading
37 ;;; anyone as to the status of such modified files, you must delete
38 ;;; this exception notice from them.
39 ;;;
40 ;;; If you write modifications of your own for GUILE, it is your choice
41 ;;; whether to permit this exception to apply to your modifications.
42 ;;; If you do not wish that, delete this exception notice.
43
44 ;;; Commentary:
45
46 ;;; Implementation of SRFI-16. `case-lambda' is a syntactic form
47 ;;; which permits writing functions acting different according to the
48 ;;; number of arguments passed.
49 ;;;
50 ;;; The syntax of the `case-lambda' form is defined in the following
51 ;;; EBNF grammar.
52 ;;;
53 ;;; <case-lambda>
54 ;;; --> (case-lambda <case-lambda-clause>)
55 ;;; <case-lambda-clause>
56 ;;; --> (<signature> <definition-or-command>*)
57 ;;; <signature>
58 ;;; --> (<identifier>*)
59 ;;; | (<identifier>* . <identifier>)
60 ;;; | <identifier>
61 ;;;
62 ;;; The value returned by a `case-lambda' form is a procedure which
63 ;;; matches the number of actual arguments against the signatures in
64 ;;; the various clauses, in order. The first matching clause is
65 ;;; selected, the corresponding values from the actual parameter list
66 ;;; are bound to the variable names in the clauses and the body of the
67 ;;; clause is evaluated.
68
69 ;;; Author: Martin Grabmueller
70
71 ;;; Code:
72 (define-module (srfi srfi-16))
73
74 (export-syntax case-lambda)
75
76 (cond-expand-provide (current-module) '(srfi-16))
77
78 (define-macro (case-lambda . clauses)
79
80 ;; Return the length of the list @var{l}, but allow dotted list.
81 ;;
82 (define (alength l)
83 (cond ((null? l) 0)
84 ((pair? l) (+ 1 (alength (cdr l))))
85 (else 0)))
86
87 ;; Return @code{#t} if @var{l} is a dotted list, @code{#f} if it is
88 ;; a normal list.
89 ;;
90 (define (dotted? l)
91 (cond ((null? l) #f)
92 ((pair? l) (dotted? (cdr l)))
93 (else #t)))
94
95 ;; Return the expression for accessing the @var{index}th element of
96 ;; the list called @var{args-name}. If @var{tail?} is true, code
97 ;; for accessing the list-tail is generated, otherwise for accessing
98 ;; the list element itself.
99 ;;
100 (define (accessor args-name index tail?)
101 (if tail?
102 (case index
103 ((0) `,args-name)
104 ((1) `(cdr ,args-name))
105 ((2) `(cddr ,args-name))
106 ((3) `(cdddr ,args-name))
107 ((4) `(cddddr ,args-name))
108 (else `(list-tail ,args-name ,index)))
109 (case index
110 ((0) `(car ,args-name))
111 ((1) `(cadr ,args-name))
112 ((2) `(caddr ,args-name))
113 ((3) `(cadddr ,args-name))
114 (else `(list-ref ,args-name ,index)))))
115
116 ;; Generate the binding lists of the variables of one case-lambda
117 ;; clause. @var{vars} is the (possibly dotted) list of variables
118 ;; and @var{args-name} is the generated name used for the argument
119 ;; list.
120 ;;
121 (define (gen-temps vars args-name)
122 (let lp ((v vars) (i 0))
123 (cond ((null? v) '())
124 ((pair? v)
125 (cons `(,(car v) ,(accessor args-name i #f))
126 (lp (cdr v) (+ i 1))))
127 (else `((,v ,(accessor args-name i #t)))))))
128
129 ;; Generate the cond clauses for each of the clauses of case-lambda,
130 ;; including the parameter count check, binding of the parameters
131 ;; and the code of the corresponding body.
132 ;;
133 (define (gen-clauses l length-name args-name)
134 (cond ((null? l) (list '(else (error "too few arguments"))))
135 (else
136 (cons
137 `((,(if (dotted? (caar l)) '>= '=)
138 ,length-name ,(alength (caar l)))
139 (let ,(gen-temps (caar l) args-name)
140 ,@(cdar l)))
141 (gen-clauses (cdr l) length-name args-name)))))
142
143 (let ((args-name (gensym))
144 (length-name (gensym)))
145 (let ((proc
146 `(lambda ,args-name
147 (let ((,length-name (length ,args-name)))
148 (cond ,@(gen-clauses clauses length-name args-name))))))
149 proc)))