Merge branch 'master' of ssh://git.code.sf.net/p/maxima/code
[maxima/cygwin.git] / src / defmfun-check.lisp
bloba7e104451f86b4a48e0984335df26a521157125c
1 ;;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
3 (in-package :maxima)
5 ;; We got this code from cmucl, so we don't actually need all of this.
6 #+cmucl
7 (progn
8 (defun parse-lambda-list (list)
9 (kernel:parse-lambda-list list))
10 (defun parse-body (body environment &optional (doc-string-allowed t))
11 (system:parse-body body environment doc-string-allowed))
14 #-cmucl
15 (eval-when (compile load eval)
16 ;;;; Borrowed from cmucl src/code/extensions.lisp. Used in parsing
17 ;;;; lambda lists.
19 ;;;; The Collect macro:
21 ;;; Collect-Normal-Expander -- Internal
22 ;;;
23 ;;; This function does the real work of macroexpansion for normal collection
24 ;;; macros. N-Value is the name of the variable which holds the current
25 ;;; value. Fun is the function which does collection. Forms is the list of
26 ;;; forms whose values we are supposed to collect.
27 ;;;
28 (defun collect-normal-expander (n-value fun forms)
29 `(progn
30 ,@(mapcar #'(lambda (form) `(setq ,n-value (,fun ,form ,n-value))) forms)
31 ,n-value))
33 ;;; Collect-List-Expander -- Internal
34 ;;;
35 ;;; This function deals with the list collection case. N-Tail is the pointer
36 ;;; to the current tail of the list, which is NIL if the list is empty.
37 ;;;
38 (defun collect-list-expander (n-value n-tail forms)
39 (let ((n-res (gensym)))
40 `(progn
41 ,@(mapcar #'(lambda (form)
42 `(let ((,n-res (cons ,form nil)))
43 (cond (,n-tail
44 (setf (cdr ,n-tail) ,n-res)
45 (setq ,n-tail ,n-res))
47 (setq ,n-tail ,n-res ,n-value ,n-res)))))
48 forms)
49 ,n-value)))
52 ;;; Collect -- Public
53 ;;;
54 ;;; The ultimate collection macro...
55 ;;;
56 (defmacro collect (collections &body body)
57 "Collect ({(Name [Initial-Value] [Function])}*) {Form}*
58 Collect some values somehow. Each of the collections specifies a bunch of
59 things which collected during the evaluation of the body of the form. The
60 name of the collection is used to define a local macro, a la MACROLET.
61 Within the body, this macro will evaluate each of its arguments and collect
62 the result, returning the current value after the collection is done. The
63 body is evaluated as a PROGN; to get the final values when you are done, just
64 call the collection macro with no arguments.
66 Initial-Value is the value that the collection starts out with, which
67 defaults to NIL. Function is the function which does the collection. It is
68 a function which will accept two arguments: the value to be collected and the
69 current collection. The result of the function is made the new value for the
70 collection. As a totally magical special-case, the Function may be Collect,
71 which tells us to build a list in forward order; this is the default. If an
72 Initial-Value is supplied for Collect, the stuff will be rplacd'd onto the
73 end. Note that Function may be anything that can appear in the functional
74 position, including macros and lambdas."
76 (let ((macros ())
77 (binds ()))
78 (dolist (spec collections)
79 (unless (<= 1 (length spec) 3)
80 (error (intl:gettext "Malformed collection specifier: ~S.") spec))
81 (let ((n-value (gensym))
82 (name (first spec))
83 (default (second spec))
84 (kind (or (third spec) 'collect)))
85 (push `(,n-value ,default) binds)
86 (if (eq kind 'collect)
87 (let ((n-tail (gensym)))
88 (if default
89 (push `(,n-tail (last ,n-value)) binds)
90 (push n-tail binds))
91 (push `(,name (&rest args)
92 (collect-list-expander ',n-value ',n-tail args))
93 macros))
94 (push `(,name (&rest args)
95 (collect-normal-expander ',n-value ',kind args))
96 macros))))
97 `(macrolet ,macros (let* ,(nreverse binds) ,@body))))
99 ;;; Borrowed from cmucl src/compiler/proclaim.lisp
101 ;;; Parse-Lambda-List -- Interface
103 ;;; Break a lambda-list into its component parts. We return eleven values:
104 ;;; 1] A list of the required args.
105 ;;; 2] A list of the optional arg specs.
106 ;;; 3] True if a rest arg was specified.
107 ;;; 4] The rest arg.
108 ;;; 5] A boolean indicating whether keywords args are present.
109 ;;; 6] A list of the keyword arg specs.
110 ;;; 7] True if &allow-other-keys was specified.
111 ;;; 8] A list of the &aux specifiers.
112 ;;; 9] True if a more arg was specified.
113 ;;; 10] The &more context var
114 ;;; 11] The &more count var
116 ;;; The top-level lambda-list syntax is checked for validity, but the arg
117 ;;; specifiers are just passed through untouched. If something is wrong, we
118 ;;; use Compiler-Error, aborting compilation to the last recovery point.
120 (defun parse-lambda-list (list)
121 (declare (list list))
122 (collect ((required)
123 (optional)
124 (keys)
125 (aux))
126 (flet ((compiler-error (&rest args)
127 (apply #'error args))
128 (compiler-note (&rest args)
129 (apply #'warn args)))
130 (let ((restp nil)
131 (rest nil)
132 (morep nil)
133 (more-context nil)
134 (more-count nil)
135 (keyp nil)
136 (allowp nil)
137 (state :required))
138 (dolist (arg list)
139 ;; check for arguments that have the syntactic form of a
140 ;; keyword argument without being a recognized lambda-list keyword
141 (when (and (symbolp arg)
142 (let ((name (symbol-name arg)))
143 (and (/= (length name) 0)
144 (char= (char name 0) #\&))))
145 (unless (member arg lambda-list-keywords)
146 (compiler-note
147 "~S uses lambda-list keyword naming convention, but is not a recognized lambda-list keyword."
148 arg)))
149 (if (member arg lambda-list-keywords)
150 (ecase arg
151 (&optional
152 (unless (eq state :required)
153 (compiler-error "Misplaced &optional in lambda-list: ~S." list))
154 (setq state '&optional))
155 (&rest
156 (unless (member state '(:required &optional))
157 (compiler-error "Misplaced &rest in lambda-list: ~S." list))
158 (setq state '&rest))
159 (&more
160 (unless (member state '(:required &optional))
161 (compiler-error "Misplaced &more in lambda-list: ~S." list))
162 (setq morep t state '&more-context))
163 (&key
164 (unless (member state '(:required &optional :post-rest
165 :post-more))
166 (compiler-error "Misplaced &key in lambda-list: ~S." list))
167 (setq keyp t)
168 (setq state '&key))
169 (&allow-other-keys
170 (unless (eq state '&key)
171 (compiler-error "Misplaced &allow-other-keys in lambda-list: ~S." list))
172 (setq allowp t state '&allow-other-keys))
173 (&aux
174 (when (member state '(&rest &more-context &more-count))
175 (compiler-error "Misplaced &aux in lambda-list: ~S." list))
176 (setq state '&aux)))
177 (case state
178 (:required (required arg))
179 (&optional (optional arg))
180 (&rest
181 (setq restp t rest arg state :post-rest))
182 (&more-context
183 (setq more-context arg state '&more-count))
184 (&more-count
185 (setq more-count arg state :post-more))
186 (&key (keys arg))
187 (&aux (aux arg))
189 (compiler-error "Found garbage in lambda-list when expecting a keyword: ~S." arg)))))
191 (when (eq state '&rest)
192 (compiler-error "&rest not followed by required variable."))
194 (values (required) (optional) restp rest keyp (keys) allowp (aux)
195 morep more-context more-count)))))
197 (defun parse-body (body environment &optional (doc-string-allowed t))
198 "This function is to parse the declarations and doc-string out of the body of
199 a defun-like form. Body is the list of stuff which is to be parsed.
200 Environment is ignored. If Doc-String-Allowed is true, then a doc string
201 will be parsed out of the body and returned. If it is false then a string
202 will terminate the search for declarations. Three values are returned: the
203 tail of Body after the declarations and doc strings, a list of declare forms,
204 and the doc-string, or NIL if none."
205 (declare (ignore environment))
206 (let ((decls ())
207 (doc nil))
208 (do ((tail body (cdr tail)))
209 ((endp tail)
210 (values tail (nreverse decls) doc))
211 (let ((form (car tail)))
212 (cond ((and (stringp form) (cdr tail))
213 (if doc-string-allowed
214 (setq doc form
215 ;; Only one doc string is allowed.
216 doc-string-allowed nil)
217 (return (values tail (nreverse decls) doc))))
218 ((not (and (consp form) (symbolp (car form))))
219 (return (values tail (nreverse decls) doc)))
220 ((eq (car form) 'declare)
221 (push form decls))
223 (return (values tail (nreverse decls) doc))))))))
227 ;; Define user-exposed functions that are written in Lisp.
229 ;; If the function name NAME starts with #\$ we check the number of
230 ;; arguments. In this case, two functions are created: NAME and
231 ;; NAME-IMPL (without the leading $). NAME is the user function that
232 ;; checks for the argument count and NAME-IMPL is the actual
233 ;; implementation..
235 ;; If the function name doesn't start with $, we still allow it, but
236 ;; these should be replaced with plain defun eventually.
238 ;; The lambda-list supports &optional and &rest args. Keyword args
239 ;; are an error.
240 (defmacro defmfun (name lambda-list &body body)
241 (cond
242 ((char/= #\$ (aref (string name) 0))
243 ;; If NAME doesn't start with $, it's an internal function not
244 ;; directly exposed to the user. Basically define the function
245 ;; as is, taking care to support the Maclisp narg syntax.
246 (cond ((and (symbolp lambda-list) (not (null lambda-list)))
247 ;; Support MacLisp narg syntax: (defun foo a ...)
248 `(progn
249 (defprop ,name t translated)
250 (defun ,name (&rest narg-rest-argument
251 &aux (,lambda-list (length narg-rest-argument)))
252 ,@body)))
254 `(progn
255 (defprop ,name t translated)
256 (defun ,name ,lambda-list ,@body)))))
258 ;; Function name begins with $, so it's exposed to the user;
259 ;; carefully check the number of arguments and print a nice
260 ;; message if the number doesn't match the expected number.
261 #+nil
262 (unless (char= #\$ (aref (string name) 0))
263 (warn "First character of function name must start with $: ~S~%" name))
264 (multiple-value-bind (required-args
265 optional-args
266 restp
267 rest-arg
268 keywords-present-p)
269 (parse-lambda-list lambda-list)
271 (when keywords-present-p
272 (error "Keyword arguments are not supported"))
274 (let* ((required-len (length required-args))
275 (optional-len (length optional-args))
276 (impl-name (intern (concatenate 'string
277 (string name)
278 "-IMPL")))
279 (impl-doc (format nil "Implementation for ~S" name))
280 (nargs (gensym "NARGS-"))
281 (args (gensym "REST-ARG-"))
282 (rest-name (gensym "REST-ARGS"))
283 (pretty-fname
284 (cond (optional-args
285 ;; Can't do much with optional args, so just use the function name.
286 name)
287 (restp
288 ;; Use maxima syntax for rest args: foo(a,b,[c]);
289 `((,name) ,@required-args ((mlist) ,rest-arg)))
291 ;; Just have required args: foo(a,b)
292 `((,name) ,@required-args)))))
294 (multiple-value-bind (forms decls doc-string)
295 (parse-body body nil t)
296 (setf doc-string (if doc-string (list doc-string)))
297 `(progn
298 (defun ,impl-name ,lambda-list
299 ,impl-doc
300 ,@decls
301 (block ,name
302 ,@forms))
303 (defprop ,name t translated)
304 (defun ,name (&rest ,args)
305 ,@doc-string
306 (let ((,nargs (length ,args)))
307 (declare (ignorable ,nargs))
308 ,@(cond
309 (restp
310 ;; When a rest arg is given, there's no upper
311 ;; limit to the number of args. Just check that
312 ;; we have enough args to satisfy the required
313 ;; args.
314 (unless (null required-args)
315 `((when (< ,nargs ,required-len)
316 (merror (intl:gettext "~M: expected at least ~M arguments but got ~M: ~M")
317 ',pretty-fname
318 ,required-len
319 ,nargs
320 (list* '(mlist) ,args))))))
321 (optional-args
322 ;; There are optional args (but no rest
323 ;; arg). Verify that we don't have too many args,
324 ;; and that we still have all the required args.
326 (when (> ,nargs ,(+ required-len optional-len))
327 (merror (intl:gettext "~M: expected at most ~M arguments but got ~M: ~M")
328 ',pretty-fname
329 ,(+ required-len optional-len)
330 ,nargs
331 (list* '(mlist) ,args)))
332 (when (< ,nargs ,required-len)
333 (merror (intl:gettext "~M: expected at least ~M arguments but got ~M: ~M")
334 ',pretty-fname
335 ,required-len
336 ,nargs
337 (list* '(mlist) ,args)))))
339 ;; We only have required args.
340 `((unless (= ,nargs ,required-len)
341 (merror (intl:gettext "~M: expected exactly ~M arguments but got ~M: ~M")
342 ',pretty-fname
343 ,required-len
344 ,nargs
345 (list* '(mlist) ,args))))))
346 (apply #',impl-name ,args)))
347 (define-compiler-macro ,name (&rest ,rest-name)
348 `(,',impl-name ,@,rest-name)))))))))
350 ;; Examples:
351 ;; (defmfun $foobar (a b) (list '(mlist) a b))
352 ;; (defmfun $foobar1 (a b &optional c) (list '(mlist) a b c))
353 ;; (defmfun $foobar1a (a b &optional (c 99)) (list '(mlist) a b c))
354 ;; (defmfun $foobar2 (a b &rest c) (list '(mlist) a b (list* '(mlist) c)))
355 ;; (defmfun $foobar3 (a b &optional c &rest d) "foobar3 function" (list '(mlist) a b c (list* '(mlist) d)))
357 ;; This works by accident, kind of:
358 ;; (defmfun $baz (a &aux (b (1+ a))) (list '(mlist) a b))
360 ;; This should produce compile errors
361 ;; (defmfun $zot (a &key b) (list '(mlist) a b))