raz / commit 51c3b8b8

commit
51c3b8b81b4cd453cd42efa5d98cb368afba40e5
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
54a45fc7
conform/ needs only the core: library tests go to test/run, generic tests use local modules

patch · log

48 files changed, +442 -263

conform/err/BadProc.raz (history) modified +2 -2

@@ -1,6 +1,6 @@
11 module BadProc;
2-import Str, M := Map([]U8, I64, Str.cmp, Str.eq);
2+import Str, M := Table([]U8, I64, Str.cmp, Str.eq);
33
4-var m: M.Map;
4+var m: M.Table;
55
66 end BadProc.

conform/err/FirstUseLate.raz (history) modified +2 -2

@@ -1,7 +1,7 @@
11 module FirstUseLate;
2-import L := List(Later);
2+import L := Pile(Later);
33
4-var l: L.List; (* made here, before Later is declared *)
4+var l: L.Pile; (* made here, before Later is declared *)
55
66 type Later = I64;
77

conform/err/NoArgs.raz (history) modified +1 -1

@@ -1,3 +1,3 @@
11 module NoArgs;
2-import List;
2+import Pile;
33 end NoArgs.

conform/err/StmtArgKind.raz (history) modified +2 -2

@@ -1,6 +1,6 @@
11 module StmtArgKind;
2-import Mem, L := List(Mem.Page);
2+import Mem, L := Pile(Mem.Page);
33
4-var l: L.List;
4+var l: L.Pile;
55
66 end StmtArgKind.

conform/err/StmtManyTypeArgs.raz (history) modified +2 -2

@@ -1,6 +1,6 @@
11 module StmtManyTypeArgs;
2-import L := List(I64, I64);
2+import L := Pile(I64, I64);
33
4-var l: L.List;
4+var l: L.Pile;
55
66 end StmtManyTypeArgs.

conform/err/StmtNeedProc.raz (history) modified +2 -2

@@ -1,6 +1,6 @@
11 module StmtNeedProc;
2-import Str, M := Map([]U8, I64, I64, Str.eq);
2+import Str, M := Table([]U8, I64, I64, Str.eq);
33
4-var m: M.Map;
4+var m: M.Table;
55
66 end StmtNeedProc.

conform/err/StmtNeedType.raz (history) modified +2 -2

@@ -1,6 +1,6 @@
11 module StmtNeedType;
2-import Str, M := Map(Str.cmp, I64, Str.cmp, Str.eq);
2+import Str, M := Table(Str.cmp, I64, Str.cmp, Str.eq);
33
4-var m: M.Map;
4+var m: M.Table;
55
66 end StmtNeedType.

conform/err/TwoTypes.raz (history) modified +2 -2

@@ -1,7 +1,7 @@
11 module TwoTypes;
2-import A := List(I64), B := List(I32);
2+import A := Pile(I64), B := Pile(I32);
33 proc main*;
4-var a: A.List; b: B.List;
4+var a: A.Pile; b: B.Pile;
55 begin
66 a := b;
77 end;

conform/err/mods/Pile.raz (history) added +36 -0

@@ -0,0 +1,36 @@
1+(* A generic module that needs no library: a stack of at most eight
2+ values of T. *)
3+module Pile(T);
4+
5+const Max = 8;
6+
7+type Pile* = record
8+ items: [Max]T;
9+ len*: INT;
10+ end;
11+
12+(* x on top; false if the pile is full. *)
13+proc push*(p: ^Pile; x: T): BOOL;
14+begin
15+ if p^.len = Max then return false; end;
16+ p^.items[p^.len] := x;
17+ INC(p^.len);
18+ return true;
19+end;
20+
21+(* The top, taken off; false if the pile is empty. *)
22+proc pop*(p: ^Pile): (T, BOOL);
23+var zero: T;
24+begin
25+ if p^.len = 0 then return zero, false; end;
26+ DEC(p^.len);
27+ return p^.items[p^.len], true;
28+end;
29+
30+(* The top, left where it is; the pile must not be empty. *)
31+proc top*(p: ^Pile): T;
32+begin
33+ return p^.items[p^.len - 1];
34+end;
35+
36+end Pile.

conform/err/mods/Table.raz (history) added +48 -0

@@ -0,0 +1,48 @@
1+(* A generic module with procedure parameters and no library: a table
2+ of sixteen slots, filled by linear probing. *)
3+module Table(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL);
4+
5+const Slots = 16;
6+
7+type Table* = record
8+ used: [Slots]BOOL;
9+ keys: [Slots]K;
10+ vals: [Slots]V;
11+ len*: INT;
12+ end;
13+
14+(* v for k, a new entry or a change; false if the table is full. *)
15+proc put*(t: ^Table; k: K; v: V): BOOL;
16+var i, n: INT;
17+begin
18+ i := INT(hash(k) mod Slots);
19+ for n := 1 to Slots do
20+ if not t^.used[i] then
21+ t^.used[i] := true;
22+ t^.keys[i] := k;
23+ t^.vals[i] := v;
24+ INC(t^.len);
25+ return true;
26+ elsif eq(t^.keys[i], k) then
27+ t^.vals[i] := v;
28+ return true;
29+ end;
30+ i := (i + 1) mod Slots;
31+ end;
32+ return false;
33+end;
34+
35+(* What k has; false if it has nothing. *)
36+proc get*(t: ^Table; k: K): (V, BOOL);
37+var i, n: INT; zero: V;
38+begin
39+ i := INT(hash(k) mod Slots);
40+ for n := 1 to Slots do
41+ if not t^.used[i] then return zero, false; end;
42+ if eq(t^.keys[i], k) then return t^.vals[i], true; end;
43+ i := (i + 1) mod Slots;
44+ end;
45+ return zero, false;
46+end;
47+
48+end Table.

conform/run/Alloc.expect deleted +0 -5

@@ -1,5 +0,0 @@
1-1001000
2-1001000
3-7
4-10100
5-nil again 0

conform/run/Alloc.raz deleted +0 -66

@@ -1,66 +0,0 @@
1-module Alloc;
2-import Out, Mem, Arena, Heap;
3-
4-type
5- Node = record next: ^Node; val: I64; pad: [5]I64; end;
6- Big = record data: [1000]I64; end;
7-
8-proc build(a: ^Mem.Allocator; n: I64): ^Node;
9-var head, p: ^Node; i: I64;
10-begin
11- head := nil;
12- for i := 1 to n do
13- NEW(p, a);
14- if p = nil then return nil; end;
15- if p^.val # 0 then Out.str("not zeroed "); end;
16- p^.val := i;
17- p^.pad[4] := i;
18- p^.next := head;
19- head := p;
20- end;
21- return head;
22-end;
23-
24-proc sum(p: ^Node): I64;
25-var s: I64;
26-begin
27- s := 0;
28- while p # nil do s := s + p^.val + p^.pad[4]; p := p^.next; end;
29- return s;
30-end;
31-
32-proc release(a: ^Mem.Allocator; p: ^Node);
33-var next: ^Node;
34-begin
35- while p # nil do
36- next := p^.next;
37- Mem.free(a, ^U8(p));
38- p := next;
39- end;
40-end;
41-
42-proc main*;
43-var ar: Arena.Arena; l, m: ^Node; b: ^Big; i: I64; small: ^U8; again: ^Node;
44-begin
45- l := build(Heap.heap, 1000);
46- Out.int(sum(l)); Out.ln;
47- release(Heap.heap, l);
48- m := build(Heap.heap, 1000);
49- Out.int(sum(m)); Out.ln;
50- NEW(b, Heap.heap);
51- b^.data[999] := 7;
52- Out.int(b^.data[999] + b^.data[0]); Out.ln;
53- Mem.free(Heap.heap, ^U8(b));
54- if not Arena.init(ADR(ar), 8192) then Out.str("no arena"); end;
55- l := build(ADR(ar.base), 100);
56- Out.int(sum(l)); Out.ln;
57- l := build(ADR(ar.base), 1000);
58- if l = nil then Out.str("nil "); end;
59- Arena.reset(ADR(ar));
60- NEW(again, ADR(ar.base));
61- if again # nil then Out.str("again "); Out.int(again^.val); end;
62- Out.ln;
63- Arena.done(ADR(ar));
64-end;
65-
66-end Alloc.

conform/run/FirstUse.raz (history) modified +5 -6

@@ -1,10 +1,10 @@
11 module FirstUse;
2-import Out, Str, Heap, Table := Map([]U8, Colour, Str.hash, Str.eq),
3- Ints := List(I64), More := List(I64), Never := Loud(I64), Ones := Loud(U8);
2+import Out, Str, Table := Table([]U8, Colour, Str.hash, Str.eq),
3+ Ints := Pile(I64), More := Pile(I64), Never := Loud(I64), Ones := Loud(U8);
44
55 type Colour = (red, green, blue);
66
7-var names: Table.Map; (* Table is made here, after Colour *)
7+var names: Table.Table; (* Table is made here, after Colour *)
88
99 proc show(k: []U8);
1010 var c: Colour; found: BOOL;
@@ -17,10 +17,10 @@
1717
1818 (* Ones is first used in a procedure, and made before its code *)
1919 proc main*;
20-var a: Ints.List; b: More.List;
20+var a: Ints.Pile; b: More.Pile;
2121 begin
2222 a := b; (* two imports, one instance *)
23- if a.head = nil then Out.str("an empty list\n"); end;
23+ if a.len = 0 then Out.str("an empty list\n"); end;
2424 Out.str("in main, Ones has size "); Out.int(Ones.size); Out.ln;
2525 show("sky");
2626 show("rose");
@@ -28,7 +28,6 @@
2828 end;
2929
3030 begin
31- Table.init(ADR(names), Heap.heap);
3231 _ := Table.put(ADR(names), "rose", red);
3332 _ := Table.put(ADR(names), "sky", blue);
3433 end FirstUse.

conform/run/Generic.expect (history) modified +0 -2

@@ -2,5 +2,3 @@
22 8 1
33 abtrue
44 [] false
5-157 152 -1
6-{ BEEF } x18446744073709551615

conform/run/Generic.raz (history) modified +11 -15

@@ -1,30 +1,26 @@
11 module Generic;
2-import Fmt, Buf, Str, Heap, Stack, Ints := List(I64), More := List(I64), Strs := List([]U8);
2+import Out, Stack, Ints := Pile(I64), More := Pile(I64), Strs := Pile([]U8);
33
4-proc twice(x: I64): I64; begin return x * 2; end;
4+proc yes(b: BOOL);
5+begin
6+ if b then Out.str("true"); else Out.str("false"); end;
7+end;
58
69 proc main*;
7-var l: Strs.List; v: I64; s: []U8; ok: BOOL; b: Buf.Buf; n: ^Ints.Node;
10+var l: Strs.Pile; v: I64; s: []U8; ok: BOOL;
811 begin
912 ok := Ints.push(Stack.p, 7);
10- ok := More.push(Stack.p, 8);
11- Fmt.print("{} {} {}\n", [Fmt.int(Stack.top()), Fmt.int(Stack.p^.len), Fmt.bool(ok)]);
12- n := Stack.p^.head;
13+ ok := More.push(Stack.p, 8); (* two imports, one instance: one pile *)
14+ Out.int(Stack.top()); Out.char(' '); Out.int(Stack.p^.len); Out.char(' '); yes(ok); Out.ln;
1315 v, ok := Ints.pop(Stack.p);
14- Fmt.print("{} {}\n", [Fmt.int(v), Fmt.int(Stack.p^.len)]);
15- Strs.init(ADR(l), Heap.heap);
16+ Out.int(v); Out.char(' '); Out.int(Stack.p^.len); Out.ln;
1617 ok := Strs.push(ADR(l), "b");
1718 ok := Strs.push(ADR(l), "a");
1819 s, ok := Strs.pop(ADR(l));
19- Fmt.print("{}{}{}\n", [Fmt.str(s), Fmt.str(l.head^.val), Fmt.bool(ok)]);
20+ Out.str(s); Out.str(Strs.top(ADR(l))); yes(ok); Out.ln;
2021 s, ok := Strs.pop(ADR(l));
2122 s, ok := Strs.pop(ADR(l));
22- Fmt.print("[{}] {}\n", [Fmt.str(s), Fmt.bool(ok)]);
23- Buf.init(ADR(b), Heap.heap);
24- for v := 1 to 40 do Fmt.write(ADR(b), "{},", [Fmt.int(v * v)]); end;
25- Fmt.print("{} {} {}\n", [Fmt.int(b.len), Fmt.int(Str.find(Buf.bytes(ADR(b)), "1600")), Fmt.int(Str.cmp("ab", "b"))]);
26- Buf.done(ADR(b));
27- Fmt.print("{{ {} }} {}{}\n", [Fmt.hex(48879), Fmt.char('x'), Fmt.uint(0FFFFFFFFFFFFFFFFH)]);
23+ Out.char('['); Out.str(s); Out.str("] "); yes(ok); Out.ln;
2824 end;
2925
3026 end Generic.

conform/run/HeapCheck.expect deleted +0 -10

@@ -1,10 +0,0 @@
1-nil
2-nil
3-nil
4-nil
5-nil
6-nil
7-a block
8-aligned
9-a block
10-freeing it again

conform/run/HeapCheck.raz deleted +0 -34

@@ -1,34 +0,0 @@
1-(* The heap refuses bad sizes and alignments, and stops the program
2- when a block is freed twice. *)
3-module HeapCheck;
4-import Out, Heap, Mem;
5-
6-proc show(p: ^U8);
7-begin
8- if p = nil then Out.str("nil\n"); else Out.str("a block\n"); end;
9-end;
10-
11-proc main*;
12-var p, q: ^U8;
13-begin
14- show(Mem.alloc(Heap.heap, -1, 8));
15- show(Mem.alloc(Heap.heap, 1 shl 60, 8));
16- show(Mem.alloc(Heap.heap, 7FFFFFFFFFFFFFF0H, 8));
17- show(Mem.alloc(Heap.heap, 10, 3));
18- show(Mem.alloc(Heap.heap, 10, 0));
19- show(Mem.alloc(Heap.heap, 10, 8192));
20- p := Mem.alloc(Heap.heap, 10, 64);
21- show(p);
22- if INT(p) mod 64 = 0 then Out.str("aligned\n"); end;
23- Mem.free(Heap.heap, p);
24- q := Mem.alloc(Heap.heap, 100000, 8);
25- show(q);
26- Mem.free(Heap.heap, q);
27- p := Mem.alloc(Heap.heap, 24, 8);
28- Mem.free(Heap.heap, p);
29- Out.str("freeing it again\n");
30- Mem.free(Heap.heap, p);
31- Out.str("not reached\n");
32-end;
33-
34-end HeapCheck.

conform/run/HeapCheck.status deleted +0 -1

@@ -1 +0,0 @@
1-2

conform/run/HeapStray.expect deleted +0 -1

@@ -1 +0,0 @@
1-freeing a stray pointer

conform/run/HeapStray.raz deleted +0 -14

@@ -1,14 +0,0 @@
1-(* Freeing what the heap never gave out stops the program. *)
2-module HeapStray;
3-import Out, Heap, Mem;
4-
5-var a: [4]I64;
6-
7-proc main*;
8-begin
9- Out.str("freeing a stray pointer\n");
10- Mem.free(Heap.heap, ^U8(ADR(a[2])));
11- Out.str("not reached\n");
12-end;
13-
14-end HeapStray.

conform/run/HeapStray.status deleted +0 -1

@@ -1 +0,0 @@
1-2

conform/run/MapRemove.expect deleted +0 -2

@@ -1,2 +0,0 @@
1-341 entries, 341 of them, summing 6658308
2-walked 341, summing 6658308

conform/run/MapRemove.raz deleted +0 -56

@@ -1,56 +0,0 @@
1-(* Map's remove and next, against an array that holds the same: keys
2- hashed into runs of eight, so that removing moves entries back,
3- around the end of the slots too. *)
4-module MapRemove;
5-import Fmt, Heap, Rt, Ints := Map(I64, I64, near, same);
6-
7-const N = 500;
8-
9-proc near(k: I64): U64;
10-begin
11- return U64(k div 8 * 977);
12-end;
13-
14-proc same(a, b: I64): BOOL;
15-begin
16- return a = b;
17-end;
18-
19-proc main*;
20-var
21- m: Ints.Map; want: [N]I64; i: INT; k, v, n, sum, seed: I64;
22- found, _ok: BOOL;
23-begin
24- Ints.init(ADR(m), Heap.heap);
25- seed := 1;
26- for i := 1 to 20000 do
27- seed := (seed * 1103515245 + 12345) & 7FFFFFFFH;
28- k := seed mod N;
29- if seed div N mod 3 = 0 then
30- if Ints.remove(ADR(m), k) # (want[k] # 0) then Rt.stop("remove: wrong answer"); end;
31- want[k] := 0;
32- else
33- _ok := Ints.put(ADR(m), k, i);
34- want[k] := i;
35- end;
36- end;
37- for k := 0 to N - 1 do
38- v, found := Ints.get(ADR(m), k);
39- if (found # (want[k] # 0)) or (v # want[k]) then Rt.stop("get: not what was put"); end;
40- if found then INC(n); INC(sum, v); end;
41- end;
42- if Ints.remove(ADR(m), N) then Rt.stop("remove: a key never put"); end;
43- Fmt.print("{} entries, {} of them, summing {}\n", [Fmt.int(m.len), Fmt.int(n), Fmt.int(sum)]);
44- (* the walk sees each entry once *)
45- n := 0; sum := 0;
46- i, k, v := Ints.next(ADR(m), 0);
47- while i >= 0 do
48- if want[k] # v then Rt.stop("next: not what was put"); end;
49- want[k] := 0;
50- INC(n); INC(sum, v);
51- i, k, v := Ints.next(ADR(m), i);
52- end;
53- Fmt.print("walked {}, summing {}\n", [Fmt.int(n), Fmt.int(sum)]);
54-end;
55-
56-end MapRemove.

conform/run/RealRead.expect (history) modified +5 -5

@@ -1,10 +1,10 @@
11 7506AC5B262CA1FF
22 3FB999999999999A
3-FFFFFFFFFFFFF
4-1
5-1
6-0
7-0
3+000FFFFFFFFFFFFF
4+0000000000000001
5+0000000000000001
6+0000000000000000
7+0000000000000000
88 7FEFFFFFFFFFFFFF
99 4340000000000000
1010 4340000000000002

conform/run/RealRead.raz (history) modified +2 -2

@@ -1,7 +1,7 @@
11 (* Real literals read to the nearest F64, ties to the even one: their
22 bits, for numbers a reader of decimals easily gets one off. *)
33 module RealRead;
4-import Fmt;
4+import Out;
55
66 proc bits(x: F64): U64;
77 var p: ^U64;
@@ -12,7 +12,7 @@
1212
1313 proc show(x: F64);
1414 begin
15- Fmt.print("{}\n", [Fmt.hex(bits(x))]);
15+ Out.hex(bits(x), 16); Out.ln;
1616 end;
1717
1818 proc main*;

conform/run/Sorted.raz (history) modified +3 -3

@@ -1,7 +1,7 @@
1-(* Sort: slices in many orders and of every length up to 40 come out
1+(* A generic sort: slices in many orders and of every length up to 40 come out
22 in order, holding what they held; and a sort by a record's field. *)
33 module Sorted;
4-import Fmt, Rt, Ints := Sort(I64, below), ByAge := Sort(Person, younger);
4+import Out, Rt, Ints := Order(I64, below), ByAge := Order(Person, younger);
55
66 type Person = record name: []U8; age: I64; end;
77
@@ -45,7 +45,7 @@
4545 end;
4646 people := [{name: "Ana", age: 41}, {name: "Bo", age: 7}, {name: "Cy", age: 90}, {name: "Di", age: 23}];
4747 ByAge.sort(people);
48- for i := 0 to 3 do Fmt.print("{} {}\n", [Fmt.str(people[i].name), Fmt.int(people[i].age)]); end;
48+ for i := 0 to 3 do Out.str(people[i].name); Out.char(' '); Out.int(people[i].age); Out.ln; end;
4949 end;
5050
5151 end Sorted.

conform/run/mods/Order.raz (history) added +19 -0

@@ -0,0 +1,19 @@
1+(* A generic module whose procedure parameter orders its values: an
2+ insertion sort of a slice, in place and stable. *)
3+module Order(T; less: proc(a, b: T): BOOL);
4+
5+proc sort*(s: []T);
6+var i, j: INT; t: T;
7+begin
8+ for i := 1 to LEN(s) - 1 do
9+ t := s[i];
10+ j := i - 1;
11+ while (j >= 0) and less(t, s[j]) do
12+ s[j + 1] := s[j];
13+ DEC(j);
14+ end;
15+ s[j + 1] := t;
16+ end;
17+end;
18+
19+end Order.

conform/run/mods/Pile.raz (history) added +36 -0

@@ -0,0 +1,36 @@
1+(* A generic module that needs no library: a stack of at most eight
2+ values of T. *)
3+module Pile(T);
4+
5+const Max = 8;
6+
7+type Pile* = record
8+ items: [Max]T;
9+ len*: INT;
10+ end;
11+
12+(* x on top; false if the pile is full. *)
13+proc push*(p: ^Pile; x: T): BOOL;
14+begin
15+ if p^.len = Max then return false; end;
16+ p^.items[p^.len] := x;
17+ INC(p^.len);
18+ return true;
19+end;
20+
21+(* The top, taken off; false if the pile is empty. *)
22+proc pop*(p: ^Pile): (T, BOOL);
23+var zero: T;
24+begin
25+ if p^.len = 0 then return zero, false; end;
26+ DEC(p^.len);
27+ return p^.items[p^.len], true;
28+end;
29+
30+(* The top, left where it is; the pile must not be empty. *)
31+proc top*(p: ^Pile): T;
32+begin
33+ return p^.items[p^.len - 1];
34+end;
35+
36+end Pile.

conform/run/mods/Stack.raz (history) modified +4 -5

@@ -1,16 +1,15 @@
11 module Stack;
2-import Ints := List(I64), Heap;
2+import Ints := Pile(I64);
33
44 var
5- s: Ints.List;
6- p*: ^Ints.List; (* s, which only this module could change by its name *)
5+ s: Ints.Pile;
6+ p*: ^Ints.Pile; (* s, which only this module could change by its name *)
77
88 proc top*(): I64;
99 begin
10- return s.head^.val;
10+ return Ints.top(p);
1111 end;
1212
1313 begin
14- Ints.init(ADR(s), Heap.heap);
1514 p := ADR(s);
1615 end Stack.

conform/run/mods/Table.raz (history) added +48 -0

@@ -0,0 +1,48 @@
1+(* A generic module with procedure parameters and no library: a table
2+ of sixteen slots, filled by linear probing. *)
3+module Table(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL);
4+
5+const Slots = 16;
6+
7+type Table* = record
8+ used: [Slots]BOOL;
9+ keys: [Slots]K;
10+ vals: [Slots]V;
11+ len*: INT;
12+ end;
13+
14+(* v for k, a new entry or a change; false if the table is full. *)
15+proc put*(t: ^Table; k: K; v: V): BOOL;
16+var i, n: INT;
17+begin
18+ i := INT(hash(k) mod Slots);
19+ for n := 1 to Slots do
20+ if not t^.used[i] then
21+ t^.used[i] := true;
22+ t^.keys[i] := k;
23+ t^.vals[i] := v;
24+ INC(t^.len);
25+ return true;
26+ elsif eq(t^.keys[i], k) then
27+ t^.vals[i] := v;
28+ return true;
29+ end;
30+ i := (i + 1) mod Slots;
31+ end;
32+ return false;
33+end;
34+
35+(* What k has; false if it has nothing. *)
36+proc get*(t: ^Table; k: K): (V, BOOL);
37+var i, n: INT; zero: V;
38+begin
39+ i := INT(hash(k) mod Slots);
40+ for n := 1 to Slots do
41+ if not t^.used[i] then return zero, false; end;
42+ if eq(t^.keys[i], k) then return t^.vals[i], true; end;
43+ i := (i + 1) mod Slots;
44+ end;
45+ return zero, false;
46+end;
47+
48+end Table.

test/msg/BadProc.err (history) modified +6 -6

@@ -1,7 +1,7 @@
1-conform/err/BadProc.raz:2:33: error: cmp doesn't have the signature of hash
2- 2 | import Str, M := Map([]U8, I64, Str.cmp, Str.eq);
3- | ^^^^^^^
4-lib/Map.raz:4:18: note: the signature is here
5- 4 | module Map(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL);
6- | ^^^^
1+conform/err/BadProc.raz:2:35: error: cmp doesn't have the signature of hash
2+ 2 | import Str, M := Table([]U8, I64, Str.cmp, Str.eq);
3+ | ^^^^^^^
4+conform/err/mods/Table.raz:3:20: note: the signature is here
5+ 3 | module Table(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL);
6+ | ^^^^
77 raz: 1 error

test/msg/FirstUseLate.err (history) modified +2 -2

@@ -1,7 +1,7 @@
11 conform/err/FirstUseLate.raz:2:18: error: Later isn't declared
2- 2 | import L := List(Later);
2+ 2 | import L := Pile(Later);
33 | ^^^^^
44 conform/err/FirstUseLate.raz:4:8: note: the instance is made here, where it is first used
5- 4 | var l: L.List; (* made here, before Later is declared *)
5+ 4 | var l: L.Pile; (* made here, before Later is declared *)
66 | ^
77 raz: 1 error

test/msg/NoArgs.err (history) modified +2 -2

@@ -1,4 +1,4 @@
1-conform/err/NoArgs.raz:2:8: error: List needs a type for T
2- 2 | import List;
1+conform/err/NoArgs.raz:2:8: error: Pile needs a type for T
2+ 2 | import Pile;
33 | ^^^^
44 raz: 1 error

test/msg/StmtArgKind.err (history) modified +1 -1

@@ -1,4 +1,4 @@
11 conform/err/StmtArgKind.raz:2:23: error: Page isn't a type or a procedure
2- 2 | import Mem, L := List(Mem.Page);
2+ 2 | import Mem, L := Pile(Mem.Page);
33 | ^^^^^^^^
44 raz: 1 error

test/msg/StmtManyTypeArgs.err (history) modified +2 -2

@@ -1,4 +1,4 @@
1-conform/err/StmtManyTypeArgs.raz:2:8: error: too many type arguments for List
2- 2 | import L := List(I64, I64);
1+conform/err/StmtManyTypeArgs.raz:2:8: error: too many type arguments for Pile
2+ 2 | import L := Pile(I64, I64);
33 | ^^^^^^^^^^^^^^^^^^^
44 raz: 1 error

test/msg/StmtNeedProc.err (history) modified +3 -3

@@ -1,4 +1,4 @@
1-conform/err/StmtNeedProc.raz:2:33: error: Map needs a procedure for hash
2- 2 | import Str, M := Map([]U8, I64, I64, Str.eq);
3- | ^^^
1+conform/err/StmtNeedProc.raz:2:35: error: Table needs a procedure for hash
2+ 2 | import Str, M := Table([]U8, I64, I64, Str.eq);
3+ | ^^^
44 raz: 1 error

test/msg/StmtNeedType.err (history) modified +3 -3

@@ -1,4 +1,4 @@
1-conform/err/StmtNeedType.raz:2:22: error: Map needs a type for K
2- 2 | import Str, M := Map(Str.cmp, I64, Str.cmp, Str.eq);
3- | ^^^^^^^
1+conform/err/StmtNeedType.raz:2:24: error: Table needs a type for K
2+ 2 | import Str, M := Table(Str.cmp, I64, Str.cmp, Str.eq);
3+ | ^^^^^^^
44 raz: 1 error

test/msg/TwoTypes.err (history) modified +1 -1

@@ -1,4 +1,4 @@
1-conform/err/TwoTypes.raz:6:10: error: can't use List(I32).List as List(I64).List
1+conform/err/TwoTypes.raz:6:10: error: can't use Pile(I32).Pile as Pile(I64).Pile
22 6 | a := b;
33 | ^
44 raz: 1 error

test/run/Alloc.expect (history) added +5 -0

@@ -0,0 +1,5 @@
1+1001000
2+1001000
3+7
4+10100
5+nil again 0

test/run/Alloc.raz (history) added +66 -0

@@ -0,0 +1,66 @@
1+module Alloc;
2+import Out, Mem, Arena, Heap;
3+
4+type
5+ Node = record next: ^Node; val: I64; pad: [5]I64; end;
6+ Big = record data: [1000]I64; end;
7+
8+proc build(a: ^Mem.Allocator; n: I64): ^Node;
9+var head, p: ^Node; i: I64;
10+begin
11+ head := nil;
12+ for i := 1 to n do
13+ NEW(p, a);
14+ if p = nil then return nil; end;
15+ if p^.val # 0 then Out.str("not zeroed "); end;
16+ p^.val := i;
17+ p^.pad[4] := i;
18+ p^.next := head;
19+ head := p;
20+ end;
21+ return head;
22+end;
23+
24+proc sum(p: ^Node): I64;
25+var s: I64;
26+begin
27+ s := 0;
28+ while p # nil do s := s + p^.val + p^.pad[4]; p := p^.next; end;
29+ return s;
30+end;
31+
32+proc release(a: ^Mem.Allocator; p: ^Node);
33+var next: ^Node;
34+begin
35+ while p # nil do
36+ next := p^.next;
37+ Mem.free(a, ^U8(p));
38+ p := next;
39+ end;
40+end;
41+
42+proc main*;
43+var ar: Arena.Arena; l, m: ^Node; b: ^Big; i: I64; small: ^U8; again: ^Node;
44+begin
45+ l := build(Heap.heap, 1000);
46+ Out.int(sum(l)); Out.ln;
47+ release(Heap.heap, l);
48+ m := build(Heap.heap, 1000);
49+ Out.int(sum(m)); Out.ln;
50+ NEW(b, Heap.heap);
51+ b^.data[999] := 7;
52+ Out.int(b^.data[999] + b^.data[0]); Out.ln;
53+ Mem.free(Heap.heap, ^U8(b));
54+ if not Arena.init(ADR(ar), 8192) then Out.str("no arena"); end;
55+ l := build(ADR(ar.base), 100);
56+ Out.int(sum(l)); Out.ln;
57+ l := build(ADR(ar.base), 1000);
58+ if l = nil then Out.str("nil "); end;
59+ Arena.reset(ADR(ar));
60+ NEW(again, ADR(ar.base));
61+ if again # nil then Out.str("again "); Out.int(again^.val); end;
62+ Out.ln;
63+ Arena.done(ADR(ar));
64+end;
65+
66+end Alloc.

test/run/HeapCheck.expect (history) added +10 -0

@@ -0,0 +1,10 @@
1+nil
2+nil
3+nil
4+nil
5+nil
6+nil
7+a block
8+aligned
9+a block
10+freeing it again

test/run/HeapCheck.raz (history) added +34 -0

@@ -0,0 +1,34 @@
1+(* The heap refuses bad sizes and alignments, and stops the program
2+ when a block is freed twice. *)
3+module HeapCheck;
4+import Out, Heap, Mem;
5+
6+proc show(p: ^U8);
7+begin
8+ if p = nil then Out.str("nil\n"); else Out.str("a block\n"); end;
9+end;
10+
11+proc main*;
12+var p, q: ^U8;
13+begin
14+ show(Mem.alloc(Heap.heap, -1, 8));
15+ show(Mem.alloc(Heap.heap, 1 shl 60, 8));
16+ show(Mem.alloc(Heap.heap, 7FFFFFFFFFFFFFF0H, 8));
17+ show(Mem.alloc(Heap.heap, 10, 3));
18+ show(Mem.alloc(Heap.heap, 10, 0));
19+ show(Mem.alloc(Heap.heap, 10, 8192));
20+ p := Mem.alloc(Heap.heap, 10, 64);
21+ show(p);
22+ if INT(p) mod 64 = 0 then Out.str("aligned\n"); end;
23+ Mem.free(Heap.heap, p);
24+ q := Mem.alloc(Heap.heap, 100000, 8);
25+ show(q);
26+ Mem.free(Heap.heap, q);
27+ p := Mem.alloc(Heap.heap, 24, 8);
28+ Mem.free(Heap.heap, p);
29+ Out.str("freeing it again\n");
30+ Mem.free(Heap.heap, p);
31+ Out.str("not reached\n");
32+end;
33+
34+end HeapCheck.

test/run/HeapCheck.status (history) added +1 -0

@@ -0,0 +1 @@
1+2

test/run/HeapStray.expect (history) added +1 -0

@@ -0,0 +1 @@
1+freeing a stray pointer

test/run/HeapStray.raz (history) added +14 -0

@@ -0,0 +1,14 @@
1+(* Freeing what the heap never gave out stops the program. *)
2+module HeapStray;
3+import Out, Heap, Mem;
4+
5+var a: [4]I64;
6+
7+proc main*;
8+begin
9+ Out.str("freeing a stray pointer\n");
10+ Mem.free(Heap.heap, ^U8(ADR(a[2])));
11+ Out.str("not reached\n");
12+end;
13+
14+end HeapStray.

test/run/HeapStray.status (history) added +1 -0

@@ -0,0 +1 @@
1+2

test/run/MapRemove.expect (history) added +2 -0

@@ -0,0 +1,2 @@
1+341 entries, 341 of them, summing 6658308
2+walked 341, summing 6658308

test/run/MapRemove.raz (history) added +56 -0

@@ -0,0 +1,56 @@
1+(* Map's remove and next, against an array that holds the same: keys
2+ hashed into runs of eight, so that removing moves entries back,
3+ around the end of the slots too. *)
4+module MapRemove;
5+import Fmt, Heap, Rt, Ints := Map(I64, I64, near, same);
6+
7+const N = 500;
8+
9+proc near(k: I64): U64;
10+begin
11+ return U64(k div 8 * 977);
12+end;
13+
14+proc same(a, b: I64): BOOL;
15+begin
16+ return a = b;
17+end;
18+
19+proc main*;
20+var
21+ m: Ints.Map; want: [N]I64; i: INT; k, v, n, sum, seed: I64;
22+ found, _ok: BOOL;
23+begin
24+ Ints.init(ADR(m), Heap.heap);
25+ seed := 1;
26+ for i := 1 to 20000 do
27+ seed := (seed * 1103515245 + 12345) & 7FFFFFFFH;
28+ k := seed mod N;
29+ if seed div N mod 3 = 0 then
30+ if Ints.remove(ADR(m), k) # (want[k] # 0) then Rt.stop("remove: wrong answer"); end;
31+ want[k] := 0;
32+ else
33+ _ok := Ints.put(ADR(m), k, i);
34+ want[k] := i;
35+ end;
36+ end;
37+ for k := 0 to N - 1 do
38+ v, found := Ints.get(ADR(m), k);
39+ if (found # (want[k] # 0)) or (v # want[k]) then Rt.stop("get: not what was put"); end;
40+ if found then INC(n); INC(sum, v); end;
41+ end;
42+ if Ints.remove(ADR(m), N) then Rt.stop("remove: a key never put"); end;
43+ Fmt.print("{} entries, {} of them, summing {}\n", [Fmt.int(m.len), Fmt.int(n), Fmt.int(sum)]);
44+ (* the walk sees each entry once *)
45+ n := 0; sum := 0;
46+ i, k, v := Ints.next(ADR(m), 0);
47+ while i >= 0 do
48+ if want[k] # v then Rt.stop("next: not what was put"); end;
49+ want[k] := 0;
50+ INC(n); INC(sum, v);
51+ i, k, v := Ints.next(ADR(m), i);
52+ end;
53+ Fmt.print("walked {}, summing {}\n", [Fmt.int(n), Fmt.int(sum)]);
54+end;
55+
56+end MapRemove.