Buf, Files, Fmt and Str take the Buf by var (report 2026-10.33) Buf.init, room, add, str, spare, cut, reset and done take var b: Buf, and bytes takes it as a value, a few words; Files.load and read, Fmt.write, Str.join and replace take var b: Buf.Buf. None kept the pointer it was given. Every caller drops its ADR; the Bufs src/ holds by pointer (Diag.say, Sym.typeName, X64's listing) are written b^. Helpers that only lend the Buf on take var too: Sym.typeText, Opt's say, usageOf, left and item, Doc's addLine and write, Widths' load, hex and table, Sh's dollar and readLine. Fmt's own format and put keep the pointer, since nil means standard output there. A trace shows no var parameter, so BufCut, BufFull and BufInit's standard error lose their b = 0x? lines. Volume 2, test/doc/Str and the site's Fmt lesson say the new signatures.
| @@ -602,8 +602,8 @@ | ||
| 602 | 602 | proc hexVal*(c: U8): INT; (* c's value as a hexadecimal digit, or -1 *) |
| 603 | 603 | proc lower*(s: []U8); (* s's ASCII capitals made small, in place *) |
| 604 | 604 | proc upper*(s: []U8); (* s's ASCII small letters made capitals, in place *) |
| 605 | -proc join*(b: ^Buf.Buf; parts: [][]U8; sep: []U8); (* the parts added to b, sep between each two *) | |
| 606 | -proc replace*(b: ^Buf.Buf; s, old, new: []U8); (* s added to b, with new for each old in it *) | |
| 605 | +proc join*(var b: Buf.Buf; parts: [][]U8; sep: []U8); (* the parts added to b, sep between each two *) | |
| 606 | +proc replace*(var b: Buf.Buf; s, old, new: []U8); (* s added to b, with new for each old in it *) | |
| 607 | 607 | proc encode*(c: I32): ([4]U8, INT); (* c in UTF-8: its bytes, and how many *) |
| 608 | 608 | proc decode*(s: []U8): (I32, INT); (* the character s starts with, and its length in bytes *) |
| 609 | 609 | proc decodeLast*(s: []U8): (I32, INT); (* the character s ends with, and its length in bytes *) |
| @@ -620,15 +620,15 @@ | ||
| 620 | 620 | |
| 621 | 621 | module Buf; |
| 622 | 622 | type Buf* = record len*: INT; ... end; |
| 623 | -proc init*(b: ^Buf; a: ^Mem.Allocator); | |
| 624 | -proc room*(b: ^Buf; n: INT): BOOL; (* room for n more bytes; false if the allocator has none *) | |
| 625 | -proc add*(b: ^Buf; c: U8); | |
| 626 | -proc str*(b: ^Buf; s: []U8); | |
| 627 | -proc spare*(b: ^Buf; n: INT): []U8; (* room for n more bytes, and those bytes, to fill *) | |
| 628 | -proc bytes*(b: ^Buf): []U8; (* what it holds, until it next grows *) | |
| 629 | -proc cut*(b: ^Buf; n: INT); (* only the first n bytes kept, its memory kept *) | |
| 630 | -proc reset*(b: ^Buf); (* emptied, its memory kept *) | |
| 631 | -proc done*(b: ^Buf); (* its memory given back *) | |
| 623 | +proc init*(var b: Buf; a: ^Mem.Allocator); | |
| 624 | +proc room*(var b: Buf; n: INT): BOOL; (* room for n more bytes; false if the allocator has none *) | |
| 625 | +proc add*(var b: Buf; c: U8); | |
| 626 | +proc str*(var b: Buf; s: []U8); | |
| 627 | +proc spare*(var b: Buf; n: INT): []U8; (* room for n more bytes, and those bytes, to fill *) | |
| 628 | +proc bytes*(b: Buf): []U8; (* what it holds, until it next grows *) | |
| 629 | +proc cut*(var b: Buf; n: INT); (* only the first n bytes kept, its memory kept *) | |
| 630 | +proc reset*(var b: Buf); (* emptied, its memory kept *) | |
| 631 | +proc done*(var b: Buf); (* its memory given back *) | |
| 632 | 632 | |
| 633 | 633 | module Fmt; |
| 634 | 634 | type Arg* = variant |
| @@ -642,7 +642,7 @@ | ||
| 642 | 642 | end; |
| 643 | 643 | proc print*(f: []U8; args: []Arg); (* to standard output *) |
| 644 | 644 | proc eprint*(f: []U8; args: []Arg); (* to standard error, once Out is flushed *) |
| 645 | -proc write*(b: ^Buf.Buf; f: []U8; args: []Arg); (* added to b *) | |
| 645 | +proc write*(var b: Buf.Buf; f: []U8; args: []Arg); (* added to b *) | |
| 646 | 646 | |
| 647 | 647 | module List(T); |
| 648 | 648 | type |
| @@ -709,8 +709,8 @@ | ||
| 709 | 709 | failed check does, if the allocator has no room; `room` asks first. A |
| 710 | 710 | program reads into a `Buf` through `spare`, then adds what it read to |
| 711 | 711 | `len`: `s := Buf.spare(b, 4096); n := Sys.read(fd, s); if n > 0 then |
| 712 | -INC(b^.len, n); end;`. `cut` takes back what was added, to a length it | |
| 713 | -had: `start := b^.len; ...; Buf.cut(b, start);`; a length past `len` | |
| 712 | +INC(b.len, n); end;`. `cut` takes back what was added, to a length it | |
| 713 | +had: `start := b.len; ...; Buf.cut(b, start);`; a length past `len` | |
| 714 | 714 | stops the program, as an index past the end does. A `Vec` is `Buf` for any `T`: one block, which |
| 715 | 715 | doubles when it is full, so `push` costs the same on average however |
| 716 | 716 | long it grows. `all` gives the elements as a slice, indexed and |
| @@ -821,8 +821,8 @@ | ||
| 821 | 821 | ``` |
| 822 | 822 | module Files; |
| 823 | 823 | proc arg*(i: INT): []U8; (* argument i; 0 is the program's name *) |
| 824 | -proc read*(path: []U8; b: ^Buf.Buf): BOOL; (* the whole file added to b *) | |
| 825 | -proc load*(path: []U8; b: ^Buf.Buf): INT; (* as read: 0, or minus the errno that stopped it *) | |
| 824 | +proc read*(path: []U8; var b: Buf.Buf): BOOL; (* the whole file added to b *) | |
| 825 | +proc load*(path: []U8; var b: Buf.Buf): INT; (* as read: 0, or minus the errno that stopped it *) | |
| 826 | 826 | proc write*(path, data: []U8): BOOL; (* the file made to hold data *) |
| 827 | 827 | |
| 828 | 828 | module Opt; |
| @@ -44,13 +44,13 @@ | ||
| 44 | 44 | proc main*; |
| 45 | 45 | var b: Buf.Buf; f: []U8; h: Header; i, at, loaded, exec: INT; r: [3]U8; |
| 46 | 46 | begin |
| 47 | - Buf.init(ADR(b), Heap.heap); | |
| 48 | - defer Buf.done(ADR(b)); | |
| 49 | - if not Files.read("/proc/self/exe", ADR(b)) then | |
| 47 | + Buf.init(b, Heap.heap); | |
| 48 | + defer Buf.done(b); | |
| 49 | + if not Files.read("/proc/self/exe", b) then | |
| 50 | 50 | Out.str("can't read the program\n"); |
| 51 | 51 | return; |
| 52 | 52 | end; |
| 53 | - f := Buf.bytes(ADR(b)); | |
| 53 | + f := Buf.bytes(b); | |
| 54 | 54 | |
| 55 | 55 | (* the header, copied out of the bytes whole *) |
| 56 | 56 | h := f[0].(Header); |
| @@ -47,12 +47,12 @@ | ||
| 47 | 47 | proc say(f: []U8; args: []Fmt.Arg); |
| 48 | 48 | var b: Buf.Buf; _n: INT; |
| 49 | 49 | begin |
| 50 | - Buf.init(ADR(b), Heap.heap); | |
| 51 | - Fmt.write(ADR(b), "Sh: ", []); | |
| 52 | - Fmt.write(ADR(b), f, args); | |
| 53 | - Buf.add(ADR(b), 10); | |
| 54 | - _n := Sys.write(Sys.stderr, Buf.bytes(ADR(b))); | |
| 55 | - Buf.done(ADR(b)); | |
| 50 | + Buf.init(b, Heap.heap); | |
| 51 | + Fmt.write(b, "Sh: ", []); | |
| 52 | + Fmt.write(b, f, args); | |
| 53 | + Buf.add(b, 10); | |
| 54 | + _n := Sys.write(Sys.stderr, Buf.bytes(b)); | |
| 55 | + Buf.done(b); | |
| 56 | 56 | end; |
| 57 | 57 | |
| 58 | 58 | proc special(c: U8): BOOL; |
| @@ -65,7 +65,7 @@ | ||
| 65 | 65 | |
| 66 | 66 | (* $NAME or $? at s[at], its value added to b; a $ before anything else |
| 67 | 67 | is itself. *) |
| 68 | -proc dollar(b: ^Buf.Buf); | |
| 68 | +proc dollar(var b: Buf.Buf); | |
| 69 | 69 | var start: INT; |
| 70 | 70 | begin |
| 71 | 71 | INC(at); |
| @@ -85,34 +85,34 @@ | ||
| 85 | 85 | proc readWord; |
| 86 | 86 | var b: Buf.Buf; q: U8; |
| 87 | 87 | begin |
| 88 | - Buf.init(ADR(b), ADR(arena.base)); | |
| 88 | + Buf.init(b, ADR(arena.base)); | |
| 89 | 89 | while (at < LEN(s)) and not special(s[at]) do |
| 90 | 90 | q := s[at]; |
| 91 | 91 | match q of |
| 92 | 92 | | 39: (* '...': every character as it is *) |
| 93 | 93 | INC(at); |
| 94 | - while (at < LEN(s)) and (s[at] # 39) do Buf.add(ADR(b), s[at]); INC(at); end; | |
| 94 | + while (at < LEN(s)) and (s[at] # 39) do Buf.add(b, s[at]); INC(at); end; | |
| 95 | 95 | INC(at); |
| 96 | 96 | | '"': (* "...": dollars, and \" and \\ *) |
| 97 | 97 | INC(at); |
| 98 | 98 | while (at < LEN(s)) and (s[at] # '"') do |
| 99 | - if s[at] = '$' then dollar(ADR(b)); | |
| 99 | + if s[at] = '$' then dollar(b); | |
| 100 | 100 | else |
| 101 | 101 | if (s[at] = '\\') and (at + 1 < LEN(s)) and ((s[at + 1] = '"') or (s[at + 1] = '\\')) then INC(at); end; |
| 102 | - Buf.add(ADR(b), s[at]); | |
| 102 | + Buf.add(b, s[at]); | |
| 103 | 103 | INC(at); |
| 104 | 104 | end; |
| 105 | 105 | end; |
| 106 | 106 | INC(at); |
| 107 | 107 | | '$': |
| 108 | - dollar(ADR(b)); | |
| 108 | + dollar(b); | |
| 109 | 109 | | else: |
| 110 | 110 | if (q = '\\') and (at + 1 < LEN(s)) then INC(at); end; |
| 111 | - Buf.add(ADR(b), s[at]); | |
| 111 | + Buf.add(b, s[at]); | |
| 112 | 112 | INC(at); |
| 113 | 113 | end; |
| 114 | 114 | end; |
| 115 | - text := Buf.bytes(ADR(b)); | |
| 115 | + text := Buf.bytes(b); | |
| 116 | 116 | end; |
| 117 | 117 | |
| 118 | 118 | proc next: Token; |
| @@ -185,10 +185,10 @@ | ||
| 185 | 185 | if (i = LEN(path)) or (path[i] = ':') then |
| 186 | 186 | dir := path[start..i]; |
| 187 | 187 | start := i + 1; |
| 188 | - Buf.init(ADR(b), ADR(arena.base)); | |
| 189 | - Fmt.write(ADR(b), "{}/{}", [Fmt.str(dir), Fmt.str(name)]); | |
| 190 | - info, err := Sys.info(Buf.bytes(ADR(b))); | |
| 191 | - if (err = 0) and info.file and (info.mode & 49H # 0) then return Buf.bytes(ADR(b)), true; end; | |
| 188 | + Buf.init(b, ADR(arena.base)); | |
| 189 | + Fmt.write(b, "{}/{}", [Fmt.str(dir), Fmt.str(name)]); | |
| 190 | + info, err := Sys.info(Buf.bytes(b)); | |
| 191 | + if (err = 0) and info.file and (info.mode & 49H # 0) then return Buf.bytes(b), true; end; | |
| 192 | 192 | end; |
| 193 | 193 | end; |
| 194 | 194 | return "", false; |
| @@ -314,7 +314,7 @@ | ||
| 314 | 314 | |
| 315 | 315 | (* A line from standard input, a byte at a time, so that what follows |
| 316 | 316 | it is left for the programs the line starts; false at its end. *) |
| 317 | -proc readLine(b: ^Buf.Buf): BOOL; | |
| 317 | +proc readLine(var b: Buf.Buf): BOOL; | |
| 318 | 318 | var c: [1]U8; |
| 319 | 319 | begin |
| 320 | 320 | Buf.reset(b); |
| @@ -349,10 +349,10 @@ | ||
| 349 | 349 | group := Sys.pid(); |
| 350 | 350 | _ok := Sys.foreground(Sys.stdin, group) = 0; |
| 351 | 351 | end; |
| 352 | - Buf.init(ADR(line), Heap.heap); | |
| 352 | + Buf.init(line, Heap.heap); | |
| 353 | 353 | if interactive then reap; show("$ ", []); end; |
| 354 | - while readLine(ADR(line)) do | |
| 355 | - run(Buf.bytes(ADR(line))); | |
| 354 | + while readLine(line) do | |
| 355 | + run(Buf.bytes(line)); | |
| 356 | 356 | if interactive then reap; show("$ ", []); end; |
| 357 | 357 | end; |
| 358 | 358 | if interactive then show("\n", []); end; |
| @@ -372,9 +372,9 @@ | ||
| 372 | 372 | if given then |
| 373 | 373 | run(script); |
| 374 | 374 | elsif found then |
| 375 | - Buf.init(ADR(b), Heap.heap); | |
| 376 | - if not Files.read(file, ADR(b)) then say("{}: can't read it", [Fmt.str(file)]); return 127; end; | |
| 377 | - run(Buf.bytes(ADR(b))); | |
| 375 | + Buf.init(b, Heap.heap); | |
| 376 | + if not Files.read(file, b) then say("{}: can't read it", [Fmt.str(file)]); return 127; end; | |
| 377 | + run(Buf.bytes(b)); | |
| 378 | 378 | else |
| 379 | 379 | interact; |
| 380 | 380 | end; |
| @@ -10,20 +10,20 @@ | ||
| 10 | 10 | end; |
| 11 | 11 | |
| 12 | 12 | (* An empty buffer, which takes its memory from a. *) |
| 13 | -proc init*(b: ^Buf; a: ^Mem.Allocator); | |
| 13 | +proc init*(var b: Buf; a: ^Mem.Allocator); | |
| 14 | 14 | begin |
| 15 | - b^ := {a: a}; | |
| 15 | + b := {a: a}; | |
| 16 | 16 | end; |
| 17 | 17 | |
| 18 | 18 | (* Room for n more bytes; false if the allocator has none. *) |
| 19 | -proc room*(b: ^Buf; n: INT): BOOL; | |
| 19 | +proc room*(var b: Buf; n: INT): BOOL; | |
| 20 | 20 | var cap: INT; p: ^U8; |
| 21 | 21 | begin |
| 22 | - if b^.len + n <= b^.cap then return true; end; | |
| 23 | - cap := b^.cap * 2; | |
| 22 | + if b.len + n <= b.cap then return true; end; | |
| 23 | + cap := b.cap * 2; | |
| 24 | 24 | if cap < 64 then cap := 64; end; |
| 25 | - while cap < b^.len + n do cap := cap * 2; end; | |
| 26 | - if b^.a = nil then | |
| 25 | + while cap < b.len + n do cap := cap * 2; end; | |
| 26 | + if b.a = nil then | |
| 27 | 27 | (* a zero Buf has no allocator: the core has no heap to assume, |
| 28 | 28 | so init must give it one. Said plainly, with the calls under |
| 29 | 29 | way, rather than as a nil pointer in Mem.alloc *) |
| @@ -31,19 +31,19 @@ | ||
| 31 | 31 | Rt.trace; |
| 32 | 32 | Rt.exit(2); |
| 33 | 33 | end; |
| 34 | - p := Mem.alloc(b^.a, cap, 1); | |
| 34 | + p := Mem.alloc(b.a, cap, 1); | |
| 35 | 35 | if p = nil then return false; end; |
| 36 | - if b^.len > 0 then COPY(p[0..b^.len], b^.data[0..b^.len]); end; | |
| 37 | - if b^.data # nil then Mem.free(b^.a, b^.data); end; | |
| 38 | - b^.data := p; | |
| 39 | - b^.cap := cap; | |
| 36 | + if b.len > 0 then COPY(p[0..b.len], b.data[0..b.len]); end; | |
| 37 | + if b.data # nil then Mem.free(b.a, b.data); end; | |
| 38 | + b.data := p; | |
| 39 | + b.cap := cap; | |
| 40 | 40 | return true; |
| 41 | 41 | end; |
| 42 | 42 | |
| 43 | 43 | (* Room for n more bytes, or the program stops: a buffer that went on |
| 44 | 44 | short of what was added to it would be wrong data, and nothing would |
| 45 | 45 | say so. A program that can do better asks room first. *) |
| 46 | -proc need(b: ^Buf; n: INT); | |
| 46 | +proc need(var b: Buf; n: INT); | |
| 47 | 47 | begin |
| 48 | 48 | if not room(b, n) then |
| 49 | 49 | _ := Sys.write(Sys.stderr, "Buf: out of memory\n"); |
| @@ -53,64 +53,64 @@ | ||
| 53 | 53 | end; |
| 54 | 54 | |
| 55 | 55 | (* Adds the byte c at the end. *) |
| 56 | -proc add*(b: ^Buf; c: U8); | |
| 56 | +proc add*(var b: Buf; c: U8); | |
| 57 | 57 | begin |
| 58 | 58 | need(b, 1); |
| 59 | - b^.data[b^.len] := c; | |
| 60 | - INC(b^.len); | |
| 59 | + b.data[b.len] := c; | |
| 60 | + INC(b.len); | |
| 61 | 61 | end; |
| 62 | 62 | |
| 63 | 63 | (* Adds the bytes of s at the end. *) |
| 64 | -proc str*(b: ^Buf; s: []U8); | |
| 64 | +proc str*(var b: Buf; s: []U8); | |
| 65 | 65 | begin |
| 66 | 66 | need(b, LEN(s)); |
| 67 | - COPY(b^.data[b^.len..b^.len + LEN(s)], s); | |
| 68 | - INC(b^.len, LEN(s)); | |
| 67 | + COPY(b.data[b.len..b.len + LEN(s)], s); | |
| 68 | + INC(b.len, LEN(s)); | |
| 69 | 69 | end; |
| 70 | 70 | |
| 71 | 71 | (* Room for n more bytes, and those bytes, for a read to fill: the |
| 72 | 72 | program then adds to len what it filled. Good until the buffer next |
| 73 | 73 | grows, as bytes is. *) |
| 74 | -proc spare*(b: ^Buf; n: INT): []U8; | |
| 74 | +proc spare*(var b: Buf; n: INT): []U8; | |
| 75 | 75 | begin |
| 76 | 76 | if n = 0 then return ""; end; (* a new buffer has no data to slice *) |
| 77 | 77 | need(b, n); |
| 78 | - return b^.data[b^.len..b^.len + n]; | |
| 78 | + return b.data[b.len..b.len + n]; | |
| 79 | 79 | end; |
| 80 | 80 | |
| 81 | 81 | (* What the buffer holds, until it next grows. *) |
| 82 | -proc bytes*(b: ^Buf): []U8; | |
| 82 | +proc bytes*(b: Buf): []U8; | |
| 83 | 83 | begin |
| 84 | - if b^.len = 0 then return ""; end; | |
| 85 | - return b^.data[0..b^.len]; | |
| 84 | + if b.len = 0 then return ""; end; | |
| 85 | + return b.data[0..b.len]; | |
| 86 | 86 | end; |
| 87 | 87 | |
| 88 | 88 | (* Keeps only the first n bytes, and the memory, for what comes next. |
| 89 | 89 | An n past what the buffer holds is the program's mistake, and stops |
| 90 | 90 | it, as an index past the end does. *) |
| 91 | -proc cut*(b: ^Buf; n: INT); | |
| 91 | +proc cut*(var b: Buf; n: INT); | |
| 92 | 92 | begin |
| 93 | - if (n < 0) or (n > b^.len) then | |
| 93 | + if (n < 0) or (n > b.len) then | |
| 94 | 94 | _ := Sys.write(Sys.stderr, "Buf.cut past the end\n"); |
| 95 | 95 | Rt.trace; |
| 96 | 96 | Rt.exit(2); |
| 97 | 97 | end; |
| 98 | - b^.len := n; | |
| 98 | + b.len := n; | |
| 99 | 99 | end; |
| 100 | 100 | |
| 101 | 101 | (* Empties the buffer, keeping its memory for what comes next. *) |
| 102 | -proc reset*(b: ^Buf); | |
| 102 | +proc reset*(var b: Buf); | |
| 103 | 103 | begin |
| 104 | 104 | cut(b, 0); |
| 105 | 105 | end; |
| 106 | 106 | |
| 107 | 107 | (* Gives the buffer's memory back, leaving it empty. *) |
| 108 | -proc done*(b: ^Buf); | |
| 108 | +proc done*(var b: Buf); | |
| 109 | 109 | begin |
| 110 | - if b^.data # nil then Mem.free(b^.a, b^.data); end; | |
| 111 | - b^.data := nil; | |
| 112 | - b^.len := 0; | |
| 113 | - b^.cap := 0; | |
| 110 | + if b.data # nil then Mem.free(b.a, b.data); end; | |
| 111 | + b.data := nil; | |
| 112 | + b.len := 0; | |
| 113 | + b.cap := 0; | |
| 114 | 114 | end; |
| 115 | 115 | |
| 116 | 116 | (* The module's test, over an arena. *) |
| @@ -118,26 +118,26 @@ | ||
| 118 | 118 | var ar: Arena.Arena; b: Buf; i: INT; s: []U8; |
| 119 | 119 | begin |
| 120 | 120 | if not Arena.init(ADR(ar), 1 shl 20) then Rt.stop("Buf: no arena"); end; |
| 121 | - init(ADR(b), ADR(ar.base)); | |
| 122 | - if LEN(bytes(ADR(b))) # 0 then Rt.stop("Buf: not empty"); end; | |
| 123 | - for i := 0 to 9999 do add(ADR(b), U8(i mod 251)); end; | |
| 124 | - str(ADR(b), "end"); | |
| 125 | - s := bytes(ADR(b)); | |
| 121 | + init(b, ADR(ar.base)); | |
| 122 | + if LEN(bytes(b)) # 0 then Rt.stop("Buf: not empty"); end; | |
| 123 | + for i := 0 to 9999 do add(b, U8(i mod 251)); end; | |
| 124 | + str(b, "end"); | |
| 125 | + s := bytes(b); | |
| 126 | 126 | if (LEN(s) # 10003) or (s[9998] # U8(9998 mod 251)) or (s[10000..10003] # "end") then |
| 127 | 127 | Rt.stop("Buf: contents"); |
| 128 | 128 | end; |
| 129 | - reset(ADR(b)); | |
| 130 | - str(ADR(b), "again"); | |
| 131 | - if bytes(ADR(b)) # "again" then Rt.stop("Buf.reset"); end; | |
| 132 | - cut(ADR(b), 2); | |
| 133 | - str(ADR(b), "ain"); | |
| 134 | - cut(ADR(b), 5); | |
| 135 | - if bytes(ADR(b)) # "again" then Rt.stop("Buf.cut"); end; | |
| 136 | - s := spare(ADR(b), 3); | |
| 129 | + reset(b); | |
| 130 | + str(b, "again"); | |
| 131 | + if bytes(b) # "again" then Rt.stop("Buf.reset"); end; | |
| 132 | + cut(b, 2); | |
| 133 | + str(b, "ain"); | |
| 134 | + cut(b, 5); | |
| 135 | + if bytes(b) # "again" then Rt.stop("Buf.cut"); end; | |
| 136 | + s := spare(b, 3); | |
| 137 | 137 | COPY(s, "abc"); |
| 138 | 138 | INC(b.len, 2); |
| 139 | - if (LEN(s) # 3) or (bytes(ADR(b)) # "againab") then Rt.stop("Buf.spare"); end; | |
| 140 | - done(ADR(b)); | |
| 139 | + if (LEN(s) # 3) or (bytes(b) # "againab") then Rt.stop("Buf.spare"); end; | |
| 140 | + done(b); | |
| 141 | 141 | Arena.done(ADR(ar)); |
| 142 | 142 | end; |
| 143 | 143 |
| @@ -17,21 +17,21 @@ | ||
| 17 | 17 | the read that failed, as Sys gives them: -2 (ENOENT) for no such |
| 18 | 18 | file, -13 (EACCES) for one not allowed, -21 (EISDIR) for a |
| 19 | 19 | directory. What was read before a read failed stays in b. *) |
| 20 | -proc load*(path: []U8; b: ^Buf.Buf): INT; | |
| 20 | +proc load*(path: []U8; var b: Buf.Buf): INT; | |
| 21 | 21 | var fd, n: INT; |
| 22 | 22 | begin |
| 23 | 23 | fd := Sys.openRead(path); |
| 24 | 24 | if fd < 0 then return fd; end; |
| 25 | 25 | repeat |
| 26 | 26 | n := Sys.read(fd, Buf.spare(b, 65536)); (* straight into b *) |
| 27 | - if n > 0 then INC(b^.len, n); end; | |
| 27 | + if n > 0 then INC(b.len, n); end; | |
| 28 | 28 | until (n <= 0) and (n # Sys.errIntr); (* a signal Sys.handle took; read again *) |
| 29 | 29 | Sys.close(fd); |
| 30 | 30 | return n; |
| 31 | 31 | end; |
| 32 | 32 | |
| 33 | 33 | (* Adds the file at path to b; false if it can't be read. *) |
| 34 | -proc read*(path: []U8; b: ^Buf.Buf): BOOL; | |
| 34 | +proc read*(path: []U8; var b: Buf.Buf): BOOL; | |
| 35 | 35 | begin |
| 36 | 36 | return load(path, b) = 0; |
| 37 | 37 | end; |
| @@ -57,15 +57,15 @@ | ||
| 57 | 57 | proc main*; |
| 58 | 58 | var b: Buf.Buf; |
| 59 | 59 | begin |
| 60 | - Buf.init(ADR(b), Heap.heap); | |
| 60 | + Buf.init(b, Heap.heap); | |
| 61 | 61 | if not write("/tmp/raz-files-test", "abc") then Rt.stop("Files.write"); end; |
| 62 | - if (load("/tmp/raz-files-test", ADR(b)) # 0) or (Buf.bytes(ADR(b)) # "abc") then Rt.stop("Files.load"); end; | |
| 63 | - if not read("/tmp/raz-files-test", ADR(b)) or (Buf.bytes(ADR(b)) # "abcabc") then Rt.stop("Files.read"); end; | |
| 62 | + if (load("/tmp/raz-files-test", b) # 0) or (Buf.bytes(b) # "abc") then Rt.stop("Files.load"); end; | |
| 63 | + if not read("/tmp/raz-files-test", b) or (Buf.bytes(b) # "abcabc") then Rt.stop("Files.read"); end; | |
| 64 | 64 | _ := Sys.remove("/tmp/raz-files-test"); |
| 65 | - if load("/tmp/raz-files-test", ADR(b)) # Sys.errNoEntry then Rt.stop("Files.load: no such file"); end; | |
| 66 | - if load("/", ADR(b)) # -21 then Rt.stop("Files.load: a directory"); end; | |
| 67 | - if read("/", ADR(b)) or (b.len # 6) then Rt.stop("Files.read: a directory"); end; | |
| 68 | - Buf.done(ADR(b)); | |
| 65 | + if load("/tmp/raz-files-test", b) # Sys.errNoEntry then Rt.stop("Files.load: no such file"); end; | |
| 66 | + if load("/", b) # -21 then Rt.stop("Files.load: a directory"); end; | |
| 67 | + if read("/", b) or (b.len # 6) then Rt.stop("Files.read: a directory"); end; | |
| 68 | + Buf.done(b); | |
| 69 | 69 | end; |
| 70 | 70 | |
| 71 | 71 | end Files. |
| @@ -37,7 +37,7 @@ | ||
| 37 | 37 | (* Where the pieces go: to b, or to standard output if b is nil. *) |
| 38 | 38 | proc put(b: ^Buf.Buf; s: []U8); |
| 39 | 39 | begin |
| 40 | - if b = nil then Out.str(s); else Buf.str(b, s); end; | |
| 40 | + if b = nil then Out.str(s); else Buf.str(b^, s); end; | |
| 41 | 41 | end; |
| 42 | 42 | |
| 43 | 43 | (* x as Arg's real says. The whole part and the fraction are taken |
| @@ -135,9 +135,9 @@ | ||
| 135 | 135 | begin |
| 136 | 136 | Out.flush; |
| 137 | 137 | if not Arena.init(ADR(ar), 1 shl 20) then Rt.stop("Fmt.eprint: no arena"); end; |
| 138 | - Buf.init(ADR(b), ADR(ar.base)); | |
| 138 | + Buf.init(b, ADR(ar.base)); | |
| 139 | 139 | format(ADR(b), f, args); |
| 140 | - s := Buf.bytes(ADR(b)); | |
| 140 | + s := Buf.bytes(b); | |
| 141 | 141 | while LEN(s) > 0 do |
| 142 | 142 | n := Sys.write(Sys.stderr, s); |
| 143 | 143 | if n > 0 then s := s[n..LEN(s)]; |
| @@ -148,9 +148,9 @@ | ||
| 148 | 148 | end; |
| 149 | 149 | |
| 150 | 150 | (* Appends f to b, each {} replaced by the next of args. *) |
| 151 | -proc write*(b: ^Buf.Buf; f: []U8; args: []Arg); | |
| 151 | +proc write*(var b: Buf.Buf; f: []U8; args: []Arg); | |
| 152 | 152 | begin |
| 153 | - format(b, f, args); | |
| 153 | + format(ADR(b), f, args); (* format's nil is standard output *) | |
| 154 | 154 | end; |
| 155 | 155 | |
| 156 | 156 | (* The module's test: formats into a buffer over an arena. *) |
| @@ -158,21 +158,21 @@ | ||
| 158 | 158 | var ar: Arena.Arena; b: Buf.Buf; zero: F64; |
| 159 | 159 | begin |
| 160 | 160 | if not Arena.init(ADR(ar), 1 shl 16) then Rt.stop("Fmt: no arena"); end; |
| 161 | - Buf.init(ADR(b), ADR(ar.base)); | |
| 162 | - write(ADR(b), "{} {} {} {}|{}{}{} {{}}", [int(-42), uint(0FFFFFFFFFFFFFFFFH), hex(48879), | |
| 161 | + Buf.init(b, ADR(ar.base)); | |
| 162 | + write(b, "{} {} {} {}|{}{}{} {{}}", [int(-42), uint(0FFFFFFFFFFFFFFFFH), hex(48879), | |
| 163 | 163 | str("s"), char('c'), bool(true), int(0)]); |
| 164 | - if Buf.bytes(ADR(b)) # "-42 18446744073709551615 BEEF s|ctrue0 {}" then Rt.stop("Fmt.write"); end; | |
| 165 | - Buf.reset(ADR(b)); | |
| 166 | - write(ADR(b), "{} and {}", [int(1)]); | |
| 167 | - if Buf.bytes(ADR(b)) # "1 and " then Rt.stop("Fmt: too few arguments"); end; | |
| 168 | - Buf.reset(ADR(b)); | |
| 169 | - write(ADR(b), "{} {} {} {} {} {} {} {}", [real(3.14159, 2), real(-2.5, 0), real(0.999, 2), real(0.05, 1), | |
| 164 | + if Buf.bytes(b) # "-42 18446744073709551615 BEEF s|ctrue0 {}" then Rt.stop("Fmt.write"); end; | |
| 165 | + Buf.reset(b); | |
| 166 | + write(b, "{} and {}", [int(1)]); | |
| 167 | + if Buf.bytes(b) # "1 and " then Rt.stop("Fmt: too few arguments"); end; | |
| 168 | + Buf.reset(b); | |
| 169 | + write(b, "{} {} {} {} {} {} {} {}", [real(3.14159, 2), real(-2.5, 0), real(0.999, 2), real(0.05, 1), | |
| 170 | 170 | real(1.0, 12), real(-0.001, 2), real(123.0, 0), real(999999999999999.9, 0)]); |
| 171 | - if Buf.bytes(ADR(b)) # "3.14 -3 1.00 0.1 1.000000000 -0.00 123 1000000000000000" then Rt.stop("Fmt.real"); end; | |
| 172 | - Buf.reset(ADR(b)); | |
| 173 | - write(ADR(b), "{} {} {} {} {} {}", [real(1.5E17, 2), real(-9.999E20, 2), real(1.0E15, 0), real(1.7976931348623157E308, 3), | |
| 171 | + if Buf.bytes(b) # "3.14 -3 1.00 0.1 1.000000000 -0.00 123 1000000000000000" then Rt.stop("Fmt.real"); end; | |
| 172 | + Buf.reset(b); | |
| 173 | + write(b, "{} {} {} {} {} {}", [real(1.5E17, 2), real(-9.999E20, 2), real(1.0E15, 0), real(1.7976931348623157E308, 3), | |
| 174 | 174 | real(1.0 / zero, 2), real(zero / zero, 2)]); |
| 175 | - if Buf.bytes(ADR(b)) # "1.50e17 -1.00e21 1e15 1.798e308 inf nan" then Rt.stop("Fmt.real: an exponent"); end; | |
| 175 | + if Buf.bytes(b) # "1.50e17 -1.00e21 1e15 1.798e308 inf nan" then Rt.stop("Fmt.real: an exponent"); end; | |
| 176 | 176 | Arena.done(ADR(ar)); |
| 177 | 177 | end; |
| 178 | 178 |
| @@ -38,7 +38,7 @@ | ||
| 38 | 38 | const largest = 9223372036854775807; (* the largest I64, a number with no top *) |
| 39 | 39 | |
| 40 | 40 | (* b written whole to fd, then freed. *) |
| 41 | -proc say(fd: INT; b: ^Buf.Buf); | |
| 41 | +proc say(fd: INT; var b: Buf.Buf); | |
| 42 | 42 | var s: []U8; n: INT; |
| 43 | 43 | begin |
| 44 | 44 | s := Buf.bytes(b); |
| @@ -62,7 +62,7 @@ | ||
| 62 | 62 | (* The scope's synopsis lines, as the help and the mistakes begin: |
| 63 | 63 | "usage: " and the first, the others under it; or, for a scope with |
| 64 | 64 | none, "usage: keep start". *) |
| 65 | -proc usageOf(p: ^Parser; b: ^Buf.Buf); | |
| 65 | +proc usageOf(p: ^Parser; var b: Buf.Buf); | |
| 66 | 66 | var i, lines: INT; c: U8; |
| 67 | 67 | begin |
| 68 | 68 | for i := 0 to LEN(p^.table) - 1 do |
| @@ -93,12 +93,12 @@ | ||
| 93 | 93 | proc mistake(p: ^Parser; f: []U8; args: []Fmt.Arg); |
| 94 | 94 | var b: Buf.Buf; |
| 95 | 95 | begin |
| 96 | - Buf.init(ADR(b), Heap.heap); | |
| 97 | - Fmt.write(ADR(b), "{}: ", [Fmt.str(p^.name)]); | |
| 98 | - Fmt.write(ADR(b), f, args); | |
| 99 | - Buf.add(ADR(b), 10); | |
| 100 | - usageOf(p, ADR(b)); | |
| 101 | - say(Sys.stderr, ADR(b)); | |
| 96 | + Buf.init(b, Heap.heap); | |
| 97 | + Fmt.write(b, "{}: ", [Fmt.str(p^.name)]); | |
| 98 | + Fmt.write(b, f, args); | |
| 99 | + Buf.add(b, 10); | |
| 100 | + usageOf(p, b); | |
| 101 | + say(Sys.stderr, b); | |
| 102 | 102 | Rt.exit(2); |
| 103 | 103 | end; |
| 104 | 104 | |
| @@ -119,7 +119,7 @@ | ||
| 119 | 119 | (* The left column of a row in the help, as "-b, --bar label", or, for |
| 120 | 120 | a value that may be left out, "-c, --color[=when]", as grep's help |
| 121 | 121 | has it. *) |
| 122 | -proc left(b: ^Buf.Buf; r: ^Row); | |
| 122 | +proc left(var b: Buf.Buf; r: ^Row); | |
| 123 | 123 | begin |
| 124 | 124 | match r^ of |
| 125 | 125 | | sub(_s, name, _help): Buf.str(b, name); |
| @@ -140,16 +140,16 @@ | ||
| 140 | 140 | proc leftWidth(r: ^Row): INT; |
| 141 | 141 | var b: Buf.Buf; n: INT; |
| 142 | 142 | begin |
| 143 | - Buf.init(ADR(b), Heap.heap); | |
| 144 | - left(ADR(b), r); | |
| 145 | - n := LEN(Buf.bytes(ADR(b))); | |
| 146 | - Buf.done(ADR(b)); | |
| 143 | + Buf.init(b, Heap.heap); | |
| 144 | + left(b, r); | |
| 145 | + n := LEN(Buf.bytes(b)); | |
| 146 | + Buf.done(b); | |
| 147 | 147 | return n; |
| 148 | 148 | end; |
| 149 | 149 | |
| 150 | 150 | (* A line of the list: the left column, padded to width and three |
| 151 | 151 | spaces more, and the line of help. *) |
| 152 | -proc item(b: ^Buf.Buf; r: ^Row; width: INT); | |
| 152 | +proc item(var b: Buf.Buf; r: ^Row; width: INT); | |
| 153 | 153 | var i: INT; |
| 154 | 154 | begin |
| 155 | 155 | Buf.str(b, " "); |
| @@ -198,9 +198,9 @@ | ||
| 198 | 198 | h := 'h'; |
| 199 | 199 | if ownsH(p) then h := 0; end; |
| 200 | 200 | own := option(p^.scope, h, "help", "", "print this and exit"); |
| 201 | - Buf.init(ADR(b), Heap.heap); | |
| 202 | - usageOf(p, ADR(b)); | |
| 203 | - Buf.add(ADR(b), 10); | |
| 201 | + Buf.init(b, Heap.heap); | |
| 202 | + usageOf(p, b); | |
| 203 | + Buf.add(b, 10); | |
| 204 | 204 | width := leftWidth(ADR(own)); |
| 205 | 205 | for i := 0 to LEN(p^.table) - 1 do |
| 206 | 206 | r := ADR(p^.table[i]); |
| @@ -209,19 +209,19 @@ | ||
| 209 | 209 | for i := 0 to LEN(p^.table) - 1 do |
| 210 | 210 | r := ADR(p^.table[i]); |
| 211 | 211 | match r^ of |
| 212 | - | sub(s, _name, _help): if s = p^.scope then item(ADR(b), r, width); end; | |
| 212 | + | sub(s, _name, _help): if s = p^.scope then item(b, r, width); end; | |
| 213 | 213 | | else: |
| 214 | 214 | end; |
| 215 | 215 | end; |
| 216 | 216 | for i := 0 to LEN(p^.table) - 1 do |
| 217 | 217 | r := ADR(p^.table[i]); |
| 218 | 218 | match r^ of |
| 219 | - | option(s, _l, _long, _v, _help): if s = p^.scope then item(ADR(b), r, width); end; | |
| 219 | + | option(s, _l, _long, _v, _help): if s = p^.scope then item(b, r, width); end; | |
| 220 | 220 | | else: |
| 221 | 221 | end; |
| 222 | 222 | end; |
| 223 | - item(ADR(b), ADR(own), width); | |
| 224 | - say(Sys.stdout, ADR(b)); | |
| 223 | + item(b, ADR(own), width); | |
| 224 | + say(Sys.stdout, b); | |
| 225 | 225 | Rt.exit(0); |
| 226 | 226 | end; |
| 227 | 227 | |
| @@ -315,9 +315,9 @@ | ||
| 315 | 315 | proc badTable(p: ^Parser; row: INT; why: []U8); |
| 316 | 316 | var b: Buf.Buf; |
| 317 | 317 | begin |
| 318 | - Buf.init(ADR(b), Heap.heap); | |
| 319 | - Fmt.write(ADR(b), "{}: option table, row {}: {}\n", [Fmt.str(p^.name), Fmt.int(row), Fmt.str(why)]); | |
| 320 | - say(Sys.stderr, ADR(b)); | |
| 318 | + Buf.init(b, Heap.heap); | |
| 319 | + Fmt.write(b, "{}: option table, row {}: {}\n", [Fmt.str(p^.name), Fmt.int(row), Fmt.str(why)]); | |
| 320 | + say(Sys.stderr, b); | |
| 321 | 321 | Rt.exit(1); |
| 322 | 322 | end; |
| 323 | 323 | |
| @@ -431,9 +431,9 @@ | ||
| 431 | 431 | proc fail*(p: ^Parser; text: []U8); |
| 432 | 432 | var b: Buf.Buf; |
| 433 | 433 | begin |
| 434 | - Buf.init(ADR(b), Heap.heap); | |
| 435 | - Fmt.write(ADR(b), "{}: {}\n", [Fmt.str(p^.name), Fmt.str(text)]); | |
| 436 | - say(Sys.stderr, ADR(b)); | |
| 434 | + Buf.init(b, Heap.heap); | |
| 435 | + Fmt.write(b, "{}: {}\n", [Fmt.str(p^.name), Fmt.str(text)]); | |
| 436 | + say(Sys.stderr, b); | |
| 437 | 437 | Rt.exit(2); |
| 438 | 438 | end; |
| 439 | 439 | |
| @@ -463,9 +463,9 @@ | ||
| 463 | 463 | w, ok := operand(ADR(p)); |
| 464 | 464 | if not ok then fail(ADR(p), "no command; see keep -h"); end; |
| 465 | 465 | if w # "start" then |
| 466 | - Buf.init(ADR(b), Heap.heap); | |
| 467 | - Fmt.write(ADR(b), "no command {}; see keep -h", [Fmt.str(w)]); | |
| 468 | - fail(ADR(p), Buf.bytes(ADR(b))); | |
| 466 | + Buf.init(b, Heap.heap); | |
| 467 | + Fmt.write(b, "no command {}; see keep -h", [Fmt.str(w)]); | |
| 468 | + fail(ADR(p), Buf.bytes(b)); | |
| 469 | 469 | end; |
| 470 | 470 | scope(ADR(p), "start"); |
| 471 | 471 | while next(ADR(p)) do |
| @@ -219,7 +219,7 @@ | ||
| 219 | 219 | end; |
| 220 | 220 | |
| 221 | 221 | (* The parts added to b, with sep between each two. *) |
| 222 | -proc join*(b: ^Buf.Buf; parts: [][]U8; sep: []U8); | |
| 222 | +proc join*(var b: Buf.Buf; parts: [][]U8; sep: []U8); | |
| 223 | 223 | var i: INT; |
| 224 | 224 | begin |
| 225 | 225 | for i := 0 to LEN(parts) - 1 do |
| @@ -230,7 +230,7 @@ | ||
| 230 | 230 | |
| 231 | 231 | (* s added to b, with new for each old in it, from the left, none |
| 232 | 232 | overlapping another; s as it is if old is empty. *) |
| 233 | -proc replace*(b: ^Buf.Buf; s, old, new: []U8); | |
| 233 | +proc replace*(var b: Buf.Buf; s, old, new: []U8); | |
| 234 | 234 | var i: INT; |
| 235 | 235 | begin |
| 236 | 236 | i := -1; |
| @@ -547,18 +547,18 @@ | ||
| 547 | 547 | if up # "AB-Z9" then Rt.stop("Str.upper"); end; |
| 548 | 548 | lower(up); |
| 549 | 549 | if up # "ab-z9" then Rt.stop("Str.lower"); end; |
| 550 | - Buf.init(ADR(b), Heap.heap); | |
| 551 | - join(ADR(b), ["a", "b", "c"], ", "); | |
| 552 | - if Buf.bytes(ADR(b)) # "a, b, c" then Rt.stop("Str.join"); end; | |
| 553 | - Buf.reset(ADR(b)); | |
| 554 | - join(ADR(b), [], "-"); | |
| 555 | - replace(ADR(b), "a.b..c", ".", "::"); | |
| 556 | - if Buf.bytes(ADR(b)) # "a::b::::c" then Rt.stop("Str.replace"); end; | |
| 557 | - Buf.reset(ADR(b)); | |
| 558 | - replace(ADR(b), "aaa", "aa", "b"); | |
| 559 | - replace(ADR(b), "xy", "", "-"); | |
| 560 | - if Buf.bytes(ADR(b)) # "baxy" then Rt.stop("Str.replace: overlapping, empty"); end; | |
| 561 | - Buf.done(ADR(b)); | |
| 550 | + Buf.init(b, Heap.heap); | |
| 551 | + join(b, ["a", "b", "c"], ", "); | |
| 552 | + if Buf.bytes(b) # "a, b, c" then Rt.stop("Str.join"); end; | |
| 553 | + Buf.reset(b); | |
| 554 | + join(b, [], "-"); | |
| 555 | + replace(b, "a.b..c", ".", "::"); | |
| 556 | + if Buf.bytes(b) # "a::b::::c" then Rt.stop("Str.replace"); end; | |
| 557 | + Buf.reset(b); | |
| 558 | + replace(b, "aaa", "aa", "b"); | |
| 559 | + replace(b, "xy", "", "-"); | |
| 560 | + if Buf.bytes(b) # "baxy" then Rt.stop("Str.replace: overlapping, empty"); end; | |
| 561 | + Buf.done(b); | |
| 562 | 562 | end; |
| 563 | 563 | |
| 564 | 564 | proc testChars; |
| @@ -67,7 +67,7 @@ | ||
| 67 | 67 | import Out, Buf, Fmt, Heap; |
| 68 | 68 | |
| 69 | 69 | (* "name (age n)", built in a Buf the caller owns *) |
| 70 | -proc describe(b: ^Buf.Buf; name: []U8; age: INT); | |
| 70 | +proc describe(var b: Buf.Buf; name: []U8; age: INT); | |
| 71 | 71 | begin |
| 72 | 72 | Fmt.write(b, "{} (age {})", [Fmt.str(name), Fmt.int(age)]); |
| 73 | 73 | end; |
| @@ -75,12 +75,12 @@ | ||
| 75 | 75 | proc main*; |
| 76 | 76 | var b: Buf.Buf; |
| 77 | 77 | begin |
| 78 | - Buf.init(ADR(b), Heap.heap); | |
| 79 | - defer Buf.done(ADR(b)); | |
| 80 | - describe(ADR(b), "Ada", 36); | |
| 81 | - Buf.str(ADR(b), "; "); | |
| 82 | - describe(ADR(b), "Grace", 45); | |
| 83 | - Out.str(Buf.bytes(ADR(b))); Out.ln; | |
| 78 | + Buf.init(b, Heap.heap); | |
| 79 | + defer Buf.done(b); | |
| 80 | + describe(b, "Ada", 36); | |
| 81 | + Buf.str(b, "; "); | |
| 82 | + describe(b, "Grace", 45); | |
| 83 | + Out.str(Buf.bytes(b)); Out.ln; | |
| 84 | 84 | Out.int(b.len); Out.str(" bytes\n"); |
| 85 | 85 | end; |
| 86 | 86 |
| @@ -17,15 +17,15 @@ | ||
| 17 | 17 | begin |
| 18 | 18 | ok := Files.write("notes.txt", "first line\nsecond line\n"); |
| 19 | 19 | if not ok then Out.str("can't write\n"); return; end; |
| 20 | - Buf.init(ADR(b), Heap.heap); | |
| 21 | - defer Buf.done(ADR(b)); | |
| 22 | - ok := Files.read("notes.txt", ADR(b)); | |
| 20 | + Buf.init(b, Heap.heap); | |
| 21 | + defer Buf.done(b); | |
| 22 | + ok := Files.read("notes.txt", b); | |
| 23 | 23 | if ok then |
| 24 | - Out.int(b.len); Out.str(" bytes: "); Out.str(Buf.bytes(ADR(b))); | |
| 24 | + Out.int(b.len); Out.str(" bytes: "); Out.str(Buf.bytes(b)); | |
| 25 | 25 | end; |
| 26 | - ok := Files.read("missing.txt", ADR(b)); | |
| 26 | + ok := Files.read("missing.txt", b); | |
| 27 | 27 | if not ok then Out.str("missing.txt: not there\n"); end; |
| 28 | - if Files.load("missing.txt", ADR(b)) = -2 then Out.str("missing.txt: ENOENT\n"); end; | |
| 28 | + if Files.load("missing.txt", b) = -2 then Out.str("missing.txt: ENOENT\n"); end; | |
| 29 | 29 | end; |
| 30 | 30 | |
| 31 | 31 | end Notes. |
| @@ -119,12 +119,12 @@ | ||
| 119 | 119 | end; |
| 120 | 120 | Sys.closeDir(ADR(d)); |
| 121 | 121 | Order.sort(names[0..n]); |
| 122 | - Buf.init(ADR(path), Heap.heap); | |
| 122 | + Buf.init(path, Heap.heap); | |
| 123 | 123 | for i := 0 to n - 1 do |
| 124 | - Buf.reset(ADR(path)); | |
| 125 | - Buf.str(ADR(path), "proj/"); | |
| 126 | - Buf.str(ADR(path), names[i]); | |
| 127 | - info, err := Sys.info(Buf.bytes(ADR(path))); | |
| 124 | + Buf.reset(path); | |
| 125 | + Buf.str(path, "proj/"); | |
| 126 | + Buf.str(path, names[i]); | |
| 127 | + info, err := Sys.info(Buf.bytes(path)); | |
| 128 | 128 | Out.str(names[i]); |
| 129 | 129 | if err < 0 then Out.str(" ?"); |
| 130 | 130 | elsif info.dir then Out.str("/"); |
| @@ -133,7 +133,7 @@ | ||
| 133 | 133 | Out.ln; |
| 134 | 134 | Mem.free(Heap.heap, ^U8(ADR(names[i][0]))); |
| 135 | 135 | end; |
| 136 | - Buf.done(ADR(path)); | |
| 136 | + Buf.done(path); | |
| 137 | 137 | end; |
| 138 | 138 | |
| 139 | 139 | end Tree. |
| @@ -109,16 +109,16 @@ | ||
| 109 | 109 | p.fds[1] := wr; |
| 110 | 110 | pid := Sys.spawn(p); |
| 111 | 111 | Sys.close(wr); (* only the child writes now *) |
| 112 | - Buf.init(ADR(b), Heap.heap); | |
| 113 | - defer Buf.done(ADR(b)); | |
| 112 | + Buf.init(b, Heap.heap); | |
| 113 | + defer Buf.done(b); | |
| 114 | 114 | n := Sys.read(rd, chunk); |
| 115 | 115 | while n > 0 do |
| 116 | - Buf.str(ADR(b), chunk[0..n]); | |
| 116 | + Buf.str(b, chunk[0..n]); | |
| 117 | 117 | n := Sys.read(rd, chunk); |
| 118 | 118 | end; |
| 119 | 119 | Sys.close(rd); |
| 120 | 120 | _, how, status := Sys.wait(pid, true); |
| 121 | - Out.str("captured: "); Out.str(Buf.bytes(ADR(b))); | |
| 121 | + Out.str("captured: "); Out.str(Buf.bytes(b)); | |
| 122 | 122 | Out.str("status "); Out.int(status); Out.ln; |
| 123 | 123 | if how = Sys.exited then Out.str("(a normal exit)\n"); end; |
| 124 | 124 | end; |
| @@ -147,11 +147,11 @@ | ||
| 147 | 147 | var c: Counted.Counted; b: Buf.Buf; l: Names.List; i: INT; p: ^U8; |
| 148 | 148 | begin |
| 149 | 149 | Counted.init(ADR(c), Heap.heap); |
| 150 | - Buf.init(ADR(b), ADR(c.base)); | |
| 151 | - for i := 1 to 200 do Buf.str(ADR(b), "0123456789"); end; | |
| 150 | + Buf.init(b, ADR(c.base)); | |
| 151 | + for i := 1 to 200 do Buf.str(b, "0123456789"); end; | |
| 152 | 152 | Fmt.print("a buffer of {} bytes took {} allocations\n", |
| 153 | 153 | [Fmt.int(b.len), Fmt.int(c.allocs)]); |
| 154 | - Buf.done(ADR(b)); | |
| 154 | + Buf.done(b); | |
| 155 | 155 | |
| 156 | 156 | Names.init(ADR(l), ADR(c.base)); |
| 157 | 157 | for i := 1 to 10 do _ := Names.push(ADR(l), "x"); end; |
| @@ -100,21 +100,21 @@ | ||
| 100 | 100 | proc main*; |
| 101 | 101 | var b: Buf.Buf; i: INT; |
| 102 | 102 | begin |
| 103 | - Buf.init(ADR(b), Heap.heap); | |
| 104 | - defer Buf.done(ADR(b)); | |
| 103 | + Buf.init(b, Heap.heap); | |
| 104 | + defer Buf.done(b); | |
| 105 | 105 | for i := 1 to 5 do |
| 106 | - Buf.str(ADR(b), "item "); | |
| 107 | - Buf.add(ADR(b), U8('0' + i)); | |
| 108 | - Buf.add(ADR(b), ','); | |
| 106 | + Buf.str(b, "item "); | |
| 107 | + Buf.add(b, U8('0' + i)); | |
| 108 | + Buf.add(b, ','); | |
| 109 | 109 | end; |
| 110 | - Out.str(Buf.bytes(ADR(b))); Out.ln; | |
| 110 | + Out.str(Buf.bytes(b)); Out.ln; | |
| 111 | 111 | Out.int(b.len); Out.str(" bytes\n"); |
| 112 | - Buf.reset(ADR(b)); | |
| 113 | - Str.join(ADR(b), ["red", "green", "blue"], ", "); | |
| 114 | - Out.str(Buf.bytes(ADR(b))); Out.ln; | |
| 115 | - Buf.reset(ADR(b)); | |
| 116 | - Str.replace(ADR(b), "a-b-c", "-", " and "); | |
| 117 | - Out.str(Buf.bytes(ADR(b))); Out.ln; | |
| 112 | + Buf.reset(b); | |
| 113 | + Str.join(b, ["red", "green", "blue"], ", "); | |
| 114 | + Out.str(Buf.bytes(b)); Out.ln; | |
| 115 | + Buf.reset(b); | |
| 116 | + Str.replace(b, "a-b-c", "-", " and "); | |
| 117 | + Out.str(Buf.bytes(b)); Out.ln; | |
| 118 | 118 | end; |
| 119 | 119 | |
| 120 | 120 | end Build. |
| @@ -79,16 +79,16 @@ | ||
| 79 | 79 | var b: Buf.Buf; |
| 80 | 80 | begin |
| 81 | 81 | if n < 0 then return false; end; |
| 82 | - Buf.init(ADR(b), Heap.heap); | |
| 83 | - defer Buf.done(ADR(b)); (* freed on every way out below *) | |
| 84 | - Buf.str(ADR(b), "n is "); | |
| 82 | + Buf.init(b, Heap.heap); | |
| 83 | + defer Buf.done(b); (* freed on every way out below *) | |
| 84 | + Buf.str(b, "n is "); | |
| 85 | 85 | if n > 9 then |
| 86 | - Buf.str(ADR(b), "large"); | |
| 87 | - Out.str(Buf.bytes(ADR(b))); Out.ln; | |
| 86 | + Buf.str(b, "large"); | |
| 87 | + Out.str(Buf.bytes(b)); Out.ln; | |
| 88 | 88 | return true; |
| 89 | 89 | end; |
| 90 | - Buf.str(ADR(b), "small"); | |
| 91 | - Out.str(Buf.bytes(ADR(b))); Out.ln; | |
| 90 | + Buf.str(b, "small"); | |
| 91 | + Out.str(Buf.bytes(b)); Out.ln; | |
| 92 | 92 | return true; |
| 93 | 93 | end; |
| 94 | 94 |
| @@ -71,12 +71,12 @@ | ||
| 71 | 71 | if not cwdKnown then |
| 72 | 72 | d, err := Sys.workDir(); |
| 73 | 73 | if err = 0 then |
| 74 | - Buf.str(ADR(cwd), d); | |
| 75 | - Buf.add(ADR(cwd), '/'); | |
| 74 | + Buf.str(cwd, d); | |
| 75 | + Buf.add(cwd, '/'); | |
| 76 | 76 | end; |
| 77 | 77 | cwdKnown := true; |
| 78 | 78 | end; |
| 79 | - c := Buf.bytes(ADR(cwd)); | |
| 79 | + c := Buf.bytes(cwd); | |
| 80 | 80 | if (LEN(c) > 0) and Str.hasPrefix(path, c) then return path[LEN(c)..LEN(path)]; end; |
| 81 | 81 | return path; |
| 82 | 82 | end; |
| @@ -127,17 +127,17 @@ | ||
| 127 | 127 | |
| 128 | 128 | proc put(s: []U8); |
| 129 | 129 | begin |
| 130 | - Buf.str(ADR(out), s); | |
| 130 | + Buf.str(out, s); | |
| 131 | 131 | end; |
| 132 | 132 | |
| 133 | 133 | proc putc(c: U8); |
| 134 | 134 | begin |
| 135 | - Buf.add(ADR(out), c); | |
| 135 | + Buf.add(out, c); | |
| 136 | 136 | end; |
| 137 | 137 | |
| 138 | 138 | proc putNum(n: INT); |
| 139 | 139 | begin |
| 140 | - Fmt.write(ADR(out), "{}", [Fmt.int(n)]); | |
| 140 | + Fmt.write(out, "{}", [Fmt.int(n)]); | |
| 141 | 141 | end; |
| 142 | 142 | |
| 143 | 143 | (* The text of a number *) |
| @@ -145,9 +145,9 @@ | ||
| 145 | 145 | var b: ^Buf.Buf; |
| 146 | 146 | begin |
| 147 | 147 | b := ^Buf.Buf(grab(SIZE(Buf.Buf))); |
| 148 | - Buf.init(b, Heap.heap); | |
| 149 | - Fmt.write(b, f, args); | |
| 150 | - return Buf.bytes(b); | |
| 148 | + Buf.init(b^, Heap.heap); | |
| 149 | + Fmt.write(b^, f, args); | |
| 150 | + return Buf.bytes(b^); | |
| 151 | 151 | end; |
| 152 | 152 | |
| 153 | 153 | (* c, when colour is on *) |
| @@ -172,13 +172,13 @@ | ||
| 172 | 172 | proc flush; |
| 173 | 173 | var s: []U8; n: INT; |
| 174 | 174 | begin |
| 175 | - s := Buf.bytes(ADR(out)); | |
| 175 | + s := Buf.bytes(out); | |
| 176 | 176 | while LEN(s) > 0 do |
| 177 | 177 | n := Sys.write(Sys.stderr, s); |
| 178 | 178 | if n <= 0 then break; end; |
| 179 | 179 | s := s[n..LEN(s)]; |
| 180 | 180 | end; |
| 181 | - Buf.reset(ADR(out)); | |
| 181 | + Buf.reset(out); | |
| 182 | 182 | end; |
| 183 | 183 | |
| 184 | 184 | (* Positions *) |
| @@ -405,6 +405,6 @@ | ||
| 405 | 405 | end; |
| 406 | 406 | |
| 407 | 407 | begin |
| 408 | - Buf.init(ADR(cwd), Heap.heap); | |
| 409 | - Buf.init(ADR(out), Heap.heap); | |
| 408 | + Buf.init(cwd, Heap.heap); | |
| 409 | + Buf.init(out, Heap.heap); | |
| 410 | 410 | end Diag. |
| @@ -3603,10 +3603,10 @@ | ||
| 3603 | 3603 | proc moduleParams(m: PModule): ERROR; |
| 3604 | 3604 | var i: INT; o, a: PObj; sig: PType; s: Buf.Buf; at: Diag.Pos; text: []U8; |
| 3605 | 3605 | begin |
| 3606 | - Buf.init(ADR(s), Heap.heap); | |
| 3606 | + Buf.init(s, Heap.heap); | |
| 3607 | 3607 | i := 0; |
| 3608 | - Buf.str(ADR(s), nameOf(m^.name)); | |
| 3609 | - Buf.add(ADR(s), '('); | |
| 3608 | + Buf.str(s, nameOf(m^.name)); | |
| 3609 | + Buf.add(s, '('); | |
| 3610 | 3610 | Scan.next; |
| 3611 | 3611 | repeat |
| 3612 | 3612 | if Scan.tok # Scan.sIdent then return expected("a name"); end; |
| @@ -3619,8 +3619,8 @@ | ||
| 3619 | 3619 | o := Sym.newObj(Sym.oType, Scan.id, m^.args.t[i]); |
| 3620 | 3620 | o^.used := true; (* a parameter of the module, not a declaration *) |
| 3621 | 3621 | try declare(o); |
| 3622 | - Buf.str(ADR(s), Sym.typeName(m^.args.t[i])); | |
| 3623 | - Buf.add(ADR(s), ','); | |
| 3622 | + Buf.str(s, Sym.typeName(m^.args.t[i])); | |
| 3623 | + Buf.add(s, ','); | |
| 3624 | 3624 | INC(i); |
| 3625 | 3625 | Scan.next; |
| 3626 | 3626 | if Scan.tok # Scan.sComma then break; end; |
| @@ -3650,20 +3650,20 @@ | ||
| 3650 | 3650 | o^.typ := a^.typ; o^.chunk := a^.chunk; o^.isCode := a^.isCode; o^.ext := a^.ext; |
| 3651 | 3651 | o^.used := true; |
| 3652 | 3652 | try declare(o); |
| 3653 | - Buf.str(ADR(s), nameOf(a^.home^.name)); | |
| 3654 | - Buf.add(ADR(s), '.'); | |
| 3655 | - Buf.str(ADR(s), nameOf(a^.name)); | |
| 3656 | - Buf.add(ADR(s), ','); | |
| 3653 | + Buf.str(s, nameOf(a^.home^.name)); | |
| 3654 | + Buf.add(s, '.'); | |
| 3655 | + Buf.str(s, nameOf(a^.name)); | |
| 3656 | + Buf.add(s, ','); | |
| 3657 | 3657 | INC(i); |
| 3658 | 3658 | end; |
| 3659 | 3659 | try expect(Scan.sRparen); |
| 3660 | 3660 | if i # m^.args.n then |
| 3661 | 3661 | return Scan.markAt(m^.args.at, say1("too many type arguments for {}", nameOf(m^.name))); |
| 3662 | 3662 | end; |
| 3663 | - text := Buf.bytes(ADR(s)); | |
| 3663 | + text := Buf.bytes(s); | |
| 3664 | 3664 | text[LEN(text) - 1] := ')'; |
| 3665 | 3665 | m^.sname := internName(text); |
| 3666 | - Buf.done(ADR(s)); | |
| 3666 | + Buf.done(s); | |
| 3667 | 3667 | return nil; |
| 3668 | 3668 | end; |
| 3669 | 3669 |
| @@ -510,7 +510,7 @@ | ||
| 510 | 510 | |
| 511 | 511 | (* What a type is called, added to b: the names a program gives it, else |
| 512 | 512 | what it is made of *) |
| 513 | -proc typeText(b: ^Buf.Buf; t: ^Type); | |
| 513 | +proc typeText(var b: Buf.Buf; t: ^Type); | |
| 514 | 514 | var f: ^Obj; |
| 515 | 515 | begin |
| 516 | 516 | if t = nil then Buf.str(b, "nothing"); return; end; |
| @@ -574,9 +574,9 @@ | ||
| 574 | 574 | var b: ^Buf.Buf; |
| 575 | 575 | begin |
| 576 | 576 | b := ^Buf.Buf(Scan.grab(SIZE(Buf.Buf))); |
| 577 | - Buf.init(b, Heap.heap); | |
| 578 | - typeText(b, t); | |
| 579 | - return Buf.bytes(b); | |
| 577 | + Buf.init(b^, Heap.heap); | |
| 578 | + typeText(b^, t); | |
| 579 | + return Buf.bytes(b^); | |
| 580 | 580 | end; |
| 581 | 581 | |
| 582 | 582 | (* A fingerprint of what a declaration exports, for the interface |
| @@ -293,22 +293,22 @@ | ||
| 293 | 293 | var b: ^Buf.Buf; |
| 294 | 294 | begin |
| 295 | 295 | b := ^Buf.Buf(Scan.grab(SIZE(Buf.Buf))); |
| 296 | - Buf.init(b, Heap.heap); | |
| 297 | - if size = 1 then Buf.str(b, "byte "); | |
| 298 | - elsif size = 2 then Buf.str(b, "word "); | |
| 299 | - elsif size = 4 then Buf.str(b, "dword "); | |
| 300 | - elsif size = 8 then Buf.str(b, "qword "); | |
| 296 | + Buf.init(b^, Heap.heap); | |
| 297 | + if size = 1 then Buf.str(b^, "byte "); | |
| 298 | + elsif size = 2 then Buf.str(b^, "word "); | |
| 299 | + elsif size = 4 then Buf.str(b^, "dword "); | |
| 300 | + elsif size = 8 then Buf.str(b^, "qword "); | |
| 301 | 301 | end; |
| 302 | - Buf.str(b, "["); | |
| 303 | - if m.base = RIP then Buf.str(b, Chunk.chunkName(m.chunk)); else Buf.str(b, rn(m.base)); end; | |
| 304 | - if m.index # NoIndex then Fmt.write(b, " + {} * {}", [Fmt.str(rn(m.index)), Fmt.int(m.scale)]); end; | |
| 302 | + Buf.str(b^, "["); | |
| 303 | + if m.base = RIP then Buf.str(b^, Chunk.chunkName(m.chunk)); else Buf.str(b^, rn(m.base)); end; | |
| 304 | + if m.index # NoIndex then Fmt.write(b^, " + {} * {}", [Fmt.str(rn(m.index)), Fmt.int(m.scale)]); end; | |
| 305 | 305 | if m.disp > 0 then |
| 306 | - Fmt.write(b, " + {}", [Fmt.int(m.disp)]); | |
| 306 | + Fmt.write(b^, " + {}", [Fmt.int(m.disp)]); | |
| 307 | 307 | elsif m.disp < 0 then |
| 308 | - Fmt.write(b, " - {}", [Fmt.int(-m.disp)]); | |
| 308 | + Fmt.write(b^, " - {}", [Fmt.int(-m.disp)]); | |
| 309 | 309 | end; |
| 310 | - Buf.str(b, "]"); | |
| 311 | - return Buf.bytes(b); | |
| 310 | + Buf.str(b^, "]"); | |
| 311 | + return Buf.bytes(b^); | |
| 312 | 312 | end; |
| 313 | 313 | |
| 314 | 314 | proc mn(m: Mem): []U8; |
| @@ -84,10 +84,10 @@ | ||
| 84 | 84 | proc upper*(s: []U8); |
| 85 | 85 | s's small ASCII letters made capitals, in place. |
| 86 | 86 | |
| 87 | -proc join*(b: ^Buf.Buf; parts: [][]U8; sep: []U8); | |
| 87 | +proc join*(var b: Buf.Buf; parts: [][]U8; sep: []U8); | |
| 88 | 88 | The parts added to b, with sep between each two. |
| 89 | 89 | |
| 90 | -proc replace*(b: ^Buf.Buf; s, old, new: []U8); | |
| 90 | +proc replace*(var b: Buf.Buf; s, old, new: []U8); | |
| 91 | 91 | s added to b, with new for each old in it, from the left, none |
| 92 | 92 | overlapping another; s as it is if old is empty. |
| 93 | 93 |
| @@ -6,9 +6,9 @@ | ||
| 6 | 6 | proc main*; |
| 7 | 7 | var b: Buf.Buf; |
| 8 | 8 | begin |
| 9 | - Buf.init(ADR(b), Heap.heap); | |
| 10 | - Buf.str(ADR(b), "hello"); | |
| 11 | - Buf.cut(ADR(b), 6); | |
| 9 | + Buf.init(b, Heap.heap); | |
| 10 | + Buf.str(b, "hello"); | |
| 11 | + Buf.cut(b, 6); | |
| 12 | 12 | end; |
| 13 | 13 | |
| 14 | 14 | end BufCut. |
| @@ -1,3 +1,3 @@ | ||
| 1 | 1 | Buf.cut past the end |
| 2 | - in Buf.cut at lib/Buf.raz:95:9: b = 0x?, n = 6 | |
| 2 | + in Buf.cut at lib/Buf.raz:95:9: n = 6 | |
| 3 | 3 | in BufCut.main at test/run/BufCut.raz:11:5 |
| @@ -7,8 +7,8 @@ | ||
| 7 | 7 | var ar: Arena.Arena; b: Buf.Buf; big: [10000]U8; |
| 8 | 8 | begin |
| 9 | 9 | if not Arena.init(ADR(ar), 4096) then Rt.stop("no arena"); end; |
| 10 | - Buf.init(ADR(b), ADR(ar.base)); | |
| 11 | - Buf.str(ADR(b), big); | |
| 10 | + Buf.init(b, ADR(ar.base)); | |
| 11 | + Buf.str(b, big); | |
| 12 | 12 | end; |
| 13 | 13 | |
| 14 | 14 | end BufFull. |
| @@ -1,4 +1,4 @@ | ||
| 1 | 1 | Buf: out of memory |
| 2 | - in Buf.need at lib/Buf.raz:50:9: b = 0x?, n = 10000 | |
| 3 | - in Buf.str at lib/Buf.raz:66:5: b = 0x? | |
| 2 | + in Buf.need at lib/Buf.raz:50:9: n = 10000 | |
| 3 | + in Buf.str at lib/Buf.raz:66:5 | |
| 4 | 4 | in BufFull.main at test/run/BufFull.raz:11:5 |
| @@ -6,7 +6,7 @@ | ||
| 6 | 6 | proc main*; |
| 7 | 7 | var b: Buf.Buf; |
| 8 | 8 | begin |
| 9 | - Buf.str(ADR(b), "hello"); | |
| 9 | + Buf.str(b, "hello"); | |
| 10 | 10 | end; |
| 11 | 11 | |
| 12 | 12 | end BufInit. |
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | Buf used before Buf.init |
| 2 | - in Buf.room at lib/Buf.raz:31:9: b = 0x?, n = 5, cap = 64, p = nil | |
| 3 | - in Buf.need at lib/Buf.raz:48:5: b = 0x?, n = 5 | |
| 4 | - in Buf.str at lib/Buf.raz:66:5: b = 0x? | |
| 2 | + in Buf.room at lib/Buf.raz:31:9: n = 5, cap = 64, p = nil | |
| 3 | + in Buf.need at lib/Buf.raz:48:5: n = 5 | |
| 4 | + in Buf.str at lib/Buf.raz:66:5 | |
| 5 | 5 | in BufInit.main at test/run/BufInit.raz:9:5 |
| @@ -304,9 +304,9 @@ | ||
| 304 | 304 | proc document(path: []U8); |
| 305 | 305 | var b: Buf.Buf; start, ds, de: INT; |
| 306 | 306 | begin |
| 307 | - Buf.init(ADR(b), Heap.heap); | |
| 308 | - if not Files.read(path, ADR(b)) then Rt.stop("Doc: can't read the file"); end; | |
| 309 | - src := Buf.bytes(ADR(b)); | |
| 307 | + Buf.init(b, Heap.heap); | |
| 308 | + if not Files.read(path, b) then Rt.stop("Doc: can't read the file"); end; | |
| 309 | + src := Buf.bytes(b); | |
| 310 | 310 | Scan.init(ADR(sc), src); |
| 311 | 311 | next; |
| 312 | 312 | while (sc.kind # Scan.eof) and not is("module") do next; end; |
| @@ -336,7 +336,7 @@ | ||
| 336 | 336 | (* line, from the report or a manual page's literal block, added to b |
| 337 | 337 | as the program has it: in a page, \e is a backslash, \(ga a |
| 338 | 338 | backquote and \& nothing. *) |
| 339 | -proc addLine(b: ^Buf.Buf; line: []U8; page: BOOL); | |
| 339 | +proc addLine(var b: Buf.Buf; line: []U8; page: BOOL); | |
| 340 | 340 | var i: INT; |
| 341 | 341 | begin |
| 342 | 342 | i := 0; |
| @@ -357,7 +357,7 @@ | ||
| 357 | 357 | end; |
| 358 | 358 | |
| 359 | 359 | (* text written to the file dir/name and ext, its path made in out *) |
| 360 | -proc write(out: ^Buf.Buf; dir, name, ext, text: []U8); | |
| 360 | +proc write(var out: Buf.Buf; dir, name, ext, text: []U8); | |
| 361 | 361 | begin |
| 362 | 362 | Buf.reset(out); |
| 363 | 363 | Buf.str(out, dir); Buf.str(out, "/"); Buf.str(out, name); Buf.str(out, ext); |
| @@ -374,11 +374,11 @@ | ||
| 374 | 374 | b, out, prog: Buf.Buf; s, line, first, last, name, shows: []U8; |
| 375 | 375 | i, k, n, after: INT; inBlock, whole, page, fence: BOOL; |
| 376 | 376 | begin |
| 377 | - Buf.init(ADR(b), Heap.heap); | |
| 378 | - Buf.init(ADR(out), Heap.heap); | |
| 379 | - Buf.init(ADR(prog), Heap.heap); | |
| 380 | - if not Files.read(path, ADR(b)) then Rt.stop("Doc: can't read the examples' file"); end; | |
| 381 | - s := Buf.bytes(ADR(b)); | |
| 377 | + Buf.init(b, Heap.heap); | |
| 378 | + Buf.init(out, Heap.heap); | |
| 379 | + Buf.init(prog, Heap.heap); | |
| 380 | + if not Files.read(path, b) then Rt.stop("Doc: can't read the examples' file"); end; | |
| 381 | + s := Buf.bytes(b); | |
| 382 | 382 | page := (LEN(path) < 3) or (path[LEN(path) - 3..LEN(path)] # ".md"); |
| 383 | 383 | inBlock := false; |
| 384 | 384 | after := -1; |
| @@ -394,22 +394,22 @@ | ||
| 394 | 394 | n := 0; |
| 395 | 395 | while (n < LEN(name)) and Scan.isWord(name[n]) do INC(n); end; |
| 396 | 396 | name := name[0..n]; |
| 397 | - write(ADR(out), dir, name, ".raz", Buf.bytes(ADR(prog))); | |
| 397 | + write(out, dir, name, ".raz", Buf.bytes(prog)); | |
| 398 | 398 | Out.str(name); Out.ln; |
| 399 | 399 | after := k + 1; |
| 400 | 400 | elsif LEN(shows) > 0 then |
| 401 | - write(ADR(out), dir, shows, ".shown", Buf.bytes(ADR(prog))); | |
| 401 | + write(out, dir, shows, ".shown", Buf.bytes(prog)); | |
| 402 | 402 | end; |
| 403 | 403 | inBlock := false; |
| 404 | 404 | elsif not inBlock and (fence or page and Str.hasPrefix(line, ".Bd -literal")) then |
| 405 | 405 | inBlock := true; |
| 406 | 406 | shows := ""; |
| 407 | 407 | if page and (i = after) then shows := name; end; |
| 408 | - Buf.reset(ADR(prog)); | |
| 408 | + Buf.reset(prog); | |
| 409 | 409 | first := ""; last := ""; |
| 410 | 410 | whole := true; |
| 411 | 411 | elsif inBlock then |
| 412 | - addLine(ADR(prog), line, page); | |
| 412 | + addLine(prog, line, page); | |
| 413 | 413 | if Str.trim(line) = "..." then whole := false; end; |
| 414 | 414 | if LEN(Str.trim(line)) > 0 then |
| 415 | 415 | if LEN(first) = 0 then first := Str.trim(line); end; |
| @@ -62,9 +62,9 @@ | ||
| 62 | 62 | var buf: Buf.Buf; n, op, i: INT; a, b, c: Scan.Scanner; |
| 63 | 63 | begin |
| 64 | 64 | if ARGC() # 3 then Rt.stop("usage: Mutate File.raz seed"); end; |
| 65 | - Buf.init(ADR(buf), Heap.heap); | |
| 66 | - if not Files.read(Files.arg(1), ADR(buf)) then Rt.stop("Mutate: can't read the file"); end; | |
| 67 | - src := Buf.bytes(ADR(buf)); | |
| 65 | + Buf.init(buf, Heap.heap); | |
| 66 | + if not Files.read(Files.arg(1), buf) then Rt.stop("Mutate: can't read the file"); end; | |
| 67 | + src := Buf.bytes(buf); | |
| 68 | 68 | seed(Files.arg(2)); |
| 69 | 69 | n := count(); |
| 70 | 70 | if n < 2 then Out.str(src); return; end; |
| @@ -55,14 +55,14 @@ | ||
| 55 | 55 | |
| 56 | 56 | proc spaces(n: INT); |
| 57 | 57 | begin |
| 58 | - while n > 0 do Buf.add(ADR(out), ' '); DEC(n); end; | |
| 58 | + while n > 0 do Buf.add(out, ' '); DEC(n); end; | |
| 59 | 59 | end; |
| 60 | 60 | |
| 61 | 61 | (* Drops the spaces at the end of the line in out. *) |
| 62 | 62 | proc trim; |
| 63 | 63 | var s: []U8; |
| 64 | 64 | begin |
| 65 | - s := Buf.bytes(ADR(out)); | |
| 65 | + s := Buf.bytes(out); | |
| 66 | 66 | while (out.len > lineAt) and ((s[out.len - 1] = ' ') or (s[out.len - 1] = '\t')) do |
| 67 | 67 | DEC(out.len); |
| 68 | 68 | end; |
| @@ -71,7 +71,7 @@ | ||
| 71 | 71 | proc newLine; |
| 72 | 72 | begin |
| 73 | 73 | trim; |
| 74 | - Buf.add(ADR(out), '\n'); | |
| 74 | + Buf.add(out, '\n'); | |
| 75 | 75 | lineAt := out.len; |
| 76 | 76 | end; |
| 77 | 77 | |
| @@ -215,12 +215,12 @@ | ||
| 215 | 215 | if (n < 1) and (sc.start > last) then n := 1; end; |
| 216 | 216 | spaces(n); |
| 217 | 217 | elsif afterComment then |
| 218 | - Buf.str(ADR(out), src[last..sc.start]); | |
| 218 | + Buf.str(out, src[last..sc.start]); | |
| 219 | 219 | elsif Scan.is(ADR(sc), ",") or Scan.is(ADR(sc), ";") then |
| 220 | 220 | elsif Scan.is(ADR(prev), ",") or Scan.is(ADR(prev), ";") or binaryNow or prevBinary then |
| 221 | 221 | if not prevUnary then spaces(1); end; |
| 222 | 222 | elsif not prevUnary then |
| 223 | - Buf.str(ADR(out), src[last..sc.start]); | |
| 223 | + Buf.str(out, src[last..sc.start]); | |
| 224 | 224 | end; |
| 225 | 225 | end; |
| 226 | 226 | |
| @@ -233,7 +233,7 @@ | ||
| 233 | 233 | i := sc.start; |
| 234 | 234 | while i < sc.stop do |
| 235 | 235 | if (src[i] = '\n') and (sc.kind = Scan.literal) then |
| 236 | - Buf.add(ADR(out), '\n'); | |
| 236 | + Buf.add(out, '\n'); | |
| 237 | 237 | lineAt := out.len; |
| 238 | 238 | INC(i); |
| 239 | 239 | elsif src[i] = '\n' then |
| @@ -243,7 +243,7 @@ | ||
| 243 | 243 | if (k < sc.stop) and (src[k] # '\n') then spaces(k - (i + 1) + lineDelta); end; |
| 244 | 244 | i := k; |
| 245 | 245 | else |
| 246 | - Buf.add(ADR(out), src[i]); | |
| 246 | + Buf.add(out, src[i]); | |
| 247 | 247 | INC(i); |
| 248 | 248 | end; |
| 249 | 249 | end; |
| @@ -289,7 +289,7 @@ | ||
| 289 | 289 | var b: BOOL; |
| 290 | 290 | begin |
| 291 | 291 | src := text; |
| 292 | - Buf.reset(ADR(out)); | |
| 292 | + Buf.reset(out); | |
| 293 | 293 | lineAt := 0; |
| 294 | 294 | depth := 0; |
| 295 | 295 | brackets := 0; |
| @@ -325,23 +325,23 @@ | ||
| 325 | 325 | proc main*; |
| 326 | 326 | var b: Buf.Buf; i: INT; path: []U8; |
| 327 | 327 | begin |
| 328 | - Buf.init(ADR(b), Heap.heap); | |
| 329 | - Buf.init(ADR(out), Heap.heap); | |
| 328 | + Buf.init(b, Heap.heap); | |
| 329 | + Buf.init(out, Heap.heap); | |
| 330 | 330 | if (ARGC() >= 3) and (Files.arg(1) = "-w") then |
| 331 | 331 | for i := 2 to ARGC() - 1 do |
| 332 | 332 | path := Files.arg(i); |
| 333 | - Buf.reset(ADR(b)); | |
| 334 | - if not Files.read(path, ADR(b)) then Rt.stop("Pretty: can't read a file"); end; | |
| 335 | - format(Buf.bytes(ADR(b))); | |
| 336 | - if Buf.bytes(ADR(out)) # Buf.bytes(ADR(b)) then | |
| 337 | - if not Files.write(path, Buf.bytes(ADR(out))) then Rt.stop("Pretty: can't write a file"); end; | |
| 333 | + Buf.reset(b); | |
| 334 | + if not Files.read(path, b) then Rt.stop("Pretty: can't read a file"); end; | |
| 335 | + format(Buf.bytes(b)); | |
| 336 | + if Buf.bytes(out) # Buf.bytes(b) then | |
| 337 | + if not Files.write(path, Buf.bytes(out)) then Rt.stop("Pretty: can't write a file"); end; | |
| 338 | 338 | Out.str(path); Out.ln; |
| 339 | 339 | end; |
| 340 | 340 | end; |
| 341 | 341 | elsif ARGC() = 2 then |
| 342 | - if not Files.read(Files.arg(1), ADR(b)) then Rt.stop("Pretty: can't read the file"); end; | |
| 343 | - format(Buf.bytes(ADR(b))); | |
| 344 | - Out.str(Buf.bytes(ADR(out))); | |
| 342 | + if not Files.read(Files.arg(1), b) then Rt.stop("Pretty: can't read the file"); end; | |
| 343 | + format(Buf.bytes(b)); | |
| 344 | + Out.str(Buf.bytes(out)); | |
| 345 | 345 | else |
| 346 | 346 | Rt.stop("usage: Pretty Module.raz, or Pretty -w File.raz..."); |
| 347 | 347 | end; |
| @@ -20,7 +20,7 @@ | ||
| 20 | 20 | w: [Last + 1]U8; (* each character's width, 1 unless a file says *) |
| 21 | 21 | |
| 22 | 22 | (* The whole file at path. *) |
| 23 | -proc load(path: []U8; b: ^Buf.Buf): []U8; | |
| 23 | +proc load(path: []U8; var b: Buf.Buf): []U8; | |
| 24 | 24 | begin |
| 25 | 25 | Buf.init(b, Heap.heap); |
| 26 | 26 | if not Files.read(path, b) then |
| @@ -116,7 +116,7 @@ | ||
| 116 | 116 | end; |
| 117 | 117 | |
| 118 | 118 | (* c as a constant of raz: 0, six hexadecimal digits and H. *) |
| 119 | -proc hex(b: ^Buf.Buf; c: INT); | |
| 119 | +proc hex(var b: Buf.Buf; c: INT); | |
| 120 | 120 | const Digits = "0123456789ABCDEF"; |
| 121 | 121 | var i: INT; |
| 122 | 122 | begin |
| @@ -127,7 +127,7 @@ | ||
| 127 | 127 | |
| 128 | 128 | (* The constant name, the ranges of characters whose width is width, |
| 129 | 129 | each its first and last, four ranges to a line. *) |
| 130 | -proc table(b: ^Buf.Buf; name: []U8; width: U8); | |
| 130 | +proc table(var b: Buf.Buf; name: []U8; width: U8); | |
| 131 | 131 | var c, lo, n: INT; |
| 132 | 132 | begin |
| 133 | 133 | Buf.str(b, " "); Buf.str(b, name); Buf.str(b, ": []I32 = ["); |
| @@ -153,7 +153,7 @@ | ||
| 153 | 153 | Out.str("usage: Widths lib/Width.raz EastAsianWidth.txt UnicodeData.txt PropList.txt\n"); |
| 154 | 154 | Rt.exit(2); |
| 155 | 155 | end; |
| 156 | - s := load(Files.arg(1), ADR(src)); | |
| 156 | + s := load(Files.arg(1), src); | |
| 157 | 157 | before, after, ok := Str.cut(s, Start); |
| 158 | 158 | if ok then _, after, ok := Str.cut(after, Stop); end; |
| 159 | 159 | if not ok then |
| @@ -161,22 +161,22 @@ | ||
| 161 | 161 | Rt.exit(1); |
| 162 | 162 | end; |
| 163 | 163 | mark(0, Last, 1); |
| 164 | - version := eastAsian(load(Files.arg(2), ADR(eaw))); | |
| 165 | - categories(load(Files.arg(3), ADR(ucd))); | |
| 166 | - concatenation(load(Files.arg(4), ADR(props))); | |
| 164 | + version := eastAsian(load(Files.arg(2), eaw)); | |
| 165 | + categories(load(Files.arg(3), ucd)); | |
| 166 | + concatenation(load(Files.arg(4), props)); | |
| 167 | 167 | mark(1160H, 11FFH, 0); (* Hangul's medial and final jamo *) |
| 168 | 168 | mark(0D7B0H, 0D7FFH, 0); |
| 169 | 169 | mark(3164H, 3164H, 0); (* the Hangul fillers, which draw nothing *) |
| 170 | 170 | mark(0FFA0H, 0FFA0H, 0); |
| 171 | - Buf.init(ADR(out), Heap.heap); | |
| 172 | - Buf.str(ADR(out), before); | |
| 173 | - Buf.str(ADR(out), Start); | |
| 174 | - Buf.str(ADR(out), "(* Unicode "); Buf.str(ADR(out), version); Buf.str(ADR(out), " *)\nconst\n"); | |
| 175 | - table(ADR(out), "Zero", 0); | |
| 176 | - table(ADR(out), "Wide", 2); | |
| 177 | - Buf.str(ADR(out), Stop); | |
| 178 | - Buf.str(ADR(out), after); | |
| 179 | - if not Files.write(Files.arg(1), Buf.bytes(ADR(out))) then | |
| 171 | + Buf.init(out, Heap.heap); | |
| 172 | + Buf.str(out, before); | |
| 173 | + Buf.str(out, Start); | |
| 174 | + Buf.str(out, "(* Unicode "); Buf.str(out, version); Buf.str(out, " *)\nconst\n"); | |
| 175 | + table(out, "Zero", 0); | |
| 176 | + table(out, "Wide", 2); | |
| 177 | + Buf.str(out, Stop); | |
| 178 | + Buf.str(out, after); | |
| 179 | + if not Files.write(Files.arg(1), Buf.bytes(out)) then | |
| 180 | 180 | Out.str(Files.arg(1)); Out.str(": can't be written\n"); |
| 181 | 181 | Rt.exit(1); |
| 182 | 182 | end; |