-
Notifications
You must be signed in to change notification settings - Fork 0
/
clweb.clw
8427 lines (7193 loc) · 330 KB
/
clweb.clw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% -*-CLWEB-*-
\font\sc=cmcsc10
\font\tenec=ecrm1000
\font\eighttt=cmtt8
\def\ldq{{\tenec \char'23}} % left guillemet
\def\rdq{{\tenec \char'24}} % right guillemet
\def\pb{\.{|...|}} % program brackets
\def\v{\.{\char'174}} % vertical bar in typewriter font
\def\bull{\vrule height .9ex width .8ex depth -.1ex } % square bullet
\def\WEB{{\tt WEB}}
\def\CWEB{{\tt CWEB}}
\def\CLWEB{{\tt CLWEB}}
\def\EOF{{\sc eof}}
\def\progname{{\eighttt CLWEB}}
\def\etc.{{\it \char`&c.\spacefactor1000}}
\def\<#1>{\leavevmode\hbox{$\mkern-2mu\langle${\it #1\/}$\rangle$}}
\def\api{{\sc api}}
\def\ansi{{\sc ansi}}
\def\asdf{{\sc asdf}}
\def\cltl{{\sc cl{\rm t}l}-2} % Common Lisp, the Language (2nd ed.)
\def\files#1{\left\{\,\vcenter{\normalbaselines
\ialign{&##\hfil\crcr#1\crcr}}\right.}
\def\mapfiles#1\to#2{\enspace:\enspace$#1\to\files{#2}$}
% macros for non-centered displays from manmac.tex
\outer\def\begindisplay{\obeylines\startdisplay}
{\obeylines\gdef\startdisplay#1
{\catcode`\^^M=5$$#1\halign\bgroup\indent##\hfil&&\qquad##\hfil\cr}}
\outer\def\enddisplay{\crcr\egroup$$}
@*Introduction. This is \CLWEB, a literate programming system for
Common Lisp by Alex Plotnick. It~is modeled after the \CWEB\ system
by Silvio Levy and Donald E.~Knuth, which was in turn adapted from
Knuth's original \WEB\ system. It shares with those systems not only
their underlying philosophy, but also most of their syntax.
Readers unfamiliar with either of them---or with literate programming
in general---should consult the \CWEB\ manual or Knuth's {\it \ldq Literate
Programming\/\rdq} ({\sc csli}:~1992).
@^Knuth, Donald Ervin@>
@^Levy, Silvio@>
@^Plotnick, Alexander F.@>
@^\WEB@>
@^\CWEB@>
@ A \CLWEB\ source file, or {\it web}, consists of a mixture of \TeX, Lisp,
and~\WEB\ control codes. Which is primary depends on your point of view;
the \CWEB\ manual says that ``[w]riting \CWEB\ programs is something like
writing \TeX\ documents, but with an additional `C mode' that is added to
\TeX's horizontal mode, vertical mode, and math mode.'' The same applies,
{\it mutatis mutandis}, to the current system, but one might also think
of a web as a Lisp program with documentation blocks and special directives
sprinkled throughout, or as a completely separate language containing
blocks that happen to have the syntax (more or less) of \TeX\ and~Lisp.
For the purposes of understanding the implementation, this last view
is perhaps the most useful, since the control codes determine which syntax
to use in reading the material that follows.
@^\WEB@>
@^\CWEB@>
The syntax of the \CLWEB\ control codes themselves is similar to that
of dispatching reader macro characters in Lisp: they all begin with
`\.{@@}$x$', where~$x$ is a single character that selects the control code.
Most of the \CLWEB\ control codes are quite similar to the ones used in
\CWEB; see the \CWEB\ manual for detailed descriptions of the individual
codes.
@ A literate programming system provides two primary operations:
{\it tangling\/} and {\it weaving}. The tangler prepares a web for
compilation and evaluation by a machine, and the weaver pretty-prints it
for reading by a human. These operations reflect the two uses of a literate
program, and the two audiences by whom it is to be read: the computer on
the one hand, and the human programmers that must understand and maintain
it on the other.
Our tangler has two main interface functions: |clweb:tangle-file| and
|clweb:load-web|. The first is analogous to |compile-file|: given a file
containing \CLWEB\ source, it produces an output file that can be loaded
into a Lisp image with |load|. The function |clweb:load-web| is analogous
to |load|: it loads a web into the Lisp environment without first tangling
and compiling~it. (There's also |clweb:load-sections-from-temp-file|,
which is a special-purpose routine designed to be used in conjunction with
an editor such as Emacs to provide incremental redefinition of sections;
the user should generally never call it directly.) Tangling can also produce
and compile a {\it tests file}, which contains any tests defined alongside
the main program as part of the web.
The weaver has a single entry point: |clweb:weave| takes a web as input
and generates a \TeX\ file containing a pretty-printed version of it,
along with a complete index of the classes, functions, variables,~\etc.
defined by the program.
@ We begin by defining a package for the \CLWEB\ system. In addition
to the top-level tangling and weaving functions, we export a handful
of public utility functions, some global variables that control operations
of the tangler and weaver, and condition classes for errors and warnings
that may be signaled while processing a~web. There's also the pair of symbols
|clweb-file| and |weave-op|, which are not used for anything in this program,
but are used to name \asdf\ operations in the optional companion web
|asdf-operations.clw|.
@l
@e
(eval-when (:compile-toplevel :load-toplevel :execute)
#+sbcl (require "SB-CLTL2"))
@e
(defpackage "CLWEB"
(:documentation "A literate programming system for Common Lisp.")
(:use "COMMON-LISP")
(:export "TANGLE-FILE"
"LOAD-WEB"
"WEAVE"
"LOAD-SECTIONS-FROM-TEMP-FILE"
"INPUT-FILE-PATHNAME"
"LISP-FILE-PATHNAME"
"TEX-FILE-PATHNAME"
"INDEX-FILE-PATHNAME"
"SECTIONS-FILE-PATHNAME"
"FASL-FILE-PATHNAME"
"TESTS-FILE-PATHNAME"
"TANGLE-FILE-PATHNAMES"
"WEAVE-PATHNAMES"
"*WEB-PATHNAME-DEFAULTS*"
"*LISP-PATHNAME-DEFAULTS*"
"*TEX-PATHNAME-DEFAULTS*"
"*INDEX-PATHNAME-DEFAULTS*"
"*SECTIONS-PATHNAME-DEFAULTS*"
"*TESTS-FILE-PATHNAME-FUNCTION*"
"*COMPILE-TESTS-FILE*"
"*TANGLE-FILE-PATHNAME*"
"*TANGLE-FILE-TRUENAME*"
"*WEAVE-PRINT*"
"*WEAVE-VERBOSE*"
"*INDEX-LEXICAL-VARIABLES*"
"AMBIGUOUS-PREFIX-ERROR"
"SECTION-NAME-CONTEXT-ERROR"
"SECTION-NAME-USE-ERROR"
"SECTION-NAME-DEFINITION-ERROR"
"UNUSED-NAMED-SECTION-WARNING"
"CLWEB-FILE" "WEAVE-OP") ; see |asdf-operations|
#+(or sbcl ccl allegro)
(:import-from #+sbcl "SB-CLTL2" #+ccl "CCL" #+allegro "SYS"
#-allegro "FUNCTION-INFORMATION"
#-allegro "VARIABLE-INFORMATION"
#-allegro "PARSE-MACRO"
"AUGMENT-ENVIRONMENT")
#+sbcl
(:import-from "SB-INT" "NAMED-LAMBDA"))
@e
(in-package "CLWEB")
@t*Test suite. The test suite for this system uses Richard Waters's
{\sc rt} library, a copy of which is included in the distribution. For more
information on {\sc rt}, see Richard C.~Waters, ``Supporting the Regression
Testing of Lisp Programs,'' {\it SIGPLAN Lisp Pointers\/}~4, no.~2 (1991):
47--53.
The \CLWEB\ package does not |(use-package "RT")| because the main program
does not depend on the test suite. To avoid playing symbol games, we will
define a trivial wrapper around {\sc rt}'s |deftest| macro, but not import
any of its symbols.
@l
(in-package "CLWEB")
(defmacro deftest (name form &rest values)
`(rt:deftest ,name ,form ,@values))
@ We'll define our global variables and condition classes as we need them,
but we'd like them to appear near the top of the tangled output.
@l
@<Global variables@>
@<Condition classes@>
@ Next we'll define some utility functions and macros that we'll use
throughout the program. Our first function simply treats its argument
as a list designator.
@l
(defun ensure-list (object)
(if (listp object)
object
(list object)))
@ This auxiliary function---shamelessly stolen from \cltl, Appendix~C---is
like |mapcar| but has two extra purposes: (1)~it handles dotted lists; (2)~it
tries to make the result share with the argument |x| as much as possible.
@l
(defun maptree (fn x)
(if (atom x)
(funcall fn x)
(let ((a (funcall fn (car x)))
(d (maptree fn (cdr x))))
(if (and (eql a (car x)) (eql d (cdr x)))
x
(cons a d)))))
@ And here's one taken from {\sc pcl}: |mapappend| is like |mapcar| except
that the results are appended together.
@l
(defun mapappend (fn &rest args)
(if (some #'null args)
()
(append (apply fn (mapcar #'car args))
(apply #'mapappend fn (mapcar #'cdr args)))))
@ When we're accumulating text, we usually won't want to bother with
empty strings. In such cases we'll use the following macro, which is
like |push| but does nothing if the new object is an empty string or~|nil|.
@l
(defmacro maybe-push (obj place &aux (temp (gensym)))
`(let ((,temp ,obj))
(when (if (stringp ,temp) (plusp (length ,temp)) ,temp)
(push ,temp ,place))))
@t@l
(deftest maybe-push
(let ((list '()))
(maybe-push 'a list)
(maybe-push nil list)
(maybe-push 'b list)
(maybe-push "" list)
(maybe-push "foo" list)
(nreverse list))
(a b "foo"))
@ Our last utility macro goes by many names. Paul Graham calls it
|with-gensyms|, but we'll use the more descriptive |with-unique-names|.
It captures a common idiom used in writing macros: given a list of symbols,
it executes the body in an environment augmented with bindings for each
of those symbols to a fresh, uninterned symbol.
@l
(eval-when (:compile-toplevel :load-toplevel :execute)
(defmacro with-unique-names (symbols &body body)
`(let ,(loop for symbol in symbols ;
collect `(,symbol (copy-symbol ',symbol)))
,@body)))
@*File names. In \TeX, `\.{\\input foo}' reads tokens from the file named
`\.{foo.tex}'. In Common Lisp, |(compile-file "foo")| compiles the file named
`\.{foo.lisp}' (or an implementation-dependent variant~thereof). Those are
just defaults, of course; `\.{\\input foo.bar}' causes \TeX\ to read tokens
from `\.{foo.bar}', and similarly |(compile-file "foo.bar")| compiles Lisp
forms from `\.{foo.bar}'. In both systems, the type components of input
filenames default in reasonable ways when omitted, but types that the user
supplies are respected.
Output filenames are defaulted from the input filenames. For instance,
`\.{\\input foo}' will cause \TeX\ to write a device-independent file
named `\.{foo.dvi}' as output, along with `\.{foo.log}'. We will say
that \TeX\ implements the {\it default filename mapping}
\begindisplay
\.{\\input foo}\mapfiles
\.{foo.tex}\to{\.{foo.dvi}\cr
\.{foo.log}\cr}
\enddisplay
Similarly, compiling a Lisp source file named `\.{foo.lisp}' produces
by default an output file named `\.{foo.fasl}' (or an implementation-%
dependent variant thereof). So the Common Lisp file compiler implements
the default filename mapping
\begindisplay
|(compile-file "foo")|\mapfiles\.{foo.lisp}\to{\.{foo.fasl}\cr}
\enddisplay
(or an implementation-dependent variant thereof).
\CLWEB\ implements the default filename mappings
\begindisplay
|(tangle-file "foo")|\mapfiles
\.{foo.clw}\to{\.{foo.fasl}\cr
\.{foo.lisp}\cr
\.{foo-tests.fasl}\cr
\.{foo-tests.lisp}\cr}
\enddisplay
and
\begindisplay
|(weave "foo")|\mapfiles
\.{foo.clw}\to{\.{foo.tex}\cr
\.{foo.idx}\cr
\.{foo.scn}\cr}
\enddisplay
Filename defaulting is performed by the pathname manipulation functions
that follow. We define a defaulting function for each type of file that
\CLWEB\ operates on, and one `main' routine for each of |tangle-file|
and |weave| (|tangle-file-pathnames| and |weave-pathnames|, respectively)
that returns a fully defaulted set of filenames that the corresponding
operation will write into. Thanks to Richard M.~Kreuter for his careful
analysis of the requirements for these routines.
@^Kreuter, Richard M.@>
@ The type componenents of the various input and output files take their
defaults from the following global variables. We use a tiny helper function
to make them so that their version and host parameters are set correctly.
@<Global variables@>=
(defvar *physical-pathname-host*
#+sbcl ""
#-sbcl :unspecific)
(defun make-default-pathname (type)
(make-pathname :type type
:host *physical-pathname-host*
:version :newest))
(defvar *web-pathname-defaults* (make-default-pathname "clw"))
(defvar *lisp-pathname-defaults* (make-default-pathname "lisp"))
(defvar *tex-pathname-defaults* (make-default-pathname "tex"))
(defvar *index-pathname-defaults* (make-default-pathname "idx"))
(defvar *sections-pathname-defaults* (make-default-pathname "scn"))
@t We'll shortly be defining a number of tests for pathname-generating
functions. To maximize portability, we will mention only logical pathnames
on the host `\.{CLWEB-TEST}', but will compare only physical pathnames.
@l
@e
(eval-when (:compile-toplevel :load-toplevel :execute)
(setf (logical-pathname-translations "clweb-test") '(("**;*.*.*" ""))))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun physical-pathname-or-null (pathspec)
(and pathspec (translate-logical-pathname pathspec))))
@t The macro |with-pathname-test-variables| ensures that tests are independent
of the current pathname defaults.
@l
(defmacro with-pathname-test-variables (&body body &aux (fix-lpn (gensym)))
`(flet ((,fix-lpn (lpn)
(make-pathname :defaults lpn :directory nil :name nil)))
(progv '(*web-pathname-defaults*
*lisp-pathname-defaults*
*tex-pathname-defaults*
*index-pathname-defaults*
*sections-pathname-defaults*)
(mapcar #',fix-lpn
'(#P"clweb-test:foo.clw.newest"
#P"clweb-test:foo.lisp.newest"
#P"clweb-test:foo.tex.newest"
#P"clweb-test:foo.idx.newest"
#P"clweb-test:foo.scn.newest"))
,@body)))
@t And here's the pathname-test generating macro. It wraps |deftest|,
coercing the returned and expected values to physical pathnames.
@l
(defmacro define-pathname-test (name form &rest expected-values)
(with-unique-names (values)
`(deftest ,name
(with-pathname-test-variables
(let* ((*default-pathname-defaults* #P"clweb-test:")
(,values (multiple-value-list ,form)))
(values-list (mapcar #'physical-pathname-or-null ,values))))
,@(mapcar #'physical-pathname-or-null expected-values))))
@ All of the filename defaulting code eventually calls down to either
|input-file-pathname| or |output-file-pathname|. Both return a fully
defaulted, untranslated pathname.
@l
(defun input-file-pathname (input-file)
(let ((defaults (merge-pathnames *web-pathname-defaults*)))
(merge-pathnames input-file defaults)))
(defun output-file-pathname (input-file &key output-file ;
(defaults *default-pathname-defaults*))
(assert (or (and output-file (pathname-type output-file))
(pathname-type defaults))
(defaults)
"Missing default output file type.")
(let* ((input-file (input-file-pathname input-file))
(defaults (merge-pathnames ;
(make-pathname :type nil ;
:version :newest ;
:defaults input-file) ;
defaults)))
(if output-file
(merge-pathnames output-file defaults)
defaults)))
@t@l
(define-pathname-test input-file-pathname
(values (input-file-pathname #P"clweb-test:foo")
(input-file-pathname #P"clweb-test:foo.in"))
#P"clweb-test:foo.clw.newest"
#P"clweb-test:foo.in.newest")
(define-pathname-test output-file-pathname
(values (ignore-errors (output-file-pathname #P"clweb-test:foo"))
(output-file-pathname #P"clweb-test:foo"
:defaults *lisp-pathname-defaults*)
(output-file-pathname #P"clweb-test:foo"
:output-file #P"clweb-test:bar"
:defaults *lisp-pathname-defaults*)
(output-file-pathname #P"clweb-test:foo"
:output-file #P"clweb-test:bar.baz"
:defaults *lisp-pathname-defaults*))
nil
#P"clweb-test:foo.lisp.newest"
#P"clweb-test:bar.lisp.newest"
#P"clweb-test:bar.baz.newest")
@t The default version of an output file is |:newest|. But if a version
is supplied on the |output-file|, we should respect that.
@l
(deftest (output-file-pathname version :newest)
(pathname-version ;
(output-file-pathname #P"clweb-test:foo.clw.1"
:defaults *lisp-pathname-defaults*))
#-allegro :newest
#+allegro :unspecific)
(deftest (output-file-pathname version output-file)
(pathname-version ;
(output-file-pathname #P"clweb-test:foo.clw.1"
:output-file #P"clweb-test:foo.clw.2"
:defaults *lisp-pathname-defaults*))
#-allegro 2
#+allegro :unspecific)
@ Now we can define pathname defaulting routines for each of the output
file types produced by \CLWEB. Most of them are trivial wrappers around
|output-file-pathname| that simply provide the appropriate defaults.
@l
(macrolet ((defdefaults (name (input) defaults)
(with-unique-names (args)
`(defun ,name (,input &rest ,args)
(apply #'output-file-pathname ,input ;
:defaults ,defaults ;
,args)))))
(defdefaults lisp-file-pathname (input-file) *lisp-pathname-defaults*)
(defdefaults tex-file-pathname (input-file) *tex-pathname-defaults*)
(defdefaults index-file-pathname (output-file) *index-pathname-defaults*)
(defdefaults sections-file-pathname (index-file) *sections-pathname-defaults*))
@t@l
(define-pathname-test lisp-file-pathname
(lisp-file-pathname #P"clweb-test:foo")
#P"clweb-test:foo.lisp.newest")
(define-pathname-test tex-file-pathname
(tex-file-pathname #P"clweb-test:foo")
#P"clweb-test:foo.tex.newest")
(define-pathname-test index-file-pathname
(index-file-pathname #P"clweb-test:foo")
#P"clweb-test:foo.idx.newest")
(define-pathname-test sections-file-pathname
(sections-file-pathname #P"clweb-test:foo")
#P"clweb-test:foo.scn.newest")
@ The tangler's primary output is a compiled Lisp file whose type is
implementation-defined. So we must ask the file compiler where it would
write its output if given a defaulted input filename.
@l
(defun fasl-file-pathname (input-file &rest args)
(apply #'compile-file-pathname (lisp-file-pathname input-file) ;
:allow-other-keys t ;
args))
@t@l
(define-pathname-test fasl-file-pathname
(fasl-file-pathname #P"clweb-test:foo")
#.(compile-file-pathname #P"clweb-test:foo.lisp.newest"))
@ Both |tangle-file| and |weave| take a |tests-file| argument with
especially hairy defaulting behavior. If it's supplied as~|nil|, no tests
file will be written. If it's supplied and non-|nil|, it is defaulted
from the output filename. And if it's unsupplied, the function stored
in |*tests-file-pathname-function*| is called to construct a~new filename
from the input and output filenames.
@l
(defun tests-file-pathname (input-file &key output-file ;
(tests-file nil tests-file-supplied) ;
&allow-other-keys)
(if tests-file
(output-file-pathname input-file ;
:output-file tests-file ;
:defaults output-file)
(unless tests-file-supplied ;
(funcall *tests-file-pathname-function* input-file output-file))))
@ The default tests filename function appends the string \.{"-tests"}
to the output file's name and defaults everything else.
@<Global variables@>=
(defvar *tests-file-pathname-function*
(lambda (input-file output-file &aux
(output-file (pathname output-file))
(tests-file (make-pathname :name (concatenate 'string ;
(pathname-name output-file) ;
"-tests")
:version :newest
:defaults output-file)))
(output-file-pathname input-file
:output-file tests-file
:defaults output-file)))
@t@l
(define-pathname-test (tests-file-pathname 1)
(tests-file-pathname #P"clweb-test:foo.clw"
:output-file #P"clweb-test:foo.lisp"
:tests-file #P"clweb-test:bar")
#P"clweb-test:bar.lisp.newest")
(define-pathname-test (tests-file-pathname 2)
(tests-file-pathname #P"clweb-test:foo.clw"
:output-file #P"clweb-test:foo"
:tests-file nil)
nil)
(define-pathname-test (tests-file-pathname 3)
(tests-file-pathname #P"clweb-test:foo.clw"
:output-file #P"clweb-test:a;b;bar.tex")
#P"clweb-test:a;b;bar-tests.tex.newest")
@ And now we come to our final filename defaulting functions:
|tangle-file-pathnames| returns the defaulted output filenames for
|tangle-file|, and |weave-pathnames| returns the defaulted output files
for |weave|. The conditionals are for supplied but null output filename
arguments, which disable writing of the corresponding outputs; e.g.,
the tests or index files.
@l
(defun tangle-file-pathnames (input-file &rest args &key ;
output-file tests-file &allow-other-keys)
"Compute and return the defaulted names of the tangler output files."
(declare (ignorable output-file tests-file))
(let* ((input-file (input-file-pathname input-file))
(output-file (apply #'fasl-file-pathname input-file args))
(lisp-file (lisp-file-pathname output-file))
(tests-file (apply #'tests-file-pathname input-file ;
:output-file lisp-file ;
args))
(tests-output-file (and tests-file (fasl-file-pathname tests-file))))
(values output-file
lisp-file
tests-output-file
tests-file)))
(defun weave-pathnames (input-file &key output-file ;
(index-file nil index-file-supplied) ;
&allow-other-keys)
"Compute and return the defaulted names of the weaver output files."
(let* ((input-file (input-file-pathname input-file))
(output-file (tex-file-pathname input-file :output-file output-file))
(index-file (unless (and index-file-supplied (not index-file))
(index-file-pathname output-file ;
:output-file index-file)))
(sections-file (and index-file (sections-file-pathname index-file))))
(values output-file
index-file
sections-file)))
@t@l
(define-pathname-test (tangle-file-pathnames 1)
(tangle-file-pathnames #P"clweb-test:foo")
#.(compile-file-pathname #P"clweb-test:foo.lisp.newest")
#P"clweb-test:foo.lisp.newest"
#.(compile-file-pathname #P"clweb-test:foo-tests.lisp.newest")
#P"clweb-test:foo-tests.lisp.newest")
(define-pathname-test (tangle-file-pathnames 2)
(let* ((input-file #P"clweb-test:foo")
(fasl-type (pathname-type (compile-file-pathname input-file)))
(output-file (make-pathname :type fasl-type ;
:defaults #P"clweb-test:a;b;bar")))
(tangle-file-pathnames input-file :output-file output-file))
#.(compile-file-pathname #P"clweb-test:a;b;bar.lisp.newest")
#P"clweb-test:a;b;bar.lisp.newest"
#.(compile-file-pathname #P"clweb-test:a;b;bar-tests.lisp.newest")
#P"clweb-test:a;b;bar-tests.lisp.newest")
(define-pathname-test (tangle-file-pathnames 3)
(tangle-file-pathnames #P"clweb-test:foo" :tests-file nil)
#.(compile-file-pathname #P"clweb-test:foo.lisp.newest")
#P"clweb-test:foo.lisp.newest"
nil
nil)
@t@l
(define-pathname-test (weave-pathnames foo)
(weave-pathnames #P"clweb-test:foo")
#P"clweb-test:foo.tex.newest"
#P"clweb-test:foo.idx.newest"
#P"clweb-test:foo.scn.newest")
(define-pathname-test (weave-pathnames foo.bar)
(weave-pathnames #P"clweb-test:foo.bar")
#P"clweb-test:foo.tex.newest"
#P"clweb-test:foo.idx.newest"
#P"clweb-test:foo.scn.newest")
(define-pathname-test (weave-pathnames foo :output-file t)
(weave-pathnames #P"clweb-test:foo" ;
:output-file #P"clweb-test:a;bar.baz.newest")
#P"clweb-test:a;bar.baz.newest"
#P"clweb-test:a;bar.idx.newest"
#P"clweb-test:a;bar.scn.newest")
(define-pathname-test (weave-pathnames foo :output-file bar)
(weave-pathnames #P"clweb-test:foo" ;
:output-file #P"clweb-test:bar")
#P"clweb-test:bar.tex.newest"
#P"clweb-test:bar.idx.newest"
#P"clweb-test:bar.scn.newest")
(deftest (weave-pathnames foo :output-file bar.t)
(pathname-type
(weave-pathnames #P"clweb-test:foo" ;
:output-file #P"clweb-test:bar.t")
:case :common)
"T")
#-allegro
(deftest (weave-pathnames foo :output-file (:type :unspecific))
(pathname-type
(weave-pathnames #P"clweb-test:foo" ;
:output-file (make-pathname :host "clweb-test" ;
:type :unspecific)))
:unspecific)
(define-pathname-test (weave-pathnames foo :index-file nil)
(weave-pathnames #P"clweb-test:foo" :index-file nil)
#P"clweb-test:foo.tex.newest"
nil
nil)
(define-pathname-test (weave-pathnames foo :index-file bar)
(weave-pathnames #P"clweb-test:foo" :index-file #P"clweb-test:bar")
#P"clweb-test:foo.tex.newest"
#P"clweb-test:bar.idx.newest"
#P"clweb-test:bar.scn.newest")
(define-pathname-test (weave-pathnames foo :output-file t :index-file t)
(weave-pathnames #P"clweb-test:foo"
:output-file #P"clweb-test:a;bar.tex.newest"
:index-file #P"clweb-test:b;index.idx.newest")
#P"clweb-test:a;bar.tex.newest"
#P"clweb-test:b;index.idx.newest"
#P"clweb-test:b;index.scn.newest")
@*Sections. The fundamental unit of a web is the {\it section}, which may
be either {\it named\/} or~{\it unnamed}. Named sections are conceptually
very much like parameterless macros, except that they can be defined
piecemeal. The tangler replaces references to a named section with all of
the code defined in all of the sections with that name. (This is where the
name `tangling' comes from: code may be broken up and presented in whatever
order suits the expository purposes of the author, but is then spliced back
together into the order that the programming language expects.) Unnamed
sections, on the other hand, are evaluated or written out to a file for
compilation in the order in which they appear in the source file.
Every section is assigned a number, which the weaver uses for generating
cross-references. The numbers themselves never appear in the source file:
they are generated automatically by the system.
Aside from a name, a section may have a {\it commentary part}, optionally
followed by a {\it code part}. (We don't support the `middle' part of a
section that \WEB\ and \CWEB's sections have, since the kinds of definitions
that can appear there are essentially irrelevant in Lisp.) The commentary
part consists of \TeX\ material that describes the section; the weaver
copies it (nearly) verbatim into the \TeX\ output file, and the tangler
ignores it. The code part contains Lisp forms and named section references;
the tangler will eventually evaluate or compile those forms, while the
weaver pretty-prints them to the \TeX\ output file.
@^\WEB@>
@^\CWEB@>
Finally, the section keeps track of where it was read, in a slot
called |source-location|.
Three control codes begin a section: \.{@@\ }, \.{@@*}, and \.{@@t}.
Most sections will begin with \.{@@\ }: these are `regular' sections,
which might be named or unnamed.
@l
(defclass section ()
((name :accessor section-name :initarg :name)
(number :accessor section-number)
(commentary :accessor section-commentary :initarg :commentary)
(code :accessor section-code :initarg :code)
(source-location :reader section-source-location :initarg :source-location))
(:default-initargs :name nil :commentary nil :code nil :source-location nil))
@t It's occasionally useful in testing to be able to use numbers as if
they were sections, at least for numbering purposes.
@l
(defmethod section-number ((section integer)) section)
@ Sections introduced with \.{@@*} (`starred' sections) begin a new group
of sections, and get some special formatting during weaving. The control
code \.{@@*} should be immediately followed by a title for this group,
terminated by a period. That title will appear as a run-in heading at the
beginning of the section, as a running head on all subsequent pages until
the next starred section, and in the table of contents.
Starred sections have a numeric parameter associated with them called
`depth'. The depth of a starred section affects how it is formatted in the
woven output: the section name is indented according to its depth in the
table of contents, and sections with small depths (i.e., close to 0) force
a page break.
The tangler makes no distinction at all between sections with stars and
ones with none upon thars.
@l
(defclass starred-section (section)
((depth :reader section-depth :initarg :depth))
(:default-initargs :depth 0))
(defun starred-section-p (object) (typep object 'starred-section))
@ Non-starred sections have no depth, but it shouldn't be an error to ask.
@l
(defmethod section-depth ((section section)) nil)
@ Sections that begin with \.{@@t} are {\it test sections}. They are used to
include test cases alongside the program, and are treated specially by both
the tangler and the weaver. The tangler writes them out to a separate
file, and the weaver may elide them entirely.
Test sections are automatically associated with the last non-test section
defined, on the assumption that tests will be defined immediately after the
code they're designed to exercise.
@l
(defclass test-section (section)
((test-for :accessor test-for-section :initform nil)))
(defclass starred-test-section (test-section starred-section) ())
(defun test-section-p (object) (typep object 'test-section))
(defmethod initialize-instance :after ((section test-section) &key)
(when (> (fill-pointer *sections*) 0)
(setf (test-for-section section) ;
(elt *sections* (1- (fill-pointer *sections*))))))
@ There can also be \TeX\ text preceding the start of the first section (i.e.,
before the first \.{@@\ } or \.{@@*}), called {\it limbo text}. Limbo text
is generally used to define document-specific formatting macros, set up
fonts, \etc. The weaver passes it through virtually verbatim to the output
file (only replacing occurrences of `\.{@@@@}' with~`\.{@@}'), and the
tangler ignores it completely.
A single instance of the class |limbo-section| contains the limbo text in
its |commentary| slot; it will never have a code part.
@l
(defclass limbo-section (section) ())
@ Whenever we create a non-test section, we store it in the global
|*sections*| vector and set its number to its index therein. This means
that section objects won't be collected by the garbage collector even after
the tangling or weaving has completed, but there's a good reason: keeping
them around allows incremental redefinition of a web, which is important
for interactive development.
We'll also keep the global variable |*current-section*| pointing to the
last section (test or not) created.
@<Global variables@>=
(defvar *sections* (make-array 128 :adjustable t :fill-pointer 0))
(defvar *current-section* nil)
@ @<Initialize global variables@>=
(setf (fill-pointer *sections*) 0)
(setf *current-section* nil)
@ Here's where section numbers are assigned. We use a generic function
for |push-section| so that we can override it for test sections.
@l
(defgeneric push-section (section))
(defmethod push-section ((section section))
(setf (section-number section) (vector-push-extend section *sections*))
section)
(defmethod initialize-instance :after ((section section) &key)
(setq *current-section* (push-section section)))
@t Ensure that the |*current-section*| variable is updated after a new
section instance is made.
@l
(deftest current-section
(let ((*sections* (make-array 1 :fill-pointer 0)))
(eql (make-instance 'section) *current-section*))
t)
@ Test sections aren't stored in the |*sections*| vector; we keep them
separate so that they won't interfere with the numbering of the other
sections.
@<Global variables@>=
(defvar *test-sections* (make-array 128 :adjustable t :fill-pointer 0))
@ @<Initialize global variables@>=
(setf (fill-pointer *test-sections*) 0)
@ @l
(defmethod push-section ((section test-section))
(let ((*sections* *test-sections*))
(call-next-method)))
@ The test sections all get woven to a separate output file, and we'll
need a copy of the limbo text there, too.
@l
(defmethod push-section :after ((section limbo-section))
(vector-push-extend section *test-sections*))
@t To make testing with sections a little easier, we'll define a macro,
|with-temporary-sections|, that will help ensure that we don't accidentally
clobber any real sections. It executes |body| in an environment where
|*sections*| and |*named-sections*| have been rebound and augmented with
the new sections specified by the |sections| argument. That argument should
be a list of {\it section specifiers}, which are lists beginning with one of
the keywords |:section|, |:starred-section|, or~|:limbo|, followed by keyword
arguments that will be used to initialize new |section| instances; e.g.,
|:name| and~|:code|.
@l
(defmacro with-temporary-sections (sections &body body)
(with-unique-names (spec section name)
`(let ((*sections* (make-array 16 :adjustable t :fill-pointer 0))
(*test-sections* (make-array 16 :adjustable t :fill-pointer 0))
(*named-sections* nil))
(dolist (,spec ,sections)
(let* ((,section (apply #'make-instance
(ecase (pop ,spec)
(:section 'section)
(:starred-section 'starred-section)
(:limbo 'limbo-section))
,spec))
(,name (section-name ,section)))
(when ,name
(push ,section (named-section-sections (find-section ,name))))))
,@body)))
@ We keep named sections in a binary search tree whose keys are section
names and whose values are code forms; the tangler will replace references
to those names with the associated code. We use a tree instead of, say,
a hash table so that we can support abbreviations (see below).
@l
(defclass binary-search-tree ()
((key :accessor node-key :initarg :key)
(left-child :accessor left-child :initarg :left)
(right-child :accessor right-child :initarg :right))
(:default-initargs :left nil :right nil))
@ The primary interface to the {\sc bst} is the following routine, which
attempts to locate the node with key |item| in the tree rooted at |root|.
If it is not already present and the |insert-if-not-found| argument
is true, a new node is created with that key and added to the tree. The
arguments |predicate| and |test| should be designators for functions
of two arguments, both of which will be node keys; |predicate| should
return true iff its first argument precedes its second in the total
ordering used for the tree, and |test| should return true iff the two
keys are to be considered equivalent.
Two values are returned: the node with key |item| (or |nil| if no such node
was found and |insert-if-not-found| is false), and a boolean representing
whether or not the node was already in the tree.
@l
(defgeneric find-or-insert (item root &key predicate test insert-if-not-found))
(defmethod find-or-insert (item (root binary-search-tree) &key
(predicate #'<) (test #'eql)
(insert-if-not-found t))
(flet ((lessp (item node) (funcall predicate item (node-key node)))
(samep (item node) (funcall test item (node-key node))))
(do ((parent nil node)
(node root (if (lessp item node)
(left-child node)
(right-child node))))
((or (null node) (samep item node))
(if node
(values node t)
(if insert-if-not-found
@<Insert a new node with key |item| and return it@>
(values nil nil)))))))
@ @<Insert a new node...@>=
(let ((node (make-instance (class-of root) :key item)))
(when parent
(if (lessp item parent)
(setf (left-child parent) node)
(setf (right-child parent) node)))
(values node nil))
@ Besides searching, probably the most common operation on a {\sc bst} is
to traverse it in-order, applying some function to each node.
@l
(defgeneric map-bst (function tree))
(defmethod map-bst (function (tree null))
(declare (ignore function)))
(defmethod map-bst (function (tree binary-search-tree))
(map-bst function (left-child tree))
(funcall function tree)
(map-bst function (right-child tree)))
@t Make sure our binary search trees are working as advertised.
@l
(deftest (bst simple)
(let ((tree (make-instance 'binary-search-tree :key 0)))
(find-or-insert -1 tree)
(find-or-insert 1 tree)
(values (node-key tree)
(node-key (left-child tree))
(node-key (right-child tree))))
0 -1 1)
(deftest (bst random)
(let ((tree (make-instance 'binary-search-tree :key 0))
(numbers (cons 0 (loop with limit = 1000
for i from 1 to limit
collect (random limit)))))
(dolist (n numbers)
(find-or-insert n tree))
(let ((keys '()))
(flet ((push-key (node)
(push (node-key node) keys)))
(map-bst #'push-key tree)
(equal (nreverse keys)
(remove-duplicates (sort numbers #'<))))))
t)
(deftest (bst find-no-insert)
(let ((tree (make-instance 'binary-search-tree :key 0)))
(find-or-insert -1 tree :insert-if-not-found nil))
nil nil)
@ As mentioned above, named sections can be defined piecemeal, with the
code spread out over several sections in the \CLWEB\ source. We might think
of a named section as a sort of `virtual' section, which consists of a
name, the combined code parts of all of the physical sections with that
name, and the number of the first such section.
And that's what we store in the {\sc bst}: nodes that look like sections,
inasmuch as they have specialized |section-name|, |section-code|, and
|section-number| methods, but are not actually instances of the class
|section|. The commentary and code are stored in the |section| instances
that comprise a given named section: references to those sections are
stored in the |sections| slot.
The weaver uses the last two slots, |used-by| and~|cited-by|, to generate
cross-references. They will be populated during reading with lists of all
the sections that reference this named section.
@l
(defclass named-section (binary-search-tree)
((key :accessor section-name :initarg :name)
(sections :accessor named-section-sections :initform '())
(used-by :accessor used-by :initform '())
(cited-by :accessor cited-by :initform '())))
(defmethod named-section-sections :around ((section named-section))
(sort (copy-list (call-next-method)) #'< :key #'section-number))
(defmethod section-code ((section named-section))
(mapappend #'section-code (named-section-sections section)))
(defmethod section-number ((section named-section))
(let ((sections (named-section-sections section)))
(when (null sections)
(error 'undefined-named-section-error
:format-control "Undefined named section <~A>."
:format-arguments (list (section-name section))))
(section-number (first sections))))
@t@l
(deftest named-section-number/code
(with-temporary-sections
'((:section :name "foo" :code (1))
(:section :name "foo" :code (2))
(:section :name "foo" :code (3)))
(let ((section (find-section "foo")))
(values (section-code section)
(section-number section))))
(1 2 3)