1 ;;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
5 ;; We got this code from cmucl, so we don't actually need all of this.
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
))
15 (eval-when (compile load eval
)
16 ;;;; Borrowed from cmucl src/code/extensions.lisp. Used in parsing
19 ;;;; The Collect macro:
21 ;;; Collect-Normal-Expander -- Internal
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.
28 (defun collect-normal-expander (n-value fun forms
)
30 ,@(mapcar #'(lambda (form) `(setq ,n-value
(,fun
,form
,n-value
))) forms
)
33 ;;; Collect-List-Expander -- Internal
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.
38 (defun collect-list-expander (n-value n-tail forms
)
39 (let ((n-res (gensym)))
41 ,@(mapcar #'(lambda (form)
42 `(let ((,n-res
(cons ,form nil
)))
44 (setf (cdr ,n-tail
) ,n-res
)
45 (setq ,n-tail
,n-res
))
47 (setq ,n-tail
,n-res
,n-value
,n-res
)))))
54 ;;; The ultimate collection macro...
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."
78 (dolist (spec collections
)
79 (unless (<= 1 (length spec
) 3)
80 (error (intl:gettext
"Malformed collection specifier: ~S.") spec
))
81 (let ((n-value (gensym))
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)))
89 (push `(,n-tail
(last ,n-value
)) binds
)
91 (push `(,name
(&rest args
)
92 (collect-list-expander ',n-value
',n-tail args
))
94 (push `(,name
(&rest args
)
95 (collect-normal-expander ',n-value
',kind args
))
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.
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
))
126 (flet ((compiler-error (&rest args
)
127 (apply #'error args
))
128 (compiler-note (&rest args
)
129 (apply #'warn args
)))
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
)
147 "~S uses lambda-list keyword naming convention, but is not a recognized lambda-list keyword."
149 (if (member arg lambda-list-keywords
)
152 (unless (eq state
:required
)
153 (compiler-error "Misplaced &optional in lambda-list: ~S." list
))
154 (setq state
'&optional
))
156 (unless (member state
'(:required
&optional
))
157 (compiler-error "Misplaced &rest in lambda-list: ~S." list
))
160 (unless (member state
'(:required
&optional
))
161 (compiler-error "Misplaced &more in lambda-list: ~S." list
))
162 (setq morep t state
'&more-context
))
164 (unless (member state
'(:required
&optional
:post-rest
166 (compiler-error "Misplaced &key in lambda-list: ~S." list
))
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
))
174 (when (member state
'(&rest
&more-context
&more-count
))
175 (compiler-error "Misplaced &aux in lambda-list: ~S." list
))
178 (:required
(required arg
))
179 (&optional
(optional arg
))
181 (setq restp t rest arg state
:post-rest
))
183 (setq more-context arg state
'&more-count
))
185 (setq more-count arg state
:post-more
))
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
))
208 (do ((tail body
(cdr tail
)))
210 (values tail
(nreverse decls
) doc
))
211 (let ((form (car tail
)))
212 (cond ((and (stringp form
) (cdr tail
))
213 (if doc-string-allowed
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
)
223 (return (values tail
(nreverse decls
) doc
))))))))
226 (defun defmfun-keywords (fname options valid-keywords
)
227 ;; options looks like (((mequal) $opt1 val1) ((mequal) $opt2 val2) ...)
229 ;; Convert to a new list that looks like (:opt1 val1 :opt2 val2 ...)
231 (unless (listp options
)
232 (merror "Invalid Maxima keyword options: ~M" options
))
233 (when (every #'(lambda (o)
234 ;; Make sure every option has the right form.
235 (let ((ok (and (listp o
)
237 (eq (caar o
) 'mequal
))))
239 (merror (intl:gettext
"~M: Badly formed keyword option: ~M")
243 (mapcan #'(lambda (o)
244 (destructuring-bind (mequal opt val
)
246 (declare (ignore mequal
))
247 (if (or (null valid-keywords
)
248 (member opt valid-keywords
))
249 (flet ((keywordify (x)
250 (intern (subseq (symbol-name x
) 1) :keyword
)))
251 (list (keywordify opt
) val
))
252 (merror (intl:gettext
"~M: Unrecognized keyword: ~M")
256 ;; Define user-exposed functions that are written in Lisp.
258 ;; If the function name NAME starts with #\$ we check the number of
259 ;; arguments. In this case, two functions are created: NAME and
260 ;; NAME-IMPL (without the leading $). NAME is the user function that
261 ;; checks for the argument count and NAME-IMPL is the actual
264 ;; If the function name doesn't start with $, we still allow it, but
265 ;; these should be replaced with plain defun eventually.
267 ;; The lambda-list supports &optional and &rest args. Keyword args
270 ;; The variable %%PRETTY-FNAME is defined such that the body can refer
271 ;; to this variable to get the pretty name of the defined function for
272 ;; use in printing error messages or what not. This allows the
273 ;; implementation to print out the function name that would also be
274 ;; used when printint out error messages for incorrect number of
276 (defmacro defmfun
(name lambda-list
&body body
)
278 ;; We make sure that the ARG-LIST property is added
279 ;; first, so that it will end up last in the list.
281 (putprop ',name
',lambda-list
'arg-list
)
282 (defprop ,name t translated
))))
283 (let ((maclisp-narg-p (and (symbolp lambda-list
) (not (null lambda-list
)))))
285 ((or (char/= #\$
(aref (string name
) 0))
287 ;; If NAME doesn't start with $, it's an internal function not
288 ;; directly exposed to the user. Basically define the function
289 ;; as is, taking care to support the Maclisp narg syntax.
290 (cond (maclisp-narg-p
291 ;; Support MacLisp narg syntax: (defun foo a ...)
294 (defun ,name
(&rest narg-rest-argument
295 &aux
(,lambda-list
(length narg-rest-argument
)))
300 (defun ,name
,lambda-list
,@body
)))))
302 ;; Function name begins with $, so it's exposed to the user;
303 ;; carefully check the number of arguments and print a nice
304 ;; message if the number doesn't match the expected number.
306 (unless (char= #\$
(aref (string name
) 0))
307 (warn "First character of function name must start with $: ~S~%" name
))
308 (multiple-value-bind (required-args
315 (parse-lambda-list lambda-list
)
317 (when (and keywords-present-p
318 (or optional-args restp
))
319 (error "Keyword args cannot be used with optional args or rest args"))
321 (let* ((required-len (length required-args
))
322 (optional-len (length optional-args
))
323 (impl-name (intern (concatenate 'string
324 (subseq (string name
) 1)
326 (impl-doc (format nil
"Implementation for ~S" name
))
327 (nargs (gensym "NARGS-"))
328 (args (gensym "REST-ARG-"))
329 (rest-name (gensym "REST-ARGS"))
332 ;; Can't do much with optional args, so just use the function name.
335 ;; Use maxima syntax for rest args: foo(a,b,[c]);
336 `((,name
) ,@required-args
((mlist) ,rest-arg
)))
338 ;; Not exactly sure how to do this
342 (multiple-value-bind (name val
)
345 (intern (format nil
"$~A" (car k
)))
348 (intern (format nil
"$~A" k
))
351 `((mequal) ,name
,val
)))
353 `((,name
) ,@required-args
,@keys
)))
355 ;; Just have required args: foo(a,b)
356 `((,name
) ,@required-args
))))
358 (unless allow-other-keys-p
359 (mapcar #'(lambda (x)
368 (multiple-value-bind (forms decls doc-string
)
369 (parse-body body nil t
)
370 (setf doc-string
(if doc-string
(list doc-string
)))
372 (defun ,impl-name
,lambda-list
376 (let ((%%pretty-fname
',pretty-fname
))
377 (declare (ignorable %%pretty-fname
))
380 ; We don't put this putprop in add-props because
381 ; add-props is for both user and internal functions
382 ; while the impl-name property is only for user
384 (putprop ',name
',impl-name
'impl-name
)
385 (defun ,name
(&rest
,args
)
387 (let ((,nargs
(length ,args
)))
388 (declare (ignorable ,nargs
))
390 ((or restp keywords-present-p
)
391 ;; When a rest arg is given, there's no upper
392 ;; limit to the number of args. Just check that
393 ;; we have enough args to satisfy the required
395 (unless (null required-args
)
396 `((when (< ,nargs
,required-len
)
397 (merror (intl:gettext
"~M: expected at least ~M arguments but got ~M: ~M")
401 (list* '(mlist) ,args
))))))
403 ;; There are optional args (but no rest
404 ;; arg). Verify that we don't have too many args,
405 ;; and that we still have all the required args.
407 (when (> ,nargs
,(+ required-len optional-len
))
408 (merror (intl:gettext
"~M: expected at most ~M arguments but got ~M: ~M")
410 ,(+ required-len optional-len
)
412 (list* '(mlist) ,args
)))
413 (when (< ,nargs
,required-len
)
414 (merror (intl:gettext
"~M: expected at least ~M arguments but got ~M: ~M")
418 (list* '(mlist) ,args
)))))
420 ;; We only have required args.
421 `((unless (= ,nargs
,required-len
)
422 (merror (intl:gettext
"~M: expected exactly ~M arguments but got ~M: ~M")
426 (list* '(mlist) ,args
))))))
431 (subseq ,args
0 ,required-len
)
432 (defmfun-keywords ',pretty-fname
433 (nthcdr ,required-len
,args
)
434 ',maxima-keywords
))))
436 `(apply #',impl-name
,args
)))))
439 `(define-compiler-macro ,name
(&rest
,rest-name
)
440 (let ((args (append (subseq ,rest-name
0 ,required-len
)
441 (defmfun-keywords ',pretty-fname
442 (nthcdr ,required-len
,rest-name
)
443 ',maxima-keywords
))))
444 `(,',impl-name
,@args
))))
446 `(define-compiler-macro ,name
(&rest
,rest-name
)
447 `(,',impl-name
,@,rest-name
)))))))))))))
450 ;; (defmfun $foobar (a b) (list '(mlist) a b))
451 ;; (defmfun $foobar1 (a b &optional c) (list '(mlist) a b c))
452 ;; (defmfun $foobar1a (a b &optional (c 99)) (list '(mlist) a b c))
453 ;; (defmfun $foobar2 (a b &rest c) (list '(mlist) a b (list* '(mlist) c)))
454 ;; (defmfun $foobar3 (a b &optional c &rest d) "foobar3 function" (list '(mlist) a b c (list* '(mlist) d)))
456 ;; (defmfun $foobar4 (a b &key c) (list '(mlist) a b c))
457 ;; (defmfun $foobar5 (a b &key (c 42)) (list '(mlist) a b c))
458 ;; (defmfun $foobar6 (a b &key (c 42) &allow-other-keys) (list '(mlist) a b c))
460 ;; foobar5(1,2) => [1, 2, 42]
461 ;; foobar5(1,2,c=99) => [1, 2, 99]
462 ;; foobar5(1,2,c=99,d=4) => error: unrecognized keyword d
463 ;; foobar6(1,2,c=42,d=99) => [1, 2, 42]
465 ;; This works by accident, kind of:
466 ;; (defmfun $baz (a &aux (b (1+ a))) (list '(mlist) a b))
468 ;; This should produce compile errors
469 ;; (defmfun $zot (a &optional c &key b) (list '(mlist) a b))