hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 78098e670b8645ac2c336811ab6c791890c7c3c8
parent 3d2c0b707e1bbc987e6be885d12ebc89ec230edd
Author: Sebastian <sebastian@sebsite.pw>
Date:   Fri,  4 Aug 2023 00:29:40 -0400

Use cap builtin

Signed-off-by: Sebastian <sebastian@sebsite.pw>

Diffstat:
Mhare/parse/expr.ha | 4++--
Mrt/ensure.ha | 26+++++++++++++-------------
Mscripts/gen-stdlib | 2+-
Dslices/cap.ha | 9---------
Mstdlib.mk | 4++--
Mstrio/stream.ha | 8++++----
6 files changed, 22 insertions(+), 31 deletions(-)

diff --git a/hare/parse/expr.ha b/hare/parse/expr.ha @@ -170,12 +170,12 @@ fn alloc_expr(lexer: *lex::lexer) (ast::expr | error) = { const expr = switch (want(lexer, ltok::COMMA, ltok::ELLIPSIS, ltok::RPAREN)?.0) { case ltok::COMMA => - const cap = alloc(expr(lexer)?); + const capacity = alloc(expr(lexer)?); want(lexer, ltok::RPAREN)?; yield ast::alloc_expr { init = init, form = ast::alloc_form::COPY, - capacity = cap, + capacity = capacity, }; case ltok::ELLIPSIS => want(lexer, ltok::RPAREN)?; diff --git a/rt/ensure.ha b/rt/ensure.ha @@ -9,31 +9,31 @@ export type slice = struct { }; export fn ensure(s: *slice, membsz: size) void = { - let cap = s.capacity; - if (cap >= s.length) { + let capacity = s.capacity; + if (capacity >= s.length) { return; }; - for (cap < s.length) { - assert(cap >= s.capacity, "slice out of memory (overflow)"); - if (cap == 0) { - cap = s.length; + for (capacity < s.length) { + assert(capacity >= s.capacity, "slice out of memory (overflow)"); + if (capacity == 0) { + capacity = s.length; } else { - cap *= 2; + capacity *= 2; }; }; - s.capacity = cap; + s.capacity = capacity; const data = realloc(s.data, s.capacity * membsz); assert(data != null || s.capacity * membsz == 0); s.data = data; }; export fn unensure(s: *slice, membsz: size) void = { - let cap = s.capacity; - for (cap > s.length) { - cap /= 2; + let capacity = s.capacity; + for (capacity > s.length) { + capacity /= 2; }; - cap *= 2; - s.capacity = cap; + capacity *= 2; + s.capacity = capacity; const data = realloc(s.data, s.capacity * membsz); assert(data != null || s.capacity * membsz == 0); s.data = data; diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib @@ -1342,7 +1342,7 @@ strio() { gen_srcs strio \ stream.ha \ ops.ha - gen_ssa strio errors io strings slices encoding::utf8 + gen_ssa strio errors io strings encoding::utf8 } temp() { diff --git a/slices/cap.ha b/slices/cap.ha @@ -1,9 +0,0 @@ -// License: MPL-2.0 -// (c) 2022 Sebastian <sebastian@sebsite.pw> -use types; - -// Returns the capacity of a slice. -export fn cap(slice: []const void) size = { - const slice = &slice: *types::slice; - return slice.capacity; -}; diff --git a/stdlib.mk b/stdlib.mk @@ -2088,7 +2088,7 @@ stdlib_strio_any_srcs = \ $(STDLIB)/strio/stream.ha \ $(STDLIB)/strio/ops.ha -$(HARECACHE)/strio/strio-any.ssa: $(stdlib_strio_any_srcs) $(stdlib_rt) $(stdlib_errors_$(PLATFORM)) $(stdlib_io_$(PLATFORM)) $(stdlib_strings_$(PLATFORM)) $(stdlib_slices_$(PLATFORM)) $(stdlib_encoding_utf8_$(PLATFORM)) +$(HARECACHE)/strio/strio-any.ssa: $(stdlib_strio_any_srcs) $(stdlib_rt) $(stdlib_errors_$(PLATFORM)) $(stdlib_io_$(PLATFORM)) $(stdlib_strings_$(PLATFORM)) $(stdlib_encoding_utf8_$(PLATFORM)) @printf 'HAREC \t$@\n' @mkdir -p $(HARECACHE)/strio @$(stdlib_env) $(HAREC) $(HAREFLAGS) -o $@ -Nstrio \ @@ -4575,7 +4575,7 @@ testlib_strio_any_srcs = \ $(STDLIB)/strio/stream.ha \ $(STDLIB)/strio/ops.ha -$(TESTCACHE)/strio/strio-any.ssa: $(testlib_strio_any_srcs) $(testlib_rt) $(testlib_errors_$(PLATFORM)) $(testlib_io_$(PLATFORM)) $(testlib_strings_$(PLATFORM)) $(testlib_slices_$(PLATFORM)) $(testlib_encoding_utf8_$(PLATFORM)) +$(TESTCACHE)/strio/strio-any.ssa: $(testlib_strio_any_srcs) $(testlib_rt) $(testlib_errors_$(PLATFORM)) $(testlib_io_$(PLATFORM)) $(testlib_strings_$(PLATFORM)) $(testlib_encoding_utf8_$(PLATFORM)) @printf 'HAREC \t$@\n' @mkdir -p $(TESTCACHE)/strio @$(testlib_env) $(HAREC) $(TESTHAREFLAGS) -o $@ -Nstrio \ diff --git a/strio/stream.ha b/strio/stream.ha @@ -3,7 +3,6 @@ // (c) 2021 Drew DeVault <sir@cmpwn.com> use errors; use io; -use slices; use strings; const fixed_vtable: io::vtable = io::vtable { @@ -42,9 +41,10 @@ export fn fixed(in: []u8) stream = { fn fixed_write(s: *io::stream, buf: const []u8) (size | io::error) = { let s = s: *stream; - let cap = slices::cap(s.buf); - if (cap == len(s.buf)) return errors::overflow; - let n = if (cap - len(s.buf) < len(buf)) cap - len(s.buf) else len(buf); + if (cap(s.buf) == len(s.buf)) return errors::overflow; + let n = if (cap(s.buf) - len(s.buf) < len(buf)) { + yield cap(s.buf) - len(s.buf); + } else len(buf); static append(s.buf, buf[..n]...); return n; };