From 21350d288053addebabaec43ba3ca1ad1e1b7af3 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sat, 10 Oct 2026 12:08:28 +0000 Subject: [PATCH] Step 15: versions, tools/Doc.jed, checked examples, library tests The report says its version, 2026-10.0, and ends with what each version changed; jed --version prints its own and the report's, and make check sees that they agree. tools/Doc.jed prints a module's own comment and its exported declarations, each with the comment just before it (a comment after code on its line stays with that code); and writes the report's examples that are whole modules to files. Three examples of the report become whole modules: defer (it named a Sys.open that never existed), divmod, and mixing Out with C's stdio. make check compiles them all and runs those with a main. Library modules carry a main that tests them, which make check runs; Sys over C is tested through test/libc/SysTest. Sys gains openRead, create, read and close, for Doc. Heap's test found a bug: free wrote the free list's link over the block's size class before reading it, so every freed block went to the smallest class; it reads it first. --- diff --git a/PLAN.md b/PLAN.md index 5695ccc..dfcc48d 100644 --- a/PLAN.md +++ b/PLAN.md @@ -129,7 +129,7 @@ Tools -- [ ] 15. Versions first: the report says its version, by date +- [x] 15. Versions first: the report says its version, by date (`2026-10.0`, then `.1` for the next change that month), and ends with what each version changed; `jed --version` prints its own and the report's, `jed 0.3.0, report 2026-10.0`. Each @@ -141,7 +141,8 @@ `make check` also compiles those examples, running the ones with a `main`, and runs each library module's `main` as its test. The test runner stays in the Makefile: it must work when the - compiler doesn't. + compiler doesn't. (Tagging is left for later, at the owner's + word.) - [ ] 16. Editor support: `editor/vim` (`syntax/jed.vim`, `indent/jed.vim` by `shiftwidth()`, and `ftdetect/jed.vim`, which sets 4 spaces) diff --git a/doc/jed.md b/doc/jed.md index f2ffb6d..f915ad5 100644 --- a/doc/jed.md +++ b/doc/jed.md @@ -9,6 +9,9 @@ This report defines the language. It is short on purpose. Whatever it doesn't allow, jed doesn't have. +This is version 2026-10.0 of the report; section 12 says what each +version changed. + ## 1. A first look ``` @@ -642,10 +645,32 @@ reached run. ``` -fd := Sys.open(name, Sys.rdonly); -if fd < 0 then return Sys.Failed; end; -defer Sys.close(fd); -... +module Head; +import Sys, Out; + +(* Prints the first line of the file at path. *) +proc head(path: []U8): BOOL; +var fd, n, i: I64; buf: [256]U8; +begin + fd := Sys.openRead(path); + if fd < 0 then return false; end; + defer Sys.close(fd); + n := Sys.read(fd, buf); + if n < 0 then return false; end; + for i := 0 to n - 1 do + if buf[i] = 10 then n := i; break; end; + end; + Out.str(buf[0..n]); + Out.ln; + return true; +end; + +proc main*; +begin + if not head("/etc/hostname") then Out.str("no hostname\n"); end; +end; + +end Head. ``` `return e;` evaluates `e` before the deferred statements run. A @@ -682,12 +707,22 @@ assignment, or alone as a statement. ``` +module Divide; +import Out; + proc divmod(a, b: I64): (I64, I64); begin return a div b, a mod b; end; -q, r := divmod(17, 5); +proc main*; +var q, r: I64; +begin + q, r := divmod(17, 5); + Out.int(q); Out.str(" rest "); Out.int(r); Out.ln; +end; + +end Divide. ``` **forward.** `proc f(x: I64): I64; forward;` declares `f` so that it @@ -814,7 +849,7 @@ | Module | What | |---|---| | `Rt` | ending the program, `exit` and `atExit`; `stop`, which ends it with a message; what the compiler's checks call when they fail; string comparison | -| `Sys` | system calls: `write`, `mmap`, `munmap`, `yield`, `exit`; one file per target, and `Sys.jed` over the C library for the others | +| `Sys` | system calls: `openRead`, `create`, `read`, `write`, `close`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; one file per target, and `Sys.jed` over the C library for the others | | `Out` | writing text and numbers to standard output, buffered; safe to call from several threads | | `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C | | `Lock` | a lock for short sections, for C's threads and `Thread`'s alike | @@ -847,13 +882,26 @@ text comes out of order: ``` -extern proc printf(format: ^U8; ...): I32; -extern proc fflush(f: ^U8): I32; -extern var stdout: ^U8; (* C's FILE *stdout *) -... -Out.str("from jed\n"); -Out.flush; -printf("from C\n"); -fflush(stdout); -Out.str("from jed again\n"); +module Both; +import Out, C; (* C: printf, fflush and C's FILE *stdout *) + +proc main*; +begin + Out.str("from jed\n"); + Out.flush; + C.printf("from C\n"); + C.fflush(C.stdout); + Out.str("from jed again\n"); +end; + +end Both. ``` + +## 12. Versions + +A version of this report is named by the month it was made in and a +count: `2026-10.0`, then `2026-10.1` for the next change that month. +`jed --version` says which version the compiler follows. + +- **2026-10.0.** The first numbered version: the language of compiler + 0.3.0. diff --git a/lib/Arena.jed b/lib/Arena.jed index bb9ae1e..278014c 100644 --- a/lib/Arena.jed +++ b/lib/Arena.jed @@ -2,7 +2,7 @@ at once: free does nothing, reset forgets everything. An Arena is its allocator: ADR(arena.base) is the ^Mem.Allocator to give NEW. *) module Arena; -import Mem; +import Mem, Rt; type Arena* = record @@ -47,6 +47,22 @@ if ar^.buf # nil then Mem.unmap(ar^.buf, ar^.cap); end; ar^.buf := nil; ar^.cap := 0; +end; + +(* The module's test. *) +proc main*; +var ar: Arena; a: ^Mem.Allocator; p, q: ^U8; +begin + if not init(ADR(ar), 4096) then Rt.stop("Arena.init"); end; + a := ADR(ar.base); + p := Mem.alloc(a, 3, 1); + q := Mem.alloc(a, 8, 8); + if (p = nil) or (q # p + 8) then Rt.stop("Arena: alignment"); end; + if Mem.alloc(a, 5000, 1) # nil then Rt.stop("Arena: too big"); end; + if Mem.alloc(a, -1, 1) # nil then Rt.stop("Arena: negative"); end; + reset(ADR(ar)); + if Mem.alloc(a, 4096, 1) # p then Rt.stop("Arena.reset"); end; + done(ADR(ar)); end; end Arena. diff --git a/lib/Buf.jed b/lib/Buf.jed index 660f40f..276ba65 100644 --- a/lib/Buf.jed +++ b/lib/Buf.jed @@ -1,6 +1,6 @@ (* A growable byte buffer, its memory from an allocator. *) module Buf; -import Mem; +import Mem, Arena, Rt; type Buf* = record @@ -66,6 +66,26 @@ b^.data := nil; b^.len := 0; b^.cap := 0; +end; + +(* The module's test, over an arena. *) +proc main*; +var ar: Arena.Arena; b: Buf; i: I64; s: []U8; +begin + if not Arena.init(ADR(ar), 1 shl 20) then Rt.stop("Buf: no arena"); end; + init(ADR(b), ADR(ar.base)); + if LEN(bytes(ADR(b))) # 0 then Rt.stop("Buf: not empty"); end; + for i := 0 to 9999 do add(ADR(b), U8(i mod 251)); end; + str(ADR(b), "end"); + s := bytes(ADR(b)); + if (LEN(s) # 10003) or (s[9998] # U8(9998 mod 251)) or (s[10000..10003] # "end") then + Rt.stop("Buf: contents"); + end; + reset(ADR(b)); + str(ADR(b), "again"); + if bytes(ADR(b)) # "again" then Rt.stop("Buf.reset"); end; + done(ADR(b)); + Arena.done(ADR(ar)); end; end Buf. diff --git a/lib/C.jed b/lib/C.jed index 57f05bd..d2ece07 100644 --- a/lib/C.jed +++ b/lib/C.jed @@ -4,6 +4,10 @@ module C; extern proc write*(fd: I32; buf: ^U8; n: U64): I64; +extern proc read*(fd: I32; buf: ^U8; n: U64): I64; +extern proc open*(path: ^U8; flags: I32; ...): I32; +extern proc creat*(path: ^U8; mode: U32): I32; +extern proc close*(fd: I32): I32; extern proc mmap*(addr: ^U8; n: U64; prot, flags, fd: I32; off: I64): ^U8; extern proc munmap*(addr: ^U8; n: U64): I32; extern proc isatty*(fd: I32): I32; diff --git a/lib/Fmt.jed b/lib/Fmt.jed index 4c29a1a..bc8e059 100644 --- a/lib/Fmt.jed +++ b/lib/Fmt.jed @@ -3,7 +3,7 @@ {{ and }} stand for { and }. print writes to standard output, write appends to a Buf. *) module Fmt; -import Out, Buf; +import Out, Buf, Arena, Rt; type Arg* = variant @@ -85,6 +85,21 @@ proc write*(b: ^Buf.Buf; f: []U8; args: []Arg); begin format(b, f, args); +end; + +(* The module's test: formats into a buffer over an arena. *) +proc main*; +var ar: Arena.Arena; b: Buf.Buf; +begin + if not Arena.init(ADR(ar), 1 shl 16) then Rt.stop("Fmt: no arena"); end; + Buf.init(ADR(b), ADR(ar.base)); + write(ADR(b), "{} {} {} {}|{}{}{} {{}}", [int(-42), uint(0FFFFFFFFFFFFFFFFH), hex(48879), + str("s"), char('c'), bool(true), int(0)]); + if Buf.bytes(ADR(b)) # "-42 18446744073709551615 BEEF s|ctrue0 {}" then Rt.stop("Fmt.write"); end; + Buf.reset(ADR(b)); + write(ADR(b), "{} and {}", [int(1)]); + if Buf.bytes(ADR(b)) # "1 and " then Rt.stop("Fmt: too few arguments"); end; + Arena.done(ADR(ar)); end; end Fmt. diff --git a/lib/Heap.jed b/lib/Heap.jed index aa50fe1..d51c8e5 100644 --- a/lib/Heap.jed +++ b/lib/Heap.jed @@ -78,7 +78,7 @@ end; proc free(a: ^Mem.Allocator; p: ^U8); -var h: ^Head; b: ^Block; +var h: ^Head; b: ^Block; c: I64; begin if p = nil then return; end; h := ^Head(p - Header); @@ -91,11 +91,35 @@ Lock.give(ADR(lock)); Mem.unmap(^U8(I64(h) - I64(h) mod Mem.Page), -h^.size); else + c := h^.size; (* before next, which takes its place, is set *) b := ^Block(h); - b^.next := lists[h^.size]; - lists[h^.size] := b; + b^.next := lists[c]; + lists[c] := b; Lock.give(ADR(lock)); end; +end; + +(* The module's test: blocks of every class and big ones, written, + freed and taken again. *) +proc main*; +var p: [40]^U8; i, j, n: I64; again: ^U8; +begin + for i := 0 to 39 do + n := (i * i * 37) mod 9000 + 1; + p[i] := Mem.alloc(heap, n, 8); + if (p[i] = nil) or (I64(p[i]) mod 8 # 0) then Rt.stop("Heap: alloc"); end; + for j := 0 to n - 1 do p[i][j] := U8(i); end; + end; + for i := 0 to 39 do + n := (i * i * 37) mod 9000 + 1; + if (p[i][0] # U8(i)) or (p[i][n - 1] # U8(i)) then Rt.stop("Heap: overlap"); end; + end; + Mem.free(heap, p[5]); + again := Mem.alloc(heap, (25 * 37) mod 9000 + 1, 8); + if again # p[5] then Rt.stop("Heap: a freed block isn't taken again"); end; + for i := 0 to 39 do Mem.free(heap, p[i]); end; + if Mem.alloc(heap, -1, 8) # nil then Rt.stop("Heap: negative size"); end; + if Mem.alloc(heap, 7FFFFFFFFFFFFFFFH - 4, 8) # nil then Rt.stop("Heap: huge size"); end; end; begin diff --git a/lib/Lock.jed b/lib/Lock.jed index 143c46d..cbb1179 100644 --- a/lib/Lock.jed +++ b/lib/Lock.jed @@ -3,7 +3,7 @@ giving up the processor now and then. It works with C's threads and with Thread's alike; Thread.Mutex is the one that sleeps. *) module Lock; -import Sys; +import Sys, Rt; type Lock* = record held: I64; end; @@ -20,6 +20,18 @@ proc give*(l: ^Lock); begin XCHG(ADR(l^.held), 0); +end; + +(* The module's test, in one thread: taken, given, taken again. *) +proc main*; +var l: Lock; +begin + take(ADR(l)); + if l.held # 1 then Rt.stop("Lock.take"); end; + give(ADR(l)); + take(ADR(l)); + give(ADR(l)); + if l.held # 0 then Rt.stop("Lock.give"); end; end; end Lock. diff --git a/lib/Mem.jed b/lib/Mem.jed index fa26f08..55ceb3c 100644 --- a/lib/Mem.jed +++ b/lib/Mem.jed @@ -1,7 +1,7 @@ (* Memory: Allocator, the interface every allocator offers, and pages from the system. NEW(p, a) calls a^.alloc. *) module Mem; -import Sys; +import Sys, Rt; type Allocator* = record @@ -30,6 +30,19 @@ proc free*(a: ^Allocator; p: ^U8); begin a^.free(a, p); +end; + +(* The module's test. *) +proc main*; +var p: ^U8; i: I64; +begin + p := pages(10000); + if p = nil then Rt.stop("Mem.pages"); end; + for i := 0 to 12287 do + if p[i] # 0 then Rt.stop("Mem.pages: not zeroed"); end; + end; + p[12287] := 1; + unmap(p, 10000); end; end Mem. diff --git a/lib/Str.jed b/lib/Str.jed index 22bb5b3..7519b07 100644 --- a/lib/Str.jed +++ b/lib/Str.jed @@ -1,6 +1,7 @@ (* Operations on strings, which are []U8. = and # compare them; this module orders, searches and hashes them. *) module Str; +import Rt; (* -1, 0 or 1 as a sorts before, with or after b, byte by byte. *) proc cmp*(a, b: []U8): I64; @@ -48,6 +49,18 @@ proc hasPrefix*(s, p: []U8): BOOL; begin return (LEN(s) >= LEN(p)) and (s[0..LEN(p)] = p); +end; + +(* The module's test: stops the program if something is wrong. *) +proc main*; +begin + if (cmp("a", "b") # -1) or (cmp("b", "a") # 1) or (cmp("ab", "ab") # 0) + or (cmp("a", "ab") # -1) or (cmp("", "") # 0) then Rt.stop("Str.cmp"); end; + if not eq("x", "x") or eq("x", "y") then Rt.stop("Str.eq"); end; + if (hash("") # 0CBF29CE484222325H) or (hash("a") # 0AF63DC4C8601EC8CH) then Rt.stop("Str.hash"); end; + if (find("hello", "ll") # 2) or (find("hello", "x") # -1) or (find("", "") # 0) + or (find("ab", "abc") # -1) then Rt.stop("Str.find"); end; + if not hasPrefix("hello", "he") or hasPrefix("he", "hello") then Rt.stop("Str.hasPrefix"); end; end; end Str. diff --git a/lib/Sys.jed b/lib/Sys.jed index c1a9dc7..2d40230 100644 --- a/lib/Sys.jed +++ b/lib/Sys.jed @@ -28,6 +28,54 @@ return r; end; +(* Reads into buf from fd; the number of bytes read, 0 at the end, or + minus an errno. *) +proc read*(fd: I64; buf: []U8): I64; +var r: I64; +begin + if LEN(buf) = 0 then return 0; end; + r := C.read(I32(fd), ADR(buf[0]), U64(LEN(buf))); + if r < 0 then return failed(); end; + return r; +end; + +(* path, a C string in buf, or false if it doesn't fit. *) +proc cstr(path: []U8; var buf: [4096]U8): BOOL; +var i: I64; +begin + if LEN(path) >= LEN(buf) then return false; end; + for i := 0 to LEN(path) - 1 do buf[i] := path[i]; end; + buf[LEN(path)] := 0; + return true; +end; + +(* The file at path, opened for reading: its file descriptor, or minus + an errno. *) +proc openRead*(path: []U8): I64; +var buf: [4096]U8; r: I64; +begin + if not cstr(path, buf) then return -36; end; + r := I64(C.open(ADR(buf[0]), 0)); (* O_RDONLY is 0 everywhere *) + if r < 0 then return failed(); end; + return r; +end; + +(* The file at path, made empty or new, opened for writing, readable by + all: its file descriptor, or minus an errno. *) +proc create*(path: []U8): I64; +var buf: [4096]U8; r: I64; +begin + if not cstr(path, buf) then return -36; end; + r := I64(C.creat(ADR(buf[0]), 420)); (* mode 644 *) + if r < 0 then return failed(); end; + return r; +end; + +proc close*(fd: I64); +begin + C.close(I32(fd)); +end; + (* size bytes of fresh, zeroed memory from the system, a multiple of pages, or nil. *) proc mmap*(size: I64): ^U8; @@ -57,6 +105,26 @@ proc exit*(status: I64); begin C._exit(I32(status)); +end; + +(* The module's test: a file written, read back and closed. *) +proc main*; +var fd, n: I64; buf: [16]U8; p: ^U8; +begin + fd := create("/tmp/jed-sys-test"); + if fd < 0 then exit(2); end; + if write(fd, "hello") # 5 then exit(2); end; + close(fd); + fd := openRead("/tmp/jed-sys-test"); + if fd < 0 then exit(2); end; + n := read(fd, buf); + if (n # 5) or (buf[0..5] # "hello") or (read(fd, buf) # 0) then exit(2); end; + close(fd); + if openRead("/no/such/file") >= 0 then exit(2); end; + if isTerminal(-1) then exit(2); end; + p := mmap(100); + if (p = nil) or (p[99] # 0) then exit(2); end; + munmap(p, 100); end; end Sys. diff --git a/lib/Sys.linux-x86_64.jed b/lib/Sys.linux-x86_64.jed index e1bcb53..7e707e8 100644 --- a/lib/Sys.linux-x86_64.jed +++ b/lib/Sys.linux-x86_64.jed @@ -15,6 +15,47 @@ return SYSCALL(1, fd, ADR(s[0]), LEN(s)); end; +(* Reads into buf from fd; the number of bytes read, 0 at the end, or + minus an errno. *) +proc read*(fd: I64; buf: []U8): I64; +begin + if LEN(buf) = 0 then return 0; end; + return SYSCALL(0, fd, ADR(buf[0]), LEN(buf)); +end; + +(* path, a C string in buf, or false if it doesn't fit. *) +proc cstr(path: []U8; var buf: [4096]U8): BOOL; +var i: I64; +begin + if LEN(path) >= LEN(buf) then return false; end; + for i := 0 to LEN(path) - 1 do buf[i] := path[i]; end; + buf[LEN(path)] := 0; + return true; +end; + +(* The file at path, opened for reading: its file descriptor, or minus + an errno. *) +proc openRead*(path: []U8): I64; +var buf: [4096]U8; +begin + if not cstr(path, buf) then return -36; end; (* ENAMETOOLONG *) + return SYSCALL(2, ADR(buf), 0); (* open, O_RDONLY *) +end; + +(* The file at path, made empty or new, opened for writing, readable by + all: its file descriptor, or minus an errno. *) +proc create*(path: []U8): I64; +var buf: [4096]U8; +begin + if not cstr(path, buf) then return -36; end; + return SYSCALL(85, ADR(buf), 420); (* creat, mode 644 *) +end; + +proc close*(fd: I64); +begin + SYSCALL(3, fd); +end; + (* size bytes of fresh, zeroed memory from the system, a multiple of pages, or nil. *) proc mmap*(size: I64): ^U8; @@ -48,6 +89,26 @@ proc exit*(status: I64); begin SYSCALL(231, status); +end; + +(* The module's test: a file written, read back and closed. *) +proc main*; +var fd, n: I64; buf: [16]U8; p: ^U8; +begin + fd := create("/tmp/jed-sys-test"); + if fd < 0 then exit(2); end; + if write(fd, "hello") # 5 then exit(2); end; + close(fd); + fd := openRead("/tmp/jed-sys-test"); + if fd < 0 then exit(2); end; + n := read(fd, buf); + if (n # 5) or (buf[0..5] # "hello") or (read(fd, buf) # 0) then exit(2); end; + close(fd); + if openRead("/no/such/file") >= 0 then exit(2); end; + if isTerminal(-1) then exit(2); end; + p := mmap(100); + if (p = nil) or (p[99] # 0) then exit(2); end; + munmap(p, 100); end; end Sys. diff --git a/src/jed.pas b/src/jed.pas index 8c274b0..46d63c2 100644 --- a/src/jed.pas +++ b/src/jed.pas @@ -1,6 +1,7 @@ { jed.pas: the command line. jed [-I dir]... [-o file] Main.jed + jed --version Compiles the main module and every module it imports, writes the entry point, links what it reaches, and writes the executable: by @@ -22,6 +23,10 @@ uses math, scan, sym, x64, gen, parse, link, elf; +const + Version = '0.3.0'; + Report = '2026-10.0'; { the version of doc/jed.md it follows } + { The modules the program uses: the main module, Rt, and every module they import. A module read only for the interface check is not. } procedure Use(m: PModule); @@ -99,6 +104,9 @@ end else if s = '-o' then begin inc(i); outPath := ParamStr(i); + end else if s = '--version' then begin + writeln('jed ', Version, ', report ', Report); + Halt(0); end else if (s <> '') and (s[1] = '-') then Fatal('unknown option ' + s) else path := s; diff --git a/test/check.sh b/test/check.sh index 9f12869..defc208 100755 --- a/test/check.sh +++ b/test/check.sh @@ -7,7 +7,9 @@ # X.status its exit status (optional; 0 if missing) # A program without X.expect is compiled, not run. test/libc puts # lib/Sys.jed, over the C library, before the system call one. -# Programs in test/err must not compile, and must print X.err. +# Programs in test/err must not compile, and must print X.err. Then +# the version, tools/Doc.jed, the report's examples, and the library's +# own tests. jed=./jed out=build/check @@ -57,5 +59,49 @@ pass=$((pass + 1)) fi done +# the report's version is the one the compiler says it follows +want=$(sed -n 's/^This is version \([0-9.-]*\) of the report.*/\1/p' doc/jed.md) +if ./jed --version | grep -qF "report $want"; then pass=$((pass + 1)) +else echo "FAIL jed --version: not report $want"; fail=$((fail + 1)); fi + +# tools/Doc.jed: on every library module, and as test/doc expects +if ./jed -o $out/Doc tools/Doc.jed 2>$out/Doc.cerr; then + for f in lib/*.jed; do + if ! $out/Doc $f > $out/doc.out; then echo "FAIL Doc $f"; fail=$((fail + 1)); fi + done + for f in test/doc/*.expect; do + name=$(basename $f .expect) + m=lib/$name.jed + [ -f $m ] || m=lib/$name.linux-x86_64.jed + if $out/Doc $m | cmp -s - $f; then pass=$((pass + 1)) + else echo "FAIL Doc $m: output differs"; fail=$((fail + 1)); fi + done + + # the report's examples that are whole modules: each compiles, and + # each with a main runs and ends with status 0 + rm -rf $out/report; mkdir -p $out/report + for name in $($out/Doc examples doc/jed.md $out/report); do + if ! $jed -o $out/report/$name $out/report/$name.jed 2>$out/report/$name.cerr; then + echo "FAIL report example $name: $(cat $out/report/$name.cerr)"; fail=$((fail + 1)) + elif grep -q '^proc main\*' $out/report/$name.jed && ! $out/report/$name > /dev/null 2>&1; then + echo "FAIL report example $name: status $?"; fail=$((fail + 1)) + else pass=$((pass + 1)); fi + done +else + echo "FAIL tools/Doc.jed: $(cat $out/Doc.cerr)"; fail=$((fail + 1)) +fi + +# each library module's main is its test; Sys over C is tested in test/libc +for f in lib/*.jed; do + [ $f = lib/Sys.jed ] && continue + grep -q '^proc main\*' $f || continue + name=lib_$(basename $f .jed) + if ! $jed -o $out/$name $f 2>$out/$name.cerr; then + echo "FAIL $f: doesn't compile: $(cat $out/$name.cerr)"; fail=$((fail + 1)) + elif ! $out/$name > $out/$name.out 2>&1; then + echo "FAIL $f: its test says $(cat $out/$name.out)"; fail=$((fail + 1)) + else pass=$((pass + 1)); fi +done + echo "$pass passed, $fail failed" [ $fail = 0 ] diff --git a/test/doc/Str.expect b/test/doc/Str.expect new file mode 100644 index 0000000..f44b026 --- /dev/null +++ b/test/doc/Str.expect @@ -0,0 +1,19 @@ +module Str; + Operations on strings, which are []U8. = and # compare them; this + module orders, searches and hashes them. + +proc cmp*(a, b: []U8): I64; + -1, 0 or 1 as a sorts before, with or after b, byte by byte. + +proc eq*(a, b: []U8): BOOL; + +proc hash*(s: []U8): U64; + FNV-1a, 64 bits. + +proc find*(s, sub: []U8): I64; + Where sub first starts in s, or -1. + +proc hasPrefix*(s, p: []U8): BOOL; + +proc main*; + The module's test: stops the program if something is wrong. diff --git a/test/doc/Thread.expect b/test/doc/Thread.expect new file mode 100644 index 0000000..30d4c31 --- /dev/null +++ b/test/doc/Thread.expect @@ -0,0 +1,29 @@ +module Thread; + Threads for programs without C, made with the clone system call. + Each runs on a stack of its own, mmaped, with a page below it that + can't be touched, so that running off the stack stops the program + rather than spoiling what lies below. join waits on the thread's id, + which the kernel clears, and wakes it for, when the thread ends; + a Mutex sleeps on a futex. A program with C threads uses C's, + through pthread_create, and so can't use these. + +const Failed*: ERROR = "can't start a thread"; + +type Thread* = record + tid: I64; (* the kernel's id, until the thread ends *) + base: ^U8; (* its stack's mapping, the guard page first *) + size: I64; +end; + +type Mutex* = record state: I64; end; + 0 free, 1 taken, 2 taken with a thread waiting + +proc spawn*(p: proc(arg: ^U8); arg: ^U8): (^Thread, ERROR); + Starts p(arg) in a new thread. + +proc join*(t: ^Thread); + Waits for t to end, then frees its stack. + +proc lock*(m: ^Mutex); + +proc unlock*(m: ^Mutex); diff --git a/test/libc/SysTest.expect b/test/libc/SysTest.expect new file mode 100644 index 0000000..e69de29 diff --git a/test/libc/SysTest.jed b/test/libc/SysTest.jed new file mode 100644 index 0000000..88666c4 --- /dev/null +++ b/test/libc/SysTest.jed @@ -0,0 +1,10 @@ +(* Sys over the C library, through its own test. *) +module SysTest; +import Sys; + +proc main*; +begin + Sys.main; +end; + +end SysTest. diff --git a/tools/Doc.jed b/tools/Doc.jed new file mode 100644 index 0000000..979a81a --- /dev/null +++ b/tools/Doc.jed @@ -0,0 +1,422 @@ +(* Doc: what a module offers, from its source. + + Doc Module.jed + prints the module's own comment, the one just before module, then + each exported declaration as it is written, with the comment just + before it. + + Doc examples report.md dir + writes each example of the report that is a whole module, from + module to end, with no ... left out, to dir/Name.jed, and prints + the names, one a line. *) +module Doc; +import Out, Sys, Rt, Str, Buf, Heap; + +type + Kind = (ident, other, eof); + +var + src: []U8; (* the text being read *) + pos: I64; + kind: Kind; (* the current token *) + tokStart, tokEnd: I64; + docStart, docEnd: I64; (* the comment just before it, or -1 *) + +(* Argument i, a C string, as a slice. *) +proc arg(i: I64): []U8; +var s: ^U8; n: I64; +begin + s := ARGV()[i]; + n := 0; + while s[n] # 0 do INC(n); end; + return s[0..n]; +end; + +proc readFile(path: []U8; b: ^Buf.Buf): BOOL; +var fd, n: I64; chunk: [4096]U8; +begin + fd := Sys.openRead(path); + if fd < 0 then return false; end; + repeat + n := Sys.read(fd, chunk); + if n > 0 then Buf.str(b, chunk[0..n]); end; + until n <= 0; + Sys.close(fd); + return n = 0; +end; + +proc writeFile(path, data: []U8): BOOL; +var fd, n, at: I64; +begin + fd := Sys.create(path); + if fd < 0 then return false; end; + at := 0; + while at < LEN(data) do + n := Sys.write(fd, data[at..LEN(data)]); + if n <= 0 then Sys.close(fd); return false; end; + at := at + n; + end; + Sys.close(fd); + return true; +end; + +proc isSpace(c: U8): BOOL; +begin + return (c = ' ') or (c = 9) or (c = 10) or (c = 13); +end; + +proc isWord(c: U8): BOOL; +begin + return (c >= 'a') and (c <= 'z') or (c >= 'A') and (c <= 'Z') + or (c >= '0') and (c <= '9') or (c = '_'); +end; + +(* Past a string or character literal that starts at pos. *) +proc skipLiteral; +var q: U8; +begin + q := src[pos]; + INC(pos); + while (pos < LEN(src)) and (src[pos] # q) do + if src[pos] = '\\' then INC(pos); end; + INC(pos); + end; + INC(pos); +end; + +(* Past the comment at pos. *) +proc skipComment; +var depth: I64; +begin + depth := 0; + repeat + if (src[pos] = '(') and (pos + 1 < LEN(src)) and (src[pos + 1] = '*') then + INC(depth); INC(pos, 2); + elsif (src[pos] = '*') and (pos + 1 < LEN(src)) and (src[pos + 1] = ')') then + DEC(depth); INC(pos, 2); + else + INC(pos); + end; + until (depth = 0) or (pos >= LEN(src)); +end; + +proc atComment(p: I64): BOOL; +begin + return (p + 1 < LEN(src)) and (src[p] = '(') and (src[p + 1] = '*'); +end; + +(* Past spaces and comments, keeping where the last comment that starts + a line was: one after code on its line belongs to that code. *) +proc skipSpace; +var start: I64; newLine: BOOL; +begin + docStart := -1; + newLine := pos = 0; + while pos < LEN(src) do + if isSpace(src[pos]) then + if src[pos] = 10 then newLine := true; end; + INC(pos); + elsif atComment(pos) then + start := pos; + skipComment; + if newLine then docStart := start; docEnd := pos; end; + else + break; + end; + end; +end; + +proc next; +begin + skipSpace; + tokStart := pos; + if pos >= LEN(src) then + kind := eof; + elsif isWord(src[pos]) then + while (pos < LEN(src)) and isWord(src[pos]) do INC(pos); end; + kind := ident; + elsif (src[pos] = '"') or (src[pos] = '\'') then + skipLiteral; + kind := other; + else + INC(pos); + kind := other; + end; + tokEnd := pos; +end; + +proc text: []U8; +begin + return src[tokStart..tokEnd]; +end; + +proc is(s: []U8): BOOL; +begin + return text() = s; +end; + +(* Whether the current word starts a declaration, a section or the end. *) +proc reserved: BOOL; +begin + return is("const") or is("type") or is("var") or is("proc") or is("extern") + or is("import") or is("link") or is("begin") or is("end"); +end; + +(* On to the ; that ends what starts here, past anything bracketed and + any record or variant; returns where it ends. *) +proc toSemicolon: I64; +var depth: I64; +begin + depth := 0; + while kind # eof do + if is("(") or is("[") or is("{") or is("record") or is("variant") then INC(depth); + elsif is(")") or is("]") or is("}") or is("end") then DEC(depth); + elsif is(";") and (depth <= 0) then return tokEnd; + end; + next; + end; + return pos; +end; + +(* stop, or the end of a comment that follows it on its line. *) +proc trailing(stop: I64): I64; +var save: I64; +begin + while (stop < LEN(src)) and ((src[stop] = ' ') or (src[stop] = 9)) do INC(stop); end; + if not atComment(stop) then return stop; end; + save := pos; + pos := stop; + skipComment; + stop := pos; + pos := save; + return stop; +end; + +(* Where the line holding p starts. *) +proc lineStart(p: I64): I64; +begin + while (p > 0) and (src[p - 1] # 10) do DEC(p); end; + return p; +end; + +(* src[start..stop] as written, each line after the first moved left as + far as the first one's column. *) +proc block(start, stop: I64); +var col, i, n: I64; +begin + col := start - lineStart(start); + i := start; + while i < stop do + Out.char(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; +end; + +(* The comment from start to stop, without its brackets, each line + indented by four. *) +proc comment(start, stop: I64); +var i, j: I64; atLine: BOOL; +begin + if start < 0 then return; end; + i := start + 2; + j := stop - 2; + while (i < j) and isSpace(src[i]) do INC(i); end; + while (j > i) and isSpace(src[j - 1]) do DEC(j); end; + atLine := true; + 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; + end; + INC(i); + end; + Out.ln; +end; + +(* One declaration, from its first token, the comment before it kept. *) +proc declaration(prefix: []U8; exported: BOOL; start, ds, de, stop: I64); +begin + if not exported then return; end; + Out.ln; + Out.str(prefix); + block(start, trailing(stop)); + comment(ds, de); +end; + +(* name, and whether a * follows it; leaves the token after them. *) +proc name: BOOL; +begin + next; + next; + if is("*") then next; return true; end; + return false; +end; + +(* A procedure's body: to the end that closes its begin, the procedure + name after it and its ;. *) +proc skipBody; +var depth: I64; sawBegin: BOOL; +begin + depth := 0; + sawBegin := false; + while kind # eof do + if is("begin") then + if depth = 0 then sawBegin := true; end; + INC(depth); + elsif is("if") or is("while") or is("for") or is("match") or is("record") or is("variant") then + INC(depth); + elsif is("end") then + DEC(depth); + if (depth = 0) and sawBegin then + next; + if kind = ident then next; end; + return; + end; + end; + next; + end; +end; + +proc procedure; +var start, ds, de, stop: I64; exp: BOOL; +begin + start := tokStart; ds := docStart; de := docEnd; + exp := name(); + stop := toSemicolon(); + next; + if is("forward") or is("code") then toSemicolon(); + else skipBody; + end; + declaration("", exp, start, ds, de, stop); + next; +end; + +proc section; +var word: []U8; start, ds, de, stop: I64; exp: BOOL; +begin + word := text(); + next; + while (kind = ident) and not reserved() do + start := tokStart; ds := docStart; de := docEnd; + pos := tokStart; (* 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; + next; + end; +end; + +proc external; +var start, ds, de, stop: I64; exp: BOOL; +begin + start := tokStart; ds := docStart; de := docEnd; + next; + exp := name(); + stop := toSemicolon(); + declaration("", exp, start, ds, de, stop); + next; +end; + +proc document(path: []U8); +var b: Buf.Buf; start, ds, de: I64; +begin + Buf.init(ADR(b), Heap.heap); + if not readFile(path, ADR(b)) then Rt.stop("Doc: can't read the file"); end; + src := Buf.bytes(ADR(b)); + pos := 0; + next; + while (kind # eof) and not is("module") do next; end; + if kind = eof then Rt.stop("Doc: no module"); end; + start := tokStart; + ds := docStart; de := docEnd; + block(start, toSemicolon()); + comment(ds, de); + next; + while (kind # eof) and not is("begin") and not is("end") do + if is("const") or is("type") or is("var") then section; + elsif is("proc") then procedure; + elsif is("extern") then external; + elsif is("import") or is("link") then toSemicolon(); next; + else next; + end; + end; +end; + +(* line, without the spaces at its ends *) +proc trim(line: []U8): []U8; +var i, j: I64; +begin + i := 0; + j := LEN(line); + while (i < j) and isSpace(line[i]) do INC(i); end; + while (j > i) and isSpace(line[j - 1]) do DEC(j); end; + return line[i..j]; +end; + +(* The report's examples that are whole modules, each to dir. *) +proc examples(path, dir: []U8); +var + b, out: Buf.Buf; s, line, first, last, name: []U8; i, start, k, n: I64; + inBlock, whole: BOOL; +begin + Buf.init(ADR(b), Heap.heap); + Buf.init(ADR(out), Heap.heap); + if not readFile(path, ADR(b)) then Rt.stop("Doc: can't read the report"); end; + s := Buf.bytes(ADR(b)); + inBlock := false; + start := 0; + i := 0; + while i < LEN(s) do + k := i; + while (k < LEN(s)) and (s[k] # 10) do INC(k); end; + line := s[i..k]; + if trim(line) = "```" then + if inBlock and whole and Str.hasPrefix(first, "module ") and Str.hasPrefix(last, "end ") then + name := first[7..LEN(first)]; + n := 0; + while (n < LEN(name)) and isWord(name[n]) do INC(n); end; + name := name[0..n]; + Buf.reset(ADR(out)); + Buf.str(ADR(out), dir); Buf.str(ADR(out), "/"); Buf.str(ADR(out), name); + Buf.str(ADR(out), ".jed"); + if not writeFile(Buf.bytes(ADR(out)), s[start..i]) then + Rt.stop("Doc: can't write an example"); + end; + Out.str(name); Out.ln; + end; + inBlock := not inBlock; + start := k + 1; + first := ""; last := ""; + whole := true; + elsif inBlock then + if trim(line) = "..." then whole := false; end; + if LEN(trim(line)) > 0 then + if LEN(first) = 0 then first := trim(line); end; + last := trim(line); + end; + end; + i := k + 1; + end; +end; + +proc main*; +begin + if (ARGC() = 4) and (arg(1) = "examples") then examples(arg(2), arg(3)); + elsif ARGC() = 2 then document(arg(1)); + else Rt.stop("usage: Doc Module.jed, or Doc examples report.md dir"); + end; +end; + +end Doc.