From f3dc770785572f840e50aae266f5af39d54537e2 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 26 Oct 2026 17:14:35 +0000 Subject: [PATCH] compiler.md: what the compiler asks of Sys, Rt and Files, held by test/asks.sh --- diff --git a/doc/compiler.md b/doc/compiler.md index 1098c95..1dc266b 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -2439,6 +2439,68 @@ The recipe for an interrupt's entry, and its test, follow. +## What the compiler asks of Sys, Rt and Files + +What `src/` takes from `lib/`, for a host that has to supply it +(`Host`, and a program that brings its own library): every qualified +name, in code, of a library module. Nothing else of `lib/` is used by +`src/` itself; the library modules listed import others in turn +(`Heap`, `Mem`, `Out`, `Files` and the rest call `Sys` and `Rt`), and a +replacement for one has to give what the others ask of it. `Rt` is +also found by name for `fail`, `exit` and the places, as the previous +section says, and `NEW(p, a)` needs `a` to be a `^Mem.Allocator` and +calls its `alloc` field, the one library type the compiler knows by +name. Besides the library, `src/` makes two system calls itself, +`stat` in `Parse.identity` (a file's device and inode, for the +one-module-of-each-name check) and `memfd_create` and `execve` in +`Raz.raz` (`raz run`), and reads `SYSTEM.ARGV`. + +`test/asks.sh` keeps this list true: it fails when `src/` uses a +library name that is not a row here, or a row names one that `src/` +no longer uses. Each row is one name; a type is listed with its +fields as the code reads them. + +`Sys`, the system: + +| Name | What for | +|---|---| +| `Sys.access` | `Raz.muslHere`: whether the dynamic loaders of glibc and musl are installed | +| `Sys.chmod` | `Elf.writeElf`: makes the program's file executable (mode 755) | +| `Sys.close` | `Scan` and `Parse`: closes a source or `RAZ-VERSION` file after reading | +| `Sys.closeDir`, `Sys.openDir`, `Sys.nextEntry`, `Sys.Dir` | `Parse`: the files of a directory, to find a module's other files for the one-module-of-each-name check | +| `Sys.env`, `Sys.envAt` | `Diag` (`NO_COLOR`) and `Raz.userLibraries` (`RAZ_PATH`, `HOME`; `envAt` tells unset from empty) | +| `Sys.errDenied`, `Sys.errNoEntry` | `Scan`: "permission denied" and "there is no such file" in a message | +| `Sys.info`, `Sys.Info` | `Scan` and `Parse`: whether a path is a file (`file`) and its length (`size`) | +| `Sys.isTerminal` | `Diag`: colour only when standard error is a terminal | +| `Sys.openRead`, `Sys.read` | `Scan`: reads a source file whole; `Parse`: the library's `RAZ-VERSION` | +| `Sys.readLink` | `Raz`: `/proc/self/exe`, to find the library beside the compiler | +| `Sys.stderr`, `Sys.write` | `Diag`: its messages | +| `Sys.workDir` | `Diag`: shows a path relative to the working directory; `Elf`: the compilation directory of the line table | + +`Rt`, the run time, besides `fail`, `exit` and the places (above): + +| Name | What for | +|---|---| +| `Rt.exit` | `Diag`: ends the compiler, with status 1, after an error that stops it | + +`Files`, whole files and the command line: + +| Name | What for | +|---|---| +| `Files.arg` | `Raz`: the command line's words, and `ARGC()` for how many | +| `Files.write` | `Elf.writeElf`: writes the executable | + +The rest of the library, which `Diag`, `Sym`, `X64` and `Parse` use for +text and `Scan` for tables: + +| Name | What for | +|---|---| +| `Buf.Buf`, `Buf.init`, `Buf.add`, `Buf.str`, `Buf.bytes`, `Buf.reset`, `Buf.done` | A growing buffer: `Diag`'s message and working directory, a type's name in `Sym`, an instruction's text in `X64`, a name made in `Parse` | +| `Fmt.write`, `Fmt.Arg`, `Fmt.str`, `Fmt.int`, `Fmt.char`, `Fmt.hex` | Formatting: `Diag.say`'s `{}` messages and a type's name | +| `Out.str`, `Out.ln`, `Out.flush` | Standard output: the `-S` listing, `-h` and `--version` | +| `Str.copy`, `Str.find`, `Str.hasPrefix`, `Str.hexVal` | A kept copy of a path (`Diag`), the one-character operators (`Scan`), a prefix of a path, a file name or a mnemonic (`Diag`, `Parse`, `X64`), and a hex digit (`Parse`) | +| `Heap.heap`, `Mem.alloc`, `Mem.free` | The allocator that `NEW`, `Buf.init` and `Str.copy` are given, and the blocks of `Scan`'s tables and `Diag`'s lists | + ## This compiler's choices What the report's appendix A leaves to an implementation, and what diff --git a/test/asks.sh b/test/asks.sh new file mode 100755 index 0000000..dc872ff --- /dev/null +++ b/test/asks.sh @@ -0,0 +1,59 @@ +#!/bin/sh +# What the compiler asks of the library: every qualified name of lib/, +# Sys.write, Rt.exit and so on, that src/ uses in code (not in a comment, +# a string or a character) must be a row of the tables under "What the +# compiler asks of Sys, Rt and Files" in doc/compiler.md, and every row +# must be used. Prints the differences and exits 1 if there are any. +# Run from the repository's root. + +doc=doc/compiler.md +tmp=${TMPDIR:-/tmp}/asks.$$ +trap 'rm -f $tmp.*' EXIT + +# src/*.raz with comments, strings and characters cut out, so that what +# is left is code. Comments (* ... *) don't nest and may span lines, and +# so may a `...` string; "..." and '...' end at the line's end at most. +code() { + awk ' + BEGIN { state = "code" } + { + n = length($0); out = ""; i = 1 + while (i <= n) { + c = substr($0, i, 1); d = substr($0, i, 2) + if (state == "comment") { + if (d == "*)") { state = "code"; i += 2 } else i++ + } else if (state == "raw") { + if (c == "`") state = "code" + i++ + } else if (state == "dq" || state == "sq") { + if (c == "\\") i += 2 + else { + if ((state == "dq" && c == "\"") || (state == "sq" && c == "\047")) state = "code" + i++ + } + } else if (d == "(*") { state = "comment"; i += 2 } + else if (c == "`") { state = "raw"; i++ } + else if (c == "\"") { state = "dq"; i++ } + else if (c == "\047") { state = "sq"; i++ } + else { out = out c; i++ } + } + if (state == "dq" || state == "sq") state = "code" + print out + }' src/*.raz +} + +# the modules of lib/, as the names a program imports them by +libs=$(ls lib | sed 's/\..*//' | sort -u | tr '\n' '|' | sed 's/|$//') + +code | grep -oE "\b($libs)\.[A-Za-z_][A-Za-z0-9_]*" | sort -u > $tmp.used +# the names in the first cell of a row of a table in the section: +# "| `Sys.close`, `Sys.Dir` | what for |" +sed -n '/^## What the compiler asks of Sys, Rt and Files/,/^## [^W]/p' $doc | + sed -n 's/^| `\([^|]*\)|.*/`\1/p' | + grep -oE '`[A-Za-z]+\.[A-Za-z_][A-Za-z0-9_]*`' | tr -d '`' | sort -u > $tmp.listed + +status=0 +if [ ! -s $tmp.listed ]; then echo "asks: $doc has no rows for the library names"; status=1; fi +if comm -23 $tmp.used $tmp.listed | sed "s|^|src/ uses it, $doc doesn't say: |" | grep .; then status=1; fi +if comm -13 $tmp.used $tmp.listed | sed "s|^|$doc lists it, src/ never uses it: |" | grep .; then status=1; fi +exit $status diff --git a/test/check.sh b/test/check.sh index e97e4ad..40c6e50 100755 --- a/test/check.sh +++ b/test/check.sh @@ -168,6 +168,10 @@ && [ "$(cat $out/own/OwnFail.out)" = 'check failed: 0' ]; then pass=$((pass + 1)) else echo "FAIL own Rt: status $status, $statusFail"; cat $out/own/Own.out $out/own/OwnFail.out; fail=$((fail + 1)); fi +# what src/ asks of lib/ is what doc/compiler.md lists (test/asks.sh) +if sh test/asks.sh > $out/asks.out 2>&1; then pass=$((pass + 1)) +else echo "FAIL test/asks.sh:"; cat $out/asks.out; fail=$((fail + 1)); fi + # raz -S: test/list/X.raz lists as X.expect for f in test/list/*.raz; do want=test/list/$(basename $f .raz).expect