A field-like access on a builtin INTEGER type name folds to a compile-time
constant of the queried type, driven by (width, signedness) arithmetic:
sN: min=-(2^(N-1)), max=2^(N-1)-1; uN: min=0, max=2^N-1
for every width s1..s64 / u1..u64 (not just power-of-two), plus usize/isize.
- type_resolver.zig: extract the single width parser (parseWidthInt) reused by
resolveNamed AND the new accessors (no second parser — issue-0083 class);
add resolveBuiltinName / integerWidthSign / integerLimitBits / integerLimitFor.
- lower.zig: lowerNumericLimit intercept beside the error.X / Struct.CONST /
pack-arity identifier-receiver intercepts; folds ints via constInt, emits a
clean diagnostic for a non-numeric receiver (bool/string/void/Any/noreturn),
falls through for floats (NL.2).
- expr_typer.zig: mirror the result type so inferExprType reports the queried type.
- program_index.zig: recognize the accessors in the comptime-int / array-dim path
so [u8.max]T (255) / [s16.max]T (32767) work; [u64.max]T is rejected oversized.
- u64.max / usize.max stored as the all-ones bit pattern with TYPE u64 (i64 -1),
asserted via union { u: u64; s: s64 } reinterpret.
Docs: specs.md numeric-limits subsection (formulas + result-type + u64 note);
readme.md language overview. Examples 0148 (positive) / 0149 (negative-receiver).
Unit tests for the value computation in type_resolver.test.zig.
Gate: zig build, zig build test (359/359), tests/run_examples.sh (416 ok, 0 failed).
66 lines
3.1 KiB
Plaintext
66 lines
3.1 KiB
Plaintext
// Integer numeric-limit accessors: `<IntType>.min` / `.max` fold to a
|
|
// compile-time constant of the QUERIED integer type, driven by the
|
|
// (width, signedness) arithmetic (`sN`: min=-(2^(N-1)), max=2^(N-1)-1; `uN`:
|
|
// min=0, max=2^N-1) — every width 1..64, not just the power-of-two ones, plus
|
|
// `usize`/`isize` (target-width). Usable in expressions and in array-dimension
|
|
// position via the comptime-int path (`[u8.max]T`).
|
|
//
|
|
// The extreme values that the s64-based integer formatter cannot render
|
|
// directly — `s64.min` (i64::MIN) and the all-ones `u64.max`/`usize.max` — are
|
|
// asserted EXACTLY via comparison and untagged-union bit reinterpret, never via
|
|
// the formatter (which prints i64::MIN as a bare "-" and u64.max as "-1").
|
|
#import "modules/std.sx";
|
|
|
|
// Untagged union for the exact u64.max bit-reinterpret check.
|
|
UU :: union { u: u64; s: s64; }
|
|
|
|
main :: () -> s32 {
|
|
// Sub-byte widths — arbitrary bit-width arithmetic, not a per-name table.
|
|
print("s1.min={} s1.max={}\n", s1.min, s1.max); // -1 0
|
|
print("s2.min={} s2.max={}\n", s2.min, s2.max); // -2 1
|
|
print("s3.max={}\n", s3.max); // 3
|
|
print("u1.min={} u1.max={}\n", u1.min, u1.max); // 0 1
|
|
print("u2.max={}\n", u2.max); // 3
|
|
|
|
// Byte / word widths.
|
|
print("s8.min={} s8.max={}\n", s8.min, s8.max); // -128 127
|
|
print("u8.max={}\n", u8.max); // 255
|
|
print("s32.min={} s32.max={}\n", s32.min, s32.max); // -2147483648 2147483647
|
|
|
|
// s64 extremes: max prints; min (i64::MIN) is pinned by relation since the
|
|
// formatter cannot render it (this is independent of this feature).
|
|
print("s64.max={}\n", s64.max); // 9223372036854775807
|
|
print("s64.min+1 == -(s64.max): {}\n", s64.min + 1 == -9223372036854775807); // true
|
|
print("s64.min + s64.max == -1: {}\n", s64.min + s64.max == -1); // true
|
|
|
|
// u64.max / usize.max = all-ones (18446744073709551615); reinterpret to s64
|
|
// to confirm the bit pattern is -1 (and NOT a mangled value).
|
|
o : UU = ---;
|
|
o.u = u64.max;
|
|
print("u64.max as s64 == -1: {}\n", o.s == -1); // true
|
|
o.u = usize.max;
|
|
print("usize.max as s64 == -1: {}\n", o.s == -1); // true (host = u64)
|
|
print("usize.max == u64.max: {}\n", usize.max == u64.max); // true
|
|
print("isize.min == s64.min: {}\n", isize.min == s64.min); // true (host = s64)
|
|
|
|
// Result carries the QUERIED type: each binding is declared with the queried
|
|
// type and round-trips, so a mistyped fold (e.g. boxed as Any / widened)
|
|
// would not type-check here.
|
|
m3 : s3 = s3.max;
|
|
mu : u8 = u8.max;
|
|
ms : s8 = s8.min;
|
|
print("typed: m3={} mu={} ms={}\n", m3, mu, ms); // 3 255 -128
|
|
|
|
// Array-dimension / comptime-int path: `[u8.max]T` and `[s16.max]T` are
|
|
// valid counts (255 and 32767), usable end-to-end.
|
|
a : [u8.max]u8 = ---;
|
|
a[254] = 7;
|
|
print("[u8.max]u8 len={} a[254]={}\n", a.len, a[254]); // 255 7
|
|
|
|
b : [s16.max]u8 = ---;
|
|
b[32766] = 9;
|
|
print("[s16.max]u8 len={} b[32766]={}\n", b.len, b[32766]); // 32767 9
|
|
|
|
return 0;
|
|
}
|