raz / commit 21350d28

commit
21350d288053addebabaec43ba3ca1ad1e1b7af3
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
e6d05672
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.

patch · log

19 files changed, +855 -26

PLAN.md (history) modified +3 -2

@@ -129,7 +129,7 @@
129129
130130 Tools
131131
132-- [ ] 15. Versions first: the report says its version, by date
132+- [x] 15. Versions first: the report says its version, by date
133133 (`2026-10.0`, then `.1` for the next change that month), and
134134 ends with what each version changed; `jed --version` prints its
135135 own and the report's, `jed 0.3.0, report 2026-10.0`. Each
@@ -141,7 +141,8 @@
141141 `make check` also compiles those examples, running the ones with
142142 a `main`, and runs each library module's `main` as its test.
143143 The test runner stays in the Makefile: it must work when the
144- compiler doesn't.
144+ compiler doesn't. (Tagging is left for later, at the owner's
145+ word.)
145146
146147 - [ ] 16. Editor support: `editor/vim` (`syntax/jed.vim`, `indent/jed.vim`
147148 by `shiftwidth()`, and `ftdetect/jed.vim`, which sets 4 spaces)

doc/jed.md modified +63 -15

@@ -9,6 +9,9 @@
99 This report defines the language. It is short on purpose. Whatever it
1010 doesn't allow, jed doesn't have.
1111
12+This is version 2026-10.0 of the report; section 12 says what each
13+version changed.
14+
1215 ## 1. A first look
1316
1417 ```
@@ -642,10 +645,32 @@
642645 reached run.
643646
644647 ```
645-fd := Sys.open(name, Sys.rdonly);
646-if fd < 0 then return Sys.Failed; end;
647-defer Sys.close(fd);
648-...
648+module Head;
649+import Sys, Out;
650+
651+(* Prints the first line of the file at path. *)
652+proc head(path: []U8): BOOL;
653+var fd, n, i: I64; buf: [256]U8;
654+begin
655+ fd := Sys.openRead(path);
656+ if fd < 0 then return false; end;
657+ defer Sys.close(fd);
658+ n := Sys.read(fd, buf);
659+ if n < 0 then return false; end;
660+ for i := 0 to n - 1 do
661+ if buf[i] = 10 then n := i; break; end;
662+ end;
663+ Out.str(buf[0..n]);
664+ Out.ln;
665+ return true;
666+end;
667+
668+proc main*;
669+begin
670+ if not head("/etc/hostname") then Out.str("no hostname\n"); end;
671+end;
672+
673+end Head.
649674 ```
650675
651676 `return e;` evaluates `e` before the deferred statements run. A
@@ -682,12 +707,22 @@
682707 assignment, or alone as a statement.
683708
684709 ```
710+module Divide;
711+import Out;
712+
685713 proc divmod(a, b: I64): (I64, I64);
686714 begin
687715 return a div b, a mod b;
688716 end;
689717
690-q, r := divmod(17, 5);
718+proc main*;
719+var q, r: I64;
720+begin
721+ q, r := divmod(17, 5);
722+ Out.int(q); Out.str(" rest "); Out.int(r); Out.ln;
723+end;
724+
725+end Divide.
691726 ```
692727
693728 **forward.** `proc f(x: I64): I64; forward;` declares `f` so that it
@@ -814,7 +849,7 @@
814849 | Module | What |
815850 |---|---|
816851 | `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 |
817-| `Sys` | system calls: `write`, `mmap`, `munmap`, `yield`, `exit`; one file per target, and `Sys.jed` over the C library for the others |
852+| `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 |
818853 | `Out` | writing text and numbers to standard output, buffered; safe to call from several threads |
819854 | `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C |
820855 | `Lock` | a lock for short sections, for C's threads and `Thread`'s alike |
@@ -847,13 +882,26 @@
847882 text comes out of order:
848883
849884 ```
850-extern proc printf(format: ^U8; ...): I32;
851-extern proc fflush(f: ^U8): I32;
852-extern var stdout: ^U8; (* C's FILE *stdout *)
853-...
854-Out.str("from jed\n");
855-Out.flush;
856-printf("from C\n");
857-fflush(stdout);
858-Out.str("from jed again\n");
885+module Both;
886+import Out, C; (* C: printf, fflush and C's FILE *stdout *)
887+
888+proc main*;
889+begin
890+ Out.str("from jed\n");
891+ Out.flush;
892+ C.printf("from C\n");
893+ C.fflush(C.stdout);
894+ Out.str("from jed again\n");
895+end;
896+
897+end Both.
859898 ```
899+
900+## 12. Versions
901+
902+A version of this report is named by the month it was made in and a
903+count: `2026-10.0`, then `2026-10.1` for the next change that month.
904+`jed --version` says which version the compiler follows.
905+
906+- **2026-10.0.** The first numbered version: the language of compiler
907+ 0.3.0.

lib/Arena.jed modified +17 -1

@@ -2,7 +2,7 @@
22 at once: free does nothing, reset forgets everything. An Arena is
33 its allocator: ADR(arena.base) is the ^Mem.Allocator to give NEW. *)
44 module Arena;
5-import Mem;
5+import Mem, Rt;
66
77 type
88 Arena* = record
@@ -47,6 +47,22 @@
4747 if ar^.buf # nil then Mem.unmap(ar^.buf, ar^.cap); end;
4848 ar^.buf := nil;
4949 ar^.cap := 0;
50+end;
51+
52+(* The module's test. *)
53+proc main*;
54+var ar: Arena; a: ^Mem.Allocator; p, q: ^U8;
55+begin
56+ if not init(ADR(ar), 4096) then Rt.stop("Arena.init"); end;
57+ a := ADR(ar.base);
58+ p := Mem.alloc(a, 3, 1);
59+ q := Mem.alloc(a, 8, 8);
60+ if (p = nil) or (q # p + 8) then Rt.stop("Arena: alignment"); end;
61+ if Mem.alloc(a, 5000, 1) # nil then Rt.stop("Arena: too big"); end;
62+ if Mem.alloc(a, -1, 1) # nil then Rt.stop("Arena: negative"); end;
63+ reset(ADR(ar));
64+ if Mem.alloc(a, 4096, 1) # p then Rt.stop("Arena.reset"); end;
65+ done(ADR(ar));
5066 end;
5167
5268 end Arena.

lib/Buf.jed modified +21 -1

@@ -1,6 +1,6 @@
11 (* A growable byte buffer, its memory from an allocator. *)
22 module Buf;
3-import Mem;
3+import Mem, Arena, Rt;
44
55 type
66 Buf* = record
@@ -66,6 +66,26 @@
6666 b^.data := nil;
6767 b^.len := 0;
6868 b^.cap := 0;
69+end;
70+
71+(* The module's test, over an arena. *)
72+proc main*;
73+var ar: Arena.Arena; b: Buf; i: I64; s: []U8;
74+begin
75+ if not Arena.init(ADR(ar), 1 shl 20) then Rt.stop("Buf: no arena"); end;
76+ init(ADR(b), ADR(ar.base));
77+ if LEN(bytes(ADR(b))) # 0 then Rt.stop("Buf: not empty"); end;
78+ for i := 0 to 9999 do add(ADR(b), U8(i mod 251)); end;
79+ str(ADR(b), "end");
80+ s := bytes(ADR(b));
81+ if (LEN(s) # 10003) or (s[9998] # U8(9998 mod 251)) or (s[10000..10003] # "end") then
82+ Rt.stop("Buf: contents");
83+ end;
84+ reset(ADR(b));
85+ str(ADR(b), "again");
86+ if bytes(ADR(b)) # "again" then Rt.stop("Buf.reset"); end;
87+ done(ADR(b));
88+ Arena.done(ADR(ar));
6989 end;
7090
7191 end Buf.

lib/C.jed modified +4 -0

@@ -4,6 +4,10 @@
44 module C;
55
66 extern proc write*(fd: I32; buf: ^U8; n: U64): I64;
7+extern proc read*(fd: I32; buf: ^U8; n: U64): I64;
8+extern proc open*(path: ^U8; flags: I32; ...): I32;
9+extern proc creat*(path: ^U8; mode: U32): I32;
10+extern proc close*(fd: I32): I32;
711 extern proc mmap*(addr: ^U8; n: U64; prot, flags, fd: I32; off: I64): ^U8;
812 extern proc munmap*(addr: ^U8; n: U64): I32;
913 extern proc isatty*(fd: I32): I32;

lib/Fmt.jed modified +16 -1

@@ -3,7 +3,7 @@
33 {{ and }} stand for { and }. print writes to standard output,
44 write appends to a Buf. *)
55 module Fmt;
6-import Out, Buf;
6+import Out, Buf, Arena, Rt;
77
88 type
99 Arg* = variant
@@ -85,6 +85,21 @@
8585 proc write*(b: ^Buf.Buf; f: []U8; args: []Arg);
8686 begin
8787 format(b, f, args);
88+end;
89+
90+(* The module's test: formats into a buffer over an arena. *)
91+proc main*;
92+var ar: Arena.Arena; b: Buf.Buf;
93+begin
94+ if not Arena.init(ADR(ar), 1 shl 16) then Rt.stop("Fmt: no arena"); end;
95+ Buf.init(ADR(b), ADR(ar.base));
96+ write(ADR(b), "{} {} {} {}|{}{}{} {{}}", [int(-42), uint(0FFFFFFFFFFFFFFFFH), hex(48879),
97+ str("s"), char('c'), bool(true), int(0)]);
98+ if Buf.bytes(ADR(b)) # "-42 18446744073709551615 BEEF s|ctrue0 {}" then Rt.stop("Fmt.write"); end;
99+ Buf.reset(ADR(b));
100+ write(ADR(b), "{} and {}", [int(1)]);
101+ if Buf.bytes(ADR(b)) # "1 and " then Rt.stop("Fmt: too few arguments"); end;
102+ Arena.done(ADR(ar));
88103 end;
89104
90105 end Fmt.

lib/Heap.jed modified +27 -3

@@ -78,7 +78,7 @@
7878 end;
7979
8080 proc free(a: ^Mem.Allocator; p: ^U8);
81-var h: ^Head; b: ^Block;
81+var h: ^Head; b: ^Block; c: I64;
8282 begin
8383 if p = nil then return; end;
8484 h := ^Head(p - Header);
@@ -91,11 +91,35 @@
9191 Lock.give(ADR(lock));
9292 Mem.unmap(^U8(I64(h) - I64(h) mod Mem.Page), -h^.size);
9393 else
94+ c := h^.size; (* before next, which takes its place, is set *)
9495 b := ^Block(h);
95- b^.next := lists[h^.size];
96- lists[h^.size] := b;
96+ b^.next := lists[c];
97+ lists[c] := b;
9798 Lock.give(ADR(lock));
9899 end;
100+end;
101+
102+(* The module's test: blocks of every class and big ones, written,
103+ freed and taken again. *)
104+proc main*;
105+var p: [40]^U8; i, j, n: I64; again: ^U8;
106+begin
107+ for i := 0 to 39 do
108+ n := (i * i * 37) mod 9000 + 1;
109+ p[i] := Mem.alloc(heap, n, 8);
110+ if (p[i] = nil) or (I64(p[i]) mod 8 # 0) then Rt.stop("Heap: alloc"); end;
111+ for j := 0 to n - 1 do p[i][j] := U8(i); end;
112+ end;
113+ for i := 0 to 39 do
114+ n := (i * i * 37) mod 9000 + 1;
115+ if (p[i][0] # U8(i)) or (p[i][n - 1] # U8(i)) then Rt.stop("Heap: overlap"); end;
116+ end;
117+ Mem.free(heap, p[5]);
118+ again := Mem.alloc(heap, (25 * 37) mod 9000 + 1, 8);
119+ if again # p[5] then Rt.stop("Heap: a freed block isn't taken again"); end;
120+ for i := 0 to 39 do Mem.free(heap, p[i]); end;
121+ if Mem.alloc(heap, -1, 8) # nil then Rt.stop("Heap: negative size"); end;
122+ if Mem.alloc(heap, 7FFFFFFFFFFFFFFFH - 4, 8) # nil then Rt.stop("Heap: huge size"); end;
99123 end;
100124
101125 begin

lib/Lock.jed modified +13 -1

@@ -3,7 +3,7 @@
33 giving up the processor now and then. It works with C's threads and
44 with Thread's alike; Thread.Mutex is the one that sleeps. *)
55 module Lock;
6-import Sys;
6+import Sys, Rt;
77
88 type Lock* = record held: I64; end;
99
@@ -20,6 +20,18 @@
2020 proc give*(l: ^Lock);
2121 begin
2222 XCHG(ADR(l^.held), 0);
23+end;
24+
25+(* The module's test, in one thread: taken, given, taken again. *)
26+proc main*;
27+var l: Lock;
28+begin
29+ take(ADR(l));
30+ if l.held # 1 then Rt.stop("Lock.take"); end;
31+ give(ADR(l));
32+ take(ADR(l));
33+ give(ADR(l));
34+ if l.held # 0 then Rt.stop("Lock.give"); end;
2335 end;
2436
2537 end Lock.

lib/Mem.jed modified +14 -1

@@ -1,7 +1,7 @@
11 (* Memory: Allocator, the interface every allocator offers, and pages
22 from the system. NEW(p, a) calls a^.alloc. *)
33 module Mem;
4-import Sys;
4+import Sys, Rt;
55
66 type
77 Allocator* = record
@@ -30,6 +30,19 @@
3030 proc free*(a: ^Allocator; p: ^U8);
3131 begin
3232 a^.free(a, p);
33+end;
34+
35+(* The module's test. *)
36+proc main*;
37+var p: ^U8; i: I64;
38+begin
39+ p := pages(10000);
40+ if p = nil then Rt.stop("Mem.pages"); end;
41+ for i := 0 to 12287 do
42+ if p[i] # 0 then Rt.stop("Mem.pages: not zeroed"); end;
43+ end;
44+ p[12287] := 1;
45+ unmap(p, 10000);
3346 end;
3447
3548 end Mem.

lib/Str.jed modified +13 -0

@@ -1,6 +1,7 @@
11 (* Operations on strings, which are []U8. = and # compare them; this
22 module orders, searches and hashes them. *)
33 module Str;
4+import Rt;
45
56 (* -1, 0 or 1 as a sorts before, with or after b, byte by byte. *)
67 proc cmp*(a, b: []U8): I64;
@@ -48,6 +49,18 @@
4849 proc hasPrefix*(s, p: []U8): BOOL;
4950 begin
5051 return (LEN(s) >= LEN(p)) and (s[0..LEN(p)] = p);
52+end;
53+
54+(* The module's test: stops the program if something is wrong. *)
55+proc main*;
56+begin
57+ if (cmp("a", "b") # -1) or (cmp("b", "a") # 1) or (cmp("ab", "ab") # 0)
58+ or (cmp("a", "ab") # -1) or (cmp("", "") # 0) then Rt.stop("Str.cmp"); end;
59+ if not eq("x", "x") or eq("x", "y") then Rt.stop("Str.eq"); end;
60+ if (hash("") # 0CBF29CE484222325H) or (hash("a") # 0AF63DC4C8601EC8CH) then Rt.stop("Str.hash"); end;
61+ if (find("hello", "ll") # 2) or (find("hello", "x") # -1) or (find("", "") # 0)
62+ or (find("ab", "abc") # -1) then Rt.stop("Str.find"); end;
63+ if not hasPrefix("hello", "he") or hasPrefix("he", "hello") then Rt.stop("Str.hasPrefix"); end;
5164 end;
5265
5366 end Str.

lib/Sys.jed modified +68 -0

@@ -28,6 +28,54 @@
2828 return r;
2929 end;
3030
31+(* Reads into buf from fd; the number of bytes read, 0 at the end, or
32+ minus an errno. *)
33+proc read*(fd: I64; buf: []U8): I64;
34+var r: I64;
35+begin
36+ if LEN(buf) = 0 then return 0; end;
37+ r := C.read(I32(fd), ADR(buf[0]), U64(LEN(buf)));
38+ if r < 0 then return failed(); end;
39+ return r;
40+end;
41+
42+(* path, a C string in buf, or false if it doesn't fit. *)
43+proc cstr(path: []U8; var buf: [4096]U8): BOOL;
44+var i: I64;
45+begin
46+ if LEN(path) >= LEN(buf) then return false; end;
47+ for i := 0 to LEN(path) - 1 do buf[i] := path[i]; end;
48+ buf[LEN(path)] := 0;
49+ return true;
50+end;
51+
52+(* The file at path, opened for reading: its file descriptor, or minus
53+ an errno. *)
54+proc openRead*(path: []U8): I64;
55+var buf: [4096]U8; r: I64;
56+begin
57+ if not cstr(path, buf) then return -36; end;
58+ r := I64(C.open(ADR(buf[0]), 0)); (* O_RDONLY is 0 everywhere *)
59+ if r < 0 then return failed(); end;
60+ return r;
61+end;
62+
63+(* The file at path, made empty or new, opened for writing, readable by
64+ all: its file descriptor, or minus an errno. *)
65+proc create*(path: []U8): I64;
66+var buf: [4096]U8; r: I64;
67+begin
68+ if not cstr(path, buf) then return -36; end;
69+ r := I64(C.creat(ADR(buf[0]), 420)); (* mode 644 *)
70+ if r < 0 then return failed(); end;
71+ return r;
72+end;
73+
74+proc close*(fd: I64);
75+begin
76+ C.close(I32(fd));
77+end;
78+
3179 (* size bytes of fresh, zeroed memory from the system, a multiple of
3280 pages, or nil. *)
3381 proc mmap*(size: I64): ^U8;
@@ -57,6 +105,26 @@
57105 proc exit*(status: I64);
58106 begin
59107 C._exit(I32(status));
108+end;
109+
110+(* The module's test: a file written, read back and closed. *)
111+proc main*;
112+var fd, n: I64; buf: [16]U8; p: ^U8;
113+begin
114+ fd := create("/tmp/jed-sys-test");
115+ if fd < 0 then exit(2); end;
116+ if write(fd, "hello") # 5 then exit(2); end;
117+ close(fd);
118+ fd := openRead("/tmp/jed-sys-test");
119+ if fd < 0 then exit(2); end;
120+ n := read(fd, buf);
121+ if (n # 5) or (buf[0..5] # "hello") or (read(fd, buf) # 0) then exit(2); end;
122+ close(fd);
123+ if openRead("/no/such/file") >= 0 then exit(2); end;
124+ if isTerminal(-1) then exit(2); end;
125+ p := mmap(100);
126+ if (p = nil) or (p[99] # 0) then exit(2); end;
127+ munmap(p, 100);
60128 end;
61129
62130 end Sys.

lib/Sys.linux-x86_64.jed modified +61 -0

@@ -15,6 +15,47 @@
1515 return SYSCALL(1, fd, ADR(s[0]), LEN(s));
1616 end;
1717
18+(* Reads into buf from fd; the number of bytes read, 0 at the end, or
19+ minus an errno. *)
20+proc read*(fd: I64; buf: []U8): I64;
21+begin
22+ if LEN(buf) = 0 then return 0; end;
23+ return SYSCALL(0, fd, ADR(buf[0]), LEN(buf));
24+end;
25+
26+(* path, a C string in buf, or false if it doesn't fit. *)
27+proc cstr(path: []U8; var buf: [4096]U8): BOOL;
28+var i: I64;
29+begin
30+ if LEN(path) >= LEN(buf) then return false; end;
31+ for i := 0 to LEN(path) - 1 do buf[i] := path[i]; end;
32+ buf[LEN(path)] := 0;
33+ return true;
34+end;
35+
36+(* The file at path, opened for reading: its file descriptor, or minus
37+ an errno. *)
38+proc openRead*(path: []U8): I64;
39+var buf: [4096]U8;
40+begin
41+ if not cstr(path, buf) then return -36; end; (* ENAMETOOLONG *)
42+ return SYSCALL(2, ADR(buf), 0); (* open, O_RDONLY *)
43+end;
44+
45+(* The file at path, made empty or new, opened for writing, readable by
46+ all: its file descriptor, or minus an errno. *)
47+proc create*(path: []U8): I64;
48+var buf: [4096]U8;
49+begin
50+ if not cstr(path, buf) then return -36; end;
51+ return SYSCALL(85, ADR(buf), 420); (* creat, mode 644 *)
52+end;
53+
54+proc close*(fd: I64);
55+begin
56+ SYSCALL(3, fd);
57+end;
58+
1859 (* size bytes of fresh, zeroed memory from the system, a multiple of
1960 pages, or nil. *)
2061 proc mmap*(size: I64): ^U8;
@@ -48,6 +89,26 @@
4889 proc exit*(status: I64);
4990 begin
5091 SYSCALL(231, status);
92+end;
93+
94+(* The module's test: a file written, read back and closed. *)
95+proc main*;
96+var fd, n: I64; buf: [16]U8; p: ^U8;
97+begin
98+ fd := create("/tmp/jed-sys-test");
99+ if fd < 0 then exit(2); end;
100+ if write(fd, "hello") # 5 then exit(2); end;
101+ close(fd);
102+ fd := openRead("/tmp/jed-sys-test");
103+ if fd < 0 then exit(2); end;
104+ n := read(fd, buf);
105+ if (n # 5) or (buf[0..5] # "hello") or (read(fd, buf) # 0) then exit(2); end;
106+ close(fd);
107+ if openRead("/no/such/file") >= 0 then exit(2); end;
108+ if isTerminal(-1) then exit(2); end;
109+ p := mmap(100);
110+ if (p = nil) or (p[99] # 0) then exit(2); end;
111+ munmap(p, 100);
51112 end;
52113
53114 end Sys.

src/jed.pas modified +8 -0

@@ -1,6 +1,7 @@
11 { jed.pas: the command line.
22
33 jed [-I dir]... [-o file] Main.jed
4+ jed --version
45
56 Compiles the main module and every module it imports, writes the
67 entry point, links what it reaches, and writes the executable: by
@@ -22,6 +23,10 @@
2223
2324 uses math, scan, sym, x64, gen, parse, link, elf;
2425
26+const
27+ Version = '0.3.0';
28+ Report = '2026-10.0'; { the version of doc/jed.md it follows }
29+
2530 { The modules the program uses: the main module, Rt, and every module
2631 they import. A module read only for the interface check is not. }
2732 procedure Use(m: PModule);
@@ -99,6 +104,9 @@
99104 end else if s = '-o' then begin
100105 inc(i);
101106 outPath := ParamStr(i);
107+ end else if s = '--version' then begin
108+ writeln('jed ', Version, ', report ', Report);
109+ Halt(0);
102110 end else if (s <> '') and (s[1] = '-') then
103111 Fatal('unknown option ' + s)
104112 else path := s;

test/check.sh (history) modified +47 -1

@@ -7,7 +7,9 @@
77 # X.status its exit status (optional; 0 if missing)
88 # A program without X.expect is compiled, not run. test/libc puts
99 # lib/Sys.jed, over the C library, before the system call one.
10-# Programs in test/err must not compile, and must print X.err.
10+# Programs in test/err must not compile, and must print X.err. Then
11+# the version, tools/Doc.jed, the report's examples, and the library's
12+# own tests.
1113
1214 jed=./jed
1315 out=build/check
@@ -57,5 +59,49 @@
5759 pass=$((pass + 1))
5860 fi
5961 done
62+# the report's version is the one the compiler says it follows
63+want=$(sed -n 's/^This is version \([0-9.-]*\) of the report.*/\1/p' doc/jed.md)
64+if ./jed --version | grep -qF "report $want"; then pass=$((pass + 1))
65+else echo "FAIL jed --version: not report $want"; fail=$((fail + 1)); fi
66+
67+# tools/Doc.jed: on every library module, and as test/doc expects
68+if ./jed -o $out/Doc tools/Doc.jed 2>$out/Doc.cerr; then
69+ for f in lib/*.jed; do
70+ if ! $out/Doc $f > $out/doc.out; then echo "FAIL Doc $f"; fail=$((fail + 1)); fi
71+ done
72+ for f in test/doc/*.expect; do
73+ name=$(basename $f .expect)
74+ m=lib/$name.jed
75+ [ -f $m ] || m=lib/$name.linux-x86_64.jed
76+ if $out/Doc $m | cmp -s - $f; then pass=$((pass + 1))
77+ else echo "FAIL Doc $m: output differs"; fail=$((fail + 1)); fi
78+ done
79+
80+ # the report's examples that are whole modules: each compiles, and
81+ # each with a main runs and ends with status 0
82+ rm -rf $out/report; mkdir -p $out/report
83+ for name in $($out/Doc examples doc/jed.md $out/report); do
84+ if ! $jed -o $out/report/$name $out/report/$name.jed 2>$out/report/$name.cerr; then
85+ echo "FAIL report example $name: $(cat $out/report/$name.cerr)"; fail=$((fail + 1))
86+ elif grep -q '^proc main\*' $out/report/$name.jed && ! $out/report/$name > /dev/null 2>&1; then
87+ echo "FAIL report example $name: status $?"; fail=$((fail + 1))
88+ else pass=$((pass + 1)); fi
89+ done
90+else
91+ echo "FAIL tools/Doc.jed: $(cat $out/Doc.cerr)"; fail=$((fail + 1))
92+fi
93+
94+# each library module's main is its test; Sys over C is tested in test/libc
95+for f in lib/*.jed; do
96+ [ $f = lib/Sys.jed ] && continue
97+ grep -q '^proc main\*' $f || continue
98+ name=lib_$(basename $f .jed)
99+ if ! $jed -o $out/$name $f 2>$out/$name.cerr; then
100+ echo "FAIL $f: doesn't compile: $(cat $out/$name.cerr)"; fail=$((fail + 1))
101+ elif ! $out/$name > $out/$name.out 2>&1; then
102+ echo "FAIL $f: its test says $(cat $out/$name.out)"; fail=$((fail + 1))
103+ else pass=$((pass + 1)); fi
104+done
105+
60106 echo "$pass passed, $fail failed"
61107 [ $fail = 0 ]

test/doc/Str.expect (history) added +19 -0

@@ -0,0 +1,19 @@
1+module Str;
2+ Operations on strings, which are []U8. = and # compare them; this
3+ module orders, searches and hashes them.
4+
5+proc cmp*(a, b: []U8): I64;
6+ -1, 0 or 1 as a sorts before, with or after b, byte by byte.
7+
8+proc eq*(a, b: []U8): BOOL;
9+
10+proc hash*(s: []U8): U64;
11+ FNV-1a, 64 bits.
12+
13+proc find*(s, sub: []U8): I64;
14+ Where sub first starts in s, or -1.
15+
16+proc hasPrefix*(s, p: []U8): BOOL;
17+
18+proc main*;
19+ The module's test: stops the program if something is wrong.

test/doc/Thread.expect (history) added +29 -0

@@ -0,0 +1,29 @@
1+module Thread;
2+ Threads for programs without C, made with the clone system call.
3+ Each runs on a stack of its own, mmaped, with a page below it that
4+ can't be touched, so that running off the stack stops the program
5+ rather than spoiling what lies below. join waits on the thread's id,
6+ which the kernel clears, and wakes it for, when the thread ends;
7+ a Mutex sleeps on a futex. A program with C threads uses C's,
8+ through pthread_create, and so can't use these.
9+
10+const Failed*: ERROR = "can't start a thread";
11+
12+type Thread* = record
13+ tid: I64; (* the kernel's id, until the thread ends *)
14+ base: ^U8; (* its stack's mapping, the guard page first *)
15+ size: I64;
16+end;
17+
18+type Mutex* = record state: I64; end;
19+ 0 free, 1 taken, 2 taken with a thread waiting
20+
21+proc spawn*(p: proc(arg: ^U8); arg: ^U8): (^Thread, ERROR);
22+ Starts p(arg) in a new thread.
23+
24+proc join*(t: ^Thread);
25+ Waits for t to end, then frees its stack.
26+
27+proc lock*(m: ^Mutex);
28+
29+proc unlock*(m: ^Mutex);

test/libc/SysTest.expect (history) added +0 -0

An empty file.

test/libc/SysTest.jed added +10 -0

@@ -0,0 +1,10 @@
1+(* Sys over the C library, through its own test. *)
2+module SysTest;
3+import Sys;
4+
5+proc main*;
6+begin
7+ Sys.main;
8+end;
9+
10+end SysTest.

tools/Doc.jed added +422 -0

@@ -0,0 +1,422 @@
1+(* Doc: what a module offers, from its source.
2+
3+ Doc Module.jed
4+ prints the module's own comment, the one just before module, then
5+ each exported declaration as it is written, with the comment just
6+ before it.
7+
8+ Doc examples report.md dir
9+ writes each example of the report that is a whole module, from
10+ module to end, with no ... left out, to dir/Name.jed, and prints
11+ the names, one a line. *)
12+module Doc;
13+import Out, Sys, Rt, Str, Buf, Heap;
14+
15+type
16+ Kind = (ident, other, eof);
17+
18+var
19+ src: []U8; (* the text being read *)
20+ pos: I64;
21+ kind: Kind; (* the current token *)
22+ tokStart, tokEnd: I64;
23+ docStart, docEnd: I64; (* the comment just before it, or -1 *)
24+
25+(* Argument i, a C string, as a slice. *)
26+proc arg(i: I64): []U8;
27+var s: ^U8; n: I64;
28+begin
29+ s := ARGV()[i];
30+ n := 0;
31+ while s[n] # 0 do INC(n); end;
32+ return s[0..n];
33+end;
34+
35+proc readFile(path: []U8; b: ^Buf.Buf): BOOL;
36+var fd, n: I64; chunk: [4096]U8;
37+begin
38+ fd := Sys.openRead(path);
39+ if fd < 0 then return false; end;
40+ repeat
41+ n := Sys.read(fd, chunk);
42+ if n > 0 then Buf.str(b, chunk[0..n]); end;
43+ until n <= 0;
44+ Sys.close(fd);
45+ return n = 0;
46+end;
47+
48+proc writeFile(path, data: []U8): BOOL;
49+var fd, n, at: I64;
50+begin
51+ fd := Sys.create(path);
52+ if fd < 0 then return false; end;
53+ at := 0;
54+ while at < LEN(data) do
55+ n := Sys.write(fd, data[at..LEN(data)]);
56+ if n <= 0 then Sys.close(fd); return false; end;
57+ at := at + n;
58+ end;
59+ Sys.close(fd);
60+ return true;
61+end;
62+
63+proc isSpace(c: U8): BOOL;
64+begin
65+ return (c = ' ') or (c = 9) or (c = 10) or (c = 13);
66+end;
67+
68+proc isWord(c: U8): BOOL;
69+begin
70+ return (c >= 'a') and (c <= 'z') or (c >= 'A') and (c <= 'Z')
71+ or (c >= '0') and (c <= '9') or (c = '_');
72+end;
73+
74+(* Past a string or character literal that starts at pos. *)
75+proc skipLiteral;
76+var q: U8;
77+begin
78+ q := src[pos];
79+ INC(pos);
80+ while (pos < LEN(src)) and (src[pos] # q) do
81+ if src[pos] = '\\' then INC(pos); end;
82+ INC(pos);
83+ end;
84+ INC(pos);
85+end;
86+
87+(* Past the comment at pos. *)
88+proc skipComment;
89+var depth: I64;
90+begin
91+ depth := 0;
92+ repeat
93+ if (src[pos] = '(') and (pos + 1 < LEN(src)) and (src[pos + 1] = '*') then
94+ INC(depth); INC(pos, 2);
95+ elsif (src[pos] = '*') and (pos + 1 < LEN(src)) and (src[pos + 1] = ')') then
96+ DEC(depth); INC(pos, 2);
97+ else
98+ INC(pos);
99+ end;
100+ until (depth = 0) or (pos >= LEN(src));
101+end;
102+
103+proc atComment(p: I64): BOOL;
104+begin
105+ return (p + 1 < LEN(src)) and (src[p] = '(') and (src[p + 1] = '*');
106+end;
107+
108+(* Past spaces and comments, keeping where the last comment that starts
109+ a line was: one after code on its line belongs to that code. *)
110+proc skipSpace;
111+var start: I64; newLine: BOOL;
112+begin
113+ docStart := -1;
114+ newLine := pos = 0;
115+ while pos < LEN(src) do
116+ if isSpace(src[pos]) then
117+ if src[pos] = 10 then newLine := true; end;
118+ INC(pos);
119+ elsif atComment(pos) then
120+ start := pos;
121+ skipComment;
122+ if newLine then docStart := start; docEnd := pos; end;
123+ else
124+ break;
125+ end;
126+ end;
127+end;
128+
129+proc next;
130+begin
131+ skipSpace;
132+ tokStart := pos;
133+ if pos >= LEN(src) then
134+ kind := eof;
135+ elsif isWord(src[pos]) then
136+ while (pos < LEN(src)) and isWord(src[pos]) do INC(pos); end;
137+ kind := ident;
138+ elsif (src[pos] = '"') or (src[pos] = '\'') then
139+ skipLiteral;
140+ kind := other;
141+ else
142+ INC(pos);
143+ kind := other;
144+ end;
145+ tokEnd := pos;
146+end;
147+
148+proc text: []U8;
149+begin
150+ return src[tokStart..tokEnd];
151+end;
152+
153+proc is(s: []U8): BOOL;
154+begin
155+ return text() = s;
156+end;
157+
158+(* Whether the current word starts a declaration, a section or the end. *)
159+proc reserved: BOOL;
160+begin
161+ return is("const") or is("type") or is("var") or is("proc") or is("extern")
162+ or is("import") or is("link") or is("begin") or is("end");
163+end;
164+
165+(* On to the ; that ends what starts here, past anything bracketed and
166+ any record or variant; returns where it ends. *)
167+proc toSemicolon: I64;
168+var depth: I64;
169+begin
170+ depth := 0;
171+ while kind # eof do
172+ if is("(") or is("[") or is("{") or is("record") or is("variant") then INC(depth);
173+ elsif is(")") or is("]") or is("}") or is("end") then DEC(depth);
174+ elsif is(";") and (depth <= 0) then return tokEnd;
175+ end;
176+ next;
177+ end;
178+ return pos;
179+end;
180+
181+(* stop, or the end of a comment that follows it on its line. *)
182+proc trailing(stop: I64): I64;
183+var save: I64;
184+begin
185+ while (stop < LEN(src)) and ((src[stop] = ' ') or (src[stop] = 9)) do INC(stop); end;
186+ if not atComment(stop) then return stop; end;
187+ save := pos;
188+ pos := stop;
189+ skipComment;
190+ stop := pos;
191+ pos := save;
192+ return stop;
193+end;
194+
195+(* Where the line holding p starts. *)
196+proc lineStart(p: I64): I64;
197+begin
198+ while (p > 0) and (src[p - 1] # 10) do DEC(p); end;
199+ return p;
200+end;
201+
202+(* src[start..stop] as written, each line after the first moved left as
203+ far as the first one's column. *)
204+proc block(start, stop: I64);
205+var col, i, n: I64;
206+begin
207+ col := start - lineStart(start);
208+ i := start;
209+ while i < stop do
210+ Out.char(src[i]);
211+ if src[i] = 10 then
212+ n := 0;
213+ while (n < col) and (i + 1 < stop) and (src[i + 1] = ' ') do INC(i); INC(n); end;
214+ end;
215+ INC(i);
216+ end;
217+ Out.ln;
218+end;
219+
220+(* The comment from start to stop, without its brackets, each line
221+ indented by four. *)
222+proc comment(start, stop: I64);
223+var i, j: I64; atLine: BOOL;
224+begin
225+ if start < 0 then return; end;
226+ i := start + 2;
227+ j := stop - 2;
228+ while (i < j) and isSpace(src[i]) do INC(i); end;
229+ while (j > i) and isSpace(src[j - 1]) do DEC(j); end;
230+ atLine := true;
231+ while i < j do
232+ if atLine then Out.str(" "); atLine := false; end;
233+ Out.char(src[i]);
234+ if src[i] = 10 then
235+ atLine := true;
236+ while (i + 1 < j) and (src[i + 1] = ' ') do INC(i); end;
237+ end;
238+ INC(i);
239+ end;
240+ Out.ln;
241+end;
242+
243+(* One declaration, from its first token, the comment before it kept. *)
244+proc declaration(prefix: []U8; exported: BOOL; start, ds, de, stop: I64);
245+begin
246+ if not exported then return; end;
247+ Out.ln;
248+ Out.str(prefix);
249+ block(start, trailing(stop));
250+ comment(ds, de);
251+end;
252+
253+(* name, and whether a * follows it; leaves the token after them. *)
254+proc name: BOOL;
255+begin
256+ next;
257+ next;
258+ if is("*") then next; return true; end;
259+ return false;
260+end;
261+
262+(* A procedure's body: to the end that closes its begin, the procedure
263+ name after it and its ;. *)
264+proc skipBody;
265+var depth: I64; sawBegin: BOOL;
266+begin
267+ depth := 0;
268+ sawBegin := false;
269+ while kind # eof do
270+ if is("begin") then
271+ if depth = 0 then sawBegin := true; end;
272+ INC(depth);
273+ elsif is("if") or is("while") or is("for") or is("match") or is("record") or is("variant") then
274+ INC(depth);
275+ elsif is("end") then
276+ DEC(depth);
277+ if (depth = 0) and sawBegin then
278+ next;
279+ if kind = ident then next; end;
280+ return;
281+ end;
282+ end;
283+ next;
284+ end;
285+end;
286+
287+proc procedure;
288+var start, ds, de, stop: I64; exp: BOOL;
289+begin
290+ start := tokStart; ds := docStart; de := docEnd;
291+ exp := name();
292+ stop := toSemicolon();
293+ next;
294+ if is("forward") or is("code") then toSemicolon();
295+ else skipBody;
296+ end;
297+ declaration("", exp, start, ds, de, stop);
298+ next;
299+end;
300+
301+proc section;
302+var word: []U8; start, ds, de, stop: I64; exp: BOOL;
303+begin
304+ word := text();
305+ next;
306+ while (kind = ident) and not reserved() do
307+ start := tokStart; ds := docStart; de := docEnd;
308+ pos := tokStart; (* name reads the name again *)
309+ exp := name();
310+ stop := toSemicolon();
311+ if exp then
312+ Out.ln;
313+ Out.str(word); Out.str(" ");
314+ block(start, trailing(stop));
315+ comment(ds, de);
316+ end;
317+ next;
318+ end;
319+end;
320+
321+proc external;
322+var start, ds, de, stop: I64; exp: BOOL;
323+begin
324+ start := tokStart; ds := docStart; de := docEnd;
325+ next;
326+ exp := name();
327+ stop := toSemicolon();
328+ declaration("", exp, start, ds, de, stop);
329+ next;
330+end;
331+
332+proc document(path: []U8);
333+var b: Buf.Buf; start, ds, de: I64;
334+begin
335+ Buf.init(ADR(b), Heap.heap);
336+ if not readFile(path, ADR(b)) then Rt.stop("Doc: can't read the file"); end;
337+ src := Buf.bytes(ADR(b));
338+ pos := 0;
339+ next;
340+ while (kind # eof) and not is("module") do next; end;
341+ if kind = eof then Rt.stop("Doc: no module"); end;
342+ start := tokStart;
343+ ds := docStart; de := docEnd;
344+ block(start, toSemicolon());
345+ comment(ds, de);
346+ next;
347+ while (kind # eof) and not is("begin") and not is("end") do
348+ if is("const") or is("type") or is("var") then section;
349+ elsif is("proc") then procedure;
350+ elsif is("extern") then external;
351+ elsif is("import") or is("link") then toSemicolon(); next;
352+ else next;
353+ end;
354+ end;
355+end;
356+
357+(* line, without the spaces at its ends *)
358+proc trim(line: []U8): []U8;
359+var i, j: I64;
360+begin
361+ i := 0;
362+ j := LEN(line);
363+ while (i < j) and isSpace(line[i]) do INC(i); end;
364+ while (j > i) and isSpace(line[j - 1]) do DEC(j); end;
365+ return line[i..j];
366+end;
367+
368+(* The report's examples that are whole modules, each to dir. *)
369+proc examples(path, dir: []U8);
370+var
371+ b, out: Buf.Buf; s, line, first, last, name: []U8; i, start, k, n: I64;
372+ inBlock, whole: BOOL;
373+begin
374+ Buf.init(ADR(b), Heap.heap);
375+ Buf.init(ADR(out), Heap.heap);
376+ if not readFile(path, ADR(b)) then Rt.stop("Doc: can't read the report"); end;
377+ s := Buf.bytes(ADR(b));
378+ inBlock := false;
379+ start := 0;
380+ i := 0;
381+ while i < LEN(s) do
382+ k := i;
383+ while (k < LEN(s)) and (s[k] # 10) do INC(k); end;
384+ line := s[i..k];
385+ if trim(line) = "```" then
386+ if inBlock and whole and Str.hasPrefix(first, "module ") and Str.hasPrefix(last, "end ") then
387+ name := first[7..LEN(first)];
388+ n := 0;
389+ while (n < LEN(name)) and isWord(name[n]) do INC(n); end;
390+ name := name[0..n];
391+ Buf.reset(ADR(out));
392+ Buf.str(ADR(out), dir); Buf.str(ADR(out), "/"); Buf.str(ADR(out), name);
393+ Buf.str(ADR(out), ".jed");
394+ if not writeFile(Buf.bytes(ADR(out)), s[start..i]) then
395+ Rt.stop("Doc: can't write an example");
396+ end;
397+ Out.str(name); Out.ln;
398+ end;
399+ inBlock := not inBlock;
400+ start := k + 1;
401+ first := ""; last := "";
402+ whole := true;
403+ elsif inBlock then
404+ if trim(line) = "..." then whole := false; end;
405+ if LEN(trim(line)) > 0 then
406+ if LEN(first) = 0 then first := trim(line); end;
407+ last := trim(line);
408+ end;
409+ end;
410+ i := k + 1;
411+ end;
412+end;
413+
414+proc main*;
415+begin
416+ if (ARGC() = 4) and (arg(1) = "examples") then examples(arg(2), arg(3));
417+ elsif ARGC() = 2 then document(arg(1));
418+ else Rt.stop("usage: Doc Module.jed, or Doc examples report.md dir");
419+ end;
420+end;
421+
422+end Doc.