raz / commit f8fb7674

commit
f8fb7674b9eb43d0d743959e5a149f8c9321d1aa
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
e38e5c53
raz-lib(3), the library, made from its sources

Doc man writes mdoc: each module under a heading of its name, its own
comment, then each exported declaration with its comment, those with
none sharing a display; a comment's lines indented past its own are
code. make man builds build/raz-lib.3 from lib/, in the report's
order, and make check lints it. Library comments that ran past 80
columns on the page are shorter, or split.

patch · log

11 files changed, +171 -54

Makefile (history) modified +13 -2

@@ -32,7 +32,7 @@
3232 rm -rf build raz
3333
3434 # every example and test, against what it should print
35-check: raz
35+check: raz build/raz-lib.3
3636 test/check.sh
3737
3838 # line numbers: gdb stops at a line and names it
@@ -74,6 +74,17 @@
7474 @rloc -f src/*.pas
7575 @rloc -c src/*.pas
7676
77+# raz-lib(3), the library's manual page, made by tools/Doc.raz from
78+# its sources, in the order of the report's section 11
79+LIBDOC = Rt Mem Heap Arena Lock Thread.linux-x86_64 Out Sys.linux-x86_64 \
80+ Str Buf Fmt List Map Sort Math Files Opt C
81+
82+man: build/raz-lib.3
83+
84+build/raz-lib.3: raz tools/Doc.raz tools/Scan.raz lib/*.raz | build
85+ ./raz -Werror -o build/Doc tools/Doc.raz
86+ build/Doc man $(LIBDOC:%=lib/%.raz) > $@
87+
7788 # the report as a PDF, by pandoc and Chromium; not part of check, as
7889 # CI has neither
7990 pdf: build/raz.pdf
@@ -99,4 +110,4 @@
99110 uninstall:
100111 rm -rf $(PREFIX)/lib/raz $(PREFIX)/bin/raz $(PREFIX)/share/man/man1/raz.1
101112
102-.PHONY: units tests check gdb bench stress fmt loc pdf clean install uninstall
113+.PHONY: units tests check gdb bench stress fmt loc man pdf clean install uninstall

PLAN.md (history) modified +1 -1

@@ -34,7 +34,7 @@
3434 Pascal and C to raz", a table of what each spells differently, as
3535 concha's tour has from sh. About 500 lines. The examples go in
3636 `test/man`, run by `make check`.
37-2. **`raz-lib(3)`, the library, made from its sources.** `tools/Doc`
37+2. Done: **`raz-lib(3)`, the library, made from its sources.** `tools/Doc`
3838 learns to write mdoc: each module's own comment, then its exported
3939 declarations with theirs, under a heading of its name. `make` builds
4040 the page from `lib/`, so it says what the library says, always.

lib/C.raz (history) modified +3 -2

@@ -13,9 +13,10 @@
1313 extern proc isatty*(fd: I32): I32;
1414 extern proc exit*(status: I32);
1515 extern proc _exit*(status: I32);
16-extern proc __errno_location*: ^I32; (* errno is this thread's: through here *)
16+extern proc __errno_location*: ^I32; (* errno, this thread's *)
1717 extern proc sched_yield*: I32;
18-extern proc pthread_create*(t: ^UINT; attr: ^U8; start: proc(arg: ^U8): ^U8; arg: ^U8): I32;
18+extern proc pthread_create*(t: ^UINT; attr: ^U8;
19+ start: proc(arg: ^U8): ^U8; arg: ^U8): I32;
1920 extern proc pthread_join*(t: UINT; result: ^^U8): I32;
2021 extern proc rename*(old, new: ^U8): I32;
2122 extern proc unlink*(path: ^U8): I32;

lib/Mem.raz (history) modified +1 -1

@@ -5,7 +5,7 @@
55
66 type
77 Allocator* = record
8- alloc*: proc(a: ^Allocator; size, align: INT): ^U8; (* nil if out of room *)
8+ alloc*: proc(a: ^Allocator; size, align: INT): ^U8; (* or nil *)
99 free*: proc(a: ^Allocator; p: ^U8);
1010 end;
1111

lib/Opt.raz (history) modified +2 -2

@@ -13,7 +13,7 @@
1313
1414 type
1515 Row* = variant
16- usage(scope, line: []U8); (* a synopsis line, without "usage: " *)
16+ usage(scope, line: []U8); (* a synopsis, without "usage: " *)
1717 sub(scope, name, help: []U8); (* a subcommand *)
1818 option(scope: []U8; letter: U8; long, value, help: []U8);
1919 end;
@@ -27,7 +27,7 @@
2727 scope: []U8;
2828 at: INT; (* the next word *)
2929 group: []U8; (* the letters left of a word of them, as -lw *)
30- spelt: []U8; (* the option just read as written, without its dashes *)
30+ spelt: []U8; (* the option just read, as written, no dashes *)
3131 ended: BOOL; (* the scope's options have ended *)
3232 help: BOOL; (* the option just read is -h or --help *)
3333 end;

lib/Rt.raz (history) modified +1 -1

@@ -22,7 +22,7 @@
2222 import Sys;
2323
2424 const
25- Full*: ERROR = "too many procedures to run at exit"; (* from atExit *)
25+ Full*: ERROR = "too many procedures to run at exit"; (* atExit's *)
2626 MaxAtExit = 32;
2727 SigFpe = 8;
2828 SigSegv = 11;

lib/Sys.linux-x86_64.raz (history) modified +9 -8

@@ -17,16 +17,17 @@
1717 type
1818 (* What open is asked for: reading, writing or both, and what else
1919 it does to the file. *)
20- Access* = (reading, writing, creating, exclusive, truncating, appending);
20+ Access* = (reading, writing, creating, exclusive, truncating,
21+ appending);
2122 Accesses* = set of Access; (* what open is given *)
2223
2324 (* What info says of a file. *)
2425 Info* = record
2526 size*: I64; (* in bytes *)
26- mtime*: I64; (* when its contents last changed, in nanoseconds since 1970 *)
27+ mtime*: I64; (* its contents' last change, in ns since 1970 *)
2728 mode*: INT; (* its permission bits, rwxrwxrwx *)
2829 dir*: BOOL; (* a directory *)
29- file*: BOOL; (* a regular file: not a directory, device, pipe or socket *)
30+ file*: BOOL; (* not a directory, device, pipe or socket *)
3031 symlink*: BOOL; (* the path itself is a symbolic link *)
3132 end;
3233
@@ -40,13 +41,13 @@
4041 Program* = record
4142 path*: []U8;
4243 args*: [][]U8; (* the first is its name *)
43- env*: [][]U8; (* NAME=value, each added to this program's environment *)
44+ env*: [][]U8; (* NAME=value, each added to this environment *)
4445 onlyEnv*: BOOL; (* env is its whole environment *)
4546 dir*: []U8; (* its working directory; "" for this program's *)
46- fds*: [3]INT; (* its standard input, output and error; 0 for this program's own *)
47- group*: INT; (* the process group it joins: 0 for this program's, -1 a new one it leads *)
48- foreground*: BOOL; (* its group takes the terminal on standard input *)
49- tty*: INT; (* a pseudo-terminal's fd, to start a session on; 0 for none *)
47+ fds*: [3]INT; (* stdin, stdout, stderr; 0 for this program's *)
48+ group*: INT; (* its group: 0 for this program's, -1 a new one *)
49+ foreground*: BOOL; (* its group gets the terminal on standard input *)
50+ tty*: INT; (* a pseudo-terminal to start a session on, or 0 *)
5051 end;
5152 (* How a program that wait waited for changed. *)
5253 Status* = (running, exited, killed, stopped);

lib/Sys.raz (history) modified +9 -8

@@ -30,16 +30,17 @@
3030 type
3131 (* What open is asked for: reading, writing or both, and what else
3232 it does to the file. *)
33- Access* = (reading, writing, creating, exclusive, truncating, appending);
33+ Access* = (reading, writing, creating, exclusive, truncating,
34+ appending);
3435 Accesses* = set of Access; (* what open is given *)
3536
3637 (* What info says of a file. *)
3738 Info* = record
3839 size*: I64; (* in bytes *)
39- mtime*: I64; (* when its contents last changed, in nanoseconds since 1970 *)
40+ mtime*: I64; (* its contents' last change, in ns since 1970 *)
4041 mode*: INT; (* its permission bits, rwxrwxrwx *)
4142 dir*: BOOL; (* a directory *)
42- file*: BOOL; (* a regular file: not a directory, device, pipe or socket *)
43+ file*: BOOL; (* not a directory, device, pipe or socket *)
4344 symlink*: BOOL; (* the path itself is a symbolic link *)
4445 end;
4546
@@ -52,13 +53,13 @@
5253 Program* = record
5354 path*: []U8;
5455 args*: [][]U8; (* the first is its name *)
55- env*: [][]U8; (* NAME=value, each added to this program's environment *)
56+ env*: [][]U8; (* NAME=value, each added to this environment *)
5657 onlyEnv*: BOOL; (* env is its whole environment *)
5758 dir*: []U8; (* its working directory; "" for this program's *)
58- fds*: [3]INT; (* its standard input, output and error; 0 for this program's own *)
59- group*: INT; (* the process group it joins: 0 for this program's, -1 a new one it leads *)
60- foreground*: BOOL; (* its group takes the terminal on standard input *)
61- tty*: INT; (* a pseudo-terminal's fd, to start a session on; 0 for none *)
59+ fds*: [3]INT; (* stdin, stdout, stderr; 0 for this program's *)
60+ group*: INT; (* its group: 0 for this program's, -1 a new one *)
61+ foreground*: BOOL; (* its group gets the terminal on standard input *)
62+ tty*: INT; (* a pseudo-terminal to start a session on, or 0 *)
6263 end;
6364 (* How a program that wait waited for changed. *)
6465 Status* = (running, exited, killed, stopped);

test/check.sh (history) modified +3 -3

@@ -98,9 +98,9 @@
9898 $raz -o $out/Busy test/list/Busy.raz
9999 if RAZ_PROFILE=1 $out/Busy 2>&1 >/dev/null | sed -n 2p | grep -q 'Busy.spin$'; then pass=$((pass + 1))
100100 else echo "FAIL RAZ_PROFILE: Busy.spin isn't first"; fail=$((fail + 1)); fi
101-# the manual pages, which groff and mandoc, where there, must read
102-# without a warning
103-for f in doc/*.[1-9]; do
101+# the manual pages, raz-lib(3) as make made it from lib/, which groff
102+# and mandoc, where there, must read without a warning
103+for f in doc/*.[1-9] build/raz-lib.3; do
104104 : > $out/man.out
105105 command -v groff > /dev/null && groff -mdoc -ww -z $f >> $out/man.out 2>&1
106106 command -v mandoc > /dev/null && mandoc -Tlint -Wwarning $f >> $out/man.out 2>&1

test/doc/Opt.expect (history) modified +2 -2

@@ -11,7 +11,7 @@
1111 that keeps -h for itself, and completion for concha.
1212
1313 type Row* = variant
14- usage(scope, line: []U8); (* a synopsis line, without "usage: " *)
14+ usage(scope, line: []U8); (* a synopsis, without "usage: " *)
1515 sub(scope, name, help: []U8); (* a subcommand *)
1616 option(scope: []U8; letter: U8; long, value, help: []U8);
1717 end;
@@ -26,7 +26,7 @@
2626 scope: []U8;
2727 at: INT; (* the next word *)
2828 group: []U8; (* the letters left of a word of them, as -lw *)
29- spelt: []U8; (* the option just read as written, without its dashes *)
29+ spelt: []U8; (* the option just read, as written, no dashes *)
3030 ended: BOOL; (* the scope's options have ended *)
3131 help: BOOL; (* the option just read is -h or --help *)
3232 end;

tools/Doc.raz (history) modified +127 -24

@@ -5,6 +5,12 @@
55 each exported declaration as it is written, with the comment just
66 before it.
77
8+ Doc man Module.raz ...
9+ prints the same of each module, as the manual page raz-lib(3), in
10+ mdoc: the module's name a heading, each declaration a literal
11+ display, each comment filled text, indented, but for its lines
12+ indented further, which are code.
13+
814 Doc examples report.md dir
915 Doc examples page.7 dir
1016 writes each example of the report, or of a manual page, that is a
@@ -12,11 +18,16 @@
1218 dir/Name.raz, and prints the names, one a line. *)
1319 module Doc;
1420 import Out, Rt, Str, Buf, Heap, Scan, Files;
21+
22+const Date = "October 12, 2026"; (* raz-lib(3)'s, when the library last changed *)
1523
1624 var
1725 sc: Scan.Scanner; (* the text being read, at the current symbol *)
1826 src: []U8;
1927 docStart, docEnd: INT; (* the comment just before it, or -1 *)
28+ man: BOOL; (* writing mdoc *)
29+ atStart: BOOL; (* emit is at the start of a line *)
30+ shown: BOOL; (* a literal display of mdoc's is open *)
2031
2132 (* On to the next symbol that isn't a comment, keeping where the last
2233 comment that starts a line was: one after code on its line belongs
@@ -35,6 +46,34 @@
3546 until sc.kind # Scan.comment;
3647 end;
3748
49+(* c, written out; in mdoc, so that roff takes it as text: a backslash
50+ is \e, and a line that would start with . or ' is a control line
51+ unless \& comes first. *)
52+proc emit(c: U8);
53+begin
54+ if man and (c = '\\') then Out.str("\\e");
55+ else
56+ if man and atStart and ((c = '.') or (c = '\'')) then Out.str("\\&"); end;
57+ Out.char(c);
58+ end;
59+ atStart := c = 10;
60+end;
61+
62+(* a line of mdoc's own, such as .Ed *)
63+proc control(s: []U8);
64+begin
65+ Out.str(s);
66+ Out.ln;
67+ atStart := true;
68+end;
69+
70+(* the literal display, if open, closed *)
71+proc close;
72+begin
73+ if shown then control(".Ed"); end;
74+ shown := false;
75+end;
76+
3877 proc text: []U8;
3978 begin
4079 return Scan.text(ADR(sc));
@@ -95,46 +134,69 @@
95134 col := start - lineStart(start);
96135 i := start;
97136 while i < stop do
98- Out.char(src[i]);
137+ emit(src[i]);
99138 if src[i] = 10 then
100139 n := 0;
101140 while (n < col) and (i + 1 < stop) and (src[i + 1] = ' ') do INC(i); INC(n); end;
102141 end;
103142 INC(i);
104143 end;
105- Out.ln;
144+ emit(10);
106145 end;
107146
108147 (* The comment from start to stop, without its brackets, each line
109- indented by four. *)
148+ indented by four; in mdoc, filled, but for the lines indented past
149+ its own, which are code, kept as they are. *)
110150 proc comment(start, stop: INT);
111-var i, j: INT; atLine: BOOL;
151+var i, j, k, m, n, deep, indent: INT;
112152 begin
113153 if start < 0 then return; end;
154+ deep := start - lineStart(start) + 3;
155+ indent := -1; (* the indent of the code being written, or -1 *)
114156 i := start + 2;
115157 j := stop - 2;
116158 while (i < j) and Scan.isSpace(src[i]) do INC(i); end;
117159 while (j > i) and Scan.isSpace(src[j - 1]) do DEC(j); end;
118- atLine := true;
160+ if man then control(".Bd -filled -offset indent -compact"); end;
119161 while i < j do
120- if atLine then Out.str(" "); atLine := false; end;
121- Out.char(src[i]);
122- if src[i] = 10 then
123- atLine := true;
124- while (i + 1 < j) and (src[i + 1] = ' ') do INC(i); end;
162+ k := i;
163+ while (k < j) and (src[k] # 10) do INC(k); end;
164+ n := 0;
165+ while (i + n < k) and (src[i + n] = ' ') do INC(n); end;
166+ if man and (n > deep) and (indent < 0) then
167+ control(".Bd -literal -offset indent -compact");
168+ indent := n;
169+ elsif man and (n <= deep) and (indent >= 0) then
170+ control(".Ed");
171+ indent := -1;
125172 end;
126- INC(i);
173+ if man and (n = k - i) and (indent < 0) then control(".Pp");
174+ else
175+ if not man then Out.str(" ");
176+ elsif (indent >= 0) and (n > indent) then n := indent;
177+ end;
178+ for m := i + n to k - 1 do emit(src[m]); end;
179+ emit(10);
180+ end;
181+ i := k + 1;
127182 end;
128- Out.ln;
183+ if indent >= 0 then control(".Ed"); end;
184+ if man then control(".Ed"); end;
129185 end;
130186
131-(* One declaration, from its first token, the comment before it kept. *)
187+(* One declaration, from its first token, the comment before it kept.
188+ In mdoc a module's test, its main, is left out, and declarations
189+ with no comment share a display. *)
132190 proc declaration(prefix: []U8; exported: BOOL; start, ds, de, stop: INT);
133191 begin
134192 if not exported then return; end;
135- Out.ln;
136- Out.str(prefix);
193+ if not man then Out.ln;
194+ elsif Str.hasPrefix(src[start..stop], "proc main*") then return;
195+ elsif not shown then control(".Bd -literal"); shown := true;
196+ end;
197+ if LEN(prefix) > 0 then Out.str(prefix); Out.str(" "); end;
137198 block(start, trailing(stop));
199+ if ds >= 0 then close; end;
138200 comment(ds, de);
139201 end;
140202
@@ -196,12 +258,7 @@
196258 sc.pos := sc.start; (* name reads the name again *)
197259 exp := name();
198260 stop := toSemicolon();
199- if exp then
200- Out.ln;
201- Out.str(word); Out.str(" ");
202- block(start, trailing(stop));
203- comment(ds, de);
204- end;
261+ declaration(word, exp, start, ds, de, stop);
205262 next;
206263 end;
207264 end;
@@ -229,8 +286,15 @@
229286 if sc.kind = Scan.eof then Rt.stop("Doc: no module"); end;
230287 start := sc.start;
231288 ds := docStart; de := docEnd;
232- block(start, toSemicolon());
233- comment(ds, de);
289+ next;
290+ if man then
291+ close;
292+ Out.str(".Ss "); Out.str(text()); emit(10);
293+ declaration("", true, start, ds, de, toSemicolon());
294+ else
295+ block(start, toSemicolon());
296+ comment(ds, de);
297+ end;
234298 next;
235299 while (sc.kind # Scan.eof) and not is("begin") and not is("end") do
236300 if is("const") or is("type") or is("var") then section;
@@ -337,11 +401,50 @@
337401 end;
338402 end;
339403
404+(* raz-lib(3): the modules named from argument 2 on, under a head and
405+ above a tail of the page's own. *)
406+proc page;
407+var i: INT;
408+begin
409+ man := true;
410+ atStart := true;
411+ control(".\\\" raz-lib(3) - the library; made by tools/Doc.raz from lib/");
412+ Out.str(".Dd "); Out.str(Date); emit(10);
413+ control(".Dt RAZ-LIB 3");
414+ control(".Os");
415+ control(".Sh NAME");
416+ control(".Nm raz-lib");
417+ control(".Nd the library of the raz language");
418+ control(".Sh DESCRIPTION");
419+ control("The modules a raz program imports from");
420+ control(".Pa lib/ ,");
421+ control("beside the compiler, each as its source gives it: the module's own");
422+ control("comment, then every declaration it exports, as it is written, with");
423+ control("the comment just before it.");
424+ control("A module's");
425+ control(".Ic main ,");
426+ control("where it has one, is its test, and is left out.");
427+ control(".Pp");
428+ control("The report, section 11, says what the library promises, and which");
429+ control("of its modules every target has.");
430+ control(".Sh MODULES");
431+ for i := 2 to ARGC() - 1 do document(Files.arg(i)); end;
432+ close;
433+ control(".Sh SEE ALSO");
434+ control(".Xr raz 1 ,");
435+ control(".Xr raz 7 ,");
436+ control(".Xr raz-tutorial 7 ;");
437+ control("the report,");
438+ control(".Pa doc/raz.md ,");
439+ control("in raz's sources.");
440+end;
441+
340442 proc main*;
341443 begin
342444 if (ARGC() = 4) and (Files.arg(1) = "examples") then examples(Files.arg(2), Files.arg(3));
445+ elsif (ARGC() >= 3) and (Files.arg(1) = "man") then page;
343446 elsif ARGC() = 2 then document(Files.arg(1));
344- else Rt.stop("usage: Doc Module.raz, or Doc examples file dir");
447+ else Rt.stop("usage: Doc Module.raz, Doc man Module.raz ..., or Doc examples file dir");
345448 end;
346449 end;
347450