lib: rename stdlib surface to Hare names; add endian/math

Sweeping rename so the lib/ surface mirrors Hare's stdlib spellings.
- ascii: rune-taking predicates; ishex -> isxdigit
- bufio: rinit -> init; take1/takeline -> readbyte/readline
- bytes: indexsub -> index
- encoding/utf8: runelen -> runesz
- errors: eEOF/eShortRead/... -> eof/underread/...
- fmt: errln -> errorln; println/fprintln return i64
- os: readfull/writefull -> readall/writeall; unlink -> remove
- path: isabs -> abs; drop lastindex (now strings.rbyteindex)
- strconv: u64toa/i64toa -> u64tos/i64tos; parse64/parseu64 -> stoi64/stou64
- strings: drop len/isempty; equal -> compare; indexbyte -> byteindex; +rbyteindex
- types: drop numeric helpers (moved to math)
- new lib/endian (htonu16/ntohu16), lib/math (absi32/absi64)
- net: drop htons (use endian.htonu16)

Callers in selfhost/, lib/ww/, cmd/w6c/cgen.c, and test/wcc/700_e2e.c
updated to match.
This commit is contained in:
2026-05-12 00:45:18 +09:00
parent 35421f2561
commit 1ac1d985f6
37 changed files with 597 additions and 568 deletions

View File

@@ -790,7 +790,7 @@ cgexpr(Cg *c, Node *n, Local *locals)
/* Use DIV (unsigned) when either operand is an unsigned
* integer type — IDIV would sign-extend a u64 with high
* bit set into a negative i64 and produce wrong results
* (see strconv.u64toa with v = 1 << 63). */
* (see strconv.u64tos with v = 1 << 63). */
int unsignd = (n->lhs && type_isunsigned(n->lhs->type))
|| (n->rhs && type_isunsigned(n->rhs->type));
ins2(c, A_MOVQ, aimm(0), areg(D_DX));
@@ -819,7 +819,7 @@ cgexpr(Cg *c, Node *n, Local *locals)
/* For ordered comparisons on unsigned operands we must
* use the JA/JAE/JB/JBE family — signed Jcc would treat
* a u64 with the high bit set as negative (e.g. the
* loop guard `n > 0` in strconv.u64toa with n=1<<63). */
* loop guard `n > 0` in strconv.u64tos with n=1<<63). */
int unsignd = (n->lhs && type_isunsigned(n->lhs->type))
|| (n->rhs && type_isunsigned(n->rhs->type));
ins2(c, A_CMPQ, areg(D_BX), areg(D_AX));

View File

@@ -1,92 +1,92 @@
// ascii — byte-class predicates and case folding for the ASCII range.
// Matches Hare's ascii::isdigit family. Bytes outside 0..127 always
// answer `false`. The lexer hot path uses these inline; they are
// expected to inline to a couple of compares.
// ascii — rune-class predicates and case folding for the ASCII range.
// Matches Hare's ascii::isdigit family (rune-taking signature). Runes
// outside 0..127 always answer `false`. The lexer hot path uses these
// inline; they are expected to inline to a couple of compares.
export fn isdigit(c: u8) bool = {
if (c < 48u8) { return false; };
if (c > 57u8) { return false; };
export fn isdigit(c: rune) bool = {
if (c < 48) { return false; };
if (c > 57) { return false; };
return true;
};
export fn isupper(c: u8) bool = {
if (c < 65u8) { return false; };
if (c > 90u8) { return false; };
export fn isupper(c: rune) bool = {
if (c < 65) { return false; };
if (c > 90) { return false; };
return true;
};
export fn islower(c: u8) bool = {
if (c < 97u8) { return false; };
if (c > 122u8) { return false; };
export fn islower(c: rune) bool = {
if (c < 97) { return false; };
if (c > 122) { return false; };
return true;
};
export fn isalpha(c: u8) bool = {
export fn isalpha(c: rune) bool = {
if (isupper(c)) { return true; };
return islower(c);
};
export fn isalnum(c: u8) bool = {
export fn isalnum(c: rune) bool = {
if (isalpha(c)) { return true; };
return isdigit(c);
};
// isspace — the C/Hare set: space, tab, NL, VT, FF, CR.
export fn isspace(c: u8) bool = {
if (c == 32u8) { return true; }; // ' '
if (c == 9u8) { return true; }; // '\t'
if (c == 10u8) { return true; }; // '\n'
if (c == 11u8) { return true; }; // '\v'
if (c == 12u8) { return true; }; // '\f'
if (c == 13u8) { return true; }; // '\r'
export fn isspace(c: rune) bool = {
if (c == 32) { return true; }; // ' '
if (c == 9) { return true; }; // '\t'
if (c == 10) { return true; }; // '\n'
if (c == 11) { return true; }; // '\v'
if (c == 12) { return true; }; // '\f'
if (c == 13) { return true; }; // '\r'
return false;
};
export fn ishex(c: u8) bool = {
export fn isxdigit(c: rune) bool = {
if (isdigit(c)) { return true; };
if (c >= 65u8) {
if (c <= 70u8) { return true; }; // 'A'..'F'
if (c >= 65) {
if (c <= 70) { return true; }; // 'A'..'F'
};
if (c >= 97u8) {
if (c <= 102u8) { return true; }; // 'a'..'f'
if (c >= 97) {
if (c <= 102) { return true; }; // 'a'..'f'
};
return false;
};
// digitval — value of `c` as a hex/decimal digit, or -1 if not one.
// Useful when scanning numeric literals.
export fn digitval(c: u8) i32 = {
if (isdigit(c)) { return (c - 48u8): i32; };
if (c >= 65u8) {
if (c <= 70u8) { return ((c - 65u8) + 10u8): i32; };
export fn digitval(c: rune) i32 = {
if (isdigit(c)) { return (c - 48): i32; };
if (c >= 65) {
if (c <= 70) { return ((c - 65) + 10): i32; };
};
if (c >= 97u8) {
if (c <= 102u8) { return ((c - 97u8) + 10u8): i32; };
if (c >= 97) {
if (c <= 102) { return ((c - 97) + 10): i32; };
};
return -1;
};
// isidstart / isidpart — identifier classes used by the lexer.
// Alpha or '_' starts; alnum or '_' continues.
export fn isidstart(c: u8) bool = {
export fn isidstart(c: rune) bool = {
if (isalpha(c)) { return true; };
if (c == 95u8) { return true; }; // '_'
if (c == 95) { return true; }; // '_'
return false;
};
export fn isidpart(c: u8) bool = {
export fn isidpart(c: rune) bool = {
if (isalnum(c)) { return true; };
if (c == 95u8) { return true; };
if (c == 95) { return true; };
return false;
};
// tolower / toupper — fold ASCII case. Non-letters pass through.
export fn tolower(c: u8) u8 = {
if (isupper(c)) { return c + 32u8; };
export fn tolower(c: rune) rune = {
if (isupper(c)) { return c + 32; };
return c;
};
export fn toupper(c: u8) u8 = {
if (islower(c)) { return c - 32u8; };
export fn toupper(c: rune) rune = {
if (islower(c)) { return c - 32; };
return c;
};

View File

@@ -17,7 +17,7 @@ type buf = struct {
w: i32, // write cursor (for writers)
};
export fn rinit(b: *buf, s: streamp, data: *u8, cap: i32) void = {
export fn init(b: *buf, s: streamp, data: *u8, cap: i32) void = {
b.s = s;
b.data = data;
b.cap = cap;
@@ -25,18 +25,10 @@ export fn rinit(b: *buf, s: streamp, data: *u8, cap: i32) void = {
b.w = 0;
};
// peek1 — look at the next byte without consuming. Returns -1 on
// empty buffer; the caller is responsible for refilling via the
// stream when this happens.
export fn peek1(b: *buf) i32 = {
if (b.r < b.w) {
return b.data[b.r]: i32;
};
return -1;
};
// take1 — pop one byte. -1 if empty.
export fn take1(b: *buf) i32 = {
// readbyte — pop one byte. -1 if empty. Hare name (transliterated
// from `read_byte`); the `-1`-for-EOF return is the sanctioned subset
// of Hare's `(u8 | EOF | error)`.
export fn readbyte(b: *buf) i32 = {
if (b.r < b.w) {
let c: u8 = b.data[b.r];
b.r += 1;
@@ -45,27 +37,23 @@ export fn take1(b: *buf) i32 = {
return -1;
};
// avail — bytes left to read out of the buffer.
export fn avail(b: *buf) i32 = {
return b.w - b.r;
};
// Distinct alias so `(str | linerr)` has two variant types the
// tagged-union machinery can keep apart at the tag level. The error
// variant carries a short description; callers compare with errors.equal
// or just inspect by length.
type linerr = str;
// takeline — Hare-style fallible line read. Drains the buffer up to
// (but not including) the next '\n' and advances the cursor past the
// newline. Returns the line as a borrowed str on success, or a linerr
// describing why no line was available:
// readline — Hare-style fallible line read (name transliterated from
// `read_line`). Drains the buffer up to (but not including) the next
// '\n' and advances the cursor past the newline. Returns the line as
// a borrowed str on success, or a linerr describing why no line was
// available:
// - "eof" when the buffer is empty
// - "no newline" when the buffer contains data but no '\n'
//
// The returned str borrows from the underlying buffer; callers must
// consume it (or copy) before refilling.
export fn takeline(b: *buf) (str | linerr) = {
export fn readline(b: *buf) (str | linerr) = {
if (b.r >= b.w) { return "eof": linerr; };
let i: i32 = b.r;
for (i < b.w) {

View File

@@ -30,9 +30,10 @@ export fn copy(dst: []u8, src: []u8) i32 = {
return n;
};
// indexsub — first index of `sub` in `s`, or -1. Mirrors
// strings.index but on []u8. Empty `sub` matches at 0.
export fn indexsub(s: []u8, sub: []u8) i32 = {
// index — first index of `sub` in `s`, or -1. Mirrors Hare's
// bytes::index (the []u8 needle variant; the u8 needle stays as
// indexbyte until we have union-arg dispatch). Empty `sub` matches at 0.
export fn index(s: []u8, sub: []u8) i32 = {
if (sub.len == 0) { return 0; };
if (sub.len > s.len) { return -1; };
let last: i32 = s.len - sub.len;

View File

@@ -4,7 +4,7 @@
def MAX: rune = 1114111; // 0x10FFFF
def BAD: rune = -1;
export fn runelen(r: rune) i32 = {
export fn runesz(r: rune) i32 = {
if (r < 0) { return -1; };
if (r < 128) { return 1; };
if (r < 2048) { return 2; };

9
lib/endian/endian.ww Normal file
View File

@@ -0,0 +1,9 @@
// endian — byte-order conversions. Subset of Hare's endian::; only
// the network-order helpers we need for net::. Host order on amd64 is
// little-endian, so hton* / ntoh* are byte swaps.
export fn htonu16(in: u16) u16 = {
return ((in << 8) | (in >> 8)) & 0xffff;
};
export fn ntohu16(in: u16) u16 = htonu16(in);

View File

@@ -3,14 +3,14 @@
type error = str;
def eEOF: error = "eof";
def eShortRead: error = "short read";
def eShortWrite: error = "short write";
def eClosed: error = "closed";
def eInvalid: error = "invalid argument";
def ePerm: error = "permission denied";
def eNotFound: error = "not found";
def eExists: error = "already exists";
def eof: error = "eof";
def underread: error = "short read";
def underwrite: error = "short write";
def closed: error = "closed";
def invalid: error = "invalid argument";
def noaccess: error = "permission denied";
def noentry: error = "not found";
def exists: error = "already exists";
export fn isnil(e: error) bool = {
return e.len == 0;

View File

@@ -10,14 +10,17 @@ export fn print(s: str) i64 = {
return os.write(1, s.ptr, s.len: u64);
};
export fn println(s: str) void = {
os.write(1, s.ptr, s.len: u64);
os.write(1, "\n".ptr, 1u64);
export fn println(s: str) i64 = {
let n: i64 = os.write(1, s.ptr, s.len: u64);
if (n < 0) { return n; };
let m: i64 = os.write(1, "\n".ptr, 1u64);
if (m < 0) { return m; };
return n + m;
};
export fn printint(v: i64) void = {
let buf: [32]u8;
let n: i32 = strconv.i64toa(buf[0:32], v);
let n: i32 = strconv.i64tos(buf[0:32], v);
os.write(1, buf.ptr, n: u64);
};
@@ -26,10 +29,13 @@ export fn printlnint(v: i64) void = {
os.write(1, "\n".ptr, 1u64);
};
// errln — write a message to stderr with a trailing newline.
export fn errln(s: str) void = {
os.write(2, s.ptr, s.len: u64);
os.write(2, "\n".ptr, 1u64);
// errorln — write a message to stderr with a trailing newline.
export fn errorln(s: str) i64 = {
let n: i64 = os.write(2, s.ptr, s.len: u64);
if (n < 0) { return n; };
let m: i64 = os.write(2, "\n".ptr, 1u64);
if (m < 0) { return m; };
return n + m;
};
// fprint / fprintln — same as print/println but on an arbitrary fd.
@@ -38,14 +44,17 @@ export fn fprint(fd: i32, s: str) i64 = {
return os.write(fd, s.ptr, s.len: u64);
};
export fn fprintln(fd: i32, s: str) void = {
os.write(fd, s.ptr, s.len: u64);
os.write(fd, "\n".ptr, 1u64);
export fn fprintln(fd: i32, s: str) i64 = {
let n: i64 = os.write(fd, s.ptr, s.len: u64);
if (n < 0) { return n; };
let m: i64 = os.write(fd, "\n".ptr, 1u64);
if (m < 0) { return m; };
return n + m;
};
export fn fprintint(fd: i32, v: i64) void = {
let buf: [32]u8;
let n: i32 = strconv.i64toa(buf[0:32], v);
let n: i32 = strconv.i64tos(buf[0:32], v);
os.write(fd, buf.ptr, n: u64);
};
@@ -56,10 +65,10 @@ export fn errpos(file: str, line: i32, col: i32, msg: str) void = {
os.write(2, file.ptr, file.len: u64);
os.write(2, ":".ptr, 1u64);
let buf: [32]u8;
let n: i32 = strconv.i64toa(buf[0:32], line: i64);
let n: i32 = strconv.i64tos(buf[0:32], line: i64);
os.write(2, buf.ptr, n: u64);
os.write(2, ":".ptr, 1u64);
n = strconv.i64toa(buf[0:32], col: i64);
n = strconv.i64tos(buf[0:32], col: i64);
os.write(2, buf.ptr, n: u64);
os.write(2, ": ".ptr, 2u64);
os.write(2, msg.ptr, msg.len: u64);

13
lib/math/math.ww Normal file
View File

@@ -0,0 +1,13 @@
// math — numeric helpers. Subset of Hare's math::; only the absolute-
// value pair for the signed integer types we currently care about. The
// return type is unsigned so that abs(I32_MIN) doesn't overflow.
export fn absi32(n: i32) u32 = {
if (n < 0) { return (-n): u32; };
return n: u32;
};
export fn absi64(n: i64) u64 = {
if (n < 0) { return (-n): u64; };
return n: u64;
};

View File

@@ -42,7 +42,6 @@ export fn listen(fd: i32, backlog: i32) i32 = {
return syscall3(SYS_LISTEN, fd: i64, backlog: i64, 0): i32;
};
// htons-equivalent: byte-swap a 16-bit port into network order.
export fn htons(p: u16) u16 = {
return ((p << 8) | (p >> 8)) & 0xffff;
};
// Byte-order conversions live in lib/endian (Hare's endian::htonu16
// is the canonical name). Use `endian.htonu16` to put a port into
// network order.

View File

@@ -119,9 +119,11 @@ export fn filesize(fd: i32) i64 = {
return end;
};
// readfull — keep reading until `n` bytes have arrived or the fd
// readall — keep reading until `n` bytes have arrived or the fd
// closes early. Returns bytes read (0..=n) or -1 on read error.
export fn readfull(fd: i32, buf: *u8, n: u64) i64 = {
// Hare name (io::readall); the buffer is caller-supplied, matching
// the Plan 9 subset convention.
export fn readall(fd: i32, buf: *u8, n: u64) i64 = {
let got: u64 = 0u64;
for (got < n) {
let r: i64 = read(fd, buf + got, n - got);
@@ -132,9 +134,10 @@ export fn readfull(fd: i32, buf: *u8, n: u64) i64 = {
return got: i64;
};
// writefull — keep writing until `n` bytes have been accepted or the
// fd refuses progress. Returns bytes written or -1.
export fn writefull(fd: i32, buf: *u8, n: u64) i64 = {
// writeall — keep writing until `n` bytes have been accepted or the
// fd refuses progress. Returns bytes written or -1. Hare name
// (io::writeall).
export fn writeall(fd: i32, buf: *u8, n: u64) i64 = {
let sent: u64 = 0u64;
for (sent < n) {
let r: i64 = write(fd, buf + sent, n - sent);
@@ -153,8 +156,8 @@ export fn access(path: *u8, mode: i32) i32 = {
return syscall2(SYS_ACCESS, path: i64, mode: i64): i32;
};
// unlink(2).
export fn unlink(path: *u8) i32 = {
// remove — unlink(2). Hare name; the underlying syscall is unlink(2).
export fn remove(path: *u8) i32 = {
return syscall1(SYS_UNLINK, path: i64): i32;
};

View File

@@ -1,15 +1,8 @@
// path — filesystem path manipulation. UTF-8 paths, '/' separator.
// Mirrors Hare's path:: surface. Reverse byte search lives in strings
// (strings::rbyteindex), not here.
export fn isabs(p: str) bool = {
export fn abs(p: str) bool = {
if (p.len == 0) { return false; };
return p[0] == ('/': u8);
};
export fn lastindex(p: str, c: u8) i32 = {
let i: i32 = p.len - 1;
for (i >= 0) {
if (p[i] == c) { return i; };
i -= 1;
};
return -1;
};

View File

@@ -2,14 +2,16 @@
// buffer. Two error idioms ship side by side:
// - Plan 9 style (atoi64): tuple `(value, ok)`. Pre-dates the
// tagged-union work; kept for callers that already use it.
// - Hare style (parse64/parseu64): `(value | str)`. The error
// - Hare style (stoi64/stou64): `(value | str)`. The error
// variant carries a short, allocation-free message describing
// why the parse failed. Prefer this for new code.
// u64toa — write `v` in decimal into `buf` and return the byte count.
// Unsigned-only so callers don't have to think about wraparound when
// printing a u64 that happens to have the high bit set.
export fn u64toa(buf: []u8, v: u64) i32 = {
// u64tos — write `v` in decimal into `buf` and return the byte count.
// Hare name; the buffer-in shape is the sanctioned Plan 9 subset of
// Hare's `u64tos(u, base) const str`. Unsigned-only so callers don't
// have to think about wraparound when printing a u64 with the high
// bit set.
export fn u64tos(buf: []u8, v: u64) i32 = {
let tmp: [32]u8;
let i: i32 = 0;
let n: u64 = v;
@@ -31,7 +33,7 @@ export fn u64toa(buf: []u8, v: u64) i32 = {
return out;
};
export fn i64toa(buf: []u8, v: i64) i32 = {
export fn i64tos(buf: []u8, v: i64) i32 = {
let neg: bool = false;
let n: i64 = v;
if (n < 0) {
@@ -81,11 +83,12 @@ export fn atoi64(s: str) (i64, bool) = {
return v, true;
};
// parse64 — Hare-style fallible signed decimal parser. The value
// variant is i64; the error variant is a short str describing the
// reason. No locale, no whitespace, no underscores: a leading '-' is
// the only non-digit accepted, and only at position 0.
export fn parse64(s: str) (i64 | str) = {
// stoi64 — Hare-style fallible signed decimal parser. The value
// variant is i64; the error variant is a short str (subset of Hare's
// (invalid | overflow) tagged-union). No locale, no whitespace, no
// underscores: a leading '-' is the only non-digit accepted, and only
// at position 0.
export fn stoi64(s: str) (i64 | str) = {
if (s.len == 0) { return "parse: empty"; };
let i: i32 = 0;
let neg: bool = false;
@@ -103,8 +106,9 @@ export fn parse64(s: str) (i64 | str) = {
return v;
};
// parseu64 — fallible unsigned decimal parser. No leading sign.
export fn parseu64(s: str) (u64 | str) = {
// stou64 — fallible unsigned decimal parser. No leading sign. Mirrors
// Hare's strconv::stou64.
export fn stou64(s: str) (u64 | str) = {
if (s.len == 0) { return "parse: empty"; };
let v: u64 = 0u64;
let i: i32 = 0;

View File

@@ -1,23 +1,21 @@
// strings — operations over the immutable str type ({ *u8, len }).
// Mirrors Hare's strings::; `len` and `is-empty` aren't functions
// (callers use `s.len` and `s.len == 0` directly).
use os;
export fn len(s: str) i32 = {
return s.len;
};
export fn isempty(s: str) bool = {
return s.len == 0;
};
export fn equal(a: str, b: str) bool = {
if (a.len != b.len) { return false; };
// compare — bytewise three-way comparison: negative if a<b, 0 if equal,
// positive if a>b. Matches Hare's strings::compare. ASCII-order, not
// locale-aware. Callers that just need equality use `compare(a, b) == 0`.
export fn compare(a: str, b: str) i32 = {
let n: i32 = a.len;
if (b.len < n) { n = b.len; };
let i: i32 = 0;
for (i < a.len) {
if (a[i] != b[i]) { return false; };
for (i < n) {
if (a[i] != b[i]) { return (a[i]: i32) - (b[i]: i32); };
i += 1;
};
return true;
return a.len - b.len;
};
export fn hasprefix(s: str, p: str) bool = {
@@ -41,10 +39,11 @@ export fn hassuffix(s: str, suf: str) bool = {
return true;
};
// indexbyte — first index of `c` in `s`, or -1 if absent. Plan 9-
// style sentinel return; callers that prefer a fallible shape can
// wrap this in their own (i32 | str). No allocation.
export fn indexbyte(s: str, c: u8) i32 = {
// byteindex — first index of byte `c` in `s`, or -1 if absent. Hare
// name (strings::byteindex). Plan 9-style sentinel return; callers that
// prefer a fallible shape can wrap this in their own (i32 | str). No
// allocation.
export fn byteindex(s: str, c: u8) i32 = {
let i: i32 = 0;
for (i < s.len) {
if (s[i] == c) { return i; };
@@ -53,6 +52,17 @@ export fn indexbyte(s: str, c: u8) i32 = {
return -1;
};
// rbyteindex — last index of byte `c` in `s`, or -1 if absent. Mirrors
// Hare's strings::rbyteindex.
export fn rbyteindex(s: str, c: u8) i32 = {
let i: i32 = s.len - 1;
for (i >= 0) {
if (s[i] == c) { return i; };
i -= 1;
};
return -1;
};
// index — first index of `sub` in `s`, or -1. Naive scan; fine for
// short patterns and small strings, which dominate config and CLI
// parsing. Empty `sub` matches at 0.

View File

@@ -1,6 +1,6 @@
// types — integer limits and helpers, the seed module that the
// rest of the stdlib depends on. Plan 9-flavoured: the names are
// short and the constants are platform-fixed (we are amd64 only).
// types — integer limits. Mirrors Hare's types::limits (I8_MAX, …)
// platform-fixed for amd64. Numeric helpers live in lib/math, matching
// Hare's split between types::limits and math::.
def I8_MAX: i8 = 127;
def I16_MAX: i16 = 32767;
@@ -16,33 +16,3 @@ def U8_MAX: u8 = 255;
def U16_MAX: u16 = 65535;
def U32_MAX: u32 = 4294967295;
def U64_MAX: u64 = 18446744073709551615;
export fn mini32(a: i32, b: i32) i32 = {
if (a < b) { return a; };
return b;
};
export fn maxi32(a: i32, b: i32) i32 = {
if (a > b) { return a; };
return b;
};
export fn mini64(a: i64, b: i64) i64 = {
if (a < b) { return a; };
return b;
};
export fn maxi64(a: i64, b: i64) i64 = {
if (a > b) { return a; };
return b;
};
export fn absi32(x: i32) i32 = {
if (x < 0) { return -x; };
return x;
};
export fn absi64(x: i64) i64 = {
if (x < 0) { return -x; };
return x;
};

View File

@@ -256,12 +256,12 @@ fn pr(fd: i32, n: *node, d: i32) void = {
if (n.kind == N_INTLIT) {
putc1(fd, 32u8);
let buf: [32]u8;
let m: i32 = strconv.u64toa(buf[0:32], n.uval);
let m: i32 = strconv.u64tos(buf[0:32], n.uval);
os.write(fd, buf.ptr, m: u64);
} else { if (n.kind == N_RUNELIT) {
putc1(fd, 32u8);
let buf: [32]u8;
let m: i32 = strconv.u64toa(buf[0:32], n.uval);
let m: i32 = strconv.u64tos(buf[0:32], n.uval);
os.write(fd, buf.ptr, m: u64);
} else { if (
n.kind == N_STRLIT ||

View File

@@ -230,18 +230,18 @@ fn escape(l: *lex, out: *i32) bool = {
let lo: i32 = lget(l);
if (hi < 0) { return false; };
if (lo < 0) { return false; };
if (!ascii.ishex(hi: u8)) {
if (!ascii.isxdigit(hi: rune)) {
let cp: pos; curpos(l, &cp);
errat(l, &cp, "bad \\x escape");
return false;
};
if (!ascii.ishex(lo: u8)) {
if (!ascii.isxdigit(lo: rune)) {
let cp: pos; curpos(l, &cp);
errat(l, &cp, "bad \\x escape");
return false;
};
let h: i32 = ascii.digitval(hi: u8);
let lv: i32 = ascii.digitval(lo: u8);
let h: i32 = ascii.digitval(hi: rune);
let lv: i32 = ascii.digitval(lo: rune);
*out = (h << 4) | lv;
return true;
};
@@ -255,7 +255,7 @@ fn scandecimalrun(l: *lex) void = {
for (true) {
let c: i32 = lpeek(l, 0u64);
if (c < 0) { break; };
if (!ascii.isdigit(c: u8)) {
if (!ascii.isdigit(c: rune)) {
if (c != 95) { break; };
};
lget(l);
@@ -266,7 +266,7 @@ fn scanhexrun(l: *lex) void = {
for (true) {
let c: i32 = lpeek(l, 0u64);
if (c < 0) { break; };
if (!ascii.ishex(c: u8)) {
if (!ascii.isxdigit(c: rune)) {
if (c != 95) { break; };
};
lget(l);
@@ -305,7 +305,7 @@ fn scanexp(l: *lex) void = {
for (true) {
let c: i32 = lpeek(l, 0u64);
if (c < 0) { break; };
if (!ascii.isdigit(c: u8)) { break; };
if (!ascii.isdigit(c: rune)) { break; };
lget(l);
};
};
@@ -390,12 +390,12 @@ fn lexnum(l: *lex, start: *pos, out: *tok) void = {
let pc: i32 = lpeek(l, 0u64);
if (pc >= 0) {
if (ascii.isidstart(pc: u8)) {
if (ascii.isidstart(pc: rune)) {
let sb: u64 = l.lpos;
for (true) {
let cc: i32 = lpeek(l, 0u64);
if (cc < 0) { break; };
if (!ascii.isidpart(cc: u8)) { break; };
if (!ascii.isidpart(cc: rune)) { break; };
lget(l);
};
let sl: u64 = l.lpos - sb;
@@ -439,7 +439,7 @@ fn lexident(l: *lex, start: *pos, out: *tok) void = {
for (true) {
let c: i32 = lpeek(l, 0u64);
if (c < 0) { break; };
if (!ascii.isidpart(c: u8)) { break; };
if (!ascii.isidpart(c: rune)) { break; };
lget(l);
};
let n: u64 = l.lpos - begin;
@@ -588,8 +588,8 @@ export fn lexnext(l: *lex, out: *tok) void = {
let c: i32 = lpeek(l, 0u64);
if (c >= 0) {
if (ascii.isidstart(c: u8)) { lexident(l, &start, out); return; };
if (ascii.isdigit(c: u8)) { lexnum(l, &start, out); return; };
if (ascii.isidstart(c: rune)) { lexident(l, &start, out); return; };
if (ascii.isdigit(c: rune)) { lexnum(l, &start, out); return; };
};
if (c == 34) { lget(l); lexstr(l, &start, out); return; };

View File

@@ -372,10 +372,10 @@ export fn tokprint(fd: i32, t: *tok) void = {
};
fputcbyte(fd, 58u8); // ':'
let buf: [32]u8;
let n: i32 = strconv.i64toa(buf[0:32], t.line: i64);
let n: i32 = strconv.i64tos(buf[0:32], t.line: i64);
os.write(fd, buf.ptr, n: u64);
fputcbyte(fd, 58u8);
n = strconv.i64toa(buf[0:32], t.col: i64);
n = strconv.i64tos(buf[0:32], t.col: i64);
os.write(fd, buf.ptr, n: u64);
fputcbyte(fd, 32u8); // ' '
fputsstr(fd, tokname(t.kind));
@@ -391,11 +391,11 @@ export fn tokprint(fd: i32, t: *tok) void = {
fputq(fd, ttext.ptr, ttext.len);
} else { if (t.kind == TK_INT) {
fputcbyte(fd, 32u8);
n = strconv.u64toa(buf[0:32], t.uval);
n = strconv.u64tos(buf[0:32], t.uval);
os.write(fd, buf.ptr, n: u64);
} else { if (t.kind == TK_RUNE) {
fputcbyte(fd, 32u8);
n = strconv.u64toa(buf[0:32], t.uval);
n = strconv.u64tos(buf[0:32], t.uval);
os.write(fd, buf.ptr, n: u64);
};};};};};
// TK_FLOAT is intentionally not handled here — %g formatting

View File

@@ -120,9 +120,11 @@ export fn filesize(fd: i32) i64 = {
return end;
};
// readfull — keep reading until `n` bytes have arrived or the fd
// readall — keep reading until `n` bytes have arrived or the fd
// closes early. Returns bytes read (0..=n) or -1 on read error.
export fn readfull(fd: i32, buf: *u8, n: u64) i64 = {
// Hare name (io::readall); the buffer is caller-supplied, matching
// the Plan 9 subset convention.
export fn readall(fd: i32, buf: *u8, n: u64) i64 = {
let got: u64 = 0u64;
for (got < n) {
let r: i64 = read(fd, buf + got, n - got);
@@ -133,9 +135,10 @@ export fn readfull(fd: i32, buf: *u8, n: u64) i64 = {
return got: i64;
};
// writefull — keep writing until `n` bytes have been accepted or the
// fd refuses progress. Returns bytes written or -1.
export fn writefull(fd: i32, buf: *u8, n: u64) i64 = {
// writeall — keep writing until `n` bytes have been accepted or the
// fd refuses progress. Returns bytes written or -1. Hare name
// (io::writeall).
export fn writeall(fd: i32, buf: *u8, n: u64) i64 = {
let sent: u64 = 0u64;
for (sent < n) {
let r: i64 = write(fd, buf + sent, n - sent);
@@ -154,8 +157,8 @@ export fn access(path: *u8, mode: i32) i32 = {
return syscall2(SYS_ACCESS, path: i64, mode: i64): i32;
};
// unlink(2).
export fn unlink(path: *u8) i32 = {
// remove — unlink(2). Hare name; the underlying syscall is unlink(2).
export fn remove(path: *u8) i32 = {
return syscall1(SYS_UNLINK, path: i64): i32;
};
@@ -2040,27 +2043,27 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
wru16(eh.ptr, 60u64, NSECT); // e_shnum
wru16(eh.ptr, 62u64, 5u16); // e_shstrndx
if (os.writefull(fd, eh.ptr, 64u64) != 64i64) { return -1; };
if (os.writeall(fd, eh.ptr, 64u64) != 64i64) { return -1; };
if (a.textlen > 0u64) {
if (os.writefull(fd, a.text, a.textlen) != a.textlen: i64) { return -1; };
if (os.writeall(fd, a.text, a.textlen) != a.textlen: i64) { return -1; };
};
if (rela.n > 0u64) {
if (os.writefull(fd, rela.p, rela.n) != rela.n: i64) { return -1; };
if (os.writeall(fd, rela.p, rela.n) != rela.n: i64) { return -1; };
};
if (sym.n > 0u64) {
if (os.writefull(fd, sym.p, sym.n) != sym.n: i64) { return -1; };
if (os.writeall(fd, sym.p, sym.n) != sym.n: i64) { return -1; };
};
if (str_.n > 0u64) {
if (os.writefull(fd, str_.p, str_.n) != str_.n: i64) { return -1; };
if (os.writeall(fd, str_.p, str_.n) != str_.n: i64) { return -1; };
};
if (shstr.n > 0u64) {
if (os.writefull(fd, shstr.p, shstr.n) != shstr.n: i64) { return -1; };
if (os.writeall(fd, shstr.p, shstr.n) != shstr.n: i64) { return -1; };
};
// Pad to 8 before shdrs.
let written: u64 = EHDR_SZ + a.textlen + rela.n + sym.n + str_.n + shstr.n;
for ((written & 7u64) != 0u64) {
os.writefull(fd, &zero, 1u64);
os.writeall(fd, &zero, 1u64);
written += 1u64;
};
@@ -2069,7 +2072,7 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
// SHT_NULL
let sn: i32 = 0;
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
os.writefull(fd, shbuf.ptr, 64u64);
os.writeall(fd, shbuf.ptr, 64u64);
// .text
sn = 0;
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
@@ -2079,7 +2082,7 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
wru64(shbuf.ptr, 24u64, offtext);
wru64(shbuf.ptr, 32u64, a.textlen);
wru64(shbuf.ptr, 48u64, 1u64); // sh_addralign
os.writefull(fd, shbuf.ptr, 64u64);
os.writeall(fd, shbuf.ptr, 64u64);
// .rela.text
sn = 0;
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
@@ -2092,7 +2095,7 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
wru32(shbuf.ptr, 44u64, 1u32); // sh_info = .text idx
wru64(shbuf.ptr, 48u64, 8u64);
wru64(shbuf.ptr, 56u64, RELA_SZ);
os.writefull(fd, shbuf.ptr, 64u64);
os.writeall(fd, shbuf.ptr, 64u64);
// .symtab
sn = 0;
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
@@ -2104,7 +2107,7 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
wru32(shbuf.ptr, 44u64, 1u32); // sh_info = one local (STN_UNDEF)
wru64(shbuf.ptr, 48u64, 8u64);
wru64(shbuf.ptr, 56u64, SYM_SZ);
os.writefull(fd, shbuf.ptr, 64u64);
os.writeall(fd, shbuf.ptr, 64u64);
// .strtab
sn = 0;
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
@@ -2113,7 +2116,7 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
wru64(shbuf.ptr, 24u64, offstr);
wru64(shbuf.ptr, 32u64, str_.n);
wru64(shbuf.ptr, 48u64, 1u64);
os.writefull(fd, shbuf.ptr, 64u64);
os.writeall(fd, shbuf.ptr, 64u64);
// .shstrtab
sn = 0;
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
@@ -2122,7 +2125,7 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
wru64(shbuf.ptr, 24u64, offshstr);
wru64(shbuf.ptr, 32u64, shstr.n);
wru64(shbuf.ptr, 48u64, 1u64);
os.writefull(fd, shbuf.ptr, 64u64);
os.writeall(fd, shbuf.ptr, 64u64);
return 0;
};
@@ -2168,7 +2171,7 @@ fn slurp(path: *u8) (*u8, u64) = {
if (n < 0i64) { os.close(fd); return nil, 0u64; };
let nz: u64 = n: u64;
let buf: *u8 = os.alloc(nz + 1u64): *u8;
let got: i64 = os.readfull(fd, buf, nz);
let got: i64 = os.readall(fd, buf, nz);
os.close(fd);
if (got != n) { return nil, 0u64; };
buf[nz] = 0u8;

View File

@@ -38,7 +38,7 @@ fn slurp(path: *u8) (*u8, u64) = {
if (n < 0i64) { os.close(fd); return nil, 0u64; };
let nz: u64 = n: u64;
let buf: *u8 = os.alloc(nz + 1u64): *u8;
let got: i64 = os.readfull(fd, buf, nz);
let got: i64 = os.readall(fd, buf, nz);
os.close(fd);
if (got != n) { return nil, 0u64; };
buf[nz] = 0u8;

View File

@@ -205,27 +205,27 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
wru16(eh.ptr, 60u64, NSECT); // e_shnum
wru16(eh.ptr, 62u64, 5u16); // e_shstrndx
if (os.writefull(fd, eh.ptr, 64u64) != 64i64) { return -1; };
if (os.writeall(fd, eh.ptr, 64u64) != 64i64) { return -1; };
if (a.textlen > 0u64) {
if (os.writefull(fd, a.text, a.textlen) != a.textlen: i64) { return -1; };
if (os.writeall(fd, a.text, a.textlen) != a.textlen: i64) { return -1; };
};
if (rela.n > 0u64) {
if (os.writefull(fd, rela.p, rela.n) != rela.n: i64) { return -1; };
if (os.writeall(fd, rela.p, rela.n) != rela.n: i64) { return -1; };
};
if (sym.n > 0u64) {
if (os.writefull(fd, sym.p, sym.n) != sym.n: i64) { return -1; };
if (os.writeall(fd, sym.p, sym.n) != sym.n: i64) { return -1; };
};
if (str_.n > 0u64) {
if (os.writefull(fd, str_.p, str_.n) != str_.n: i64) { return -1; };
if (os.writeall(fd, str_.p, str_.n) != str_.n: i64) { return -1; };
};
if (shstr.n > 0u64) {
if (os.writefull(fd, shstr.p, shstr.n) != shstr.n: i64) { return -1; };
if (os.writeall(fd, shstr.p, shstr.n) != shstr.n: i64) { return -1; };
};
// Pad to 8 before shdrs.
let written: u64 = EHDR_SZ + a.textlen + rela.n + sym.n + str_.n + shstr.n;
for ((written & 7u64) != 0u64) {
os.writefull(fd, &zero, 1u64);
os.writeall(fd, &zero, 1u64);
written += 1u64;
};
@@ -234,7 +234,7 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
// SHT_NULL
let sn: i32 = 0;
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
os.writefull(fd, shbuf.ptr, 64u64);
os.writeall(fd, shbuf.ptr, 64u64);
// .text
sn = 0;
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
@@ -244,7 +244,7 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
wru64(shbuf.ptr, 24u64, offtext);
wru64(shbuf.ptr, 32u64, a.textlen);
wru64(shbuf.ptr, 48u64, 1u64); // sh_addralign
os.writefull(fd, shbuf.ptr, 64u64);
os.writeall(fd, shbuf.ptr, 64u64);
// .rela.text
sn = 0;
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
@@ -257,7 +257,7 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
wru32(shbuf.ptr, 44u64, 1u32); // sh_info = .text idx
wru64(shbuf.ptr, 48u64, 8u64);
wru64(shbuf.ptr, 56u64, RELA_SZ);
os.writefull(fd, shbuf.ptr, 64u64);
os.writeall(fd, shbuf.ptr, 64u64);
// .symtab
sn = 0;
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
@@ -269,7 +269,7 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
wru32(shbuf.ptr, 44u64, 1u32); // sh_info = one local (STN_UNDEF)
wru64(shbuf.ptr, 48u64, 8u64);
wru64(shbuf.ptr, 56u64, SYM_SZ);
os.writefull(fd, shbuf.ptr, 64u64);
os.writeall(fd, shbuf.ptr, 64u64);
// .strtab
sn = 0;
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
@@ -278,7 +278,7 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
wru64(shbuf.ptr, 24u64, offstr);
wru64(shbuf.ptr, 32u64, str_.n);
wru64(shbuf.ptr, 48u64, 1u64);
os.writefull(fd, shbuf.ptr, 64u64);
os.writeall(fd, shbuf.ptr, 64u64);
// .shstrtab
sn = 0;
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
@@ -287,7 +287,7 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
wru64(shbuf.ptr, 24u64, offshstr);
wru64(shbuf.ptr, 32u64, shstr.n);
wru64(shbuf.ptr, 48u64, 1u64);
os.writefull(fd, shbuf.ptr, 64u64);
os.writeall(fd, shbuf.ptr, 64u64);
return 0;
};

View File

@@ -120,9 +120,11 @@ export fn filesize(fd: i32) i64 = {
return end;
};
// readfull — keep reading until `n` bytes have arrived or the fd
// readall — keep reading until `n` bytes have arrived or the fd
// closes early. Returns bytes read (0..=n) or -1 on read error.
export fn readfull(fd: i32, buf: *u8, n: u64) i64 = {
// Hare name (io::readall); the buffer is caller-supplied, matching
// the Plan 9 subset convention.
export fn readall(fd: i32, buf: *u8, n: u64) i64 = {
let got: u64 = 0u64;
for (got < n) {
let r: i64 = read(fd, buf + got, n - got);
@@ -133,9 +135,10 @@ export fn readfull(fd: i32, buf: *u8, n: u64) i64 = {
return got: i64;
};
// writefull — keep writing until `n` bytes have been accepted or the
// fd refuses progress. Returns bytes written or -1.
export fn writefull(fd: i32, buf: *u8, n: u64) i64 = {
// writeall — keep writing until `n` bytes have been accepted or the
// fd refuses progress. Returns bytes written or -1. Hare name
// (io::writeall).
export fn writeall(fd: i32, buf: *u8, n: u64) i64 = {
let sent: u64 = 0u64;
for (sent < n) {
let r: i64 = write(fd, buf + sent, n - sent);
@@ -154,8 +157,8 @@ export fn access(path: *u8, mode: i32) i32 = {
return syscall2(SYS_ACCESS, path: i64, mode: i64): i32;
};
// unlink(2).
export fn unlink(path: *u8) i32 = {
// remove — unlink(2). Hare name; the underlying syscall is unlink(2).
export fn remove(path: *u8) i32 = {
return syscall1(SYS_UNLINK, path: i64): i32;
};
@@ -321,14 +324,16 @@ export fn freearena(a: *arena) void = {
// buffer. Two error idioms ship side by side:
// - Plan 9 style (atoi64): tuple `(value, ok)`. Pre-dates the
// tagged-union work; kept for callers that already use it.
// - Hare style (parse64/parseu64): `(value | str)`. The error
// - Hare style (stoi64/stou64): `(value | str)`. The error
// variant carries a short, allocation-free message describing
// why the parse failed. Prefer this for new code.
// u64toa — write `v` in decimal into `buf` and return the byte count.
// Unsigned-only so callers don't have to think about wraparound when
// printing a u64 that happens to have the high bit set.
export fn u64toa(buf: []u8, v: u64) i32 = {
// u64tos — write `v` in decimal into `buf` and return the byte count.
// Hare name; the buffer-in shape is the sanctioned Plan 9 subset of
// Hare's `u64tos(u, base) const str`. Unsigned-only so callers don't
// have to think about wraparound when printing a u64 with the high
// bit set.
export fn u64tos(buf: []u8, v: u64) i32 = {
let tmp: [32]u8;
let i: i32 = 0;
let n: u64 = v;
@@ -350,7 +355,7 @@ export fn u64toa(buf: []u8, v: u64) i32 = {
return out;
};
export fn i64toa(buf: []u8, v: i64) i32 = {
export fn i64tos(buf: []u8, v: i64) i32 = {
let neg: bool = false;
let n: i64 = v;
if (n < 0) {
@@ -400,11 +405,12 @@ export fn atoi64(s: str) (i64, bool) = {
return v, true;
};
// parse64 — Hare-style fallible signed decimal parser. The value
// variant is i64; the error variant is a short str describing the
// reason. No locale, no whitespace, no underscores: a leading '-' is
// the only non-digit accepted, and only at position 0.
export fn parse64(s: str) (i64 | str) = {
// stoi64 — Hare-style fallible signed decimal parser. The value
// variant is i64; the error variant is a short str (subset of Hare's
// (invalid | overflow) tagged-union). No locale, no whitespace, no
// underscores: a leading '-' is the only non-digit accepted, and only
// at position 0.
export fn stoi64(s: str) (i64 | str) = {
if (s.len == 0) { return "parse: empty"; };
let i: i32 = 0;
let neg: bool = false;
@@ -422,8 +428,9 @@ export fn parse64(s: str) (i64 | str) = {
return v;
};
// parseu64 — fallible unsigned decimal parser. No leading sign.
export fn parseu64(s: str) (u64 | str) = {
// stou64 — fallible unsigned decimal parser. No leading sign. Mirrors
// Hare's strconv::stou64.
export fn stou64(s: str) (u64 | str) = {
if (s.len == 0) { return "parse: empty"; };
let v: u64 = 0u64;
let i: i32 = 0;
@@ -812,10 +819,10 @@ export fn tokprint(fd: i32, t: *tok) void = {
};
fputcbyte(fd, 58u8); // ':'
let buf: [32]u8;
let n: i32 = strconv.i64toa(buf[0:32], t.line: i64);
let n: i32 = strconv.i64tos(buf[0:32], t.line: i64);
os.write(fd, buf.ptr, n: u64);
fputcbyte(fd, 58u8);
n = strconv.i64toa(buf[0:32], t.col: i64);
n = strconv.i64tos(buf[0:32], t.col: i64);
os.write(fd, buf.ptr, n: u64);
fputcbyte(fd, 32u8); // ' '
fputsstr(fd, tokname(t.kind));
@@ -831,11 +838,11 @@ export fn tokprint(fd: i32, t: *tok) void = {
fputq(fd, ttext.ptr, ttext.len);
} else { if (t.kind == TK_INT) {
fputcbyte(fd, 32u8);
n = strconv.u64toa(buf[0:32], t.uval);
n = strconv.u64tos(buf[0:32], t.uval);
os.write(fd, buf.ptr, n: u64);
} else { if (t.kind == TK_RUNE) {
fputcbyte(fd, 32u8);
n = strconv.u64toa(buf[0:32], t.uval);
n = strconv.u64tos(buf[0:32], t.uval);
os.write(fd, buf.ptr, n: u64);
};};};};};
// TK_FLOAT is intentionally not handled here — %g formatting
@@ -846,96 +853,96 @@ export fn tokprint(fd: i32, t: *tok) void = {
};
// MODULE: ascii
// ascii — byte-class predicates and case folding for the ASCII range.
// Matches Hare's ascii::isdigit family. Bytes outside 0..127 always
// answer `false`. The lexer hot path uses these inline; they are
// expected to inline to a couple of compares.
// ascii — rune-class predicates and case folding for the ASCII range.
// Matches Hare's ascii::isdigit family (rune-taking signature). Runes
// outside 0..127 always answer `false`. The lexer hot path uses these
// inline; they are expected to inline to a couple of compares.
export fn isdigit(c: u8) bool = {
if (c < 48u8) { return false; };
if (c > 57u8) { return false; };
export fn isdigit(c: rune) bool = {
if (c < 48) { return false; };
if (c > 57) { return false; };
return true;
};
export fn isupper(c: u8) bool = {
if (c < 65u8) { return false; };
if (c > 90u8) { return false; };
export fn isupper(c: rune) bool = {
if (c < 65) { return false; };
if (c > 90) { return false; };
return true;
};
export fn islower(c: u8) bool = {
if (c < 97u8) { return false; };
if (c > 122u8) { return false; };
export fn islower(c: rune) bool = {
if (c < 97) { return false; };
if (c > 122) { return false; };
return true;
};
export fn isalpha(c: u8) bool = {
export fn isalpha(c: rune) bool = {
if (isupper(c)) { return true; };
return islower(c);
};
export fn isalnum(c: u8) bool = {
export fn isalnum(c: rune) bool = {
if (isalpha(c)) { return true; };
return isdigit(c);
};
// isspace — the C/Hare set: space, tab, NL, VT, FF, CR.
export fn isspace(c: u8) bool = {
if (c == 32u8) { return true; }; // ' '
if (c == 9u8) { return true; }; // '\t'
if (c == 10u8) { return true; }; // '\n'
if (c == 11u8) { return true; }; // '\v'
if (c == 12u8) { return true; }; // '\f'
if (c == 13u8) { return true; }; // '\r'
export fn isspace(c: rune) bool = {
if (c == 32) { return true; }; // ' '
if (c == 9) { return true; }; // '\t'
if (c == 10) { return true; }; // '\n'
if (c == 11) { return true; }; // '\v'
if (c == 12) { return true; }; // '\f'
if (c == 13) { return true; }; // '\r'
return false;
};
export fn ishex(c: u8) bool = {
export fn isxdigit(c: rune) bool = {
if (isdigit(c)) { return true; };
if (c >= 65u8) {
if (c <= 70u8) { return true; }; // 'A'..'F'
if (c >= 65) {
if (c <= 70) { return true; }; // 'A'..'F'
};
if (c >= 97u8) {
if (c <= 102u8) { return true; }; // 'a'..'f'
if (c >= 97) {
if (c <= 102) { return true; }; // 'a'..'f'
};
return false;
};
// digitval — value of `c` as a hex/decimal digit, or -1 if not one.
// Useful when scanning numeric literals.
export fn digitval(c: u8) i32 = {
if (isdigit(c)) { return (c - 48u8): i32; };
if (c >= 65u8) {
if (c <= 70u8) { return ((c - 65u8) + 10u8): i32; };
export fn digitval(c: rune) i32 = {
if (isdigit(c)) { return (c - 48): i32; };
if (c >= 65) {
if (c <= 70) { return ((c - 65) + 10): i32; };
};
if (c >= 97u8) {
if (c <= 102u8) { return ((c - 97u8) + 10u8): i32; };
if (c >= 97) {
if (c <= 102) { return ((c - 97) + 10): i32; };
};
return -1;
};
// isidstart / isidpart — identifier classes used by the lexer.
// Alpha or '_' starts; alnum or '_' continues.
export fn isidstart(c: u8) bool = {
export fn isidstart(c: rune) bool = {
if (isalpha(c)) { return true; };
if (c == 95u8) { return true; }; // '_'
if (c == 95) { return true; }; // '_'
return false;
};
export fn isidpart(c: u8) bool = {
export fn isidpart(c: rune) bool = {
if (isalnum(c)) { return true; };
if (c == 95u8) { return true; };
if (c == 95) { return true; };
return false;
};
// tolower / toupper — fold ASCII case. Non-letters pass through.
export fn tolower(c: u8) u8 = {
if (isupper(c)) { return c + 32u8; };
export fn tolower(c: rune) rune = {
if (isupper(c)) { return c + 32; };
return c;
};
export fn toupper(c: u8) u8 = {
if (islower(c)) { return c - 32u8; };
export fn toupper(c: rune) rune = {
if (islower(c)) { return c - 32; };
return c;
};
@@ -1172,18 +1179,18 @@ fn escape(l: *lex, out: *i32) bool = {
let lo: i32 = lget(l);
if (hi < 0) { return false; };
if (lo < 0) { return false; };
if (!ascii.ishex(hi: u8)) {
if (!ascii.isxdigit(hi: rune)) {
let cp: pos; curpos(l, &cp);
errat(l, &cp, "bad \\x escape");
return false;
};
if (!ascii.ishex(lo: u8)) {
if (!ascii.isxdigit(lo: rune)) {
let cp: pos; curpos(l, &cp);
errat(l, &cp, "bad \\x escape");
return false;
};
let h: i32 = ascii.digitval(hi: u8);
let lv: i32 = ascii.digitval(lo: u8);
let h: i32 = ascii.digitval(hi: rune);
let lv: i32 = ascii.digitval(lo: rune);
*out = (h << 4) | lv;
return true;
};
@@ -1197,7 +1204,7 @@ fn scandecimalrun(l: *lex) void = {
for (true) {
let c: i32 = lpeek(l, 0u64);
if (c < 0) { break; };
if (!ascii.isdigit(c: u8)) {
if (!ascii.isdigit(c: rune)) {
if (c != 95) { break; };
};
lget(l);
@@ -1208,7 +1215,7 @@ fn scanhexrun(l: *lex) void = {
for (true) {
let c: i32 = lpeek(l, 0u64);
if (c < 0) { break; };
if (!ascii.ishex(c: u8)) {
if (!ascii.isxdigit(c: rune)) {
if (c != 95) { break; };
};
lget(l);
@@ -1247,7 +1254,7 @@ fn scanexp(l: *lex) void = {
for (true) {
let c: i32 = lpeek(l, 0u64);
if (c < 0) { break; };
if (!ascii.isdigit(c: u8)) { break; };
if (!ascii.isdigit(c: rune)) { break; };
lget(l);
};
};
@@ -1332,12 +1339,12 @@ fn lexnum(l: *lex, start: *pos, out: *tok) void = {
let pc: i32 = lpeek(l, 0u64);
if (pc >= 0) {
if (ascii.isidstart(pc: u8)) {
if (ascii.isidstart(pc: rune)) {
let sb: u64 = l.lpos;
for (true) {
let cc: i32 = lpeek(l, 0u64);
if (cc < 0) { break; };
if (!ascii.isidpart(cc: u8)) { break; };
if (!ascii.isidpart(cc: rune)) { break; };
lget(l);
};
let sl: u64 = l.lpos - sb;
@@ -1381,7 +1388,7 @@ fn lexident(l: *lex, start: *pos, out: *tok) void = {
for (true) {
let c: i32 = lpeek(l, 0u64);
if (c < 0) { break; };
if (!ascii.isidpart(c: u8)) { break; };
if (!ascii.isidpart(c: rune)) { break; };
lget(l);
};
let n: u64 = l.lpos - begin;
@@ -1530,8 +1537,8 @@ export fn lexnext(l: *lex, out: *tok) void = {
let c: i32 = lpeek(l, 0u64);
if (c >= 0) {
if (ascii.isidstart(c: u8)) { lexident(l, &start, out); return; };
if (ascii.isdigit(c: u8)) { lexnum(l, &start, out); return; };
if (ascii.isidstart(c: rune)) { lexident(l, &start, out); return; };
if (ascii.isdigit(c: rune)) { lexnum(l, &start, out); return; };
};
if (c == 34) { lget(l); lexstr(l, &start, out); return; };
@@ -1895,12 +1902,12 @@ fn pr(fd: i32, n: *node, d: i32) void = {
if (n.kind == N_INTLIT) {
putc1(fd, 32u8);
let buf: [32]u8;
let m: i32 = strconv.u64toa(buf[0:32], n.uval);
let m: i32 = strconv.u64tos(buf[0:32], n.uval);
os.write(fd, buf.ptr, m: u64);
} else { if (n.kind == N_RUNELIT) {
putc1(fd, 32u8);
let buf: [32]u8;
let m: i32 = strconv.u64toa(buf[0:32], n.uval);
let m: i32 = strconv.u64tos(buf[0:32], n.uval);
os.write(fd, buf.ptr, m: u64);
} else { if (
n.kind == N_STRLIT ||
@@ -7408,13 +7415,13 @@ fn emitline(s: str) void = { os.write(1, s.ptr, s.len: u64); };
fn emitint(v: i64) void = {
let buf: [32]u8;
let n: i32 = strconv.i64toa(buf[0:32], v);
let n: i32 = strconv.i64tos(buf[0:32], v);
os.write(1, buf.ptr, n: u64);
};
fn emituint(v: u64) void = {
let buf: [32]u8;
let n: i32 = strconv.u64toa(buf[0:32], v);
let n: i32 = strconv.u64tos(buf[0:32], v);
os.write(1, buf.ptr, n: u64);
};
@@ -7452,7 +7459,7 @@ fn mklabel(c: *cgen, base: str) str = {
i += 1; j += 1;
};
buf[i] = 95u8; i += 1; // '_'
let n: i32 = strconv.i64toa(buf[i:128], c.labelseq: i64);
let n: i32 = strconv.i64tos(buf[i:128], c.labelseq: i64);
c.labelseq += 1;
let total: i32 = i + n;
let p: *u8 = amalloc(c.a, (total: u64) + 1u64): *u8;
@@ -7491,7 +7498,7 @@ fn internstrlit(c: *cgen, bytes: str) str = {
// New label "_S_<seq>".
let buf: [32]u8;
buf[0] = 95u8; buf[1] = 83u8; buf[2] = 95u8; // "_S_"
let n: i32 = strconv.i64toa(buf[3:32], c.strlitseq: i64);
let n: i32 = strconv.i64tos(buf[3:32], c.strlitseq: i64);
c.strlitseq += 1;
let total: i32 = 3 + n;
let p: *u8 = amalloc(c.a, (total: u64) + 1u64): *u8;
@@ -7930,7 +7937,7 @@ fn slurp(path: *u8) (*u8, u64) = {
if (n < 0i64) { os.close(fd); return nil, 0u64; };
let nz: u64 = n: u64;
let buf: *u8 = os.alloc(nz + 1u64): *u8;
let got: i64 = os.readfull(fd, buf, nz);
let got: i64 = os.readall(fd, buf, nz);
os.close(fd);
if (got != n) { return nil, 0u64; };
buf[nz] = 0u8;

View File

@@ -46,7 +46,7 @@ fn slurp(path: *u8) (*u8, u64) = {
if (n < 0i64) { os.close(fd); return nil, 0u64; };
let nz: u64 = n: u64;
let buf: *u8 = os.alloc(nz + 1u64): *u8;
let got: i64 = os.readfull(fd, buf, nz);
let got: i64 = os.readall(fd, buf, nz);
os.close(fd);
if (got != n) { return nil, 0u64; };
buf[nz] = 0u8;

View File

@@ -123,13 +123,13 @@ fn dbasename(p: *u8) *u8 = {
// ---- file slurp --------------------------------------------------------
fn readallso(path: *u8) (*u8, u64) = {
fn slurpso(path: *u8) (*u8, u64) = {
let fd: i32 = os.open(path, os.O_RDONLY, 0i32);
if (fd < 0) { return nil, 0u64; };
let n: i64 = os.filesize(fd);
if (n < 0i64) { os.close(fd); return nil, 0u64; };
let buf: *u8 = os.alloc(n: u64): *u8;
let got: i64 = os.readfull(fd, buf, n: u64);
let got: i64 = os.readall(fd, buf, n: u64);
os.close(fd);
if (got != n) { return nil, 0u64; };
return buf, n: u64;
@@ -165,7 +165,7 @@ fn vdnameat(buf: *u8, verdefoff: u64, verdefsize: u64,
export fn loadso(l: *lnk, path: *u8) i32 = {
let buf: *u8;
let blen: u64;
buf, blen = readallso(path);
buf, blen = slurpso(path);
if (buf == nil) {
os.write(2, "w6l: cannot read .so\n".ptr, 20u64);
return -1;

View File

@@ -720,7 +720,7 @@ export fn emitdynelf(l: *lnk, fd: i32, base: u64, entry: u64) i32 = {
dbcopy(filebuf, gotpltoff, gotpltbuf, gotpltsz);
dbcopy(filebuf, dynamicoff, dynamicbuf, dynamicsz);
let wrote: i64 = os.writefull(fd, filebuf, fileend);
let wrote: i64 = os.writeall(fd, filebuf, fileend);
if (wrote != fileend: i64) {
return 1;
};

View File

@@ -120,9 +120,11 @@ export fn filesize(fd: i32) i64 = {
return end;
};
// readfull — keep reading until `n` bytes have arrived or the fd
// readall — keep reading until `n` bytes have arrived or the fd
// closes early. Returns bytes read (0..=n) or -1 on read error.
export fn readfull(fd: i32, buf: *u8, n: u64) i64 = {
// Hare name (io::readall); the buffer is caller-supplied, matching
// the Plan 9 subset convention.
export fn readall(fd: i32, buf: *u8, n: u64) i64 = {
let got: u64 = 0u64;
for (got < n) {
let r: i64 = read(fd, buf + got, n - got);
@@ -133,9 +135,10 @@ export fn readfull(fd: i32, buf: *u8, n: u64) i64 = {
return got: i64;
};
// writefull — keep writing until `n` bytes have been accepted or the
// fd refuses progress. Returns bytes written or -1.
export fn writefull(fd: i32, buf: *u8, n: u64) i64 = {
// writeall — keep writing until `n` bytes have been accepted or the
// fd refuses progress. Returns bytes written or -1. Hare name
// (io::writeall).
export fn writeall(fd: i32, buf: *u8, n: u64) i64 = {
let sent: u64 = 0u64;
for (sent < n) {
let r: i64 = write(fd, buf + sent, n - sent);
@@ -154,8 +157,8 @@ export fn access(path: *u8, mode: i32) i32 = {
return syscall2(SYS_ACCESS, path: i64, mode: i64): i32;
};
// unlink(2).
export fn unlink(path: *u8) i32 = {
// remove — unlink(2). Hare name; the underlying syscall is unlink(2).
export fn remove(path: *u8) i32 = {
return syscall1(SYS_UNLINK, path: i64): i32;
};
@@ -497,13 +500,13 @@ def RELA_ADDEND: u64 = 16u64;
// ---- file slurp --------------------------------------------------------
fn readall(path: *u8) (*u8, u64) = {
fn slurp(path: *u8) (*u8, u64) = {
let fd: i32 = os.open(path, os.O_RDONLY, 0i32);
if (fd < 0) { return nil, 0u64; };
let n: i64 = os.filesize(fd);
if (n < 0i64) { os.close(fd); return nil, 0u64; };
let buf: *u8 = os.alloc(n: u64): *u8;
let got: i64 = os.readfull(fd, buf, n: u64);
let got: i64 = os.readall(fd, buf, n: u64);
os.close(fd);
if (got != n) { return nil, 0u64; };
return buf, n: u64;
@@ -756,7 +759,7 @@ fn loadarchive(l: *lnk, path: *u8, buf: *u8, len: u64) i32 = {
export fn load(l: *lnk, path: *u8) i32 = {
let bufp: *u8;
let buflen: u64;
bufp, buflen = readall(path);
bufp, buflen = slurp(path);
if (bufp == nil) {
os.write(2, "w6l: cannot read object\n".ptr, 23u64);
return -1;
@@ -1033,13 +1036,13 @@ fn dbasename(p: *u8) *u8 = {
// ---- file slurp --------------------------------------------------------
fn readallso(path: *u8) (*u8, u64) = {
fn slurpso(path: *u8) (*u8, u64) = {
let fd: i32 = os.open(path, os.O_RDONLY, 0i32);
if (fd < 0) { return nil, 0u64; };
let n: i64 = os.filesize(fd);
if (n < 0i64) { os.close(fd); return nil, 0u64; };
let buf: *u8 = os.alloc(n: u64): *u8;
let got: i64 = os.readfull(fd, buf, n: u64);
let got: i64 = os.readall(fd, buf, n: u64);
os.close(fd);
if (got != n) { return nil, 0u64; };
return buf, n: u64;
@@ -1075,7 +1078,7 @@ fn vdnameat(buf: *u8, verdefoff: u64, verdefsize: u64,
export fn loadso(l: *lnk, path: *u8) i32 = {
let buf: *u8;
let blen: u64;
buf, blen = readallso(path);
buf, blen = slurpso(path);
if (buf == nil) {
os.write(2, "w6l: cannot read .so\n".ptr, 20u64);
return -1;
@@ -2146,7 +2149,7 @@ export fn emitdynelf(l: *lnk, fd: i32, base: u64, entry: u64) i32 = {
dbcopy(filebuf, gotpltoff, gotpltbuf, gotpltsz);
dbcopy(filebuf, dynamicoff, dynamicbuf, dynamicsz);
let wrote: i64 = os.writefull(fd, filebuf, fileend);
let wrote: i64 = os.writeall(fd, filebuf, fileend);
if (wrote != fileend: i64) {
return 1;
};
@@ -2246,10 +2249,10 @@ export fn emitelf(l: *lnk, fd: i32, base: u64, entry: u64) i32 = {
wru64(hdr, 112u64, TEXT_OFF); // p_align
// Write [0..0x1000) then .text.
let n1: i64 = os.writefull(fd, hdr, TEXT_OFF);
let n1: i64 = os.writeall(fd, hdr, TEXT_OFF);
if (n1 != TEXT_OFF: i64) { return -1; };
if (l.textlen > 0u64) {
let n2: i64 = os.writefull(fd, l.text, l.textlen);
let n2: i64 = os.writeall(fd, l.text, l.textlen);
if (n2 != l.textlen: i64) { return -1; };
};
return 0;

View File

@@ -75,13 +75,13 @@ def RELA_ADDEND: u64 = 16u64;
// ---- file slurp --------------------------------------------------------
fn readall(path: *u8) (*u8, u64) = {
fn slurp(path: *u8) (*u8, u64) = {
let fd: i32 = os.open(path, os.O_RDONLY, 0i32);
if (fd < 0) { return nil, 0u64; };
let n: i64 = os.filesize(fd);
if (n < 0i64) { os.close(fd); return nil, 0u64; };
let buf: *u8 = os.alloc(n: u64): *u8;
let got: i64 = os.readfull(fd, buf, n: u64);
let got: i64 = os.readall(fd, buf, n: u64);
os.close(fd);
if (got != n) { return nil, 0u64; };
return buf, n: u64;
@@ -334,7 +334,7 @@ fn loadarchive(l: *lnk, path: *u8, buf: *u8, len: u64) i32 = {
export fn load(l: *lnk, path: *u8) i32 = {
let bufp: *u8;
let buflen: u64;
bufp, buflen = readall(path);
bufp, buflen = slurp(path);
if (bufp == nil) {
os.write(2, "w6l: cannot read object\n".ptr, 23u64);
return -1;

View File

@@ -90,10 +90,10 @@ export fn emitelf(l: *lnk, fd: i32, base: u64, entry: u64) i32 = {
wru64(hdr, 112u64, TEXT_OFF); // p_align
// Write [0..0x1000) then .text.
let n1: i64 = os.writefull(fd, hdr, TEXT_OFF);
let n1: i64 = os.writeall(fd, hdr, TEXT_OFF);
if (n1 != TEXT_OFF: i64) { return -1; };
if (l.textlen > 0u64) {
let n2: i64 = os.writefull(fd, l.text, l.textlen);
let n2: i64 = os.writeall(fd, l.text, l.textlen);
if (n2 != l.textlen: i64) { return -1; };
};
return 0;

View File

@@ -279,13 +279,13 @@ fn emitline(s: str) void = { os.write(1, s.ptr, s.len: u64); };
fn emitint(v: i64) void = {
let buf: [32]u8;
let n: i32 = strconv.i64toa(buf[0:32], v);
let n: i32 = strconv.i64tos(buf[0:32], v);
os.write(1, buf.ptr, n: u64);
};
fn emituint(v: u64) void = {
let buf: [32]u8;
let n: i32 = strconv.u64toa(buf[0:32], v);
let n: i32 = strconv.u64tos(buf[0:32], v);
os.write(1, buf.ptr, n: u64);
};
@@ -323,7 +323,7 @@ fn mklabel(c: *cgen, base: str) str = {
i += 1; j += 1;
};
buf[i] = 95u8; i += 1; // '_'
let n: i32 = strconv.i64toa(buf[i:128], c.labelseq: i64);
let n: i32 = strconv.i64tos(buf[i:128], c.labelseq: i64);
c.labelseq += 1;
let total: i32 = i + n;
let p: *u8 = amalloc(c.a, (total: u64) + 1u64): *u8;
@@ -362,7 +362,7 @@ fn internstrlit(c: *cgen, bytes: str) str = {
// New label "_S_<seq>".
let buf: [32]u8;
buf[0] = 95u8; buf[1] = 83u8; buf[2] = 95u8; // "_S_"
let n: i32 = strconv.i64toa(buf[3:32], c.strlitseq: i64);
let n: i32 = strconv.i64tos(buf[3:32], c.strlitseq: i64);
c.strlitseq += 1;
let total: i32 = 3 + n;
let p: *u8 = amalloc(c.a, (total: u64) + 1u64): *u8;

View File

@@ -16,7 +16,7 @@ let nerrors: i32 = 0;
let nwarnings: i32 = 0;
export fn fatal(msg: str) void = {
fmt.errln(msg);
fmt.errorln(msg);
os.exit(1);
};

View File

@@ -120,9 +120,11 @@ export fn filesize(fd: i32) i64 = {
return end;
};
// readfull — keep reading until `n` bytes have arrived or the fd
// readall — keep reading until `n` bytes have arrived or the fd
// closes early. Returns bytes read (0..=n) or -1 on read error.
export fn readfull(fd: i32, buf: *u8, n: u64) i64 = {
// Hare name (io::readall); the buffer is caller-supplied, matching
// the Plan 9 subset convention.
export fn readall(fd: i32, buf: *u8, n: u64) i64 = {
let got: u64 = 0u64;
for (got < n) {
let r: i64 = read(fd, buf + got, n - got);
@@ -133,9 +135,10 @@ export fn readfull(fd: i32, buf: *u8, n: u64) i64 = {
return got: i64;
};
// writefull — keep writing until `n` bytes have been accepted or the
// fd refuses progress. Returns bytes written or -1.
export fn writefull(fd: i32, buf: *u8, n: u64) i64 = {
// writeall — keep writing until `n` bytes have been accepted or the
// fd refuses progress. Returns bytes written or -1. Hare name
// (io::writeall).
export fn writeall(fd: i32, buf: *u8, n: u64) i64 = {
let sent: u64 = 0u64;
for (sent < n) {
let r: i64 = write(fd, buf + sent, n - sent);
@@ -154,8 +157,8 @@ export fn access(path: *u8, mode: i32) i32 = {
return syscall2(SYS_ACCESS, path: i64, mode: i64): i32;
};
// unlink(2).
export fn unlink(path: *u8) i32 = {
// remove — unlink(2). Hare name; the underlying syscall is unlink(2).
export fn remove(path: *u8) i32 = {
return syscall1(SYS_UNLINK, path: i64): i32;
};
@@ -599,14 +602,14 @@ fn locateimport(a: *arena, dirs: *u8, name: *u8, namelen: u64) *u8 = {
// ---- file slurp -------------------------------------------------------
fn readall(pathcs: *u8) (*u8, u64) = {
fn slurp(pathcs: *u8) (*u8, u64) = {
let fd: i32 = os.open(pathcs, os.O_RDONLY, 0i32);
if (fd < 0) { return nil, 0u64; };
let n: i64 = os.filesize(fd);
if (n < 0i64) { os.close(fd); return nil, 0u64; };
let nu: u64 = n: u64;
let buf: *u8 = os.alloc(nu + 1u64): *u8;
let got: i64 = os.readfull(fd, buf, nu);
let got: i64 = os.readall(fd, buf, nu);
os.close(fd);
if (got != n) { return nil, 0u64; };
buf[nu] = 0u8;
@@ -707,7 +710,7 @@ fn expand(c: *expctx, pathcs: *u8) void = {
let bufp: *u8;
let blen: u64;
bufp, blen = readall(pathcs);
bufp, blen = slurp(pathcs);
if (bufp == nil) {
os.write(2, "ww: cannot read source\n".ptr, 23u64);
return;
@@ -743,12 +746,12 @@ fn expand(c: *expctx, pathcs: *u8) void = {
let mn: u64;
mp, mn = modulename(pathcs, plen);
if (mn > 0u64) {
os.writefull(c.out, "// MODULE: ".ptr, 11u64);
os.writefull(c.out, mp, mn);
os.writefull(c.out, "\n".ptr, 1u64);
os.writeall(c.out, "// MODULE: ".ptr, 11u64);
os.writeall(c.out, mp, mn);
os.writeall(c.out, "\n".ptr, 1u64);
};
os.writefull(c.out, bufp, blen);
os.writefull(c.out, "\n".ptr, 1u64);
os.writeall(c.out, bufp, blen);
os.writeall(c.out, "\n".ptr, 1u64);
};
// ---- Build pipeline ---------------------------------------------------
@@ -1342,7 +1345,7 @@ fn dorun(selfdir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
lf.libs = libs;
lf.nlibs = nlibs;
if (buildone(selfdir, resolved, tmp, incs, &lf) != 0) {
os.unlink(tmp);
os.remove(tmp);
return 1;
};
@@ -1359,7 +1362,7 @@ fn dorun(selfdir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
};
execargv[nextra + 1] = nil;
let rc: i32 = procrun(tmp, execargv);
os.unlink(tmp);
os.remove(tmp);
return rc;
};
@@ -1374,14 +1377,14 @@ fn runsingletest(selfdir: *u8, src: *u8) i32 = {
let tmp: *u8 = os.alloc(PATH_MAX): *u8;
makeruntmp(tmp);
if (buildone(selfdir, src, tmp, "\0".ptr, nil) != 0) {
os.unlink(tmp);
os.remove(tmp);
return 1;
};
let execargv: **u8 = os.alloc(16u64): **u8;
execargv[0] = tmp;
execargv[1] = nil;
let rc: i32 = procrun(tmp, execargv);
os.unlink(tmp);
os.remove(tmp);
return rc;
};
@@ -1446,7 +1449,7 @@ fn rundirtests(selfdir: *u8, dir: *u8) i32 = {
os.write(2, "\n".ptr, 1u64);
};
};
os.unlink(tmp);
os.remove(tmp);
};
off += reclen;
};

View File

@@ -280,14 +280,14 @@ fn locateimport(a: *arena, dirs: *u8, name: *u8, namelen: u64) *u8 = {
// ---- file slurp -------------------------------------------------------
fn readall(pathcs: *u8) (*u8, u64) = {
fn slurp(pathcs: *u8) (*u8, u64) = {
let fd: i32 = os.open(pathcs, os.O_RDONLY, 0i32);
if (fd < 0) { return nil, 0u64; };
let n: i64 = os.filesize(fd);
if (n < 0i64) { os.close(fd); return nil, 0u64; };
let nu: u64 = n: u64;
let buf: *u8 = os.alloc(nu + 1u64): *u8;
let got: i64 = os.readfull(fd, buf, nu);
let got: i64 = os.readall(fd, buf, nu);
os.close(fd);
if (got != n) { return nil, 0u64; };
buf[nu] = 0u8;
@@ -388,7 +388,7 @@ fn expand(c: *expctx, pathcs: *u8) void = {
let bufp: *u8;
let blen: u64;
bufp, blen = readall(pathcs);
bufp, blen = slurp(pathcs);
if (bufp == nil) {
os.write(2, "ww: cannot read source\n".ptr, 23u64);
return;
@@ -424,12 +424,12 @@ fn expand(c: *expctx, pathcs: *u8) void = {
let mn: u64;
mp, mn = modulename(pathcs, plen);
if (mn > 0u64) {
os.writefull(c.out, "// MODULE: ".ptr, 11u64);
os.writefull(c.out, mp, mn);
os.writefull(c.out, "\n".ptr, 1u64);
os.writeall(c.out, "// MODULE: ".ptr, 11u64);
os.writeall(c.out, mp, mn);
os.writeall(c.out, "\n".ptr, 1u64);
};
os.writefull(c.out, bufp, blen);
os.writefull(c.out, "\n".ptr, 1u64);
os.writeall(c.out, bufp, blen);
os.writeall(c.out, "\n".ptr, 1u64);
};
// ---- Build pipeline ---------------------------------------------------
@@ -1023,7 +1023,7 @@ fn dorun(selfdir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
lf.libs = libs;
lf.nlibs = nlibs;
if (buildone(selfdir, resolved, tmp, incs, &lf) != 0) {
os.unlink(tmp);
os.remove(tmp);
return 1;
};
@@ -1040,7 +1040,7 @@ fn dorun(selfdir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
};
execargv[nextra + 1] = nil;
let rc: i32 = procrun(tmp, execargv);
os.unlink(tmp);
os.remove(tmp);
return rc;
};
@@ -1055,14 +1055,14 @@ fn runsingletest(selfdir: *u8, src: *u8) i32 = {
let tmp: *u8 = os.alloc(PATH_MAX): *u8;
makeruntmp(tmp);
if (buildone(selfdir, src, tmp, "\0".ptr, nil) != 0) {
os.unlink(tmp);
os.remove(tmp);
return 1;
};
let execargv: **u8 = os.alloc(16u64): **u8;
execargv[0] = tmp;
execargv[1] = nil;
let rc: i32 = procrun(tmp, execargv);
os.unlink(tmp);
os.remove(tmp);
return rc;
};
@@ -1127,7 +1127,7 @@ fn rundirtests(selfdir: *u8, dir: *u8) i32 = {
os.write(2, "\n".ptr, 1u64);
};
};
os.unlink(tmp);
os.remove(tmp);
};
off += reclen;
};

View File

@@ -120,9 +120,11 @@ export fn filesize(fd: i32) i64 = {
return end;
};
// readfull — keep reading until `n` bytes have arrived or the fd
// readall — keep reading until `n` bytes have arrived or the fd
// closes early. Returns bytes read (0..=n) or -1 on read error.
export fn readfull(fd: i32, buf: *u8, n: u64) i64 = {
// Hare name (io::readall); the buffer is caller-supplied, matching
// the Plan 9 subset convention.
export fn readall(fd: i32, buf: *u8, n: u64) i64 = {
let got: u64 = 0u64;
for (got < n) {
let r: i64 = read(fd, buf + got, n - got);
@@ -133,9 +135,10 @@ export fn readfull(fd: i32, buf: *u8, n: u64) i64 = {
return got: i64;
};
// writefull — keep writing until `n` bytes have been accepted or the
// fd refuses progress. Returns bytes written or -1.
export fn writefull(fd: i32, buf: *u8, n: u64) i64 = {
// writeall — keep writing until `n` bytes have been accepted or the
// fd refuses progress. Returns bytes written or -1. Hare name
// (io::writeall).
export fn writeall(fd: i32, buf: *u8, n: u64) i64 = {
let sent: u64 = 0u64;
for (sent < n) {
let r: i64 = write(fd, buf + sent, n - sent);
@@ -154,8 +157,8 @@ export fn access(path: *u8, mode: i32) i32 = {
return syscall2(SYS_ACCESS, path: i64, mode: i64): i32;
};
// unlink(2).
export fn unlink(path: *u8) i32 = {
// remove — unlink(2). Hare name; the underlying syscall is unlink(2).
export fn remove(path: *u8) i32 = {
return syscall1(SYS_UNLINK, path: i64): i32;
};
@@ -321,14 +324,16 @@ export fn freearena(a: *arena) void = {
// buffer. Two error idioms ship side by side:
// - Plan 9 style (atoi64): tuple `(value, ok)`. Pre-dates the
// tagged-union work; kept for callers that already use it.
// - Hare style (parse64/parseu64): `(value | str)`. The error
// - Hare style (stoi64/stou64): `(value | str)`. The error
// variant carries a short, allocation-free message describing
// why the parse failed. Prefer this for new code.
// u64toa — write `v` in decimal into `buf` and return the byte count.
// Unsigned-only so callers don't have to think about wraparound when
// printing a u64 that happens to have the high bit set.
export fn u64toa(buf: []u8, v: u64) i32 = {
// u64tos — write `v` in decimal into `buf` and return the byte count.
// Hare name; the buffer-in shape is the sanctioned Plan 9 subset of
// Hare's `u64tos(u, base) const str`. Unsigned-only so callers don't
// have to think about wraparound when printing a u64 with the high
// bit set.
export fn u64tos(buf: []u8, v: u64) i32 = {
let tmp: [32]u8;
let i: i32 = 0;
let n: u64 = v;
@@ -350,7 +355,7 @@ export fn u64toa(buf: []u8, v: u64) i32 = {
return out;
};
export fn i64toa(buf: []u8, v: i64) i32 = {
export fn i64tos(buf: []u8, v: i64) i32 = {
let neg: bool = false;
let n: i64 = v;
if (n < 0) {
@@ -400,11 +405,12 @@ export fn atoi64(s: str) (i64, bool) = {
return v, true;
};
// parse64 — Hare-style fallible signed decimal parser. The value
// variant is i64; the error variant is a short str describing the
// reason. No locale, no whitespace, no underscores: a leading '-' is
// the only non-digit accepted, and only at position 0.
export fn parse64(s: str) (i64 | str) = {
// stoi64 — Hare-style fallible signed decimal parser. The value
// variant is i64; the error variant is a short str (subset of Hare's
// (invalid | overflow) tagged-union). No locale, no whitespace, no
// underscores: a leading '-' is the only non-digit accepted, and only
// at position 0.
export fn stoi64(s: str) (i64 | str) = {
if (s.len == 0) { return "parse: empty"; };
let i: i32 = 0;
let neg: bool = false;
@@ -422,8 +428,9 @@ export fn parse64(s: str) (i64 | str) = {
return v;
};
// parseu64 — fallible unsigned decimal parser. No leading sign.
export fn parseu64(s: str) (u64 | str) = {
// stou64 — fallible unsigned decimal parser. No leading sign. Mirrors
// Hare's strconv::stou64.
export fn stou64(s: str) (u64 | str) = {
if (s.len == 0) { return "parse: empty"; };
let v: u64 = 0u64;
let i: i32 = 0;
@@ -812,10 +819,10 @@ export fn tokprint(fd: i32, t: *tok) void = {
};
fputcbyte(fd, 58u8); // ':'
let buf: [32]u8;
let n: i32 = strconv.i64toa(buf[0:32], t.line: i64);
let n: i32 = strconv.i64tos(buf[0:32], t.line: i64);
os.write(fd, buf.ptr, n: u64);
fputcbyte(fd, 58u8);
n = strconv.i64toa(buf[0:32], t.col: i64);
n = strconv.i64tos(buf[0:32], t.col: i64);
os.write(fd, buf.ptr, n: u64);
fputcbyte(fd, 32u8); // ' '
fputsstr(fd, tokname(t.kind));
@@ -831,11 +838,11 @@ export fn tokprint(fd: i32, t: *tok) void = {
fputq(fd, ttext.ptr, ttext.len);
} else { if (t.kind == TK_INT) {
fputcbyte(fd, 32u8);
n = strconv.u64toa(buf[0:32], t.uval);
n = strconv.u64tos(buf[0:32], t.uval);
os.write(fd, buf.ptr, n: u64);
} else { if (t.kind == TK_RUNE) {
fputcbyte(fd, 32u8);
n = strconv.u64toa(buf[0:32], t.uval);
n = strconv.u64tos(buf[0:32], t.uval);
os.write(fd, buf.ptr, n: u64);
};};};};};
// TK_FLOAT is intentionally not handled here — %g formatting
@@ -846,96 +853,96 @@ export fn tokprint(fd: i32, t: *tok) void = {
};
// MODULE: ascii
// ascii — byte-class predicates and case folding for the ASCII range.
// Matches Hare's ascii::isdigit family. Bytes outside 0..127 always
// answer `false`. The lexer hot path uses these inline; they are
// expected to inline to a couple of compares.
// ascii — rune-class predicates and case folding for the ASCII range.
// Matches Hare's ascii::isdigit family (rune-taking signature). Runes
// outside 0..127 always answer `false`. The lexer hot path uses these
// inline; they are expected to inline to a couple of compares.
export fn isdigit(c: u8) bool = {
if (c < 48u8) { return false; };
if (c > 57u8) { return false; };
export fn isdigit(c: rune) bool = {
if (c < 48) { return false; };
if (c > 57) { return false; };
return true;
};
export fn isupper(c: u8) bool = {
if (c < 65u8) { return false; };
if (c > 90u8) { return false; };
export fn isupper(c: rune) bool = {
if (c < 65) { return false; };
if (c > 90) { return false; };
return true;
};
export fn islower(c: u8) bool = {
if (c < 97u8) { return false; };
if (c > 122u8) { return false; };
export fn islower(c: rune) bool = {
if (c < 97) { return false; };
if (c > 122) { return false; };
return true;
};
export fn isalpha(c: u8) bool = {
export fn isalpha(c: rune) bool = {
if (isupper(c)) { return true; };
return islower(c);
};
export fn isalnum(c: u8) bool = {
export fn isalnum(c: rune) bool = {
if (isalpha(c)) { return true; };
return isdigit(c);
};
// isspace — the C/Hare set: space, tab, NL, VT, FF, CR.
export fn isspace(c: u8) bool = {
if (c == 32u8) { return true; }; // ' '
if (c == 9u8) { return true; }; // '\t'
if (c == 10u8) { return true; }; // '\n'
if (c == 11u8) { return true; }; // '\v'
if (c == 12u8) { return true; }; // '\f'
if (c == 13u8) { return true; }; // '\r'
export fn isspace(c: rune) bool = {
if (c == 32) { return true; }; // ' '
if (c == 9) { return true; }; // '\t'
if (c == 10) { return true; }; // '\n'
if (c == 11) { return true; }; // '\v'
if (c == 12) { return true; }; // '\f'
if (c == 13) { return true; }; // '\r'
return false;
};
export fn ishex(c: u8) bool = {
export fn isxdigit(c: rune) bool = {
if (isdigit(c)) { return true; };
if (c >= 65u8) {
if (c <= 70u8) { return true; }; // 'A'..'F'
if (c >= 65) {
if (c <= 70) { return true; }; // 'A'..'F'
};
if (c >= 97u8) {
if (c <= 102u8) { return true; }; // 'a'..'f'
if (c >= 97) {
if (c <= 102) { return true; }; // 'a'..'f'
};
return false;
};
// digitval — value of `c` as a hex/decimal digit, or -1 if not one.
// Useful when scanning numeric literals.
export fn digitval(c: u8) i32 = {
if (isdigit(c)) { return (c - 48u8): i32; };
if (c >= 65u8) {
if (c <= 70u8) { return ((c - 65u8) + 10u8): i32; };
export fn digitval(c: rune) i32 = {
if (isdigit(c)) { return (c - 48): i32; };
if (c >= 65) {
if (c <= 70) { return ((c - 65) + 10): i32; };
};
if (c >= 97u8) {
if (c <= 102u8) { return ((c - 97u8) + 10u8): i32; };
if (c >= 97) {
if (c <= 102) { return ((c - 97) + 10): i32; };
};
return -1;
};
// isidstart / isidpart — identifier classes used by the lexer.
// Alpha or '_' starts; alnum or '_' continues.
export fn isidstart(c: u8) bool = {
export fn isidstart(c: rune) bool = {
if (isalpha(c)) { return true; };
if (c == 95u8) { return true; }; // '_'
if (c == 95) { return true; }; // '_'
return false;
};
export fn isidpart(c: u8) bool = {
export fn isidpart(c: rune) bool = {
if (isalnum(c)) { return true; };
if (c == 95u8) { return true; };
if (c == 95) { return true; };
return false;
};
// tolower / toupper — fold ASCII case. Non-letters pass through.
export fn tolower(c: u8) u8 = {
if (isupper(c)) { return c + 32u8; };
export fn tolower(c: rune) rune = {
if (isupper(c)) { return c + 32; };
return c;
};
export fn toupper(c: u8) u8 = {
if (islower(c)) { return c - 32u8; };
export fn toupper(c: rune) rune = {
if (islower(c)) { return c - 32; };
return c;
};
@@ -1172,18 +1179,18 @@ fn escape(l: *lex, out: *i32) bool = {
let lo: i32 = lget(l);
if (hi < 0) { return false; };
if (lo < 0) { return false; };
if (!ascii.ishex(hi: u8)) {
if (!ascii.isxdigit(hi: rune)) {
let cp: pos; curpos(l, &cp);
errat(l, &cp, "bad \\x escape");
return false;
};
if (!ascii.ishex(lo: u8)) {
if (!ascii.isxdigit(lo: rune)) {
let cp: pos; curpos(l, &cp);
errat(l, &cp, "bad \\x escape");
return false;
};
let h: i32 = ascii.digitval(hi: u8);
let lv: i32 = ascii.digitval(lo: u8);
let h: i32 = ascii.digitval(hi: rune);
let lv: i32 = ascii.digitval(lo: rune);
*out = (h << 4) | lv;
return true;
};
@@ -1197,7 +1204,7 @@ fn scandecimalrun(l: *lex) void = {
for (true) {
let c: i32 = lpeek(l, 0u64);
if (c < 0) { break; };
if (!ascii.isdigit(c: u8)) {
if (!ascii.isdigit(c: rune)) {
if (c != 95) { break; };
};
lget(l);
@@ -1208,7 +1215,7 @@ fn scanhexrun(l: *lex) void = {
for (true) {
let c: i32 = lpeek(l, 0u64);
if (c < 0) { break; };
if (!ascii.ishex(c: u8)) {
if (!ascii.isxdigit(c: rune)) {
if (c != 95) { break; };
};
lget(l);
@@ -1247,7 +1254,7 @@ fn scanexp(l: *lex) void = {
for (true) {
let c: i32 = lpeek(l, 0u64);
if (c < 0) { break; };
if (!ascii.isdigit(c: u8)) { break; };
if (!ascii.isdigit(c: rune)) { break; };
lget(l);
};
};
@@ -1332,12 +1339,12 @@ fn lexnum(l: *lex, start: *pos, out: *tok) void = {
let pc: i32 = lpeek(l, 0u64);
if (pc >= 0) {
if (ascii.isidstart(pc: u8)) {
if (ascii.isidstart(pc: rune)) {
let sb: u64 = l.lpos;
for (true) {
let cc: i32 = lpeek(l, 0u64);
if (cc < 0) { break; };
if (!ascii.isidpart(cc: u8)) { break; };
if (!ascii.isidpart(cc: rune)) { break; };
lget(l);
};
let sl: u64 = l.lpos - sb;
@@ -1381,7 +1388,7 @@ fn lexident(l: *lex, start: *pos, out: *tok) void = {
for (true) {
let c: i32 = lpeek(l, 0u64);
if (c < 0) { break; };
if (!ascii.isidpart(c: u8)) { break; };
if (!ascii.isidpart(c: rune)) { break; };
lget(l);
};
let n: u64 = l.lpos - begin;
@@ -1530,8 +1537,8 @@ export fn lexnext(l: *lex, out: *tok) void = {
let c: i32 = lpeek(l, 0u64);
if (c >= 0) {
if (ascii.isidstart(c: u8)) { lexident(l, &start, out); return; };
if (ascii.isdigit(c: u8)) { lexnum(l, &start, out); return; };
if (ascii.isidstart(c: rune)) { lexident(l, &start, out); return; };
if (ascii.isdigit(c: rune)) { lexnum(l, &start, out); return; };
};
if (c == 34) { lget(l); lexstr(l, &start, out); return; };
@@ -1895,12 +1902,12 @@ fn pr(fd: i32, n: *node, d: i32) void = {
if (n.kind == N_INTLIT) {
putc1(fd, 32u8);
let buf: [32]u8;
let m: i32 = strconv.u64toa(buf[0:32], n.uval);
let m: i32 = strconv.u64tos(buf[0:32], n.uval);
os.write(fd, buf.ptr, m: u64);
} else { if (n.kind == N_RUNELIT) {
putc1(fd, 32u8);
let buf: [32]u8;
let m: i32 = strconv.u64toa(buf[0:32], n.uval);
let m: i32 = strconv.u64tos(buf[0:32], n.uval);
os.write(fd, buf.ptr, m: u64);
} else { if (
n.kind == N_STRLIT ||
@@ -7408,13 +7415,13 @@ fn emitline(s: str) void = { os.write(1, s.ptr, s.len: u64); };
fn emitint(v: i64) void = {
let buf: [32]u8;
let n: i32 = strconv.i64toa(buf[0:32], v);
let n: i32 = strconv.i64tos(buf[0:32], v);
os.write(1, buf.ptr, n: u64);
};
fn emituint(v: u64) void = {
let buf: [32]u8;
let n: i32 = strconv.u64toa(buf[0:32], v);
let n: i32 = strconv.u64tos(buf[0:32], v);
os.write(1, buf.ptr, n: u64);
};
@@ -7452,7 +7459,7 @@ fn mklabel(c: *cgen, base: str) str = {
i += 1; j += 1;
};
buf[i] = 95u8; i += 1; // '_'
let n: i32 = strconv.i64toa(buf[i:128], c.labelseq: i64);
let n: i32 = strconv.i64tos(buf[i:128], c.labelseq: i64);
c.labelseq += 1;
let total: i32 = i + n;
let p: *u8 = amalloc(c.a, (total: u64) + 1u64): *u8;
@@ -7491,7 +7498,7 @@ fn internstrlit(c: *cgen, bytes: str) str = {
// New label "_S_<seq>".
let buf: [32]u8;
buf[0] = 95u8; buf[1] = 83u8; buf[2] = 95u8; // "_S_"
let n: i32 = strconv.i64toa(buf[3:32], c.strlitseq: i64);
let n: i32 = strconv.i64tos(buf[3:32], c.strlitseq: i64);
c.strlitseq += 1;
let total: i32 = 3 + n;
let p: *u8 = amalloc(c.a, (total: u64) + 1u64): *u8;
@@ -7979,7 +7986,7 @@ export fn main(argc: i32, argv: **u8) i32 = {
let a: *arena = newarena();
let buf: *u8 = amalloc(a, sz: u64): *u8;
let r: i64 = os.readfull(fd, buf, sz: u64);
let r: i64 = os.readall(fd, buf, sz: u64);
os.close(fd);
if (r != sz) {
os.write(2, "wwdump: short read\n".ptr, 19u64);
@@ -8018,11 +8025,11 @@ export fn main(argc: i32, argv: **u8) i32 = {
os.write(1, argstr(path).ptr, argstrlen(path): u64);
os.write(1, ": ".ptr, 2u64);
let buf: [32]u8;
let n: i32 = strconv.i64toa(buf[0:32], ck.nresolved: i64);
let n: i32 = strconv.i64tos(buf[0:32], ck.nresolved: i64);
os.write(1, buf.ptr, n: u64);
os.write(1, "/".ptr, 1u64);
let total: i32 = ck.nresolved + ck.nunresolved;
n = strconv.i64toa(buf[0:32], total: i64);
n = strconv.i64tos(buf[0:32], total: i64);
os.write(1, buf.ptr, n: u64);
os.write(1, " resolved\n".ptr, 10u64);
if (ck.nunresolved > 0) { return 1; };

View File

@@ -95,7 +95,7 @@ export fn main(argc: i32, argv: **u8) i32 = {
let a: *arena = newarena();
let buf: *u8 = amalloc(a, sz: u64): *u8;
let r: i64 = os.readfull(fd, buf, sz: u64);
let r: i64 = os.readall(fd, buf, sz: u64);
os.close(fd);
if (r != sz) {
os.write(2, "wwdump: short read\n".ptr, 19u64);
@@ -134,11 +134,11 @@ export fn main(argc: i32, argv: **u8) i32 = {
os.write(1, argstr(path).ptr, argstrlen(path): u64);
os.write(1, ": ".ptr, 2u64);
let buf: [32]u8;
let n: i32 = strconv.i64toa(buf[0:32], ck.nresolved: i64);
let n: i32 = strconv.i64tos(buf[0:32], ck.nresolved: i64);
os.write(1, buf.ptr, n: u64);
os.write(1, "/".ptr, 1u64);
let total: i32 = ck.nresolved + ck.nunresolved;
n = strconv.i64toa(buf[0:32], total: i64);
n = strconv.i64tos(buf[0:32], total: i64);
os.write(1, buf.ptr, n: u64);
os.write(1, " resolved\n".ptr, 10u64);
if (ck.nunresolved > 0) { return 1; };

View File

@@ -120,9 +120,11 @@ export fn filesize(fd: i32) i64 = {
return end;
};
// readfull — keep reading until `n` bytes have arrived or the fd
// readall — keep reading until `n` bytes have arrived or the fd
// closes early. Returns bytes read (0..=n) or -1 on read error.
export fn readfull(fd: i32, buf: *u8, n: u64) i64 = {
// Hare name (io::readall); the buffer is caller-supplied, matching
// the Plan 9 subset convention.
export fn readall(fd: i32, buf: *u8, n: u64) i64 = {
let got: u64 = 0u64;
for (got < n) {
let r: i64 = read(fd, buf + got, n - got);
@@ -133,9 +135,10 @@ export fn readfull(fd: i32, buf: *u8, n: u64) i64 = {
return got: i64;
};
// writefull — keep writing until `n` bytes have been accepted or the
// fd refuses progress. Returns bytes written or -1.
export fn writefull(fd: i32, buf: *u8, n: u64) i64 = {
// writeall — keep writing until `n` bytes have been accepted or the
// fd refuses progress. Returns bytes written or -1. Hare name
// (io::writeall).
export fn writeall(fd: i32, buf: *u8, n: u64) i64 = {
let sent: u64 = 0u64;
for (sent < n) {
let r: i64 = write(fd, buf + sent, n - sent);
@@ -154,8 +157,8 @@ export fn access(path: *u8, mode: i32) i32 = {
return syscall2(SYS_ACCESS, path: i64, mode: i64): i32;
};
// unlink(2).
export fn unlink(path: *u8) i32 = {
// remove — unlink(2). Hare name; the underlying syscall is unlink(2).
export fn remove(path: *u8) i32 = {
return syscall1(SYS_UNLINK, path: i64): i32;
};
@@ -213,14 +216,16 @@ export fn getdents64(fd: i32, buf: *u8, n: u64) i64 = {
// buffer. Two error idioms ship side by side:
// - Plan 9 style (atoi64): tuple `(value, ok)`. Pre-dates the
// tagged-union work; kept for callers that already use it.
// - Hare style (parse64/parseu64): `(value | str)`. The error
// - Hare style (stoi64/stou64): `(value | str)`. The error
// variant carries a short, allocation-free message describing
// why the parse failed. Prefer this for new code.
// u64toa — write `v` in decimal into `buf` and return the byte count.
// Unsigned-only so callers don't have to think about wraparound when
// printing a u64 that happens to have the high bit set.
export fn u64toa(buf: []u8, v: u64) i32 = {
// u64tos — write `v` in decimal into `buf` and return the byte count.
// Hare name; the buffer-in shape is the sanctioned Plan 9 subset of
// Hare's `u64tos(u, base) const str`. Unsigned-only so callers don't
// have to think about wraparound when printing a u64 with the high
// bit set.
export fn u64tos(buf: []u8, v: u64) i32 = {
let tmp: [32]u8;
let i: i32 = 0;
let n: u64 = v;
@@ -242,7 +247,7 @@ export fn u64toa(buf: []u8, v: u64) i32 = {
return out;
};
export fn i64toa(buf: []u8, v: i64) i32 = {
export fn i64tos(buf: []u8, v: i64) i32 = {
let neg: bool = false;
let n: i64 = v;
if (n < 0) {
@@ -292,11 +297,12 @@ export fn atoi64(s: str) (i64, bool) = {
return v, true;
};
// parse64 — Hare-style fallible signed decimal parser. The value
// variant is i64; the error variant is a short str describing the
// reason. No locale, no whitespace, no underscores: a leading '-' is
// the only non-digit accepted, and only at position 0.
export fn parse64(s: str) (i64 | str) = {
// stoi64 — Hare-style fallible signed decimal parser. The value
// variant is i64; the error variant is a short str (subset of Hare's
// (invalid | overflow) tagged-union). No locale, no whitespace, no
// underscores: a leading '-' is the only non-digit accepted, and only
// at position 0.
export fn stoi64(s: str) (i64 | str) = {
if (s.len == 0) { return "parse: empty"; };
let i: i32 = 0;
let neg: bool = false;
@@ -314,8 +320,9 @@ export fn parse64(s: str) (i64 | str) = {
return v;
};
// parseu64 — fallible unsigned decimal parser. No leading sign.
export fn parseu64(s: str) (u64 | str) = {
// stou64 — fallible unsigned decimal parser. No leading sign. Mirrors
// Hare's strconv::stou64.
export fn stou64(s: str) (u64 | str) = {
if (s.len == 0) { return "parse: empty"; };
let v: u64 = 0u64;
let i: i32 = 0;
@@ -330,96 +337,96 @@ export fn parseu64(s: str) (u64 | str) = {
};
// MODULE: ascii
// ascii — byte-class predicates and case folding for the ASCII range.
// Matches Hare's ascii::isdigit family. Bytes outside 0..127 always
// answer `false`. The lexer hot path uses these inline; they are
// expected to inline to a couple of compares.
// ascii — rune-class predicates and case folding for the ASCII range.
// Matches Hare's ascii::isdigit family (rune-taking signature). Runes
// outside 0..127 always answer `false`. The lexer hot path uses these
// inline; they are expected to inline to a couple of compares.
export fn isdigit(c: u8) bool = {
if (c < 48u8) { return false; };
if (c > 57u8) { return false; };
export fn isdigit(c: rune) bool = {
if (c < 48) { return false; };
if (c > 57) { return false; };
return true;
};
export fn isupper(c: u8) bool = {
if (c < 65u8) { return false; };
if (c > 90u8) { return false; };
export fn isupper(c: rune) bool = {
if (c < 65) { return false; };
if (c > 90) { return false; };
return true;
};
export fn islower(c: u8) bool = {
if (c < 97u8) { return false; };
if (c > 122u8) { return false; };
export fn islower(c: rune) bool = {
if (c < 97) { return false; };
if (c > 122) { return false; };
return true;
};
export fn isalpha(c: u8) bool = {
export fn isalpha(c: rune) bool = {
if (isupper(c)) { return true; };
return islower(c);
};
export fn isalnum(c: u8) bool = {
export fn isalnum(c: rune) bool = {
if (isalpha(c)) { return true; };
return isdigit(c);
};
// isspace — the C/Hare set: space, tab, NL, VT, FF, CR.
export fn isspace(c: u8) bool = {
if (c == 32u8) { return true; }; // ' '
if (c == 9u8) { return true; }; // '\t'
if (c == 10u8) { return true; }; // '\n'
if (c == 11u8) { return true; }; // '\v'
if (c == 12u8) { return true; }; // '\f'
if (c == 13u8) { return true; }; // '\r'
export fn isspace(c: rune) bool = {
if (c == 32) { return true; }; // ' '
if (c == 9) { return true; }; // '\t'
if (c == 10) { return true; }; // '\n'
if (c == 11) { return true; }; // '\v'
if (c == 12) { return true; }; // '\f'
if (c == 13) { return true; }; // '\r'
return false;
};
export fn ishex(c: u8) bool = {
export fn isxdigit(c: rune) bool = {
if (isdigit(c)) { return true; };
if (c >= 65u8) {
if (c <= 70u8) { return true; }; // 'A'..'F'
if (c >= 65) {
if (c <= 70) { return true; }; // 'A'..'F'
};
if (c >= 97u8) {
if (c <= 102u8) { return true; }; // 'a'..'f'
if (c >= 97) {
if (c <= 102) { return true; }; // 'a'..'f'
};
return false;
};
// digitval — value of `c` as a hex/decimal digit, or -1 if not one.
// Useful when scanning numeric literals.
export fn digitval(c: u8) i32 = {
if (isdigit(c)) { return (c - 48u8): i32; };
if (c >= 65u8) {
if (c <= 70u8) { return ((c - 65u8) + 10u8): i32; };
export fn digitval(c: rune) i32 = {
if (isdigit(c)) { return (c - 48): i32; };
if (c >= 65) {
if (c <= 70) { return ((c - 65) + 10): i32; };
};
if (c >= 97u8) {
if (c <= 102u8) { return ((c - 97u8) + 10u8): i32; };
if (c >= 97) {
if (c <= 102) { return ((c - 97) + 10): i32; };
};
return -1;
};
// isidstart / isidpart — identifier classes used by the lexer.
// Alpha or '_' starts; alnum or '_' continues.
export fn isidstart(c: u8) bool = {
export fn isidstart(c: rune) bool = {
if (isalpha(c)) { return true; };
if (c == 95u8) { return true; }; // '_'
if (c == 95) { return true; }; // '_'
return false;
};
export fn isidpart(c: u8) bool = {
export fn isidpart(c: rune) bool = {
if (isalnum(c)) { return true; };
if (c == 95u8) { return true; };
if (c == 95) { return true; };
return false;
};
// tolower / toupper — fold ASCII case. Non-letters pass through.
export fn tolower(c: u8) u8 = {
if (isupper(c)) { return c + 32u8; };
export fn tolower(c: rune) rune = {
if (isupper(c)) { return c + 32; };
return c;
};
export fn toupper(c: u8) u8 = {
if (islower(c)) { return c - 32u8; };
export fn toupper(c: rune) rune = {
if (islower(c)) { return c - 32; };
return c;
};
@@ -557,19 +564,19 @@ export fn main() i32 = {
// Probe 5 — strconv round-trip via the real stdlib.
let outbuf: [32]u8;
let nb: i32 = strconv.i64toa(outbuf[0:32], 4242i64);
let nb: i32 = strconv.i64tos(outbuf[0:32], 4242i64);
if (nb != 4) { return 11; };
if (outbuf[0] != 52u8) { return 12; }; // '4'
if (outbuf[3] != 50u8) { return 13; }; // '2'
// Probe 6 — ascii classifications.
if (!ascii.isdigit(53u8)) { return 14; }; // '5'
if (ascii.isdigit(65u8)) { return 15; }; // 'A' is not a digit
if (!ascii.isalpha(122u8)) { return 16; }; // 'z'
if (!ascii.isidstart(95u8)) { return 17; }; // '_'
if (!ascii.isidpart(48u8)) { return 18; }; // '0' is part
if (ascii.digitval(70u8) != 15) { return 19; }; // 'F' = 15
if (ascii.tolower(65u8) != 97u8) { return 20; }; // 'A' -> 'a'
// Probe 6 — ascii classifications (rune-taking, Hare-shaped).
if (!ascii.isdigit(53)) { return 14; }; // '5'
if (ascii.isdigit(65)) { return 15; }; // 'A' is not a digit
if (!ascii.isalpha(122)) { return 16; }; // 'z'
if (!ascii.isidstart(95)) { return 17; }; // '_'
if (!ascii.isidpart(48)) { return 18; }; // '0' is part
if (ascii.digitval(70) != 15) { return 19; }; // 'F' = 15
if (ascii.tolower(65) != 97) { return 20; }; // 'A' -> 'a'
// Probe 7 — file open/read via the new os APIs. /proc/self/cmdline
// always exists on Linux, no write side, and is non-empty.
@@ -581,7 +588,7 @@ export fn main() i32 = {
case let e: str => return 21;
};
let rbuf: [128]u8;
let n: i64 = os.readfull(fd, rbuf.ptr, 128u64);
let n: i64 = os.readall(fd, rbuf.ptr, 128u64);
os.close(fd);
if (n <= 0i64) { return 22; };

View File

@@ -131,19 +131,19 @@ export fn main() i32 = {
// Probe 5 — strconv round-trip via the real stdlib.
let outbuf: [32]u8;
let nb: i32 = strconv.i64toa(outbuf[0:32], 4242i64);
let nb: i32 = strconv.i64tos(outbuf[0:32], 4242i64);
if (nb != 4) { return 11; };
if (outbuf[0] != 52u8) { return 12; }; // '4'
if (outbuf[3] != 50u8) { return 13; }; // '2'
// Probe 6 — ascii classifications.
if (!ascii.isdigit(53u8)) { return 14; }; // '5'
if (ascii.isdigit(65u8)) { return 15; }; // 'A' is not a digit
if (!ascii.isalpha(122u8)) { return 16; }; // 'z'
if (!ascii.isidstart(95u8)) { return 17; }; // '_'
if (!ascii.isidpart(48u8)) { return 18; }; // '0' is part
if (ascii.digitval(70u8) != 15) { return 19; }; // 'F' = 15
if (ascii.tolower(65u8) != 97u8) { return 20; }; // 'A' -> 'a'
// Probe 6 — ascii classifications (rune-taking, Hare-shaped).
if (!ascii.isdigit(53)) { return 14; }; // '5'
if (ascii.isdigit(65)) { return 15; }; // 'A' is not a digit
if (!ascii.isalpha(122)) { return 16; }; // 'z'
if (!ascii.isidstart(95)) { return 17; }; // '_'
if (!ascii.isidpart(48)) { return 18; }; // '0' is part
if (ascii.digitval(70) != 15) { return 19; }; // 'F' = 15
if (ascii.tolower(65) != 97) { return 20; }; // 'A' -> 'a'
// Probe 7 — file open/read via the new os APIs. /proc/self/cmdline
// always exists on Linux, no write side, and is non-empty.
@@ -155,7 +155,7 @@ export fn main() i32 = {
case let e: str => return 21;
};
let rbuf: [128]u8;
let n: i64 = os.readfull(fd, rbuf.ptr, 128u64);
let n: i64 = os.readall(fd, rbuf.ptr, 128u64);
os.close(fd);
if (n <= 0i64) { return 22; };

View File

@@ -146,7 +146,7 @@ static const struct row rows[] = {
"fn main() i32 = {\n"
" let buf: [32]u8;\n"
" let s: []u8 = buf[0:32];\n"
" let n: i32 = strconv.i64toa(s, 12345);\n"
" let n: i32 = strconv.i64tos(s, 12345);\n"
" os.write(1, buf.ptr, n: u64);\n"
" os.write(1, \"\\n\".ptr, 1u64);\n"
" return n;\n"
@@ -703,14 +703,14 @@ static const struct row rows[] = {
" };\n"
" return acc;\n"
"};", 13 }, /* 1 byte written to fd 1, plus len(\"write failed\")=12 */
/* strconv.parse64: fallible signed decimal. Two successful
/* strconv.stoi64: fallible signed decimal. Two successful
* parses contribute their values; one bad parse contributes
* the error message length (20 = len(\"parse: invalid digit\")). */
{ "use strconv;\n"
"fn main() i32 = {\n"
" let r1: (i64 | str) = strconv.parse64(\"42\");\n"
" let r2: (i64 | str) = strconv.parse64(\"-7\");\n"
" let r3: (i64 | str) = strconv.parse64(\"abc\");\n"
" let r1: (i64 | str) = strconv.stoi64(\"42\");\n"
" let r2: (i64 | str) = strconv.stoi64(\"-7\");\n"
" let r3: (i64 | str) = strconv.stoi64(\"abc\");\n"
" let acc: i32 = 0;\n"
" match (r1) {\n"
" case let v: i64 => acc += v: i32;\n"
@@ -726,12 +726,12 @@ static const struct row rows[] = {
" };\n"
" return acc;\n"
"};", 55 }, /* 42 + (-7) + 20 */
/* strconv.parseu64: success path 123, error path captures
/* strconv.stou64: success path 123, error path captures
* len(\"parse: invalid digit\") = 20 for the leading-sign reject. */
{ "use strconv;\n"
"fn main() i32 = {\n"
" let r1: (u64 | str) = strconv.parseu64(\"123\");\n"
" let r2: (u64 | str) = strconv.parseu64(\"-1\");\n"
" let r1: (u64 | str) = strconv.stou64(\"123\");\n"
" let r2: (u64 | str) = strconv.stou64(\"-1\");\n"
" let acc: i32 = 0;\n"
" match (r1) {\n"
" case let v: u64 => acc += v: i32;\n"
@@ -743,17 +743,17 @@ static const struct row rows[] = {
" };\n"
" return acc;\n"
"};", 143 }, /* 123 + 20 */
/* strings.indexbyte (Plan 9 -1) and strings.index (substring). */
/* strings.byteindex (Plan 9 -1) and strings.index (substring). */
{ "use strings;\n"
"fn main() i32 = {\n"
" let s: str = \"hello, world\";\n"
" let i1: i32 = strings.indexbyte(s, 44u8);\n"
" let i2: i32 = strings.indexbyte(s, 122u8);\n"
" let i1: i32 = strings.byteindex(s, 44u8);\n"
" let i2: i32 = strings.byteindex(s, 122u8);\n"
" let i3: i32 = strings.index(s, \"world\");\n"
" let i4: i32 = strings.index(s, \"nope\");\n"
" return i1 + i2 + i3 + i4;\n"
"};", 10 }, /* 5 + (-1) + 7 + (-1) */
/* bytes.indexsub: substring search over []u8. */
/* bytes.index: substring search over []u8. */
{ "use bytes;\n"
"fn main() i32 = {\n"
" let buf: [12]u8;\n"
@@ -762,15 +762,15 @@ static const struct row rows[] = {
" buf[8] = 111u8; buf[9] = 114u8; buf[10] = 108u8; buf[11] = 100u8;\n"
" let needle: [3]u8;\n"
" needle[0] = 119u8; needle[1] = 111u8; needle[2] = 114u8;\n"
" return bytes.indexsub(buf[0:12], needle[0:3]);\n"
" return bytes.index(buf[0:12], needle[0:3]);\n"
"};", 7 },
/* errors.equal — sentinel comparison through a (T | error) union.
* Sets up two errors, dispatches each, and confirms the matching
* sentinel detection. */
{ "use errors;\n"
"fn parse(n: i64) (i64 | errors.error) = {\n"
" if (n < 0) { return errors.eEOF; };\n"
" if (n == 0) { return errors.eShortRead; };\n"
" if (n < 0) { return errors.eof; };\n"
" if (n == 0) { return errors.underread; };\n"
" return n;\n"
"};\n"
"fn main() i32 = {\n"
@@ -780,18 +780,18 @@ static const struct row rows[] = {
" match (r1) {\n"
" case let v: i64 => acc += -100;\n"
" case let e: errors.error =>\n"
" if (errors.equal(e, errors.eEOF)) { acc += 1; }\n"
" if (errors.equal(e, errors.eof)) { acc += 1; }\n"
" else { acc += -100; };\n"
" };\n"
" match (r2) {\n"
" case let v: i64 => acc += -100;\n"
" case let e: errors.error =>\n"
" if (errors.equal(e, errors.eShortRead)) { acc += 10; }\n"
" if (errors.equal(e, errors.underread)) { acc += 10; }\n"
" else { acc += -100; };\n"
" };\n"
" return acc;\n"
"};", 11 },
/* bufio.takeline: drain successive '\\n'-terminated lines from a
/* bufio.readline: drain successive '\\n'-terminated lines from a
* pre-filled buffer, then a trailing fragment that returns the
* `linerr` variant carrying \"no newline\" (10 chars). */
{ "use bufio;\n"
@@ -803,17 +803,17 @@ static const struct row rows[] = {
" let b: bufio.buf;\n"
" b.s = nil; b.data = raw.ptr; b.cap = 11; b.r = 0; b.w = 11;\n"
" let acc: i32 = 0;\n"
" let l1: (str | bufio.linerr) = bufio.takeline(&b);\n"
" let l1: (str | bufio.linerr) = bufio.readline(&b);\n"
" match (l1) {\n"
" case let s: str => acc += s.len: i32;\n"
" case let e: bufio.linerr => acc += -100;\n"
" };\n"
" let l2: (str | bufio.linerr) = bufio.takeline(&b);\n"
" let l2: (str | bufio.linerr) = bufio.readline(&b);\n"
" match (l2) {\n"
" case let s: str => acc += s.len: i32;\n"
" case let e: bufio.linerr => acc += -100;\n"
" };\n"
" let l3: (str | bufio.linerr) = bufio.takeline(&b);\n"
" let l3: (str | bufio.linerr) = bufio.readline(&b);\n"
" match (l3) {\n"
" case let s: str => acc += -100;\n"
" case let e: bufio.linerr => acc += e.len: i32;\n"