feat(lang): integer numeric-limit accessors (s64.max, u8.min, s3.max) [NL.1]
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).
This commit is contained in:
65
examples/0148-types-int-numeric-limits.sx
Normal file
65
examples/0148-types-int-numeric-limits.sx
Normal file
@@ -0,0 +1,65 @@
|
||||
// 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;
|
||||
}
|
||||
18
examples/0149-types-int-numeric-limits-errors.sx
Normal file
18
examples/0149-types-int-numeric-limits-errors.sx
Normal file
@@ -0,0 +1,18 @@
|
||||
// Numeric-limit accessors apply only to numeric types. `.min`/`.max` on a
|
||||
// NON-numeric receiver is a clean compile error (never a silent value, never
|
||||
// the `.unresolved` sentinel reaching codegen):
|
||||
// - a builtin non-numeric type (`bool`, `void`, `string`) → a dedicated
|
||||
// "type 'X' has no '.min'/'.max'" diagnostic from the accessor intercept;
|
||||
// - a user struct (`MyStruct`) → the type name is not a builtin, so the
|
||||
// intercept stays out and the existing field-not-found path reports it.
|
||||
// Each case is accurate and located at the access; the program exits non-zero.
|
||||
#import "modules/std.sx";
|
||||
|
||||
MyStruct :: struct { a: s64; }
|
||||
|
||||
main :: () -> s32 {
|
||||
b := bool.max;
|
||||
s := MyStruct.min;
|
||||
v := void.max;
|
||||
return 0;
|
||||
}
|
||||
1
examples/expected/0148-types-int-numeric-limits.exit
Normal file
1
examples/expected/0148-types-int-numeric-limits.exit
Normal file
@@ -0,0 +1 @@
|
||||
0
|
||||
1
examples/expected/0148-types-int-numeric-limits.stderr
Normal file
1
examples/expected/0148-types-int-numeric-limits.stderr
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
18
examples/expected/0148-types-int-numeric-limits.stdout
Normal file
18
examples/expected/0148-types-int-numeric-limits.stdout
Normal file
@@ -0,0 +1,18 @@
|
||||
s1.min=-1 s1.max=0
|
||||
s2.min=-2 s2.max=1
|
||||
s3.max=3
|
||||
u1.min=0 u1.max=1
|
||||
u2.max=3
|
||||
s8.min=-128 s8.max=127
|
||||
u8.max=255
|
||||
s32.min=-2147483648 s32.max=2147483647
|
||||
s64.max=9223372036854775807
|
||||
s64.min+1 == -(s64.max): true
|
||||
s64.min + s64.max == -1: true
|
||||
u64.max as s64 == -1: true
|
||||
usize.max as s64 == -1: true
|
||||
usize.max == u64.max: true
|
||||
isize.min == s64.min: true
|
||||
typed: m3=3 mu=255 ms=-128
|
||||
[u8.max]u8 len=255 a[254]=7
|
||||
[s16.max]u8 len=32767 b[32766]=9
|
||||
@@ -0,0 +1 @@
|
||||
1
|
||||
@@ -0,0 +1,17 @@
|
||||
error: type 'bool' has no '.max' — numeric limits apply only to integer and float types
|
||||
--> examples/0149-types-int-numeric-limits-errors.sx:14:10
|
||||
|
|
||||
14 | b := bool.max;
|
||||
| ^^^^^^^^
|
||||
|
||||
error: field 'min' not found on type 'Any'
|
||||
--> examples/0149-types-int-numeric-limits-errors.sx:15:10
|
||||
|
|
||||
15 | s := MyStruct.min;
|
||||
| ^^^^^^^^^^^^
|
||||
|
||||
error: type 'void' has no '.max' — numeric limits apply only to integer and float types
|
||||
--> examples/0149-types-int-numeric-limits-errors.sx:16:10
|
||||
|
|
||||
16 | v := void.max;
|
||||
| ^^^^^^^^
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
Reference in New Issue
Block a user