From 3fee2db6752ed1761f1fa9c5ccef785b95187576 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sat, 17 Oct 2026 08:46:26 +0000 Subject: [PATCH] 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. --- diff --git a/doc/raz-lib.md b/doc/raz-lib.md index c0f10a2..0999a82 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -602,8 +602,8 @@ proc hexVal*(c: U8): INT; (* c's value as a hexadecimal digit, or -1 *) proc lower*(s: []U8); (* s's ASCII capitals made small, in place *) proc upper*(s: []U8); (* s's ASCII small letters made capitals, in place *) -proc join*(b: ^Buf.Buf; parts: [][]U8; sep: []U8); (* the parts added to b, sep between each two *) -proc replace*(b: ^Buf.Buf; s, old, new: []U8); (* s added to b, with new for each old in it *) +proc join*(var b: Buf.Buf; parts: [][]U8; sep: []U8); (* the parts added to b, sep between each two *) +proc replace*(var b: Buf.Buf; s, old, new: []U8); (* s added to b, with new for each old in it *) proc encode*(c: I32): ([4]U8, INT); (* c in UTF-8: its bytes, and how many *) proc decode*(s: []U8): (I32, INT); (* the character s starts with, and its length in bytes *) proc decodeLast*(s: []U8): (I32, INT); (* the character s ends with, and its length in bytes *) @@ -620,15 +620,15 @@ module Buf; type Buf* = record len*: INT; ... end; -proc init*(b: ^Buf; a: ^Mem.Allocator); -proc room*(b: ^Buf; n: INT): BOOL; (* room for n more bytes; false if the allocator has none *) -proc add*(b: ^Buf; c: U8); -proc str*(b: ^Buf; s: []U8); -proc spare*(b: ^Buf; n: INT): []U8; (* room for n more bytes, and those bytes, to fill *) -proc bytes*(b: ^Buf): []U8; (* what it holds, until it next grows *) -proc cut*(b: ^Buf; n: INT); (* only the first n bytes kept, its memory kept *) -proc reset*(b: ^Buf); (* emptied, its memory kept *) -proc done*(b: ^Buf); (* its memory given back *) +proc init*(var b: Buf; a: ^Mem.Allocator); +proc room*(var b: Buf; n: INT): BOOL; (* room for n more bytes; false if the allocator has none *) +proc add*(var b: Buf; c: U8); +proc str*(var b: Buf; s: []U8); +proc spare*(var b: Buf; n: INT): []U8; (* room for n more bytes, and those bytes, to fill *) +proc bytes*(b: Buf): []U8; (* what it holds, until it next grows *) +proc cut*(var b: Buf; n: INT); (* only the first n bytes kept, its memory kept *) +proc reset*(var b: Buf); (* emptied, its memory kept *) +proc done*(var b: Buf); (* its memory given back *) module Fmt; type Arg* = variant @@ -642,7 +642,7 @@ end; proc print*(f: []U8; args: []Arg); (* to standard output *) proc eprint*(f: []U8; args: []Arg); (* to standard error, once Out is flushed *) -proc write*(b: ^Buf.Buf; f: []U8; args: []Arg); (* added to b *) +proc write*(var b: Buf.Buf; f: []U8; args: []Arg); (* added to b *) module List(T); type @@ -709,8 +709,8 @@ failed check does, if the allocator has no room; `room` asks first. A program reads into a `Buf` through `spare`, then adds what it read to `len`: `s := Buf.spare(b, 4096); n := Sys.read(fd, s); if n > 0 then -INC(b^.len, n); end;`. `cut` takes back what was added, to a length it -had: `start := b^.len; ...; Buf.cut(b, start);`; a length past `len` +INC(b.len, n); end;`. `cut` takes back what was added, to a length it +had: `start := b.len; ...; Buf.cut(b, start);`; a length past `len` stops the program, as an index past the end does. A `Vec` is `Buf` for any `T`: one block, which doubles when it is full, so `push` costs the same on average however long it grows. `all` gives the elements as a slice, indexed and @@ -821,8 +821,8 @@ ``` module Files; proc arg*(i: INT): []U8; (* argument i; 0 is the program's name *) -proc read*(path: []U8; b: ^Buf.Buf): BOOL; (* the whole file added to b *) -proc load*(path: []U8; b: ^Buf.Buf): INT; (* as read: 0, or minus the errno that stopped it *) +proc read*(path: []U8; var b: Buf.Buf): BOOL; (* the whole file added to b *) +proc load*(path: []U8; var b: Buf.Buf): INT; (* as read: 0, or minus the errno that stopped it *) proc write*(path, data: []U8): BOOL; (* the file made to hold data *) module Opt; diff --git a/examples/Elf.raz b/examples/Elf.raz index 36c3c50..ebac3f7 100644 --- a/examples/Elf.raz +++ b/examples/Elf.raz @@ -44,13 +44,13 @@ proc main*; var b: Buf.Buf; f: []U8; h: Header; i, at, loaded, exec: INT; r: [3]U8; begin - Buf.init(ADR(b), Heap.heap); - defer Buf.done(ADR(b)); - if not Files.read("/proc/self/exe", ADR(b)) then + Buf.init(b, Heap.heap); + defer Buf.done(b); + if not Files.read("/proc/self/exe", b) then Out.str("can't read the program\n"); return; end; - f := Buf.bytes(ADR(b)); + f := Buf.bytes(b); (* the header, copied out of the bytes whole *) h := f[0].(Header); diff --git a/examples/Sh.raz b/examples/Sh.raz index 90f405d..4fcbdef 100644 --- a/examples/Sh.raz +++ b/examples/Sh.raz @@ -47,12 +47,12 @@ proc say(f: []U8; args: []Fmt.Arg); var b: Buf.Buf; _n: INT; begin - Buf.init(ADR(b), Heap.heap); - Fmt.write(ADR(b), "Sh: ", []); - Fmt.write(ADR(b), f, args); - Buf.add(ADR(b), 10); - _n := Sys.write(Sys.stderr, Buf.bytes(ADR(b))); - Buf.done(ADR(b)); + Buf.init(b, Heap.heap); + Fmt.write(b, "Sh: ", []); + Fmt.write(b, f, args); + Buf.add(b, 10); + _n := Sys.write(Sys.stderr, Buf.bytes(b)); + Buf.done(b); end; proc special(c: U8): BOOL; @@ -65,7 +65,7 @@ (* $NAME or $? at s[at], its value added to b; a $ before anything else is itself. *) -proc dollar(b: ^Buf.Buf); +proc dollar(var b: Buf.Buf); var start: INT; begin INC(at); @@ -85,34 +85,34 @@ proc readWord; var b: Buf.Buf; q: U8; begin - Buf.init(ADR(b), ADR(arena.base)); + Buf.init(b, ADR(arena.base)); while (at < LEN(s)) and not special(s[at]) do q := s[at]; match q of | 39: (* '...': every character as it is *) INC(at); - while (at < LEN(s)) and (s[at] # 39) do Buf.add(ADR(b), s[at]); INC(at); end; + while (at < LEN(s)) and (s[at] # 39) do Buf.add(b, s[at]); INC(at); end; INC(at); | '"': (* "...": dollars, and \" and \\ *) INC(at); while (at < LEN(s)) and (s[at] # '"') do - if s[at] = '$' then dollar(ADR(b)); + if s[at] = '$' then dollar(b); else if (s[at] = '\\') and (at + 1 < LEN(s)) and ((s[at + 1] = '"') or (s[at + 1] = '\\')) then INC(at); end; - Buf.add(ADR(b), s[at]); + Buf.add(b, s[at]); INC(at); end; end; INC(at); | '$': - dollar(ADR(b)); + dollar(b); | else: if (q = '\\') and (at + 1 < LEN(s)) then INC(at); end; - Buf.add(ADR(b), s[at]); + Buf.add(b, s[at]); INC(at); end; end; - text := Buf.bytes(ADR(b)); + text := Buf.bytes(b); end; proc next: Token; @@ -185,10 +185,10 @@ if (i = LEN(path)) or (path[i] = ':') then dir := path[start..i]; start := i + 1; - Buf.init(ADR(b), ADR(arena.base)); - Fmt.write(ADR(b), "{}/{}", [Fmt.str(dir), Fmt.str(name)]); - info, err := Sys.info(Buf.bytes(ADR(b))); - if (err = 0) and info.file and (info.mode & 49H # 0) then return Buf.bytes(ADR(b)), true; end; + Buf.init(b, ADR(arena.base)); + Fmt.write(b, "{}/{}", [Fmt.str(dir), Fmt.str(name)]); + info, err := Sys.info(Buf.bytes(b)); + if (err = 0) and info.file and (info.mode & 49H # 0) then return Buf.bytes(b), true; end; end; end; return "", false; @@ -314,7 +314,7 @@ (* A line from standard input, a byte at a time, so that what follows it is left for the programs the line starts; false at its end. *) -proc readLine(b: ^Buf.Buf): BOOL; +proc readLine(var b: Buf.Buf): BOOL; var c: [1]U8; begin Buf.reset(b); @@ -349,10 +349,10 @@ group := Sys.pid(); _ok := Sys.foreground(Sys.stdin, group) = 0; end; - Buf.init(ADR(line), Heap.heap); + Buf.init(line, Heap.heap); if interactive then reap; show("$ ", []); end; - while readLine(ADR(line)) do - run(Buf.bytes(ADR(line))); + while readLine(line) do + run(Buf.bytes(line)); if interactive then reap; show("$ ", []); end; end; if interactive then show("\n", []); end; @@ -372,9 +372,9 @@ if given then run(script); elsif found then - Buf.init(ADR(b), Heap.heap); - if not Files.read(file, ADR(b)) then say("{}: can't read it", [Fmt.str(file)]); return 127; end; - run(Buf.bytes(ADR(b))); + Buf.init(b, Heap.heap); + if not Files.read(file, b) then say("{}: can't read it", [Fmt.str(file)]); return 127; end; + run(Buf.bytes(b)); else interact; end; diff --git a/lib/Buf.raz b/lib/Buf.raz index e226ad8..3f4b49e 100644 --- a/lib/Buf.raz +++ b/lib/Buf.raz @@ -10,20 +10,20 @@ end; (* An empty buffer, which takes its memory from a. *) -proc init*(b: ^Buf; a: ^Mem.Allocator); +proc init*(var b: Buf; a: ^Mem.Allocator); begin - b^ := {a: a}; + b := {a: a}; end; (* Room for n more bytes; false if the allocator has none. *) -proc room*(b: ^Buf; n: INT): BOOL; +proc room*(var b: Buf; n: INT): BOOL; var cap: INT; p: ^U8; begin - if b^.len + n <= b^.cap then return true; end; - cap := b^.cap * 2; + if b.len + n <= b.cap then return true; end; + cap := b.cap * 2; if cap < 64 then cap := 64; end; - while cap < b^.len + n do cap := cap * 2; end; - if b^.a = nil then + while cap < b.len + n do cap := cap * 2; end; + if b.a = nil then (* a zero Buf has no allocator: the core has no heap to assume, so init must give it one. Said plainly, with the calls under way, rather than as a nil pointer in Mem.alloc *) @@ -31,19 +31,19 @@ Rt.trace; Rt.exit(2); end; - p := Mem.alloc(b^.a, cap, 1); + p := Mem.alloc(b.a, cap, 1); if p = nil then return false; end; - if b^.len > 0 then COPY(p[0..b^.len], b^.data[0..b^.len]); end; - if b^.data # nil then Mem.free(b^.a, b^.data); end; - b^.data := p; - b^.cap := cap; + if b.len > 0 then COPY(p[0..b.len], b.data[0..b.len]); end; + if b.data # nil then Mem.free(b.a, b.data); end; + b.data := p; + b.cap := cap; return true; end; (* Room for n more bytes, or the program stops: a buffer that went on short of what was added to it would be wrong data, and nothing would say so. A program that can do better asks room first. *) -proc need(b: ^Buf; n: INT); +proc need(var b: Buf; n: INT); begin if not room(b, n) then _ := Sys.write(Sys.stderr, "Buf: out of memory\n"); @@ -53,64 +53,64 @@ end; (* Adds the byte c at the end. *) -proc add*(b: ^Buf; c: U8); +proc add*(var b: Buf; c: U8); begin need(b, 1); - b^.data[b^.len] := c; - INC(b^.len); + b.data[b.len] := c; + INC(b.len); end; (* Adds the bytes of s at the end. *) -proc str*(b: ^Buf; s: []U8); +proc str*(var b: Buf; s: []U8); begin need(b, LEN(s)); - COPY(b^.data[b^.len..b^.len + LEN(s)], s); - INC(b^.len, LEN(s)); + COPY(b.data[b.len..b.len + LEN(s)], s); + INC(b.len, LEN(s)); end; (* Room for n more bytes, and those bytes, for a read to fill: the program then adds to len what it filled. Good until the buffer next grows, as bytes is. *) -proc spare*(b: ^Buf; n: INT): []U8; +proc spare*(var b: Buf; n: INT): []U8; begin if n = 0 then return ""; end; (* a new buffer has no data to slice *) need(b, n); - return b^.data[b^.len..b^.len + n]; + return b.data[b.len..b.len + n]; end; (* What the buffer holds, until it next grows. *) -proc bytes*(b: ^Buf): []U8; +proc bytes*(b: Buf): []U8; begin - if b^.len = 0 then return ""; end; - return b^.data[0..b^.len]; + if b.len = 0 then return ""; end; + return b.data[0..b.len]; end; (* Keeps only the first n bytes, and the memory, for what comes next. An n past what the buffer holds is the program's mistake, and stops it, as an index past the end does. *) -proc cut*(b: ^Buf; n: INT); +proc cut*(var b: Buf; n: INT); begin - if (n < 0) or (n > b^.len) then + if (n < 0) or (n > b.len) then _ := Sys.write(Sys.stderr, "Buf.cut past the end\n"); Rt.trace; Rt.exit(2); end; - b^.len := n; + b.len := n; end; (* Empties the buffer, keeping its memory for what comes next. *) -proc reset*(b: ^Buf); +proc reset*(var b: Buf); begin cut(b, 0); end; (* Gives the buffer's memory back, leaving it empty. *) -proc done*(b: ^Buf); +proc done*(var b: Buf); begin - if b^.data # nil then Mem.free(b^.a, b^.data); end; - b^.data := nil; - b^.len := 0; - b^.cap := 0; + if b.data # nil then Mem.free(b.a, b.data); end; + b.data := nil; + b.len := 0; + b.cap := 0; end; (* The module's test, over an arena. *) @@ -118,26 +118,26 @@ var ar: Arena.Arena; b: Buf; i: INT; s: []U8; begin if not Arena.init(ADR(ar), 1 shl 20) then Rt.stop("Buf: no arena"); end; - init(ADR(b), ADR(ar.base)); - if LEN(bytes(ADR(b))) # 0 then Rt.stop("Buf: not empty"); end; - for i := 0 to 9999 do add(ADR(b), U8(i mod 251)); end; - str(ADR(b), "end"); - s := bytes(ADR(b)); + init(b, ADR(ar.base)); + if LEN(bytes(b)) # 0 then Rt.stop("Buf: not empty"); end; + for i := 0 to 9999 do add(b, U8(i mod 251)); end; + str(b, "end"); + s := bytes(b); if (LEN(s) # 10003) or (s[9998] # U8(9998 mod 251)) or (s[10000..10003] # "end") then Rt.stop("Buf: contents"); end; - reset(ADR(b)); - str(ADR(b), "again"); - if bytes(ADR(b)) # "again" then Rt.stop("Buf.reset"); end; - cut(ADR(b), 2); - str(ADR(b), "ain"); - cut(ADR(b), 5); - if bytes(ADR(b)) # "again" then Rt.stop("Buf.cut"); end; - s := spare(ADR(b), 3); + reset(b); + str(b, "again"); + if bytes(b) # "again" then Rt.stop("Buf.reset"); end; + cut(b, 2); + str(b, "ain"); + cut(b, 5); + if bytes(b) # "again" then Rt.stop("Buf.cut"); end; + s := spare(b, 3); COPY(s, "abc"); INC(b.len, 2); - if (LEN(s) # 3) or (bytes(ADR(b)) # "againab") then Rt.stop("Buf.spare"); end; - done(ADR(b)); + if (LEN(s) # 3) or (bytes(b) # "againab") then Rt.stop("Buf.spare"); end; + done(b); Arena.done(ADR(ar)); end; diff --git a/lib/Files.raz b/lib/Files.raz index 9673a53..7ad89d6 100644 --- a/lib/Files.raz +++ b/lib/Files.raz @@ -17,21 +17,21 @@ the read that failed, as Sys gives them: -2 (ENOENT) for no such file, -13 (EACCES) for one not allowed, -21 (EISDIR) for a directory. What was read before a read failed stays in b. *) -proc load*(path: []U8; b: ^Buf.Buf): INT; +proc load*(path: []U8; var b: Buf.Buf): INT; var fd, n: INT; begin fd := Sys.openRead(path); if fd < 0 then return fd; end; repeat n := Sys.read(fd, Buf.spare(b, 65536)); (* straight into b *) - if n > 0 then INC(b^.len, n); end; + if n > 0 then INC(b.len, n); end; until (n <= 0) and (n # Sys.errIntr); (* a signal Sys.handle took; read again *) Sys.close(fd); return n; end; (* Adds the file at path to b; false if it can't be read. *) -proc read*(path: []U8; b: ^Buf.Buf): BOOL; +proc read*(path: []U8; var b: Buf.Buf): BOOL; begin return load(path, b) = 0; end; @@ -57,15 +57,15 @@ proc main*; var b: Buf.Buf; begin - Buf.init(ADR(b), Heap.heap); + Buf.init(b, Heap.heap); if not write("/tmp/raz-files-test", "abc") then Rt.stop("Files.write"); end; - if (load("/tmp/raz-files-test", ADR(b)) # 0) or (Buf.bytes(ADR(b)) # "abc") then Rt.stop("Files.load"); end; - if not read("/tmp/raz-files-test", ADR(b)) or (Buf.bytes(ADR(b)) # "abcabc") then Rt.stop("Files.read"); end; + if (load("/tmp/raz-files-test", b) # 0) or (Buf.bytes(b) # "abc") then Rt.stop("Files.load"); end; + if not read("/tmp/raz-files-test", b) or (Buf.bytes(b) # "abcabc") then Rt.stop("Files.read"); end; _ := Sys.remove("/tmp/raz-files-test"); - if load("/tmp/raz-files-test", ADR(b)) # Sys.errNoEntry then Rt.stop("Files.load: no such file"); end; - if load("/", ADR(b)) # -21 then Rt.stop("Files.load: a directory"); end; - if read("/", ADR(b)) or (b.len # 6) then Rt.stop("Files.read: a directory"); end; - Buf.done(ADR(b)); + if load("/tmp/raz-files-test", b) # Sys.errNoEntry then Rt.stop("Files.load: no such file"); end; + if load("/", b) # -21 then Rt.stop("Files.load: a directory"); end; + if read("/", b) or (b.len # 6) then Rt.stop("Files.read: a directory"); end; + Buf.done(b); end; end Files. diff --git a/lib/Fmt.raz b/lib/Fmt.raz index da804a0..ccae034 100644 --- a/lib/Fmt.raz +++ b/lib/Fmt.raz @@ -37,7 +37,7 @@ (* Where the pieces go: to b, or to standard output if b is nil. *) proc put(b: ^Buf.Buf; s: []U8); begin - if b = nil then Out.str(s); else Buf.str(b, s); end; + if b = nil then Out.str(s); else Buf.str(b^, s); end; end; (* x as Arg's real says. The whole part and the fraction are taken @@ -135,9 +135,9 @@ begin Out.flush; if not Arena.init(ADR(ar), 1 shl 20) then Rt.stop("Fmt.eprint: no arena"); end; - Buf.init(ADR(b), ADR(ar.base)); + Buf.init(b, ADR(ar.base)); format(ADR(b), f, args); - s := Buf.bytes(ADR(b)); + s := Buf.bytes(b); while LEN(s) > 0 do n := Sys.write(Sys.stderr, s); if n > 0 then s := s[n..LEN(s)]; @@ -148,9 +148,9 @@ end; (* Appends f to b, each {} replaced by the next of args. *) -proc write*(b: ^Buf.Buf; f: []U8; args: []Arg); +proc write*(var b: Buf.Buf; f: []U8; args: []Arg); begin - format(b, f, args); + format(ADR(b), f, args); (* format's nil is standard output *) end; (* The module's test: formats into a buffer over an arena. *) @@ -158,21 +158,21 @@ var ar: Arena.Arena; b: Buf.Buf; zero: F64; begin if not Arena.init(ADR(ar), 1 shl 16) then Rt.stop("Fmt: no arena"); end; - Buf.init(ADR(b), ADR(ar.base)); - write(ADR(b), "{} {} {} {}|{}{}{} {{}}", [int(-42), uint(0FFFFFFFFFFFFFFFFH), hex(48879), + Buf.init(b, ADR(ar.base)); + write(b, "{} {} {} {}|{}{}{} {{}}", [int(-42), uint(0FFFFFFFFFFFFFFFFH), hex(48879), str("s"), char('c'), bool(true), int(0)]); - if Buf.bytes(ADR(b)) # "-42 18446744073709551615 BEEF s|ctrue0 {}" then Rt.stop("Fmt.write"); end; - Buf.reset(ADR(b)); - write(ADR(b), "{} and {}", [int(1)]); - if Buf.bytes(ADR(b)) # "1 and " then Rt.stop("Fmt: too few arguments"); end; - Buf.reset(ADR(b)); - write(ADR(b), "{} {} {} {} {} {} {} {}", [real(3.14159, 2), real(-2.5, 0), real(0.999, 2), real(0.05, 1), + if Buf.bytes(b) # "-42 18446744073709551615 BEEF s|ctrue0 {}" then Rt.stop("Fmt.write"); end; + Buf.reset(b); + write(b, "{} and {}", [int(1)]); + if Buf.bytes(b) # "1 and " then Rt.stop("Fmt: too few arguments"); end; + Buf.reset(b); + write(b, "{} {} {} {} {} {} {} {}", [real(3.14159, 2), real(-2.5, 0), real(0.999, 2), real(0.05, 1), real(1.0, 12), real(-0.001, 2), real(123.0, 0), real(999999999999999.9, 0)]); - if Buf.bytes(ADR(b)) # "3.14 -3 1.00 0.1 1.000000000 -0.00 123 1000000000000000" then Rt.stop("Fmt.real"); end; - Buf.reset(ADR(b)); - write(ADR(b), "{} {} {} {} {} {}", [real(1.5E17, 2), real(-9.999E20, 2), real(1.0E15, 0), real(1.7976931348623157E308, 3), + if Buf.bytes(b) # "3.14 -3 1.00 0.1 1.000000000 -0.00 123 1000000000000000" then Rt.stop("Fmt.real"); end; + Buf.reset(b); + write(b, "{} {} {} {} {} {}", [real(1.5E17, 2), real(-9.999E20, 2), real(1.0E15, 0), real(1.7976931348623157E308, 3), real(1.0 / zero, 2), real(zero / zero, 2)]); - if Buf.bytes(ADR(b)) # "1.50e17 -1.00e21 1e15 1.798e308 inf nan" then Rt.stop("Fmt.real: an exponent"); end; + if Buf.bytes(b) # "1.50e17 -1.00e21 1e15 1.798e308 inf nan" then Rt.stop("Fmt.real: an exponent"); end; Arena.done(ADR(ar)); end; diff --git a/lib/Opt.raz b/lib/Opt.raz index f805463..29e686b 100644 --- a/lib/Opt.raz +++ b/lib/Opt.raz @@ -38,7 +38,7 @@ const largest = 9223372036854775807; (* the largest I64, a number with no top *) (* b written whole to fd, then freed. *) -proc say(fd: INT; b: ^Buf.Buf); +proc say(fd: INT; var b: Buf.Buf); var s: []U8; n: INT; begin s := Buf.bytes(b); @@ -62,7 +62,7 @@ (* The scope's synopsis lines, as the help and the mistakes begin: "usage: " and the first, the others under it; or, for a scope with none, "usage: keep start". *) -proc usageOf(p: ^Parser; b: ^Buf.Buf); +proc usageOf(p: ^Parser; var b: Buf.Buf); var i, lines: INT; c: U8; begin for i := 0 to LEN(p^.table) - 1 do @@ -93,12 +93,12 @@ proc mistake(p: ^Parser; f: []U8; args: []Fmt.Arg); var b: Buf.Buf; begin - Buf.init(ADR(b), Heap.heap); - Fmt.write(ADR(b), "{}: ", [Fmt.str(p^.name)]); - Fmt.write(ADR(b), f, args); - Buf.add(ADR(b), 10); - usageOf(p, ADR(b)); - say(Sys.stderr, ADR(b)); + Buf.init(b, Heap.heap); + Fmt.write(b, "{}: ", [Fmt.str(p^.name)]); + Fmt.write(b, f, args); + Buf.add(b, 10); + usageOf(p, b); + say(Sys.stderr, b); Rt.exit(2); end; @@ -119,7 +119,7 @@ (* The left column of a row in the help, as "-b, --bar label", or, for a value that may be left out, "-c, --color[=when]", as grep's help has it. *) -proc left(b: ^Buf.Buf; r: ^Row); +proc left(var b: Buf.Buf; r: ^Row); begin match r^ of | sub(_s, name, _help): Buf.str(b, name); @@ -140,16 +140,16 @@ proc leftWidth(r: ^Row): INT; var b: Buf.Buf; n: INT; begin - Buf.init(ADR(b), Heap.heap); - left(ADR(b), r); - n := LEN(Buf.bytes(ADR(b))); - Buf.done(ADR(b)); + Buf.init(b, Heap.heap); + left(b, r); + n := LEN(Buf.bytes(b)); + Buf.done(b); return n; end; (* A line of the list: the left column, padded to width and three spaces more, and the line of help. *) -proc item(b: ^Buf.Buf; r: ^Row; width: INT); +proc item(var b: Buf.Buf; r: ^Row; width: INT); var i: INT; begin Buf.str(b, " "); @@ -198,9 +198,9 @@ h := 'h'; if ownsH(p) then h := 0; end; own := option(p^.scope, h, "help", "", "print this and exit"); - Buf.init(ADR(b), Heap.heap); - usageOf(p, ADR(b)); - Buf.add(ADR(b), 10); + Buf.init(b, Heap.heap); + usageOf(p, b); + Buf.add(b, 10); width := leftWidth(ADR(own)); for i := 0 to LEN(p^.table) - 1 do r := ADR(p^.table[i]); @@ -209,19 +209,19 @@ for i := 0 to LEN(p^.table) - 1 do r := ADR(p^.table[i]); match r^ of - | sub(s, _name, _help): if s = p^.scope then item(ADR(b), r, width); end; + | sub(s, _name, _help): if s = p^.scope then item(b, r, width); end; | else: end; end; for i := 0 to LEN(p^.table) - 1 do r := ADR(p^.table[i]); match r^ of - | option(s, _l, _long, _v, _help): if s = p^.scope then item(ADR(b), r, width); end; + | option(s, _l, _long, _v, _help): if s = p^.scope then item(b, r, width); end; | else: end; end; - item(ADR(b), ADR(own), width); - say(Sys.stdout, ADR(b)); + item(b, ADR(own), width); + say(Sys.stdout, b); Rt.exit(0); end; @@ -315,9 +315,9 @@ proc badTable(p: ^Parser; row: INT; why: []U8); var b: Buf.Buf; begin - Buf.init(ADR(b), Heap.heap); - Fmt.write(ADR(b), "{}: option table, row {}: {}\n", [Fmt.str(p^.name), Fmt.int(row), Fmt.str(why)]); - say(Sys.stderr, ADR(b)); + Buf.init(b, Heap.heap); + Fmt.write(b, "{}: option table, row {}: {}\n", [Fmt.str(p^.name), Fmt.int(row), Fmt.str(why)]); + say(Sys.stderr, b); Rt.exit(1); end; @@ -431,9 +431,9 @@ proc fail*(p: ^Parser; text: []U8); var b: Buf.Buf; begin - Buf.init(ADR(b), Heap.heap); - Fmt.write(ADR(b), "{}: {}\n", [Fmt.str(p^.name), Fmt.str(text)]); - say(Sys.stderr, ADR(b)); + Buf.init(b, Heap.heap); + Fmt.write(b, "{}: {}\n", [Fmt.str(p^.name), Fmt.str(text)]); + say(Sys.stderr, b); Rt.exit(2); end; @@ -463,9 +463,9 @@ w, ok := operand(ADR(p)); if not ok then fail(ADR(p), "no command; see keep -h"); end; if w # "start" then - Buf.init(ADR(b), Heap.heap); - Fmt.write(ADR(b), "no command {}; see keep -h", [Fmt.str(w)]); - fail(ADR(p), Buf.bytes(ADR(b))); + Buf.init(b, Heap.heap); + Fmt.write(b, "no command {}; see keep -h", [Fmt.str(w)]); + fail(ADR(p), Buf.bytes(b)); end; scope(ADR(p), "start"); while next(ADR(p)) do diff --git a/lib/Str.raz b/lib/Str.raz index 4896ce6..c2559c4 100644 --- a/lib/Str.raz +++ b/lib/Str.raz @@ -219,7 +219,7 @@ end; (* The parts added to b, with sep between each two. *) -proc join*(b: ^Buf.Buf; parts: [][]U8; sep: []U8); +proc join*(var b: Buf.Buf; parts: [][]U8; sep: []U8); var i: INT; begin for i := 0 to LEN(parts) - 1 do @@ -230,7 +230,7 @@ (* s added to b, with new for each old in it, from the left, none overlapping another; s as it is if old is empty. *) -proc replace*(b: ^Buf.Buf; s, old, new: []U8); +proc replace*(var b: Buf.Buf; s, old, new: []U8); var i: INT; begin i := -1; @@ -547,18 +547,18 @@ if up # "AB-Z9" then Rt.stop("Str.upper"); end; lower(up); if up # "ab-z9" then Rt.stop("Str.lower"); end; - Buf.init(ADR(b), Heap.heap); - join(ADR(b), ["a", "b", "c"], ", "); - if Buf.bytes(ADR(b)) # "a, b, c" then Rt.stop("Str.join"); end; - Buf.reset(ADR(b)); - join(ADR(b), [], "-"); - replace(ADR(b), "a.b..c", ".", "::"); - if Buf.bytes(ADR(b)) # "a::b::::c" then Rt.stop("Str.replace"); end; - Buf.reset(ADR(b)); - replace(ADR(b), "aaa", "aa", "b"); - replace(ADR(b), "xy", "", "-"); - if Buf.bytes(ADR(b)) # "baxy" then Rt.stop("Str.replace: overlapping, empty"); end; - Buf.done(ADR(b)); + Buf.init(b, Heap.heap); + join(b, ["a", "b", "c"], ", "); + if Buf.bytes(b) # "a, b, c" then Rt.stop("Str.join"); end; + Buf.reset(b); + join(b, [], "-"); + replace(b, "a.b..c", ".", "::"); + if Buf.bytes(b) # "a::b::::c" then Rt.stop("Str.replace"); end; + Buf.reset(b); + replace(b, "aaa", "aa", "b"); + replace(b, "xy", "", "-"); + if Buf.bytes(b) # "baxy" then Rt.stop("Str.replace: overlapping, empty"); end; + Buf.done(b); end; proc testChars; diff --git a/site/content/advanced/01-fmt.txt b/site/content/advanced/01-fmt.txt index bb87309..cd55d49 100644 --- a/site/content/advanced/01-fmt.txt +++ b/site/content/advanced/01-fmt.txt @@ -67,7 +67,7 @@ import Out, Buf, Fmt, Heap; (* "name (age n)", built in a Buf the caller owns *) -proc describe(b: ^Buf.Buf; name: []U8; age: INT); +proc describe(var b: Buf.Buf; name: []U8; age: INT); begin Fmt.write(b, "{} (age {})", [Fmt.str(name), Fmt.int(age)]); end; @@ -75,12 +75,12 @@ proc main*; var b: Buf.Buf; begin - Buf.init(ADR(b), Heap.heap); - defer Buf.done(ADR(b)); - describe(ADR(b), "Ada", 36); - Buf.str(ADR(b), "; "); - describe(ADR(b), "Grace", 45); - Out.str(Buf.bytes(ADR(b))); Out.ln; + Buf.init(b, Heap.heap); + defer Buf.done(b); + describe(b, "Ada", 36); + Buf.str(b, "; "); + describe(b, "Grace", 45); + Out.str(Buf.bytes(b)); Out.ln; Out.int(b.len); Out.str(" bytes\n"); end; diff --git a/site/content/advanced/04-files.txt b/site/content/advanced/04-files.txt index 5d3fb1f..3ec4b62 100644 --- a/site/content/advanced/04-files.txt +++ b/site/content/advanced/04-files.txt @@ -17,15 +17,15 @@ begin ok := Files.write("notes.txt", "first line\nsecond line\n"); if not ok then Out.str("can't write\n"); return; end; - Buf.init(ADR(b), Heap.heap); - defer Buf.done(ADR(b)); - ok := Files.read("notes.txt", ADR(b)); + Buf.init(b, Heap.heap); + defer Buf.done(b); + ok := Files.read("notes.txt", b); if ok then - Out.int(b.len); Out.str(" bytes: "); Out.str(Buf.bytes(ADR(b))); + Out.int(b.len); Out.str(" bytes: "); Out.str(Buf.bytes(b)); end; - ok := Files.read("missing.txt", ADR(b)); + ok := Files.read("missing.txt", b); if not ok then Out.str("missing.txt: not there\n"); end; - if Files.load("missing.txt", ADR(b)) = -2 then Out.str("missing.txt: ENOENT\n"); end; + if Files.load("missing.txt", b) = -2 then Out.str("missing.txt: ENOENT\n"); end; end; end Notes. @@ -119,12 +119,12 @@ end; Sys.closeDir(ADR(d)); Order.sort(names[0..n]); - Buf.init(ADR(path), Heap.heap); + Buf.init(path, Heap.heap); for i := 0 to n - 1 do - Buf.reset(ADR(path)); - Buf.str(ADR(path), "proj/"); - Buf.str(ADR(path), names[i]); - info, err := Sys.info(Buf.bytes(ADR(path))); + Buf.reset(path); + Buf.str(path, "proj/"); + Buf.str(path, names[i]); + info, err := Sys.info(Buf.bytes(path)); Out.str(names[i]); if err < 0 then Out.str(" ?"); elsif info.dir then Out.str("/"); @@ -133,7 +133,7 @@ Out.ln; Mem.free(Heap.heap, ^U8(ADR(names[i][0]))); end; - Buf.done(ADR(path)); + Buf.done(path); end; end Tree. diff --git a/site/content/advanced/05-processes.txt b/site/content/advanced/05-processes.txt index c53d58b..b120990 100644 --- a/site/content/advanced/05-processes.txt +++ b/site/content/advanced/05-processes.txt @@ -109,16 +109,16 @@ p.fds[1] := wr; pid := Sys.spawn(p); Sys.close(wr); (* only the child writes now *) - Buf.init(ADR(b), Heap.heap); - defer Buf.done(ADR(b)); + Buf.init(b, Heap.heap); + defer Buf.done(b); n := Sys.read(rd, chunk); while n > 0 do - Buf.str(ADR(b), chunk[0..n]); + Buf.str(b, chunk[0..n]); n := Sys.read(rd, chunk); end; Sys.close(rd); _, how, status := Sys.wait(pid, true); - Out.str("captured: "); Out.str(Buf.bytes(ADR(b))); + Out.str("captured: "); Out.str(Buf.bytes(b)); Out.str("status "); Out.int(status); Out.ln; if how = Sys.exited then Out.str("(a normal exit)\n"); end; end; diff --git a/site/content/advanced/06-allocators.txt b/site/content/advanced/06-allocators.txt index 0c34653..d310af1 100644 --- a/site/content/advanced/06-allocators.txt +++ b/site/content/advanced/06-allocators.txt @@ -147,11 +147,11 @@ var c: Counted.Counted; b: Buf.Buf; l: Names.List; i: INT; p: ^U8; begin Counted.init(ADR(c), Heap.heap); - Buf.init(ADR(b), ADR(c.base)); - for i := 1 to 200 do Buf.str(ADR(b), "0123456789"); end; + Buf.init(b, ADR(c.base)); + for i := 1 to 200 do Buf.str(b, "0123456789"); end; Fmt.print("a buffer of {} bytes took {} allocations\n", [Fmt.int(b.len), Fmt.int(c.allocs)]); - Buf.done(ADR(b)); + Buf.done(b); Names.init(ADR(l), ADR(c.base)); for i := 1 to 10 do _ := Names.push(ADR(l), "x"); end; diff --git a/site/content/tour/09-memory.txt b/site/content/tour/09-memory.txt index 8d93cab..528e5ba 100644 --- a/site/content/tour/09-memory.txt +++ b/site/content/tour/09-memory.txt @@ -100,21 +100,21 @@ proc main*; var b: Buf.Buf; i: INT; begin - Buf.init(ADR(b), Heap.heap); - defer Buf.done(ADR(b)); + Buf.init(b, Heap.heap); + defer Buf.done(b); for i := 1 to 5 do - Buf.str(ADR(b), "item "); - Buf.add(ADR(b), U8('0' + i)); - Buf.add(ADR(b), ','); + Buf.str(b, "item "); + Buf.add(b, U8('0' + i)); + Buf.add(b, ','); end; - Out.str(Buf.bytes(ADR(b))); Out.ln; + Out.str(Buf.bytes(b)); Out.ln; Out.int(b.len); Out.str(" bytes\n"); - Buf.reset(ADR(b)); - Str.join(ADR(b), ["red", "green", "blue"], ", "); - Out.str(Buf.bytes(ADR(b))); Out.ln; - Buf.reset(ADR(b)); - Str.replace(ADR(b), "a-b-c", "-", " and "); - Out.str(Buf.bytes(ADR(b))); Out.ln; + Buf.reset(b); + Str.join(b, ["red", "green", "blue"], ", "); + Out.str(Buf.bytes(b)); Out.ln; + Buf.reset(b); + Str.replace(b, "a-b-c", "-", " and "); + Out.str(Buf.bytes(b)); Out.ln; end; end Build. diff --git a/site/content/tour/11-defer.txt b/site/content/tour/11-defer.txt index 0d33a09..274ebb9 100644 --- a/site/content/tour/11-defer.txt +++ b/site/content/tour/11-defer.txt @@ -79,16 +79,16 @@ var b: Buf.Buf; begin if n < 0 then return false; end; - Buf.init(ADR(b), Heap.heap); - defer Buf.done(ADR(b)); (* freed on every way out below *) - Buf.str(ADR(b), "n is "); + Buf.init(b, Heap.heap); + defer Buf.done(b); (* freed on every way out below *) + Buf.str(b, "n is "); if n > 9 then - Buf.str(ADR(b), "large"); - Out.str(Buf.bytes(ADR(b))); Out.ln; + Buf.str(b, "large"); + Out.str(Buf.bytes(b)); Out.ln; return true; end; - Buf.str(ADR(b), "small"); - Out.str(Buf.bytes(ADR(b))); Out.ln; + Buf.str(b, "small"); + Out.str(Buf.bytes(b)); Out.ln; return true; end; diff --git a/src/Diag.raz b/src/Diag.raz index fc1ab64..e5204ed 100644 --- a/src/Diag.raz +++ b/src/Diag.raz @@ -71,12 +71,12 @@ if not cwdKnown then d, err := Sys.workDir(); if err = 0 then - Buf.str(ADR(cwd), d); - Buf.add(ADR(cwd), '/'); + Buf.str(cwd, d); + Buf.add(cwd, '/'); end; cwdKnown := true; end; - c := Buf.bytes(ADR(cwd)); + c := Buf.bytes(cwd); if (LEN(c) > 0) and Str.hasPrefix(path, c) then return path[LEN(c)..LEN(path)]; end; return path; end; @@ -127,17 +127,17 @@ proc put(s: []U8); begin - Buf.str(ADR(out), s); + Buf.str(out, s); end; proc putc(c: U8); begin - Buf.add(ADR(out), c); + Buf.add(out, c); end; proc putNum(n: INT); begin - Fmt.write(ADR(out), "{}", [Fmt.int(n)]); + Fmt.write(out, "{}", [Fmt.int(n)]); end; (* The text of a number *) @@ -145,9 +145,9 @@ var b: ^Buf.Buf; begin b := ^Buf.Buf(grab(SIZE(Buf.Buf))); - Buf.init(b, Heap.heap); - Fmt.write(b, f, args); - return Buf.bytes(b); + Buf.init(b^, Heap.heap); + Fmt.write(b^, f, args); + return Buf.bytes(b^); end; (* c, when colour is on *) @@ -172,13 +172,13 @@ proc flush; var s: []U8; n: INT; begin - s := Buf.bytes(ADR(out)); + s := Buf.bytes(out); while LEN(s) > 0 do n := Sys.write(Sys.stderr, s); if n <= 0 then break; end; s := s[n..LEN(s)]; end; - Buf.reset(ADR(out)); + Buf.reset(out); end; (* Positions *) @@ -405,6 +405,6 @@ end; begin - Buf.init(ADR(cwd), Heap.heap); - Buf.init(ADR(out), Heap.heap); + Buf.init(cwd, Heap.heap); + Buf.init(out, Heap.heap); end Diag. diff --git a/src/Parse.raz b/src/Parse.raz index b8acd00..4283010 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -3603,10 +3603,10 @@ proc moduleParams(m: PModule): ERROR; var i: INT; o, a: PObj; sig: PType; s: Buf.Buf; at: Diag.Pos; text: []U8; begin - Buf.init(ADR(s), Heap.heap); + Buf.init(s, Heap.heap); i := 0; - Buf.str(ADR(s), nameOf(m^.name)); - Buf.add(ADR(s), '('); + Buf.str(s, nameOf(m^.name)); + Buf.add(s, '('); Scan.next; repeat if Scan.tok # Scan.sIdent then return expected("a name"); end; @@ -3619,8 +3619,8 @@ o := Sym.newObj(Sym.oType, Scan.id, m^.args.t[i]); o^.used := true; (* a parameter of the module, not a declaration *) try declare(o); - Buf.str(ADR(s), Sym.typeName(m^.args.t[i])); - Buf.add(ADR(s), ','); + Buf.str(s, Sym.typeName(m^.args.t[i])); + Buf.add(s, ','); INC(i); Scan.next; if Scan.tok # Scan.sComma then break; end; @@ -3650,20 +3650,20 @@ o^.typ := a^.typ; o^.chunk := a^.chunk; o^.isCode := a^.isCode; o^.ext := a^.ext; o^.used := true; try declare(o); - Buf.str(ADR(s), nameOf(a^.home^.name)); - Buf.add(ADR(s), '.'); - Buf.str(ADR(s), nameOf(a^.name)); - Buf.add(ADR(s), ','); + Buf.str(s, nameOf(a^.home^.name)); + Buf.add(s, '.'); + Buf.str(s, nameOf(a^.name)); + Buf.add(s, ','); INC(i); end; try expect(Scan.sRparen); if i # m^.args.n then return Scan.markAt(m^.args.at, say1("too many type arguments for {}", nameOf(m^.name))); end; - text := Buf.bytes(ADR(s)); + text := Buf.bytes(s); text[LEN(text) - 1] := ')'; m^.sname := internName(text); - Buf.done(ADR(s)); + Buf.done(s); return nil; end; diff --git a/src/Sym.raz b/src/Sym.raz index 810c771..7b45572 100644 --- a/src/Sym.raz +++ b/src/Sym.raz @@ -510,7 +510,7 @@ (* What a type is called, added to b: the names a program gives it, else what it is made of *) -proc typeText(b: ^Buf.Buf; t: ^Type); +proc typeText(var b: Buf.Buf; t: ^Type); var f: ^Obj; begin if t = nil then Buf.str(b, "nothing"); return; end; @@ -574,9 +574,9 @@ var b: ^Buf.Buf; begin b := ^Buf.Buf(Scan.grab(SIZE(Buf.Buf))); - Buf.init(b, Heap.heap); - typeText(b, t); - return Buf.bytes(b); + Buf.init(b^, Heap.heap); + typeText(b^, t); + return Buf.bytes(b^); end; (* A fingerprint of what a declaration exports, for the interface diff --git a/src/X64.raz b/src/X64.raz index e3a744b..8f3727e 100644 --- a/src/X64.raz +++ b/src/X64.raz @@ -293,22 +293,22 @@ var b: ^Buf.Buf; begin b := ^Buf.Buf(Scan.grab(SIZE(Buf.Buf))); - Buf.init(b, Heap.heap); - if size = 1 then Buf.str(b, "byte "); - elsif size = 2 then Buf.str(b, "word "); - elsif size = 4 then Buf.str(b, "dword "); - elsif size = 8 then Buf.str(b, "qword "); + Buf.init(b^, Heap.heap); + if size = 1 then Buf.str(b^, "byte "); + elsif size = 2 then Buf.str(b^, "word "); + elsif size = 4 then Buf.str(b^, "dword "); + elsif size = 8 then Buf.str(b^, "qword "); end; - Buf.str(b, "["); - if m.base = RIP then Buf.str(b, Chunk.chunkName(m.chunk)); else Buf.str(b, rn(m.base)); end; - if m.index # NoIndex then Fmt.write(b, " + {} * {}", [Fmt.str(rn(m.index)), Fmt.int(m.scale)]); end; + Buf.str(b^, "["); + if m.base = RIP then Buf.str(b^, Chunk.chunkName(m.chunk)); else Buf.str(b^, rn(m.base)); end; + if m.index # NoIndex then Fmt.write(b^, " + {} * {}", [Fmt.str(rn(m.index)), Fmt.int(m.scale)]); end; if m.disp > 0 then - Fmt.write(b, " + {}", [Fmt.int(m.disp)]); + Fmt.write(b^, " + {}", [Fmt.int(m.disp)]); elsif m.disp < 0 then - Fmt.write(b, " - {}", [Fmt.int(-m.disp)]); + Fmt.write(b^, " - {}", [Fmt.int(-m.disp)]); end; - Buf.str(b, "]"); - return Buf.bytes(b); + Buf.str(b^, "]"); + return Buf.bytes(b^); end; proc mn(m: Mem): []U8; diff --git a/test/doc/Str.expect b/test/doc/Str.expect index dffd532..d7562f6 100644 --- a/test/doc/Str.expect +++ b/test/doc/Str.expect @@ -84,10 +84,10 @@ proc upper*(s: []U8); s's small ASCII letters made capitals, in place. -proc join*(b: ^Buf.Buf; parts: [][]U8; sep: []U8); +proc join*(var b: Buf.Buf; parts: [][]U8; sep: []U8); The parts added to b, with sep between each two. -proc replace*(b: ^Buf.Buf; s, old, new: []U8); +proc replace*(var b: Buf.Buf; s, old, new: []U8); s added to b, with new for each old in it, from the left, none overlapping another; s as it is if old is empty. diff --git a/test/run/BufCut.raz b/test/run/BufCut.raz index f42c7e3..e2b7480 100644 --- a/test/run/BufCut.raz +++ b/test/run/BufCut.raz @@ -6,9 +6,9 @@ proc main*; var b: Buf.Buf; begin - Buf.init(ADR(b), Heap.heap); - Buf.str(ADR(b), "hello"); - Buf.cut(ADR(b), 6); + Buf.init(b, Heap.heap); + Buf.str(b, "hello"); + Buf.cut(b, 6); end; end BufCut. diff --git a/test/run/BufCut.stderr b/test/run/BufCut.stderr index 756983b..eb32da3 100644 --- a/test/run/BufCut.stderr +++ b/test/run/BufCut.stderr @@ -1,3 +1,3 @@ Buf.cut past the end - in Buf.cut at lib/Buf.raz:95:9: b = 0x?, n = 6 + in Buf.cut at lib/Buf.raz:95:9: n = 6 in BufCut.main at test/run/BufCut.raz:11:5 diff --git a/test/run/BufFull.raz b/test/run/BufFull.raz index 7779b87..60ed711 100644 --- a/test/run/BufFull.raz +++ b/test/run/BufFull.raz @@ -7,8 +7,8 @@ var ar: Arena.Arena; b: Buf.Buf; big: [10000]U8; begin if not Arena.init(ADR(ar), 4096) then Rt.stop("no arena"); end; - Buf.init(ADR(b), ADR(ar.base)); - Buf.str(ADR(b), big); + Buf.init(b, ADR(ar.base)); + Buf.str(b, big); end; end BufFull. diff --git a/test/run/BufFull.stderr b/test/run/BufFull.stderr index f790f70..5ca2722 100644 --- a/test/run/BufFull.stderr +++ b/test/run/BufFull.stderr @@ -1,4 +1,4 @@ Buf: out of memory - in Buf.need at lib/Buf.raz:50:9: b = 0x?, n = 10000 - in Buf.str at lib/Buf.raz:66:5: b = 0x? + in Buf.need at lib/Buf.raz:50:9: n = 10000 + in Buf.str at lib/Buf.raz:66:5 in BufFull.main at test/run/BufFull.raz:11:5 diff --git a/test/run/BufInit.raz b/test/run/BufInit.raz index ef9e28d..773c66f 100644 --- a/test/run/BufInit.raz +++ b/test/run/BufInit.raz @@ -6,7 +6,7 @@ proc main*; var b: Buf.Buf; begin - Buf.str(ADR(b), "hello"); + Buf.str(b, "hello"); end; end BufInit. diff --git a/test/run/BufInit.stderr b/test/run/BufInit.stderr index e121cd2..45e1bb6 100644 --- a/test/run/BufInit.stderr +++ b/test/run/BufInit.stderr @@ -1,5 +1,5 @@ Buf used before Buf.init - in Buf.room at lib/Buf.raz:31:9: b = 0x?, n = 5, cap = 64, p = nil - in Buf.need at lib/Buf.raz:48:5: b = 0x?, n = 5 - in Buf.str at lib/Buf.raz:66:5: b = 0x? + in Buf.room at lib/Buf.raz:31:9: n = 5, cap = 64, p = nil + in Buf.need at lib/Buf.raz:48:5: n = 5 + in Buf.str at lib/Buf.raz:66:5 in BufInit.main at test/run/BufInit.raz:9:5 diff --git a/tools/Doc.raz b/tools/Doc.raz index 384fade..8ac1e7c 100644 --- a/tools/Doc.raz +++ b/tools/Doc.raz @@ -304,9 +304,9 @@ proc document(path: []U8); var b: Buf.Buf; start, ds, de: INT; begin - Buf.init(ADR(b), Heap.heap); - if not Files.read(path, ADR(b)) then Rt.stop("Doc: can't read the file"); end; - src := Buf.bytes(ADR(b)); + Buf.init(b, Heap.heap); + if not Files.read(path, b) then Rt.stop("Doc: can't read the file"); end; + src := Buf.bytes(b); Scan.init(ADR(sc), src); next; while (sc.kind # Scan.eof) and not is("module") do next; end; @@ -336,7 +336,7 @@ (* line, from the report or a manual page's literal block, added to b as the program has it: in a page, \e is a backslash, \(ga a backquote and \& nothing. *) -proc addLine(b: ^Buf.Buf; line: []U8; page: BOOL); +proc addLine(var b: Buf.Buf; line: []U8; page: BOOL); var i: INT; begin i := 0; @@ -357,7 +357,7 @@ end; (* text written to the file dir/name and ext, its path made in out *) -proc write(out: ^Buf.Buf; dir, name, ext, text: []U8); +proc write(var out: Buf.Buf; dir, name, ext, text: []U8); begin Buf.reset(out); Buf.str(out, dir); Buf.str(out, "/"); Buf.str(out, name); Buf.str(out, ext); @@ -374,11 +374,11 @@ b, out, prog: Buf.Buf; s, line, first, last, name, shows: []U8; i, k, n, after: INT; inBlock, whole, page, fence: BOOL; begin - Buf.init(ADR(b), Heap.heap); - Buf.init(ADR(out), Heap.heap); - Buf.init(ADR(prog), Heap.heap); - if not Files.read(path, ADR(b)) then Rt.stop("Doc: can't read the examples' file"); end; - s := Buf.bytes(ADR(b)); + Buf.init(b, Heap.heap); + Buf.init(out, Heap.heap); + Buf.init(prog, Heap.heap); + if not Files.read(path, b) then Rt.stop("Doc: can't read the examples' file"); end; + s := Buf.bytes(b); page := (LEN(path) < 3) or (path[LEN(path) - 3..LEN(path)] # ".md"); inBlock := false; after := -1; @@ -394,22 +394,22 @@ n := 0; while (n < LEN(name)) and Scan.isWord(name[n]) do INC(n); end; name := name[0..n]; - write(ADR(out), dir, name, ".raz", Buf.bytes(ADR(prog))); + write(out, dir, name, ".raz", Buf.bytes(prog)); Out.str(name); Out.ln; after := k + 1; elsif LEN(shows) > 0 then - write(ADR(out), dir, shows, ".shown", Buf.bytes(ADR(prog))); + write(out, dir, shows, ".shown", Buf.bytes(prog)); end; inBlock := false; elsif not inBlock and (fence or page and Str.hasPrefix(line, ".Bd -literal")) then inBlock := true; shows := ""; if page and (i = after) then shows := name; end; - Buf.reset(ADR(prog)); + Buf.reset(prog); first := ""; last := ""; whole := true; elsif inBlock then - addLine(ADR(prog), line, page); + addLine(prog, line, page); if Str.trim(line) = "..." then whole := false; end; if LEN(Str.trim(line)) > 0 then if LEN(first) = 0 then first := Str.trim(line); end; diff --git a/tools/Mutate.raz b/tools/Mutate.raz index c6bcaa4..dcaab3d 100644 --- a/tools/Mutate.raz +++ b/tools/Mutate.raz @@ -62,9 +62,9 @@ var buf: Buf.Buf; n, op, i: INT; a, b, c: Scan.Scanner; begin if ARGC() # 3 then Rt.stop("usage: Mutate File.raz seed"); end; - Buf.init(ADR(buf), Heap.heap); - if not Files.read(Files.arg(1), ADR(buf)) then Rt.stop("Mutate: can't read the file"); end; - src := Buf.bytes(ADR(buf)); + Buf.init(buf, Heap.heap); + if not Files.read(Files.arg(1), buf) then Rt.stop("Mutate: can't read the file"); end; + src := Buf.bytes(buf); seed(Files.arg(2)); n := count(); if n < 2 then Out.str(src); return; end; diff --git a/tools/Pretty.raz b/tools/Pretty.raz index 4266789..ba0e647 100644 --- a/tools/Pretty.raz +++ b/tools/Pretty.raz @@ -55,14 +55,14 @@ proc spaces(n: INT); begin - while n > 0 do Buf.add(ADR(out), ' '); DEC(n); end; + while n > 0 do Buf.add(out, ' '); DEC(n); end; end; (* Drops the spaces at the end of the line in out. *) proc trim; var s: []U8; begin - s := Buf.bytes(ADR(out)); + s := Buf.bytes(out); while (out.len > lineAt) and ((s[out.len - 1] = ' ') or (s[out.len - 1] = '\t')) do DEC(out.len); end; @@ -71,7 +71,7 @@ proc newLine; begin trim; - Buf.add(ADR(out), '\n'); + Buf.add(out, '\n'); lineAt := out.len; end; @@ -215,12 +215,12 @@ if (n < 1) and (sc.start > last) then n := 1; end; spaces(n); elsif afterComment then - Buf.str(ADR(out), src[last..sc.start]); + Buf.str(out, src[last..sc.start]); elsif Scan.is(ADR(sc), ",") or Scan.is(ADR(sc), ";") then elsif Scan.is(ADR(prev), ",") or Scan.is(ADR(prev), ";") or binaryNow or prevBinary then if not prevUnary then spaces(1); end; elsif not prevUnary then - Buf.str(ADR(out), src[last..sc.start]); + Buf.str(out, src[last..sc.start]); end; end; @@ -233,7 +233,7 @@ i := sc.start; while i < sc.stop do if (src[i] = '\n') and (sc.kind = Scan.literal) then - Buf.add(ADR(out), '\n'); + Buf.add(out, '\n'); lineAt := out.len; INC(i); elsif src[i] = '\n' then @@ -243,7 +243,7 @@ if (k < sc.stop) and (src[k] # '\n') then spaces(k - (i + 1) + lineDelta); end; i := k; else - Buf.add(ADR(out), src[i]); + Buf.add(out, src[i]); INC(i); end; end; @@ -289,7 +289,7 @@ var b: BOOL; begin src := text; - Buf.reset(ADR(out)); + Buf.reset(out); lineAt := 0; depth := 0; brackets := 0; @@ -325,23 +325,23 @@ proc main*; var b: Buf.Buf; i: INT; path: []U8; begin - Buf.init(ADR(b), Heap.heap); - Buf.init(ADR(out), Heap.heap); + Buf.init(b, Heap.heap); + Buf.init(out, Heap.heap); if (ARGC() >= 3) and (Files.arg(1) = "-w") then for i := 2 to ARGC() - 1 do path := Files.arg(i); - Buf.reset(ADR(b)); - if not Files.read(path, ADR(b)) then Rt.stop("Pretty: can't read a file"); end; - format(Buf.bytes(ADR(b))); - if Buf.bytes(ADR(out)) # Buf.bytes(ADR(b)) then - if not Files.write(path, Buf.bytes(ADR(out))) then Rt.stop("Pretty: can't write a file"); end; + Buf.reset(b); + if not Files.read(path, b) then Rt.stop("Pretty: can't read a file"); end; + format(Buf.bytes(b)); + if Buf.bytes(out) # Buf.bytes(b) then + if not Files.write(path, Buf.bytes(out)) then Rt.stop("Pretty: can't write a file"); end; Out.str(path); Out.ln; end; end; elsif ARGC() = 2 then - if not Files.read(Files.arg(1), ADR(b)) then Rt.stop("Pretty: can't read the file"); end; - format(Buf.bytes(ADR(b))); - Out.str(Buf.bytes(ADR(out))); + if not Files.read(Files.arg(1), b) then Rt.stop("Pretty: can't read the file"); end; + format(Buf.bytes(b)); + Out.str(Buf.bytes(out)); else Rt.stop("usage: Pretty Module.raz, or Pretty -w File.raz..."); end; diff --git a/tools/Widths.raz b/tools/Widths.raz index 3c01ffd..dabc6b8 100644 --- a/tools/Widths.raz +++ b/tools/Widths.raz @@ -20,7 +20,7 @@ w: [Last + 1]U8; (* each character's width, 1 unless a file says *) (* The whole file at path. *) -proc load(path: []U8; b: ^Buf.Buf): []U8; +proc load(path: []U8; var b: Buf.Buf): []U8; begin Buf.init(b, Heap.heap); if not Files.read(path, b) then @@ -116,7 +116,7 @@ end; (* c as a constant of raz: 0, six hexadecimal digits and H. *) -proc hex(b: ^Buf.Buf; c: INT); +proc hex(var b: Buf.Buf; c: INT); const Digits = "0123456789ABCDEF"; var i: INT; begin @@ -127,7 +127,7 @@ (* The constant name, the ranges of characters whose width is width, each its first and last, four ranges to a line. *) -proc table(b: ^Buf.Buf; name: []U8; width: U8); +proc table(var b: Buf.Buf; name: []U8; width: U8); var c, lo, n: INT; begin Buf.str(b, " "); Buf.str(b, name); Buf.str(b, ": []I32 = ["); @@ -153,7 +153,7 @@ Out.str("usage: Widths lib/Width.raz EastAsianWidth.txt UnicodeData.txt PropList.txt\n"); Rt.exit(2); end; - s := load(Files.arg(1), ADR(src)); + s := load(Files.arg(1), src); before, after, ok := Str.cut(s, Start); if ok then _, after, ok := Str.cut(after, Stop); end; if not ok then @@ -161,22 +161,22 @@ Rt.exit(1); end; mark(0, Last, 1); - version := eastAsian(load(Files.arg(2), ADR(eaw))); - categories(load(Files.arg(3), ADR(ucd))); - concatenation(load(Files.arg(4), ADR(props))); + version := eastAsian(load(Files.arg(2), eaw)); + categories(load(Files.arg(3), ucd)); + concatenation(load(Files.arg(4), props)); mark(1160H, 11FFH, 0); (* Hangul's medial and final jamo *) mark(0D7B0H, 0D7FFH, 0); mark(3164H, 3164H, 0); (* the Hangul fillers, which draw nothing *) mark(0FFA0H, 0FFA0H, 0); - Buf.init(ADR(out), Heap.heap); - Buf.str(ADR(out), before); - Buf.str(ADR(out), Start); - Buf.str(ADR(out), "(* Unicode "); Buf.str(ADR(out), version); Buf.str(ADR(out), " *)\nconst\n"); - table(ADR(out), "Zero", 0); - table(ADR(out), "Wide", 2); - Buf.str(ADR(out), Stop); - Buf.str(ADR(out), after); - if not Files.write(Files.arg(1), Buf.bytes(ADR(out))) then + Buf.init(out, Heap.heap); + Buf.str(out, before); + Buf.str(out, Start); + Buf.str(out, "(* Unicode "); Buf.str(out, version); Buf.str(out, " *)\nconst\n"); + table(out, "Zero", 0); + table(out, "Wide", 2); + Buf.str(out, Stop); + Buf.str(out, after); + if not Files.write(Files.arg(1), Buf.bytes(out)) then Out.str(Files.arg(1)); Out.str(": can't be written\n"); Rt.exit(1); end;