From f8fb7674b9eb43d0d743959e5a149f8c9321d1aa Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 12 Oct 2026 17:29:41 +0100 Subject: [PATCH] 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. --- diff --git a/Makefile b/Makefile index 858303e..fa63342 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,7 @@ rm -rf build raz # every example and test, against what it should print -check: raz +check: raz build/raz-lib.3 test/check.sh # line numbers: gdb stops at a line and names it @@ -74,6 +74,17 @@ @rloc -f src/*.pas @rloc -c src/*.pas +# raz-lib(3), the library's manual page, made by tools/Doc.raz from +# its sources, in the order of the report's section 11 +LIBDOC = Rt Mem Heap Arena Lock Thread.linux-x86_64 Out Sys.linux-x86_64 \ + Str Buf Fmt List Map Sort Math Files Opt C + +man: build/raz-lib.3 + +build/raz-lib.3: raz tools/Doc.raz tools/Scan.raz lib/*.raz | build + ./raz -Werror -o build/Doc tools/Doc.raz + build/Doc man $(LIBDOC:%=lib/%.raz) > $@ + # the report as a PDF, by pandoc and Chromium; not part of check, as # CI has neither pdf: build/raz.pdf @@ -99,4 +110,4 @@ uninstall: rm -rf $(PREFIX)/lib/raz $(PREFIX)/bin/raz $(PREFIX)/share/man/man1/raz.1 -.PHONY: units tests check gdb bench stress fmt loc pdf clean install uninstall +.PHONY: units tests check gdb bench stress fmt loc man pdf clean install uninstall diff --git a/PLAN.md b/PLAN.md index d94863b..7146107 100644 --- a/PLAN.md +++ b/PLAN.md @@ -34,7 +34,7 @@ Pascal and C to raz", a table of what each spells differently, as concha's tour has from sh. About 500 lines. The examples go in `test/man`, run by `make check`. -2. **`raz-lib(3)`, the library, made from its sources.** `tools/Doc` +2. Done: **`raz-lib(3)`, the library, made from its sources.** `tools/Doc` learns to write mdoc: each module's own comment, then its exported declarations with theirs, under a heading of its name. `make` builds the page from `lib/`, so it says what the library says, always. diff --git a/lib/C.raz b/lib/C.raz index 56a02ab..3913474 100644 --- a/lib/C.raz +++ b/lib/C.raz @@ -13,9 +13,10 @@ extern proc isatty*(fd: I32): I32; extern proc exit*(status: I32); extern proc _exit*(status: I32); -extern proc __errno_location*: ^I32; (* errno is this thread's: through here *) +extern proc __errno_location*: ^I32; (* errno, this thread's *) extern proc sched_yield*: I32; -extern proc pthread_create*(t: ^UINT; attr: ^U8; start: proc(arg: ^U8): ^U8; arg: ^U8): I32; +extern proc pthread_create*(t: ^UINT; attr: ^U8; + start: proc(arg: ^U8): ^U8; arg: ^U8): I32; extern proc pthread_join*(t: UINT; result: ^^U8): I32; extern proc rename*(old, new: ^U8): I32; extern proc unlink*(path: ^U8): I32; diff --git a/lib/Mem.raz b/lib/Mem.raz index 4813a7a..82b943e 100644 --- a/lib/Mem.raz +++ b/lib/Mem.raz @@ -5,7 +5,7 @@ type Allocator* = record - alloc*: proc(a: ^Allocator; size, align: INT): ^U8; (* nil if out of room *) + alloc*: proc(a: ^Allocator; size, align: INT): ^U8; (* or nil *) free*: proc(a: ^Allocator; p: ^U8); end; diff --git a/lib/Opt.raz b/lib/Opt.raz index 58ab612..03da195 100644 --- a/lib/Opt.raz +++ b/lib/Opt.raz @@ -13,7 +13,7 @@ type Row* = variant - usage(scope, line: []U8); (* a synopsis line, without "usage: " *) + usage(scope, line: []U8); (* a synopsis, without "usage: " *) sub(scope, name, help: []U8); (* a subcommand *) option(scope: []U8; letter: U8; long, value, help: []U8); end; @@ -27,7 +27,7 @@ scope: []U8; at: INT; (* the next word *) group: []U8; (* the letters left of a word of them, as -lw *) - spelt: []U8; (* the option just read as written, without its dashes *) + spelt: []U8; (* the option just read, as written, no dashes *) ended: BOOL; (* the scope's options have ended *) help: BOOL; (* the option just read is -h or --help *) end; diff --git a/lib/Rt.raz b/lib/Rt.raz index 4f7d684..40b10e6 100644 --- a/lib/Rt.raz +++ b/lib/Rt.raz @@ -22,7 +22,7 @@ import Sys; const - Full*: ERROR = "too many procedures to run at exit"; (* from atExit *) + Full*: ERROR = "too many procedures to run at exit"; (* atExit's *) MaxAtExit = 32; SigFpe = 8; SigSegv = 11; diff --git a/lib/Sys.linux-x86_64.raz b/lib/Sys.linux-x86_64.raz index 21ca79f..7a2533b 100644 --- a/lib/Sys.linux-x86_64.raz +++ b/lib/Sys.linux-x86_64.raz @@ -17,16 +17,17 @@ type (* What open is asked for: reading, writing or both, and what else it does to the file. *) - Access* = (reading, writing, creating, exclusive, truncating, appending); + Access* = (reading, writing, creating, exclusive, truncating, + appending); Accesses* = set of Access; (* what open is given *) (* What info says of a file. *) Info* = record size*: I64; (* in bytes *) - mtime*: I64; (* when its contents last changed, in nanoseconds since 1970 *) + mtime*: I64; (* its contents' last change, in ns since 1970 *) mode*: INT; (* its permission bits, rwxrwxrwx *) dir*: BOOL; (* a directory *) - file*: BOOL; (* a regular file: not a directory, device, pipe or socket *) + file*: BOOL; (* not a directory, device, pipe or socket *) symlink*: BOOL; (* the path itself is a symbolic link *) end; @@ -40,13 +41,13 @@ Program* = record path*: []U8; args*: [][]U8; (* the first is its name *) - env*: [][]U8; (* NAME=value, each added to this program's environment *) + env*: [][]U8; (* NAME=value, each added to this environment *) onlyEnv*: BOOL; (* env is its whole environment *) dir*: []U8; (* its working directory; "" for this program's *) - fds*: [3]INT; (* its standard input, output and error; 0 for this program's own *) - group*: INT; (* the process group it joins: 0 for this program's, -1 a new one it leads *) - foreground*: BOOL; (* its group takes the terminal on standard input *) - tty*: INT; (* a pseudo-terminal's fd, to start a session on; 0 for none *) + fds*: [3]INT; (* stdin, stdout, stderr; 0 for this program's *) + group*: INT; (* its group: 0 for this program's, -1 a new one *) + foreground*: BOOL; (* its group gets the terminal on standard input *) + tty*: INT; (* a pseudo-terminal to start a session on, or 0 *) end; (* How a program that wait waited for changed. *) Status* = (running, exited, killed, stopped); diff --git a/lib/Sys.raz b/lib/Sys.raz index 7703997..ad0fa83 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -30,16 +30,17 @@ type (* What open is asked for: reading, writing or both, and what else it does to the file. *) - Access* = (reading, writing, creating, exclusive, truncating, appending); + Access* = (reading, writing, creating, exclusive, truncating, + appending); Accesses* = set of Access; (* what open is given *) (* What info says of a file. *) Info* = record size*: I64; (* in bytes *) - mtime*: I64; (* when its contents last changed, in nanoseconds since 1970 *) + mtime*: I64; (* its contents' last change, in ns since 1970 *) mode*: INT; (* its permission bits, rwxrwxrwx *) dir*: BOOL; (* a directory *) - file*: BOOL; (* a regular file: not a directory, device, pipe or socket *) + file*: BOOL; (* not a directory, device, pipe or socket *) symlink*: BOOL; (* the path itself is a symbolic link *) end; @@ -52,13 +53,13 @@ Program* = record path*: []U8; args*: [][]U8; (* the first is its name *) - env*: [][]U8; (* NAME=value, each added to this program's environment *) + env*: [][]U8; (* NAME=value, each added to this environment *) onlyEnv*: BOOL; (* env is its whole environment *) dir*: []U8; (* its working directory; "" for this program's *) - fds*: [3]INT; (* its standard input, output and error; 0 for this program's own *) - group*: INT; (* the process group it joins: 0 for this program's, -1 a new one it leads *) - foreground*: BOOL; (* its group takes the terminal on standard input *) - tty*: INT; (* a pseudo-terminal's fd, to start a session on; 0 for none *) + fds*: [3]INT; (* stdin, stdout, stderr; 0 for this program's *) + group*: INT; (* its group: 0 for this program's, -1 a new one *) + foreground*: BOOL; (* its group gets the terminal on standard input *) + tty*: INT; (* a pseudo-terminal to start a session on, or 0 *) end; (* How a program that wait waited for changed. *) Status* = (running, exited, killed, stopped); diff --git a/test/check.sh b/test/check.sh index 2c9e75f..25bd1bb 100755 --- a/test/check.sh +++ b/test/check.sh @@ -98,9 +98,9 @@ $raz -o $out/Busy test/list/Busy.raz if RAZ_PROFILE=1 $out/Busy 2>&1 >/dev/null | sed -n 2p | grep -q 'Busy.spin$'; then pass=$((pass + 1)) else echo "FAIL RAZ_PROFILE: Busy.spin isn't first"; fail=$((fail + 1)); fi -# the manual pages, which groff and mandoc, where there, must read -# without a warning -for f in doc/*.[1-9]; do +# the manual pages, raz-lib(3) as make made it from lib/, which groff +# and mandoc, where there, must read without a warning +for f in doc/*.[1-9] build/raz-lib.3; do : > $out/man.out command -v groff > /dev/null && groff -mdoc -ww -z $f >> $out/man.out 2>&1 command -v mandoc > /dev/null && mandoc -Tlint -Wwarning $f >> $out/man.out 2>&1 diff --git a/test/doc/Opt.expect b/test/doc/Opt.expect index fe4ef02..02dbea1 100644 --- a/test/doc/Opt.expect +++ b/test/doc/Opt.expect @@ -11,7 +11,7 @@ that keeps -h for itself, and completion for concha. type Row* = variant - usage(scope, line: []U8); (* a synopsis line, without "usage: " *) + usage(scope, line: []U8); (* a synopsis, without "usage: " *) sub(scope, name, help: []U8); (* a subcommand *) option(scope: []U8; letter: U8; long, value, help: []U8); end; @@ -26,7 +26,7 @@ scope: []U8; at: INT; (* the next word *) group: []U8; (* the letters left of a word of them, as -lw *) - spelt: []U8; (* the option just read as written, without its dashes *) + spelt: []U8; (* the option just read, as written, no dashes *) ended: BOOL; (* the scope's options have ended *) help: BOOL; (* the option just read is -h or --help *) end; diff --git a/tools/Doc.raz b/tools/Doc.raz index b5bd1ee..11f7844 100644 --- a/tools/Doc.raz +++ b/tools/Doc.raz @@ -5,6 +5,12 @@ each exported declaration as it is written, with the comment just before it. + Doc man Module.raz ... + prints the same of each module, as the manual page raz-lib(3), in + mdoc: the module's name a heading, each declaration a literal + display, each comment filled text, indented, but for its lines + indented further, which are code. + Doc examples report.md dir Doc examples page.7 dir writes each example of the report, or of a manual page, that is a @@ -12,11 +18,16 @@ dir/Name.raz, and prints the names, one a line. *) module Doc; import Out, Rt, Str, Buf, Heap, Scan, Files; + +const Date = "October 12, 2026"; (* raz-lib(3)'s, when the library last changed *) var sc: Scan.Scanner; (* the text being read, at the current symbol *) src: []U8; docStart, docEnd: INT; (* the comment just before it, or -1 *) + man: BOOL; (* writing mdoc *) + atStart: BOOL; (* emit is at the start of a line *) + shown: BOOL; (* a literal display of mdoc's is open *) (* On to the next symbol that isn't a comment, keeping where the last comment that starts a line was: one after code on its line belongs @@ -35,6 +46,34 @@ until sc.kind # Scan.comment; end; +(* c, written out; in mdoc, so that roff takes it as text: a backslash + is \e, and a line that would start with . or ' is a control line + unless \& comes first. *) +proc emit(c: U8); +begin + if man and (c = '\\') then Out.str("\\e"); + else + if man and atStart and ((c = '.') or (c = '\'')) then Out.str("\\&"); end; + Out.char(c); + end; + atStart := c = 10; +end; + +(* a line of mdoc's own, such as .Ed *) +proc control(s: []U8); +begin + Out.str(s); + Out.ln; + atStart := true; +end; + +(* the literal display, if open, closed *) +proc close; +begin + if shown then control(".Ed"); end; + shown := false; +end; + proc text: []U8; begin return Scan.text(ADR(sc)); @@ -95,46 +134,69 @@ col := start - lineStart(start); i := start; while i < stop do - Out.char(src[i]); + emit(src[i]); if src[i] = 10 then n := 0; while (n < col) and (i + 1 < stop) and (src[i + 1] = ' ') do INC(i); INC(n); end; end; INC(i); end; - Out.ln; + emit(10); end; (* The comment from start to stop, without its brackets, each line - indented by four. *) + indented by four; in mdoc, filled, but for the lines indented past + its own, which are code, kept as they are. *) proc comment(start, stop: INT); -var i, j: INT; atLine: BOOL; +var i, j, k, m, n, deep, indent: INT; begin if start < 0 then return; end; + deep := start - lineStart(start) + 3; + indent := -1; (* the indent of the code being written, or -1 *) i := start + 2; j := stop - 2; while (i < j) and Scan.isSpace(src[i]) do INC(i); end; while (j > i) and Scan.isSpace(src[j - 1]) do DEC(j); end; - atLine := true; + if man then control(".Bd -filled -offset indent -compact"); end; while i < j do - if atLine then Out.str(" "); atLine := false; end; - Out.char(src[i]); - if src[i] = 10 then - atLine := true; - while (i + 1 < j) and (src[i + 1] = ' ') do INC(i); end; + k := i; + while (k < j) and (src[k] # 10) do INC(k); end; + n := 0; + while (i + n < k) and (src[i + n] = ' ') do INC(n); end; + if man and (n > deep) and (indent < 0) then + control(".Bd -literal -offset indent -compact"); + indent := n; + elsif man and (n <= deep) and (indent >= 0) then + control(".Ed"); + indent := -1; end; - INC(i); + if man and (n = k - i) and (indent < 0) then control(".Pp"); + else + if not man then Out.str(" "); + elsif (indent >= 0) and (n > indent) then n := indent; + end; + for m := i + n to k - 1 do emit(src[m]); end; + emit(10); + end; + i := k + 1; end; - Out.ln; + if indent >= 0 then control(".Ed"); end; + if man then control(".Ed"); end; end; -(* One declaration, from its first token, the comment before it kept. *) +(* One declaration, from its first token, the comment before it kept. + In mdoc a module's test, its main, is left out, and declarations + with no comment share a display. *) proc declaration(prefix: []U8; exported: BOOL; start, ds, de, stop: INT); begin if not exported then return; end; - Out.ln; - Out.str(prefix); + if not man then Out.ln; + elsif Str.hasPrefix(src[start..stop], "proc main*") then return; + elsif not shown then control(".Bd -literal"); shown := true; + end; + if LEN(prefix) > 0 then Out.str(prefix); Out.str(" "); end; block(start, trailing(stop)); + if ds >= 0 then close; end; comment(ds, de); end; @@ -196,12 +258,7 @@ sc.pos := sc.start; (* name reads the name again *) exp := name(); stop := toSemicolon(); - if exp then - Out.ln; - Out.str(word); Out.str(" "); - block(start, trailing(stop)); - comment(ds, de); - end; + declaration(word, exp, start, ds, de, stop); next; end; end; @@ -229,8 +286,15 @@ if sc.kind = Scan.eof then Rt.stop("Doc: no module"); end; start := sc.start; ds := docStart; de := docEnd; - block(start, toSemicolon()); - comment(ds, de); + next; + if man then + close; + Out.str(".Ss "); Out.str(text()); emit(10); + declaration("", true, start, ds, de, toSemicolon()); + else + block(start, toSemicolon()); + comment(ds, de); + end; next; while (sc.kind # Scan.eof) and not is("begin") and not is("end") do if is("const") or is("type") or is("var") then section; @@ -337,11 +401,50 @@ end; end; +(* raz-lib(3): the modules named from argument 2 on, under a head and + above a tail of the page's own. *) +proc page; +var i: INT; +begin + man := true; + atStart := true; + control(".\\\" raz-lib(3) - the library; made by tools/Doc.raz from lib/"); + Out.str(".Dd "); Out.str(Date); emit(10); + control(".Dt RAZ-LIB 3"); + control(".Os"); + control(".Sh NAME"); + control(".Nm raz-lib"); + control(".Nd the library of the raz language"); + control(".Sh DESCRIPTION"); + control("The modules a raz program imports from"); + control(".Pa lib/ ,"); + control("beside the compiler, each as its source gives it: the module's own"); + control("comment, then every declaration it exports, as it is written, with"); + control("the comment just before it."); + control("A module's"); + control(".Ic main ,"); + control("where it has one, is its test, and is left out."); + control(".Pp"); + control("The report, section 11, says what the library promises, and which"); + control("of its modules every target has."); + control(".Sh MODULES"); + for i := 2 to ARGC() - 1 do document(Files.arg(i)); end; + close; + control(".Sh SEE ALSO"); + control(".Xr raz 1 ,"); + control(".Xr raz 7 ,"); + control(".Xr raz-tutorial 7 ;"); + control("the report,"); + control(".Pa doc/raz.md ,"); + control("in raz's sources."); +end; + proc main*; begin if (ARGC() = 4) and (Files.arg(1) = "examples") then examples(Files.arg(2), Files.arg(3)); + elsif (ARGC() >= 3) and (Files.arg(1) = "man") then page; elsif ARGC() = 2 then document(Files.arg(1)); - else Rt.stop("usage: Doc Module.raz, or Doc examples file dir"); + else Rt.stop("usage: Doc Module.raz, Doc man Module.raz ..., or Doc examples file dir"); end; end;