conform/ needs only the core: library tests go to test/run, generic tests use local modules
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | module BadProc; |
| 2 | -import Str, M := Map([]U8, I64, Str.cmp, Str.eq); | |
| 2 | +import Str, M := Table([]U8, I64, Str.cmp, Str.eq); | |
| 3 | 3 | |
| 4 | -var m: M.Map; | |
| 4 | +var m: M.Table; | |
| 5 | 5 | |
| 6 | 6 | end BadProc. |
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | module FirstUseLate; |
| 2 | -import L := List(Later); | |
| 2 | +import L := Pile(Later); | |
| 3 | 3 | |
| 4 | -var l: L.List; (* made here, before Later is declared *) | |
| 4 | +var l: L.Pile; (* made here, before Later is declared *) | |
| 5 | 5 | |
| 6 | 6 | type Later = I64; |
| 7 | 7 |
| @@ -1,3 +1,3 @@ | ||
| 1 | 1 | module NoArgs; |
| 2 | -import List; | |
| 2 | +import Pile; | |
| 3 | 3 | end NoArgs. |
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | module StmtArgKind; |
| 2 | -import Mem, L := List(Mem.Page); | |
| 2 | +import Mem, L := Pile(Mem.Page); | |
| 3 | 3 | |
| 4 | -var l: L.List; | |
| 4 | +var l: L.Pile; | |
| 5 | 5 | |
| 6 | 6 | end StmtArgKind. |
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | module StmtManyTypeArgs; |
| 2 | -import L := List(I64, I64); | |
| 2 | +import L := Pile(I64, I64); | |
| 3 | 3 | |
| 4 | -var l: L.List; | |
| 4 | +var l: L.Pile; | |
| 5 | 5 | |
| 6 | 6 | end StmtManyTypeArgs. |
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | module StmtNeedProc; |
| 2 | -import Str, M := Map([]U8, I64, I64, Str.eq); | |
| 2 | +import Str, M := Table([]U8, I64, I64, Str.eq); | |
| 3 | 3 | |
| 4 | -var m: M.Map; | |
| 4 | +var m: M.Table; | |
| 5 | 5 | |
| 6 | 6 | end StmtNeedProc. |
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | 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); | |
| 3 | 3 | |
| 4 | -var m: M.Map; | |
| 4 | +var m: M.Table; | |
| 5 | 5 | |
| 6 | 6 | end StmtNeedType. |
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | module TwoTypes; |
| 2 | -import A := List(I64), B := List(I32); | |
| 2 | +import A := Pile(I64), B := Pile(I32); | |
| 3 | 3 | proc main*; |
| 4 | -var a: A.List; b: B.List; | |
| 4 | +var a: A.Pile; b: B.Pile; | |
| 5 | 5 | begin |
| 6 | 6 | a := b; |
| 7 | 7 | end; |
| @@ -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. |
| @@ -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. |
| @@ -1,5 +0,0 @@ | ||
| 1 | -1001000 | |
| 2 | -1001000 | |
| 3 | -7 | |
| 4 | -10100 | |
| 5 | -nil again 0 |
| @@ -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. |
| @@ -1,10 +1,10 @@ | ||
| 1 | 1 | 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); | |
| 4 | 4 | |
| 5 | 5 | type Colour = (red, green, blue); |
| 6 | 6 | |
| 7 | -var names: Table.Map; (* Table is made here, after Colour *) | |
| 7 | +var names: Table.Table; (* Table is made here, after Colour *) | |
| 8 | 8 | |
| 9 | 9 | proc show(k: []U8); |
| 10 | 10 | var c: Colour; found: BOOL; |
| @@ -17,10 +17,10 @@ | ||
| 17 | 17 | |
| 18 | 18 | (* Ones is first used in a procedure, and made before its code *) |
| 19 | 19 | proc main*; |
| 20 | -var a: Ints.List; b: More.List; | |
| 20 | +var a: Ints.Pile; b: More.Pile; | |
| 21 | 21 | begin |
| 22 | 22 | 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; | |
| 24 | 24 | Out.str("in main, Ones has size "); Out.int(Ones.size); Out.ln; |
| 25 | 25 | show("sky"); |
| 26 | 26 | show("rose"); |
| @@ -28,7 +28,6 @@ | ||
| 28 | 28 | end; |
| 29 | 29 | |
| 30 | 30 | begin |
| 31 | - Table.init(ADR(names), Heap.heap); | |
| 32 | 31 | _ := Table.put(ADR(names), "rose", red); |
| 33 | 32 | _ := Table.put(ADR(names), "sky", blue); |
| 34 | 33 | end FirstUse. |
| @@ -2,5 +2,3 @@ | ||
| 2 | 2 | 8 1 |
| 3 | 3 | abtrue |
| 4 | 4 | [] false |
| 5 | -157 152 -1 | |
| 6 | -{ BEEF } x18446744073709551615 |
| @@ -1,30 +1,26 @@ | ||
| 1 | 1 | 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); | |
| 3 | 3 | |
| 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; | |
| 5 | 8 | |
| 6 | 9 | 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; | |
| 8 | 11 | begin |
| 9 | 12 | 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; | |
| 13 | 15 | 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; | |
| 16 | 17 | ok := Strs.push(ADR(l), "b"); |
| 17 | 18 | ok := Strs.push(ADR(l), "a"); |
| 18 | 19 | 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; | |
| 20 | 21 | s, ok := Strs.pop(ADR(l)); |
| 21 | 22 | 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; | |
| 28 | 24 | end; |
| 29 | 25 | |
| 30 | 26 | end Generic. |
| @@ -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 |
| @@ -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. |
| @@ -1 +0,0 @@ | ||
| 1 | -2 |
| @@ -1 +0,0 @@ | ||
| 1 | -freeing a stray pointer |
| @@ -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. |
| @@ -1 +0,0 @@ | ||
| 1 | -2 |
| @@ -1,2 +0,0 @@ | ||
| 1 | -341 entries, 341 of them, summing 6658308 | |
| 2 | -walked 341, summing 6658308 |
| @@ -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. |
| @@ -1,10 +1,10 @@ | ||
| 1 | 1 | 7506AC5B262CA1FF |
| 2 | 2 | 3FB999999999999A |
| 3 | -FFFFFFFFFFFFF | |
| 4 | -1 | |
| 5 | -1 | |
| 6 | -0 | |
| 7 | -0 | |
| 3 | +000FFFFFFFFFFFFF | |
| 4 | +0000000000000001 | |
| 5 | +0000000000000001 | |
| 6 | +0000000000000000 | |
| 7 | +0000000000000000 | |
| 8 | 8 | 7FEFFFFFFFFFFFFF |
| 9 | 9 | 4340000000000000 |
| 10 | 10 | 4340000000000002 |
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | (* Real literals read to the nearest F64, ties to the even one: their |
| 2 | 2 | bits, for numbers a reader of decimals easily gets one off. *) |
| 3 | 3 | module RealRead; |
| 4 | -import Fmt; | |
| 4 | +import Out; | |
| 5 | 5 | |
| 6 | 6 | proc bits(x: F64): U64; |
| 7 | 7 | var p: ^U64; |
| @@ -12,7 +12,7 @@ | ||
| 12 | 12 | |
| 13 | 13 | proc show(x: F64); |
| 14 | 14 | begin |
| 15 | - Fmt.print("{}\n", [Fmt.hex(bits(x))]); | |
| 15 | + Out.hex(bits(x), 16); Out.ln; | |
| 16 | 16 | end; |
| 17 | 17 | |
| 18 | 18 | proc main*; |
| @@ -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 | |
| 2 | 2 | in order, holding what they held; and a sort by a record's field. *) |
| 3 | 3 | 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); | |
| 5 | 5 | |
| 6 | 6 | type Person = record name: []U8; age: I64; end; |
| 7 | 7 | |
| @@ -45,7 +45,7 @@ | ||
| 45 | 45 | end; |
| 46 | 46 | people := [{name: "Ana", age: 41}, {name: "Bo", age: 7}, {name: "Cy", age: 90}, {name: "Di", age: 23}]; |
| 47 | 47 | 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; | |
| 49 | 49 | end; |
| 50 | 50 | |
| 51 | 51 | end Sorted. |
| @@ -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. |
| @@ -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. |
| @@ -1,16 +1,15 @@ | ||
| 1 | 1 | module Stack; |
| 2 | -import Ints := List(I64), Heap; | |
| 2 | +import Ints := Pile(I64); | |
| 3 | 3 | |
| 4 | 4 | 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 *) | |
| 7 | 7 | |
| 8 | 8 | proc top*(): I64; |
| 9 | 9 | begin |
| 10 | - return s.head^.val; | |
| 10 | + return Ints.top(p); | |
| 11 | 11 | end; |
| 12 | 12 | |
| 13 | 13 | begin |
| 14 | - Ints.init(ADR(s), Heap.heap); | |
| 15 | 14 | p := ADR(s); |
| 16 | 15 | end Stack. |
| @@ -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. |
| @@ -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 | + | ^^^^ | |
| 7 | 7 | raz: 1 error |
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | conform/err/FirstUseLate.raz:2:18: error: Later isn't declared |
| 2 | - 2 | import L := List(Later); | |
| 2 | + 2 | import L := Pile(Later); | |
| 3 | 3 | | ^^^^^ |
| 4 | 4 | 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 *) | |
| 6 | 6 | | ^ |
| 7 | 7 | raz: 1 error |
| @@ -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; | |
| 3 | 3 | | ^^^^ |
| 4 | 4 | raz: 1 error |
| @@ -1,4 +1,4 @@ | ||
| 1 | 1 | 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); | |
| 3 | 3 | | ^^^^^^^^ |
| 4 | 4 | raz: 1 error |
| @@ -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); | |
| 3 | 3 | | ^^^^^^^^^^^^^^^^^^^ |
| 4 | 4 | raz: 1 error |
| @@ -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 | + | ^^^ | |
| 4 | 4 | raz: 1 error |
| @@ -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 | + | ^^^^^^^ | |
| 4 | 4 | raz: 1 error |
| @@ -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 | |
| 2 | 2 | 6 | a := b; |
| 3 | 3 | | ^ |
| 4 | 4 | raz: 1 error |
| @@ -0,0 +1,5 @@ | ||
| 1 | +1001000 | |
| 2 | +1001000 | |
| 3 | +7 | |
| 4 | +10100 | |
| 5 | +nil again 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. |
| @@ -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 |
| @@ -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. |
| @@ -0,0 +1 @@ | ||
| 1 | +2 |
| @@ -0,0 +1 @@ | ||
| 1 | +freeing a stray pointer |
| @@ -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. |
| @@ -0,0 +1 @@ | ||
| 1 | +2 |
| @@ -0,0 +1,2 @@ | ||
| 1 | +341 entries, 341 of them, summing 6658308 | |
| 2 | +walked 341, summing 6658308 |
| @@ -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. |