3 %%% Miscellaneous functionality
5 %%% (c) 2015 Straylight/Edgeware
8 %%%----- Licensing notice ---------------------------------------------------
10 %%% This file is part of the Sensible Object Design, an object system for C.
12 %%% SOD is free software; you can redistribute it and/or modify
13 %%% it under the terms of the GNU General Public License as published by
14 %%% the Free Software Foundation; either version 2 of the License, or
15 %%% (at your option) any later version.
17 %%% SOD is distributed in the hope that it will be useful,
18 %%% but WITHOUT ANY WARRANTY; without even the implied warranty of
19 %%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 %%% GNU General Public License for more details.
22 %%% You should have received a copy of the GNU General Public License
23 %%% along with SOD; if not, write to the Free Software Foundation,
24 %%% Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 \chapter{Miscellaneous functionality} \label{ch:misc}
28 %%%--------------------------------------------------------------------------
29 \section{Utilities} \label{sec:misc.utilities}
31 These symbols are defined in the @|sod-utilities| package.
34 \subsection{Macro utilities}
36 We begin with some simple utilities which help with writing macros. Several
37 of these are standard.
40 {with-gensyms (@{ @<var> @! (@<var> @[@<name>@]) @}^*) \\ \ind
44 Bind each @<var> (a symbol, not evaluated) to a freshly made gensym whose
45 name is based on the corresponding @<name> (a string, evaluated), and
46 evaluate the @<form>s as an implicit @|progn| in the resulting environment.
47 If @<name> is omitted, then the name of the @<var> is used as a default; a
48 bare symbol may be written in place of a singleton list.
52 {once-only (@[[ :environment @<env> @]]
53 @{ @<var> @! (@<var> @[@<value-form>@]) @}^*) \\ \ind
56 \-\nlret @<result-form>}
57 This is a helper to ensure that macro expansions evaluate their arguments
58 exactly once each, in the correct order.
60 Each @<var> is bound to an appropriate value (often a gensym) and then the
61 @<form>s are evaluated as an implicit @|progn| in the resulting environment
62 to produce an output form. This output form is then enclosed in one or
63 more binding forms to produce a @<result-form>. When the @<result-form> is
64 evaluated, the behaviour will be as if each @<value-form> is evaluated
65 exactly once each, in order, and each value is captured in the
68 A simple @|once-only| expansion might look something like
70 (let (\=(@<var>_1 (gensym)) \\
72 \>(@<var>_n (gensym))) \\ \ind
73 `(let (\=(,@<var>_1 ,@<value-form>_1) \\
75 \>(,@<var>_n ,@<value-form>_n)) \\ \ind
76 @<declaration>_1 \dots\ @<declaration>_m \\
77 @<form>_1 \dots\ @<form>_\ell))
79 However, if @|once-only| can determine that some @<value-form> is a
80 constant (e.g., it is @|quote|d, self-evaluating, or reported as
81 @|constantp| in the given environment @<env>), then it need not allocate a
82 gensym: it can instead bind the @<var> directly to the constant value.
84 If a @<value-form> is omitted, then the value of the corresponding @<var>
85 is used. It is conventional usage for a macro to wrap @|once-only| around
86 its body so as to convert the arguments which it should evaluate into safe
87 gensyms capturing their runtime values. (Not that the simple expansion
88 given above can't do this correctly.) A bare symbol may be written in
89 place of a singleton list.
93 {parse-body @<body> \&key :docp :declp
94 @> @<doc-string> @<declarations> @<body-forms>}
95 Parse the @<body> into a @<doc-string>, some @<declaration>s, and a list of
98 The @<body> is assumed to have the general syntax
100 @[[ @<doc-string> @! @<declaration>^* @]] \\
103 A @<doc-string> is permitted if and only if @<docp> is non-nil, and
104 declarations are permitted if and only if @<declp> is non-nil; both are
107 Each return value is a list, which is empty if the corresponding part of
108 the input @<body> is missing. Specifically:
110 \item @<doc-string> is either nil, or a singleton list containing a string;
111 \item @<declarations> is either nil, or a singleton list containing a
112 @|(declare \dots)| form gathering up all of the individual
113 @<declaration>s within the @<body>; and
114 \item @<body-forms> is a list of the remaining forms in the @<body>.
116 Thus, the parsed body-parts can conveniently be spliced into a macro
117 expansion using @|,@@|.
120 \begin{describe}{fun}{symbolicate \&rest @<symbols> @> @<symbol>}
121 Return the symbol, interned in the current @|*package*|, whose name is the
122 concatenation of the names of the given @<symbols>.
126 \subsection{Locatives}
128 A \emph{locative} is a value which remembers where another value is stored,
129 -- whether it's in a variable, an array element, a structure slot, a hash
130 table, etc.\ -- and can modify and retrieve it.
132 Some Lisp systems have highly efficient locatives which actually keep track
133 of the machine addresses of the places to which they refer. Common Lisp does
134 not implement true locatives of this kind, but something sufficiently useful
137 These locatives can't usefully be compared. It should be possible to compare
138 true locatives, such that two locatives compare equal if and only if they
139 refer to the same place; but that doesn't work for these locatives.
141 \begin{describe}{cls}{loc}
142 The type of locative objects.
145 \begin{describe}{fun}{locp @<object> @> @<generalized-boolean>}
146 Return non-nil if and only if @<object> is a locative.
149 \begin{describe}{mac}{locf @<place> @> @<locative>}
150 Return a fresh locative capturing the @<place>, which may be any expression
151 usable as the first operand to @|setf|.
155 {\dhead{fun}{ref @<locative> @> @<value>}
156 \dhead{fun}{setf (ref @<locative>) @<value>}}
157 Retrieve and return the current value stored in the place captured by the
158 @<locative>. With @|setf|, store the new @<value> in the place captured by
162 \begin{describe}{mac}
164 @{ @<var> @! (@{ @<var> @!
165 (@<var> @[@<locative>@]) @}^*) @} \\ \ind
168 \-\nlret @<values>^*}
169 This is a macro which hides the use of locatives from its caller using
172 Each @<locative> should be an expression which evaluates to a locative
173 value (not a general place). These are evaluated once each, left to
174 right. The @<form>s are then evaluated as an implicit @|progn|, with each
175 @<var> defined as a symbol macro which will retrieve -- or, with @|setf|,
176 modify -- the value referred to by the corresponding locative.
178 If a @<locative> is omitted, it defaults to the value of @<var>; a
179 bare symbol may be used in place of a singleton list.
183 \subsection{Anaphorics}
185 An anaphoric macro implicitly binds a well-known name to a value of interest,
186 in the course of doing something else. The concept was popularized by Paul
187 Graham \cite{FIXME:OnLisp}.
189 The macros described here all bind the variable @|it|.
191 \begin{describe}{sym}{it}
192 The symbol @|it| is exported by the @|sod-utilities| package.
195 \begin{describe}{mac}{aif @<condition> @<consequent> @[@<alt>@] @> @<value>^*}
196 Evaluate the @<condition>. If @<condition> is non-nil, then bind @|it| to
197 the resulting value and evaluate the @<consequent>, returning all of its
198 values. Otherwise, evaluate @<alt>, returning all of its values.
201 \begin{describe}{mac}{aand @<form>^* @> @<value>^*}
202 Evaluate each @<form> in turn. If any @<form> evaluates to nil, then stop
203 and return nil. Each form except the first is evaluated with @|it| bound
204 to the (necessarily non-nil) value of the previous form. If all but the
205 last form evaluate non-nil, then return all the values of the final form.
208 (No @|aor| is provided, since @|it| would necessarily be bound to nil.)
210 \begin{describe}{mac}{awhen @<condition> @<form>^* @> nil}
211 If @<condition> evaluates to a non-nil value, bind @|it| to that value, and
212 evaluate the @<form>s as an implicit @|progn|. Otherwise, return nil.
215 \begin{describe}{mac}{acond @{ (@<condition> @<form>^*) @}^* @> @<value>^*}
216 Evaluate each @<condition> in turn, until one of them produces a non-nil
217 value. If the @<condition> is followed by one or more @<form>s, then bind
218 @|it| to the non-nil value of the @<condition> and evaluate the @<form>s as
219 an implicit @|progn|; otherwise, simply return the value of the
220 @<condition>. If no @<condition> produces a non-nil value then return nil.
224 {\dhead{mac}{acase @<scrutinee>
225 @{ (@{ @<case> @! (@<case>^*) @} @<form>^*) @}^*
227 \dhead{mac}{aecase @<scrutinee>
228 @{ (@{ @<case> @! (@<case>^*) @} @<form>^*) @}^*
230 \dhead{mac}{atypecase @<scrutinee> @{ (@<type> @<form>^*) @}^*
232 \dhead{mac}{aetypecase @<scrutinee> @{ (@<type> @<form>^*) @}^*
234 These are like the Common Lisp macros @|case|, @|ecase|, @|typecase|, and
235 @|etypecase|, except that @|it| is bound to the value of the @<scrutinee>
236 while evaluating the matching @<form>s.
239 \begin{describe}{mac}{asetf @{ @<place> @<value> @}^* @> @<value>^*}
240 For each @<place> and @<value> in turn: bind @|it| to the current value of
241 the @<place>, evaluate the @<value> expression, and store the resulting
242 value back in the @<place>. Return the @<value>(s) stored by the final
243 pair: there may be more than one value, e.g., if @<place> is a @|values|
246 For example, @|(asetf @<place> (1+ it))| is almost equivalent to @|(incf
247 @<place>)|, even if evaluating @<place> has side-effects.
251 \subsection{Metaobject protocol utilities}
253 The following utilities make use of the introspection features of the CLOS
256 \begin{describe}{gf}{instance-initargs @<instance> @> @<initargs-list>}
257 Return a fresh list of plausible initargs for the given @<instance>.
259 This is done by digging through the instance's class's slot definitions and
260 enquiring about their initargs. Initargs which are handled by methods on
261 @|shared-initialize| or similar generic functions won't be discovered.
265 {\dhead{fun}{copy-instance @<instance> \&rest @<initargs>
267 \dhead{gf}{copy-instance-using-class @<class> @<instance>
270 The @|copy-instance| function creates and returns a fresh copy of a given
271 @<instance>, possibly modifying it according to the given @<initargs>.
273 It immediately calls @|copy-instance-using-class|, calling it with the
274 instance's class and the instance itself, and simply returns the result of
275 that generic function.
277 The default method on @|copy-instance-using-class| should work for most
278 classes, but may be overridden to cope with special effects. It works as
281 \item Allocate a fresh instance of @<class>, using @|allocate-instance|.
282 \item For each slot defined by @<class>, if that slot is bound in the
283 original instance, then set the corresponding slot in the new instance to
285 \item Call @|shared-initialize| on the new instance, providing it the given
286 list of @<initargs>, but inhibiting the usual initialization of slots
287 from their initforms.
288 \item Return the new instance.
293 {\dhead{gf}{generic-function-methods @<generic-function> @> @<list>}
294 \dhead{gf}{method-specializers @<method> @> @<list>}
295 \dhead{cls}{eql-specializer}
296 \dhead{gf}{eql-specializer-object @<specializer> @> @<value>}}
297 These are precisely the MOP functions and class: the symbols are
298 re-exported for portability, because different Lisp systems define these
299 symbols in different packages.
303 \subsection{Other CLOS utilities}
305 Some other minor CLOS utilities.
307 \begin{describe}{mac}
308 {default-slot (@<instance> @<slot> @[@<slot-names>@]) \\ \ind
310 This macro is useful in methods (usually @|:after| methods) on
311 @|shared-initialize|, to set slots to some sensible default values in the
312 case where no suitable initarg was given, and default initialization is too
313 complicated to be done using an initform.
315 Set a slot to a default value, obeying the @|shared-initialize| protocol.
316 If (a) the named @<slot> of @<instance> is unbound, and (b) either
317 @<slot-names> is @|t|, or @<slot> is a member of the list @<slot-names>,
318 then evaluate the @<form>s as an implicit @|progn| and store their
319 value in the @<slot>. Otherwise do nothing.
321 The @<instance>, @<slot>, and @<slot-names> (if any) are evaluated once
325 \begin{describe}{mac}
326 {define-on-demand-slot @<class> @<slot> (@<instance>) \\ \ind
327 @[[ @<declaration>^* @! @<doc-string> @]] \\
329 This macro makes slots with delayed initialization: rather than being
330 set when the object is constructed, the slot's initial value is only
331 calculated when it's first requested. This is useful if calculating the
332 slot value is expensive and often not required, or if it's not possible to
333 initialize the slot along with the rest of the object because of dependency
336 The macro arranges things as follows. Whenever @|slot-value| is called
337 (possibly indirectly, via a reader function) to read the named @<slot> (a
338 symbol, not evaluated) on an (indirect) instance of @<class>, but the slot
339 is unbound, then @<instance> is bound to the instance in question and the
340 @<form>s are evaluated as an implicit @|progn| within the lexical
341 environment of the @|define-on-demand-slot| call, and the resulting value
342 is used as the initial value of the slot. (Furthermore, a block named
343 @<slot> is wrapped around the @<form>s, allowing an early return if that
346 This macro currently works by defining a method on @|slot-unbound|.
350 \subsection{Building lists}
352 Many Lisp functions end up constructing lists. In simple cases, a function
353 like @|mapcar| will just do the job directly. In more complex cases, a
354 common idiom is to build the list using @|push| for each element in turn; but
355 a list built this way ends up in the wrong order, so an additional pass,
356 usually using @|nreverse|, is necessary to fix it.
358 A `list builder' is an object which can be used to construct a list in the
359 right order. (Currently, a list-builder is simply a cons cell, whose cdr
360 points to the first cons-cell of the list, and whose car points to its last
361 cons; an empty list-builder is a cons whose cdr is nil and whose car is the
362 cons itself, i.e., @|\#1=(\#1\# . nil)|.)
364 \begin{describe}{fun}{make-list-builder \&optional @<initial> @> @<builder>}
365 Return a fresh new list-builder, initially containing no items.
368 \begin{describe}{fun}{lbuild-add @<builder> @<item> @> @<builder>}
369 Add @<item> to the end of the list being constructed in @<builder>.
372 \begin{describe}{fun}{lbuild-add-list @<builder> @<list> @> @<builder>}
373 Append @<list> to the list being constructed in @<builder>. The list is
374 \emph{not} copied: adding further items to the list will clobber cdr of its
378 \begin{describe}{fun}{lbuild-list @<builder> @> @<list>}
379 Return the list being constructed in the @<builder>.
381 It is permitted to continue adding items to the list: this will mutate the
382 list in-place. Often, this is what you want. For example, one might write
383 an analogue to @|pushnew| like this:
385 (defun lbuild-add-new
386 (builder item \&key key test test-not \&rest keywords) \\ \ind
387 (declare (ignore key test test-not)) \\
388 (when (apply \#'member item (lbuild-list builder)
390 (lbuild-add builder item)))
395 \subsection{Merging lists}
397 The following machinery merges lists representing a partial order. The
398 primary use for this is in computing class precedence lists during class
399 finalization. By building the input lists and choosing the tie-breaking
400 @<pick> function appropriately, many different linearization algorithms can
401 be implemented fairly easily using @|merge-lists| below.
405 {inconsistent-merge-error (error) \&key :candidates :present}
406 \dhead{gf}{merge-error-candidates @<error> @> @<list>}
407 \dhead{gf}{merge-error-present-function @<error> @> @<function>}}
408 The @|inconsistent-merge-error| condition class used to represent a failure
409 of the \descref{fun}{merge-lists}[function].
411 The @<candidates> are a list of offending items from the input lists, in
412 some order: the error is reporting that the function has failed because it
413 is not possible to order the items listed in @<candidates> in any way
414 without being inconsistent with at least one of the input lists. There is
417 The @<present> function is used to convert the input items into
418 human-readable descriptions (printed using @|princ|); the default is
419 @|identity|, which will simply print the items in a `friendly' format.
420 (Using @|prin1-to-string| would print their machine-readable escaped forms
423 The functions @|merge-error-candidates| and @|merge-error-present-function|
424 respectively retrieve the candidates list and presentation function
425 assigned to a condition when it was created.
428 \begin{describe}{fun}
429 {merge-lists @<lists> \&key :pick :test :present @> @<list>}
430 Return a merge of the @<lists>, considered as partial orderings.
432 In more detail: @<lists> should be a list of lists. Each distinct item, as
433 determined by the @<test> function (by default, @|eql|) appears in the
434 result list exactly once. Furthermore, if, in some input list, an item $x$
435 appears earlier than a different item $y$, then $x$ will also precede $y$
438 If the input lists contradict each other (e.g., list $A$ has $x$ before
439 $y$, but list $B$ has $y$ before $x$), then an error of type
440 @|inconsistent-merge-error| is signalled, with the offending items attached
441 as candidates, and the function @<present> (by default, @|identity|) as the
442 presentation function.
444 Frequently, a collection of input lists has multiple valid merges.
445 Whenever @|merge-lists| must decide between two or more equally good
446 candidates, it calls the @<pick> function to choose one of them.
447 Specifically, it invokes @|(funcall @<pick> @<candidates>
448 @<merge-so-far>)|, where @<candidates> are the items it needs to choose
449 between, and @<merge-so-far> is the currently determined prefix of the
450 final merge. The order of items in the @<candidates> list reflects their
451 order in the input lists: item $x$ precedes item $y$ in @<candidates> if
452 any only if an occurrence of $x$ appears in an earlier input list than
453 $y$. (This completely determines the order of candidates: if two items
454 appear in the same list, then that list would have ordered them and we
455 wouldn't have to call @<pick> to break the tie.) The default @<pick>
456 function simply chooses the item appearing in the earliest list, i.e.,
459 (lambda (candidates merge-so-far) \\ \ind
460 (declare (ignore merge-so-far)) \\
466 \subsection{Other list utilities}
468 \begin{describe}{fun}
469 {mappend @<function> @<list> \&rest @<more-lists> @> @<result-list>}
470 Return the result of appending @<list> and @<more-lists>, in order. All
471 but the final list are copied into the @<result-list>; the last one is used
475 \begin{describe}{mac}
476 {categorize (\=@<item-var> @<items>
477 @[[ :bind (@{ @<var> @!
478 (@<var> @[@<value>@]) @}^*) @]])
480 (@{ (@<cat-var> @<cat-predicate>) @}^*) \-\\
484 Partition an input list of @<items> according to the @<cat-predicate>s.
486 First, @<items> is evaluated, to yield a list. The @<item-var> is bound,
487 an empty list is created for each @|(@<cat-var> @<cat-predicate>)| pair,
488 and an iteration is begun. For each item in the list in turn is assigned
489 to @<item-var>; then, the bindings given by the @|:bind| keyword are
490 performed, as if by @|let*|; and the @<cat-predicate>s are evaluated in the
491 resulting environment, one by one, until one of them returns non-nil. When
492 this happens, the item is added to the corresponding list. If no predicate
493 matches the item, an error is signalled.
495 Once this iteration is complete, each @<cat-var> is bound to its
496 corresponding completed list, and the body @<form>s are evaluated in the
497 resulting environment (which does not include @<item-var>), as an implicit
498 @|progn|, and the macro yields the values of the final @<form>.
501 \begin{describe}{fun}{partial-order-minima @<items> @<order> @> @<list>}
502 Return a list of minimal items from the list @<items> according to a
503 non-strict partial order defined by the function @<order>: @|(funcall
504 @<order> $x$ $y$)| should return non-nil if and only if $x \preceq y$ in
508 \begin{describe}{fun}
509 {find-duplicates @<report> @<sequence> \&key :key :test}
510 Call @<report> on each pair of duplicate items in a @<sequence>.
511 Duplicates are determined according to the @<key> (by default @|identity|)
512 and @<test> (by default @|eql|) functions, in the usual way: two items $x$
513 and $y$ are considered equal if and only if @|(funcall @<test> (funcall
514 @<key> $x$) (funcall @<key> $y$))| returns non-nil.
516 This function will work for arbitrary @<test> functions, but it will run
517 much more efficiently if @<test> is @|eq|, @|eql|, @|equal|, or @|equalp|
518 (because it can use hash-tables).
522 \subsection{Position tracking}
524 The following functions are used to maintain file positions: see
525 \xref{sec:parsing.floc}. Columns are counted starting from zero at the far
526 left. (No particular origin is needed for line numbers.) Newlines, vertical
527 tabs, and form-feeds all move to the start of the next line; horizontal tabs
528 move to the next multiple of eight columns; other characters simply advance
531 \begin{describe}{fun}
532 {update-position @<character> @<line> @<column>
533 @> @<new-line> @<new-column>}
534 Assume that we found @<character> at a particular @<line> and @<column> in
535 a file: return the @<new-line> and @<new-column> for the next character.
538 \begin{describe}{fun}
539 {backtrack-position @<character> @<line> @<column>
540 @> @<old-line> @<old-column>}
541 Assume that we are currently at a particular @<line> and @<column> in a
542 file, and wish to \emph{unread} @<character>: return an @<old-line> and
543 @<old-column> at which we might plausibly re-read the character, so that
544 the next call to \descref{fun}{update-position} will return us to @<line>
545 and @<column>. (Specifically, the @<old-column> will likely be wrong if
546 @<character> is a horizontal tab. It is expected that this won't matter:
547 the purpose of this function is to set things up so that the
548 @|update-position| call that will accompany re-reading the character will
549 return the correct values, rather than to use the @<old-line> and
550 @<old-column> for any other purpose.)
554 \subsection{Object printing}
556 \begin{describe}{mac}
557 {maybe-print-unreadable-object
560 :identity @<identity> @]]) \\ \ind
563 If @|*print-escape*| is nil, then simply evaluate the @<form>s as an
564 implicit @|progn|; otherwise, print an `unreadable' object, as if by
566 (print-unreadable-object
569 @[:identity @<identity>@]) \\ \ind
574 \begin{describe}{fun}{print-ugly-stuff @<stream> @<func> @> @<value>^*}
575 If @<stream> is a pretty-printing stream, then print a mandatory newline,
576 and call @<func> on the underlying non-pretty-printing stream. If
577 @<stream> is not a pretty-printing stream, then simply call @<func> on
580 The main purpose for this is to be able to access features of the
581 underlying stream which a pretty-printing stream can't proxy. Most
582 notably, this is used by C fragment output, which takes advantage of an
583 underlying \descref{cls}{position-aware-output-stream} to print @|\#line|
584 directives, so that a C~compiler will blame the original fragment in the
585 Sod module source rather than the generated C code.
589 \subsection{Condition utilities}
591 The following definitions are useful when working with conditions.
593 \begin{describe}{cls}
594 {simple-control-error (control-error simple-error)
595 \&key :format-control :format-arguments}
596 This is the obvious multiply-inherited subclass of @|control-error| whose
597 print form is determined by a @<format-control> and a @<format-arguments>
601 \begin{describe}{fun}
602 {designated-condition
603 \=@<default-type> @<datum> @<arguments> \\
604 \>\&key :allow-pointless-arguments
606 Creates and returns a condition object of @<default-type>, given a
607 condition designator @<datum> and @<arguments>.
609 The Common Lisp specification carefully explains how a `datum' and an
610 argument list together form a `condition designator', and how such a pair
611 are to be converted into a condition object with some default type, but
612 there's no mechanism provided to simply do this task. (Functions like
613 @|error| and @|signal| implicitly, but have possibly-undesirable
614 side-effects, and don't allow control over the default type.)
618 \item If @<datum> is a condition object, then the designated condition is
619 simply @<datum>. In this case, if @<arguments> is not an empty list and
620 @<allow-pointless-arguments> is nil (the default), an error is signalled;
621 otherwise, the @<arguments> are ignored.
623 \item If @<datum> is a symbol, then the designated condition is constructed
626 (apply \#'make-condition @<datum> @<arguments>)
629 \item If @<datum> is a string or function (i.e., a `format-control'), then
630 the designated condition is constructed by calling
632 (make-condition \=@<default-type> \\
633 \>:format-control @<datum> \\
634 \>:format-arguments @<arguments>)
637 \item Otherwise the designator is malformed, and an error is signalled.
641 \begin{describe}{fun}
642 {invoke-associated-restart @<restart> @<condition> \&rest @<arguments>}
643 Invoke the active restart named @<restart>, associated with the given
644 @<condition>, passing a list of @<arguments>.
646 The function attempts to find and invoke a restart with the given name. If
647 @<condition> is non-nil, then it searches among restarts associated with
648 that specific condition, and restarts associated with no condition; if
649 @<condition> is nil, then it searches among all restarts.
651 If a matching restart is found, it is invoked, passing the @<arguments>
652 list. Otherwise, an error (of class @|control-error|) is signalled.
656 {\dhead{cls}{enclosing-condition (condition) \&key :condition}
657 \dhead{gf}{enclosed-condition @<enclosing-condition> @> @<condition>}}
658 An @|enclosing condition| is a condition which contains another condition
659 within it. Objects of type @|enclosing-condition| are used to add
660 additional information to an existing condition, or to alter the type of a
661 condition without losing information.
663 When an @|enclosing-condition| is constructed, the @<condition> argument
664 names the existing condition to be enclosed. This enclosed condition can
665 be retrieved by calling @|enclosed-condition|.
668 \begin{describe}{cls}{information (condition) \&key}
669 A condition of class @|information| conveys information which might be of
670 interest, but does not of itself indicate that anything is wrong.
672 Within a compiler, @|information| conditions may be signalled in order to
673 present the user with additional diagnostic information about a recently
677 \begin{describe}{cls}
678 {simple-information (simple-condition information) \\ \ind
679 \&key :format-control :format-arguments}
680 This is the obvious multiply-inherited subclass of @|information|
681 whose print-representation is determined by a @<format-control> and a
682 @<format-arguments> list.
686 {\dhead{fun}{info @<datum> \&rest @<arguments> @> @<flag>}
688 \dhead{fun}{noted \&optional @<condition>}}
689 The @|info| function establishes a restart named @|noted| and signals a
690 condition of default type @|simple-information|, designated by the @<datum>
691 and @<arguments>. The @|info| function returns non-nil if and only if the
692 associated @|noted| restart was invoked.
694 The @|noted| restart accepts no arguments.
696 The @|noted| function finds and invokes a @|noted| restart: if @<condition>
697 is non-nil, then only the restart associated with that condition (and those
698 not associated with any condition) are considered; otherwise, all
699 conditions are considered.
702 \begin{describe}{fun}
703 {promiscuous-cerror @<continue-string> @<datum> \&rest @<arguments>}
704 Establish a @|continue| restart and signal an error of default type
705 @|simple-error|, designated by @<datum> and @<arguments>. The restart's
706 report format is determined by @<continue-string> and the @<arguments>.
708 Some implementations of @|cerror| associate the @|continue| restart which
709 they establish with the condition they signal. This interferes with
710 special effects -- specifically, enclosing the signalled condition and
711 resignalling it. The @|promiscuous-cerror| function carefully avoids
712 associating its restart with the condition.
715 \begin{describe}{fun}{cerror* @<datum> \&rest @<arguments>}
716 A simplified version of \descref{fun}{promiscuous-cerror} which uses the
717 hardcoded string @|Continue| for the restart. This makes calling the
718 function more similar to other condition-signalling functions, at the
719 expense of some usability in environments which don't continue after
720 continuable errors automatically.
724 \subsection{Very miscellaneous utilities}
726 \begin{describe}{fun}
727 {whitespace-char-p @<character> @> @<generalized-boolean>}
728 Return non-nil if and only if @<character> is a whitespace character.
730 A character is whitespace if @|(peek-char t @<stream>)| would skip it.
733 \begin{describe}{fun}
734 {frob-identifier @<string> \&key :swap-case :swap-hyphen
735 @> @<frobbed-string>}
736 Return a `frobbed' version of the identifier @<string>. Two different
737 transformations can be applied.
741 \item If @<swap-case> is non-nil (the default), and the letters in
742 @<string> are either all uppercase or all lowercase, then switch the case
743 of all of the letters.
745 \item If @<swap-hyphen> is non-nil (the default), and @<string> contains
746 either hyphens @`--' or underscores @`_', but not both, then replace the
747 hyphens by underscores or \emph{vice-versa}.
751 (These are the `obvious' transformations to convert a C identifier into a
756 \item @|(frob-identifier "foo")| $\Longrightarrow$ @|"FOO"|
757 \item @|(frob-identifier "FOO")| $\Longrightarrow$ @|"foo"|
758 \item @|(frob-identifier "FooBar")| $\Longrightarrow$ @|"FooBar"|
759 \item @|(frob-identifier "Foo-Bar")| $\Longrightarrow$ @|"Foo_Bar"|
760 \item @|(frob-identifier "Foo_Bar")| $\Longrightarrow$ @|"Foo-Bar"|
761 \item @|(frob-identifier "foo_bar")| $\Longrightarrow$ @|"FOO-BAR"|
762 \item @|(frob-identifier "foo_bar" :swap-hyphen nil)| $\Longrightarrow$
764 \item @|(frob-identifier "foo_bar" :swap-case nil)| $\Longrightarrow$
766 \item @|(frob-identifier "foo_bar" :swap-case nil :swap-hyphen nil)|
767 $\Longrightarrow$ @|"foo_bar"|
771 \begin{describe}{fun}
772 {compose @<functions> @> @<function>}
773 Return the left-to-right composition zero or more @<functions>.
775 Let $f_1$, $f_2$, \ldots, $f_n$ be functions, and let $g = @|(compose $f_1$
776 $f_2$ $\cdots$ $f_n$)|$ is their composition. If $g$ is applied to
777 arguments, the effect is as follows: first, $f_1$ is applied to the
778 arguments, yielding some value; $f_2$ is applied to this value, yielding a
779 second value; and so on, until finally the value yielded by $f_n$ is
780 returned as the result of $g$. Note that this is the reverse of the usual
781 mathematician's convention, but the author finds this ordering
782 significantly easier to work with:
783 \[ g = f_n \circ \cdots \circ f_2 \circ f_1 \]
785 If any of the input functions return multiple values then \emph{all} of the
786 values are passed on to the next function in the list. (If the last
787 function returns multiple values then all of the values are returned from
790 The result of composing no functions is a function which simply returns all
791 of its arguments as values; essentially, $@|(compose)| \equiv
795 \begin{describe}{mac}{defvar-unbound @<name> @<documentation> @> @<name>}
796 Define a variable called @<name>, with a @<documentation> string.
798 The Common Lisp @|defvar| macro accepts both an initial value and a
799 doc-string as optional arguments, in that order, with the result that it's
800 not possible to define a variable and establish a documentation string for
801 it without also giving it an initial value. The @|defvar-unbound| macro,
802 on the other hand, never changes the symbol's variable-value.
805 \begin{describe}{mac}
806 {dosequence (@<var> @<sequence>
807 @[[ :start @<start> @! :end @<end> @!
808 :indexvar @<index-var> @]]) \\ \ind
810 @{ @<tag> @! @<statement> @}^*}
811 Iterate over a @<sequence>. Common Lisp has a rich collection of iteration
812 primitives, and a rich collection of functions for working with sequences,
813 but no macro for iterating over the items of a sequence.
815 First, the @<sequence> is evaluated. If @<start> and/or @<end> are
816 provided, they are also evaluated (in that order), which should produce
817 integers; @<end> may be also be nil. If not provided, or nil (in the case
818 of @<end>), @<start> and @<end> default respectively to zero and the length
819 of the @<sequence>. For each item in the sequence between the @<start> and
820 @<end> positions (i.e., each item in @|(subseq @<sequence> @<start>
821 @<end>)|, in order, the body is evaluated as an implicit @|tagbody|, with
822 @<var> bound to the item and, if provided, @<index-var> bound to the item's
823 index. It is not specified whether the @<var> and @<index-var> are
824 let-bound or mutated in each iteration.
826 Unlike other Common Lisp @|do|\dots\ forms, there is no `result' form.
829 \begin{describe}{mac}
830 {define-access-wrapper @<from> @<to>
831 @[[ :read-only @<read-only-flag> @]]}
832 Define @<from> as a function of one argument, so that @|(@<from> @<thing>)|
833 is equivalent to @|(@<to> @<thing>)|. If @<read-only-flag> is nil (the
834 default), then also define @|(setf @<from>)| so that @|(setf (@<from>
835 @<thing>) @<value>)| is equivalent to @|(setf (@<to> @<thing>) @<value>)|.
837 In a @|defstruct| form, the accessor function names are constructed based
838 on the structure name and slot names. The structure name and accessor
839 names are part of the exported interface, but the slot names ideally
840 shouldn't be. This causes a problem when the slot name which will lead to
841 the right accessor is already an external symbol in some package. You can
842 solve this problem by choosing an internal name for the symbol, and then
843 using this macro to define an accessor function with the name that you
844 want, in terms of the accessor that @|defstruct| made.
847 \begin{describe}{fun}
848 {distinguished-point-shortest-paths @<root> @<neighbours-func>
850 Calculate the shortest path from the @<root> to each node reachable from it
851 in a directed graph. The nodes of the graph can be any kind of object;
852 they will be compared using @|eql|.
854 The @<neighbours-func> should be a function which, given a node~$v$ as its
855 only argument, returns a list of cons cells @|($v'$ . $c'$)|, one for each
856 node~$v'$ adjacent to $v$, indicating the cost $c'$ of traversing the arc
859 The return value is a list of cons cells @|($c$ . $p$)|, where $p$ is list
860 of nodes, in reverse order, along a path from the @<root> to some other
861 node, and $c$ is the total cost of traversing this path. (Therefore @|(car
862 $p$)| is the destination node, and @|(car (last $p$))| is always the
865 The function runs in $O(n^2)$ time, where $n$ is the number of nodes
866 reachable from the @<root>. Currently, it uses an algorithm due to Edsger
871 \subsection{Other exported symbols}
873 \begin{describe}{sym}{int}
874 The symbol @|int| is exported by the @|sod-utilities| package, without
875 giving it any particular meaning. This is done because it's given
876 non-conflicting meanings by two different packages, and it's more
877 convenient for user code not to have to deal with an unnecessary symbol
878 conflict. Specifically, the @|sod| package wants to define it as a C type
879 specifier, see \descref{cls}{simple-c-type}; and @|optparse| wants to
880 define it as an option handler, see \descref{opt}{int}.
883 %%%--------------------------------------------------------------------------
884 \section{Option parser} \label{sec:misc.optparse}
886 Most of these symbols are defined in the @|optparse| package.
888 \begin{describe}{fun}{exit \&optional (@<code> 0) \&key :abrupt}
891 \begin{describe}{var}{*program-name*}
894 \begin{describe}{var}{*command-line*}
897 \begin{describe}{fun}{set-command-line-arguments}
900 \begin{describe}{fun}{moan @<format-string> \&rest @<format-args>}
903 \begin{describe}{fun}{die @<format-string> \&rest @<format-args>}
906 \begin{describe}{var}{*options*}
909 \begin{describe}{cls}{option}
912 \begin{describe}{fun}{optionp @<object> @> @<generalized-boolean>}
915 \begin{describe}{fun}
916 {make-option \=@<long-name> @<short-name> \+\\
917 \&optional @<arg-name> \\
918 \&key :tag :negated-tag
919 :arg-optional-p :documentation \-
924 {\dhead{fun}{opt-short-name @<option> @> @<character-or-null>}
925 \dhead{fun}{setf (opt-short-name @<option>) @<character-or-null>}
926 \dhead{fun}{opt-long-name @<option> @> @<string-or-null>}
927 \dhead{fun}{setf (opt-long-name @<option>) @<string-or-null>}
928 \dhead{fun}{opt-tag @<option> @> @<tag>}
929 \dhead{fun}{setf (opt-tag @<option>) @<tag>}
930 \dhead{fun}{opt-negated-tag @<option> @> @<tag>}
931 \dhead{fun}{setf (opt-negated-tag @<option>) @<tag>}
932 \dhead{fun}{opt-arg-name @<option> @> @<string-or-null>}
933 \dhead{fun}{setf (opt-arg-name @<option>) @<string-or-null>}
934 \dhead{fun}{opt-arg-optional-p @<option> @> @<generalized-boolean>}
935 \dhead{fun}{setf (opt-arg-optional-p @<option>) @<generalized-boolean>}
936 \dhead{fun}{opt-documentation @<option> @> @<string-or-null>}
937 \dhead{fun}{setf (opt-documentation @<option>) @<string-or-null>}}
940 \begin{describe}{cls}{option-parser}
943 \begin{describe}{fun}{option-parser-p @<object> @> @<generalized-boolean>}
946 \begin{describe}{fun}
947 {make-option-parser \&key \=:args :options :non-option :numericp \+ \\
948 :negated-numeric-p long-only-p \-
949 \nlret @<option-parser>}
953 {\dhead{fun}{op-options @<option-parser> @> @<list>}
954 \dhead{fun}{setf (op-options @<option-parser>) @<list>}
955 \dhead{fun}{op-non-option @<option-parser> @> @<action>}
956 \dhead{fun}{setf (op-non-option @<option-parser>) @<action>}
957 \dhead{fun}{op-long-only-p @<option-parser> @> @<generalized-boolean>}
958 \dhead{fun}{setf (op-long-only-p @<option-parser>) @<generalized-boolean>}
959 \dhead{fun}{op-numeric-p @<option-parser> @> @<generalized-boolean>}
960 \dhead{fun}{setf (op-numeric-p @<option-parser>) @<generalized-boolean>}
961 \dhead{fun}{op-negated-numeric-p @<option-parser> @<generalized-boolean>}
962 \dhead{fun}{setf (op-negated-numeric-p @<option-parser>) @<generalized-boolean>}
963 \dhead{fun}{op-negated-p @<option-parser> @> @<generalized-boolean>}
964 \dhead{fun}{setf (op-negated-p @<option-parser>) @<generalized-boolean>}}
967 \begin{describe}{cls}
968 {option-parse-error (error simple-condition)
969 \&key :format-control :format-arguments}
972 \begin{describe}{fun}{option-parse-error @<msg> \&optional @<args>}
975 \begin{describe}{fun}{option-parse-remainder @<option-parser>}
978 \begin{describe}{fun}{option-parse-return @<tag> \&optional @<argument>}
981 \begin{describe}{fun}{option-parse-next @<option-parser>}
984 \begin{describe}{mac}{option-parse-try @<form>^*}
987 \begin{describe}{mac}{with-unix-error-reporting () @<form>^*}
990 \begin{describe}{mac}
991 {defopthandler @<name> (@<var> @[@<arg>@]) @<lambda-list> \\ \ind
992 @[[ @<declaration>^* @! @<doc-string> @]] \\
996 \begin{describe}{fun}
997 {invoke-option-handler @<handler> @<locative> @<arg> @<arguments>}
1000 \begin{describe}{opt}{set \&optional @<value>}
1003 \begin{describe}{opt}{clear \&optional @<value>}
1006 \begin{describe}{opt}{inc \&optional @<maximum> @<step>}
1009 \begin{describe}{opt}{dec \&optional @<minimum> @<step>}
1012 \begin{describe}{opt}{read}
1015 \begin{describe}{opt}{int \&key :radix :min :max}
1018 \begin{describe}{opt}{string}
1021 \begin{describe}{opt}{keyword \&optional @<valid>}
1024 \begin{describe}{opt}{list \&optional @<handler> \&rest @<handler-args>}
1027 \begin{describe}{mac}
1028 {defoptmacro @<name> @<lambda-list> \\ \ind
1029 @[[ @<declaration>^* @! @<doc-string> @]] \\
1033 \begin{describe}{fun}{parse-option-form @<form>}
1036 \begin{describe}{mac}
1037 {options @{ \=@<string> @! \+ \\
1038 @<option-macro> @! (@<option-macro> @<macro-arg>^*) @! \\
1039 (@[[ \=@<character> @! (:short-name @<character>) @! \+ \\
1040 @<string>^* @! @<symbol> @! @<rational> @!
1041 (:long-name @<string>) @! \\
1042 (@<string> @<format-arg>^+) @!
1043 (:doc @<string> @<format-arg>^*) @! \\
1044 (:arg @<arg-name>) @! (:opt-arg @<arg-name>) @! \\
1045 @<keyword> @! (:tag @<tag>) @!
1046 (:negated-tag @<tag>) @! \\
1047 @{ (@<handler> @<var> @<handler-arg>^*) @}^*
1051 \begin{describe}{fun}
1052 {simple-usage @<option-list> \&optional @<mandatory-args> @> @<list>}
1055 \begin{describe}{fun}{show-usage @<prog> @<usage> \&optional @<stream>}
1058 \begin{describe}{fun}
1059 {show-help @<prog> @<usage> @<option-list> \&optional @<stream>}
1062 \begin{describe}{fun}{sanity-check-option-list @<option-list>}
1066 {\dhead{var}{*help*}
1067 \dhead{var}{*version*}
1068 \dhead{var}{*usage*}}
1071 \begin{describe}{fun}{do-usage \&optional @<stream>}
1074 \begin{describe}{fun}{die-usage}
1077 \begin{describe}{optmac}
1078 {help-options \&key :short-help :short-version :short-usage}
1081 \begin{describe}{fun}
1082 {define-program \&key \=:program-name \+ \\
1083 :help :version :usage :full-usage \\
1087 \begin{describe}{mac}
1088 {do-options (@[[ :parser @<option-parser> @]]) \\ \ind
1089 @{ (@{ @<case> @! (@<case>^*)@} (@[@[@<opt-var>@] @<arg-var>@])
1093 \begin{describe}{fun}{sod-frontend:augment-options @<options-list>}
1096 %%%--------------------------------------------------------------------------
1097 \section{Property sets} \label{sec:misc.pset}
1099 \begin{describe}{fun}{property-key @<name> @> @<keyword>}
1102 \begin{describe}{gf}{decode-property @<raw-value> @> @<type> @<value>}
1105 \begin{describe}{cls}{property}
1108 \begin{describe}{fun}{propertyp @<object> @> @<generalized-boolean>}
1111 \begin{describe}{fun}
1112 {make-property @<name> @<raw-value> \&key :type :location :seenp}
1116 {\dhead{fun}{p-name @<property> @> @<name>}
1117 \dhead{fun}{p-value @<property> @> @<value>}
1118 \dhead{fun}{p-type @<property> @> @<type>}
1119 \dhead{fun}{p-key @<property> @> @<symbol>}
1120 \dhead{fun}{p-seenp @<property> @> @<boolean>}
1121 \dhead{fun}{setf (p-seenp @<property>) @<boolean>}}
1124 \begin{describe}{gf}
1125 {coerce-property-value @<value> @<type> @<wanted> @> @<coerced-value>}
1128 \begin{describe}{cls}{pset}
1131 \begin{describe}{fun}{psetp @<object> @> @<generalized-boolean>}
1134 \begin{describe}{fun}{make-pset @> @<pset>}
1137 \begin{describe}{fun}{pset-get @<pset> @<key> @> @<property-or-nil>}
1140 \begin{describe}{fun}{pset-store @<pset> @<property> @> @<property>}
1143 \begin{describe}{fun}{pset-map @<func> @<pset>}
1146 \begin{describe}{mac}
1147 {with-pset-iterator (@<iter> @<pset>) @<declaration>^* @<form>^*}
1150 \begin{describe}{fun}
1151 {store-property @<pset> @<name> @<value> \&key :type :location
1155 \begin{describe}{fun}
1156 {get-property @<pset> @<name> @<type> \&optional @<default>
1157 @> @<value> @<floc-or-nil>}
1160 \begin{describe}{fun}
1161 {add-property @<pset> @<name> @<value> \&key :type :location
1165 \begin{describe}{fun}{make-property-set \&rest @<plist> @> @<pset>}
1168 \begin{describe}{gf}{property-set @<thing> @> @<pset>}
1171 \begin{describe}{fun}{check-unused-properties @<pset>}
1174 \begin{describe}{mac}
1175 {default-slot-from-property
1176 (@<instance> @<slot> @[@<slot-names>@]) \\ \ind\ind
1177 (@<pset> @<property> @<type> @[@<prop-var> @<convert-form>^*@]) \- \\
1182 \begin{describe}{fun}
1183 {parse-property @<scanner> @<pset>
1184 @> @<result> @<success-flag> @<consumed-flag>}
1187 \begin{describe}{fun}
1188 {parse-property-set @<scanner>
1189 @> @<result> @<success-flag> @<consumed-flag>}
1192 %%%--------------------------------------------------------------------------
1193 \section{Miscellaneous translator features} \label{sec:misc.misc}
1195 \begin{describe}{var}{*sod-version*}
1198 \begin{describe}{var}{*debugout-pathname*}
1201 \begin{describe}{fun}
1202 {test-module @<path> \&key :reason :clear :backtrace @> @<status>}
1205 \begin{describe}{fun}
1206 {test-parse-c-type @<string>
1207 @> t @<c-type> @<kernel> @<string> @! nil @<indicator>}
1210 \begin{describe}{fun}
1211 {test-parse-pset @<string>
1212 @> t @<pset> @! nil @<indicator>}
1215 \begin{describe}{mac}
1216 {test-parser (@<scanner> \&key :backtrace) @<parser> @<input>
1217 @> @<result> @<status> @<remainder>}
1220 \begin{describe}{fun}{exercise}
1223 \begin{describe}{fun}{sod-frontend:main}
1226 %%%----- That's all, folks --------------------------------------------------
1228 %%% Local variables:
1230 %%% TeX-master: "sod.tex"