raz / commit f1ef1ab0

commit
f1ef1ab0bfee480555dfa2c806ba08713fcdc499
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
9c7712e1
MIN and MAX: the limits of an integer type, as report 2026-10.36 has them

The built-ins MIN(T) and MAX(T): Sym enters them, Parse's builtin
reads T with parseType, and Item's limit refuses a T not an integer
type and makes the constant: as many ones as T has bits, shifted once
more if T is signed, is MAX, and minus that less 1 a signed MIN.

They are untyped constants but for MAX(U64) and MAX(UINT), which are
above high(I64), where an untyped constant ends: they keep their type,
as a hexadecimal number above it is a U64.

Tests: conform/run/Limits (each integer type, in a constant, untyped
arithmetic on them, each into its own type and one past it);
conform/err MinReal, MaxEnum and MaxValue, their messages in test/msg.
compiler.md, the editors' built-in lists and CLAUDE.md's readings say
so. Code lines 11,880 -> 11,902.

patch · log

15 files changed, +130 -6

CLAUDE.md modified +4 -0

@@ -339,3 +339,7 @@
339339 width as a shift's is, `n & (w - 1)`: `ROL(x, -1)` is `ROR(x, 1)`.
340340 The result is a built-in's, not an operator's, and widens (`made`
341341 is false). Of two constants they fold, in a constant too.
342+- `MIN(T)` and `MAX(T)` (report 2026-10.36) are untyped constants, but
343+ `MAX(U64)` and `MAX(UINT)`, above `high(I64)`, keep their type, as a
344+ hex literal above it is a `U64`. A `T` not an integer type is an
345+ error; `MAX(x)` of a variable is "x isn't a type".

conform/err/MaxEnum.raz (history) added +8 -0

@@ -0,0 +1,8 @@
1+module MaxEnum;
2+type Color = (red, green, blue);
3+proc main*;
4+var n: INT;
5+begin
6+ n := MAX(Color);
7+end;
8+end MaxEnum.

conform/err/MaxValue.raz (history) added +7 -0

@@ -0,0 +1,7 @@
1+module MaxValue;
2+proc main*;
3+var n, x: INT;
4+begin
5+ n := MAX(x);
6+end;
7+end MaxValue.

conform/err/MinReal.raz (history) added +3 -0

@@ -0,0 +1,3 @@
1+module MinReal;
2+const Least = MIN(F64);
3+end MinReal.

conform/run/Limits.expect (history) added +13 -0

@@ -0,0 +1,13 @@
1+-128 127
2+-32768 32767
3+-2147483648 2147483647
4+-9223372036854775808 9223372036854775807
5+-9223372036854775808 9223372036854775807
6+0 255
7+0 65535
8+0 4294967295
9+0 FFFFFFFFFFFFFFFF
10+0 FFFFFFFFFFFFFFFF
11+256 65535 FFFFFFFFFFFFFFFF
12+-128 32767 -2147483648 9223372036854775807 -9223372036854775808
13+0 0 0 0 0

conform/run/Limits.raz (history) added +41 -0

@@ -0,0 +1,41 @@
1+(* MIN(T) and MAX(T), the smallest and the largest value of each integer
2+ type (report section 6.1), untyped constants: in a constant, in
3+ arithmetic of their own, and given to each type. MAX(U64) and
4+ MAX(UINT) are above the largest I64. *)
5+module Limits;
6+import Out;
7+
8+const
9+ Byte = MAX(U8) + 1; (* untyped: no wrap at 255 *)
10+ Span = MAX(I16) - MIN(I16);
11+ All = MAX(U64);
12+
13+var
14+ i8: I8; i16: I16; i32: I32; i64: I64; int: INT;
15+ u8: U8; u16: U16; u32: U32; u64: U64; uint: UINT;
16+
17+proc main*;
18+begin
19+ Out.int(MIN(I8)); Out.str(" "); Out.int(MAX(I8)); Out.ln;
20+ Out.int(MIN(I16)); Out.str(" "); Out.int(MAX(I16)); Out.ln;
21+ Out.int(MIN(I32)); Out.str(" "); Out.int(MAX(I32)); Out.ln;
22+ Out.int(MIN(I64)); Out.str(" "); Out.int(MAX(I64)); Out.ln;
23+ Out.int(MIN(INT)); Out.str(" "); Out.int(MAX(INT)); Out.ln;
24+ Out.int(MIN(U8)); Out.str(" "); Out.int(MAX(U8)); Out.ln;
25+ Out.int(MIN(U16)); Out.str(" "); Out.int(MAX(U16)); Out.ln;
26+ Out.int(MIN(U32)); Out.str(" "); Out.int(MAX(U32)); Out.ln;
27+ Out.int(MIN(U64)); Out.str(" "); Out.hex(MAX(U64), 16); Out.ln;
28+ Out.int(MIN(UINT)); Out.str(" "); Out.hex(U64(MAX(UINT)), 16); Out.ln;
29+ Out.int(Byte); Out.str(" "); Out.int(Span); Out.str(" "); Out.hex(All, 16); Out.ln;
30+ (* each into its own type, and one past it wraps *)
31+ i8 := MAX(I8); i16 := MIN(I16); i32 := MAX(I32); i64 := MIN(I64); int := MAX(INT);
32+ u8 := MAX(U8); u16 := MAX(U16); u32 := MAX(U32); u64 := MAX(U64); uint := MAX(UINT);
33+ INC(i8); DEC(i16); INC(i32); DEC(i64); INC(int);
34+ INC(u8); INC(u16); INC(u32); INC(u64); INC(uint);
35+ Out.int(I64(i8)); Out.str(" "); Out.int(I64(i16)); Out.str(" "); Out.int(I64(i32)); Out.str(" ");
36+ Out.int(i64); Out.str(" "); Out.int(int); Out.ln;
37+ Out.int(I64(u8)); Out.str(" "); Out.int(I64(u16)); Out.str(" "); Out.int(I64(u32)); Out.str(" ");
38+ Out.hex(u64, 1); Out.str(" "); Out.hex(U64(uint), 1); Out.ln;
39+end;
40+
41+end Limits.

doc/compiler.md (history) modified +11 -3

@@ -18,12 +18,12 @@
1818 |---|---|---|---|
1919 | `src/Diag.raz` | 422 | 324 | Messages: positions, source excerpts, colour, counts |
2020 | `src/Scan.raz` | 1,169 | 965 | Symbols, literals, comments, positions |
21-| `src/Sym.raz` | 715 | 573 | Types, objects, scopes, layout |
21+| `src/Sym.raz` | 718 | 576 | Types, objects, scopes, layout |
2222 | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes |
2323 | `src/X64.raz` | 1,584 | 1,304 | The instruction encoder, jumps within a procedure, the assembler |
2424 | `src/Gen.raz` | 2,592 | 1,843 | The stack machine, for x86-64: values, operations, checks, calls, frames |
25-| `src/Item.raz` | 2,223 | 1,785 | Items: types, constants, variables, operators, conversions, calls |
26-| `src/Parse.raz` | 4,396 | 3,796 | Declarations, statements, expressions, built-ins, recovery, warnings |
25+| `src/Item.raz` | 2,244 | 1,800 | Items: types, constants, variables, operators, conversions, calls |
26+| `src/Parse.raz` | 4,400 | 3,800 | Declarations, statements, expressions, built-ins, recovery, warnings |
2727 | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places |
2828 | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers |
2929 | `src/Raz.raz` | 418 | 333 | The command line |
@@ -1636,6 +1636,14 @@
16361636 `op1` loads an operator's left; and `rotate`, which folds two constants,
16371637 right by k as left by the width less k, or has `Gen.rotate` make `rol`
16381638 or `ror`.
1639+
1640+`MIN(T)` and `MAX(T)` (report 2026-10.36) are Item's `limit`, which
1641+refuses a `T` not an integer type and makes the constant: as many ones
1642+as `T` has bits, shifted right once more if `T` is signed, is `MAX`,
1643+and minus that less 1 is a signed `MIN`. They are untyped constants
1644+but for `MAX(U64)` and `MAX(UINT)`, which are above `high(I64)`, where
1645+an untyped constant ends, and keep their type, as a hexadecimal number
1646+above it is a `U64`.
16391647
16401648 Two mismatches in `coerce` that people meet on the way to it, a slice
16411649 where its element's pointer is wanted and a pointer where a slice is,

editor/emacs/raz-mode.el (history) modified +1 -1

@@ -33,7 +33,7 @@
3333
3434 (defconst raz-builtins
3535 '("SIZE" "ADR" "LEN" "PTR" "NEW" "MSG" "SYSCALL" "FENCE" "CAS" "XCHG" "XADD"
36- "ARGC" "ARGV" "INC" "DEC" "COPY" "ROL" "ROR"))
36+ "ARGC" "ARGV" "INC" "DEC" "COPY" "ROL" "ROR" "MIN" "MAX"))
3737
3838 (defvar raz-mode-syntax-table
3939 (let ((st (make-syntax-table)))

editor/vim/syntax/raz.vim (history) modified +1 -1

@@ -15,7 +15,7 @@
1515 syntax keyword razKeyword var variant VOLATILE while xor
1616 syntax keyword razType I8 I16 I32 I64 INT UINT U8 U16 U32 U64 BOOL F32 F64 ERROR
1717 syntax keyword razBuiltin SIZE ADR LEN PTR NEW MSG SYSCALL FENCE CAS XCHG XADD
18-syntax keyword razBuiltin ARGC ARGV INC DEC COPY ROL ROR
18+syntax keyword razBuiltin ARGC ARGV INC DEC COPY ROL ROR MIN MAX
1919 syntax keyword razConstant true false nil
2020
2121 " 123, 0FFH, 1.5, 2.0E-3

src/Item.raz (history) modified +21 -0

@@ -1560,6 +1560,27 @@
15601560 return nil;
15611561 end;
15621562
1563+(* MIN(T), or MAX(T) if max: the smallest or the largest value of the
1564+ integer type t, an untyped constant (report section 6.1); p is where
1565+ T is written. MAX(U64) and MAX(UINT) are above high(I64), where the
1566+ untyped constants end, so they keep their type, as a hexadecimal
1567+ number above it is a U64 (report section 3). *)
1568+proc limit*(var x: Item; t: ^Sym.Type; max: BOOL; p: Diag.Pos): ERROR;
1569+var name: []U8; ones: U64; v: I64;
1570+begin
1571+ if t^.form # Sym.fInt then
1572+ if max then name := "MAX"; else name := "MIN"; end;
1573+ return Scan.markAt(p, say2("{} needs an integer type, found {}", name, Sym.typeName(t)));
1574+ end;
1575+ ones := (~U64(0)) shr (64 - bits(t)); (* as many ones as t has bits *)
1576+ if t^.signed then ones := ones shr 1; end;
1577+ if max then v := I64(ones);
1578+ elsif t^.signed then v := -I64(ones) - 1;
1579+ else v := 0; end;
1580+ if max and (v < 0) then makeConst(x, t, v); else makeConst(x, Sym.tConst, v); end;
1581+ return nil;
1582+end;
1583+
15631584 proc convert*(var x: Item; t: ^Sym.Type): ERROR;
15641585 var s: ^Sym.Type; r: F64;
15651586 begin

src/Parse.raz (history) modified +5 -1

@@ -1003,7 +1003,7 @@
10031003 end;
10041004
10051005 proc builtin(var x: TItem): ERROR;
1006-var which, n: INT; y: TItem; t: PType;
1006+var which, n: INT; y: TItem; t: PType; start: Diag.Pos;
10071007 begin
10081008 which := INT(x.obj^.val);
10091009 if (which = Sym.bFence) or (which = Sym.bArgc) or (which = Sym.bArgv) then
@@ -1102,6 +1102,10 @@
11021102 try expect(Scan.sComma);
11031103 try expression(y, nil);
11041104 try Item.rotate(x, y, which = Sym.bRol);
1105+ | Sym.bMin, Sym.bMax:
1106+ start := Scan.tokPos();
1107+ t := try parseType();
1108+ try Item.limit(x, t, which = Sym.bMax, Scan.spanFrom(start));
11051109 | Sym.bCopy:
11061110 try expression(x, nil);
11071111 try Item.copyDest(x);

src/Sym.raz (history) modified +3 -0

@@ -132,6 +132,7 @@
132132 bSize* = 0; bAdr* = 1; bLen* = 2; bNew* = 3; bMsg* = 4; bSyscall* = 5;
133133 bFence* = 6; bCas* = 7; bXchg* = 8; bXadd* = 9; bArgc* = 10; bArgv* = 11;
134134 bInc* = 12; bDec* = 13; bCopy* = 14; bPtr* = 15; bRol* = 16; bRor* = 17;
135+ bMin* = 18; bMax* = 19;
135136
136137 MaxDepth* = 64; (* scopes open at once *)
137138
@@ -712,4 +713,6 @@
712713 enter("COPY", oBuiltin, nil, bCopy);
713714 enter("ROL", oBuiltin, nil, bRol);
714715 enter("ROR", oBuiltin, nil, bRor);
716+ enter("MIN", oBuiltin, nil, bMin);
717+ enter("MAX", oBuiltin, nil, bMax);
715718 end Sym.

test/msg/MaxEnum.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+conform/err/MaxEnum.raz:6:14: error: MAX needs an integer type, found Color
2+ 6 | n := MAX(Color);
3+ | ^^^^^
4+raz: 1 error

test/msg/MaxValue.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+conform/err/MaxValue.raz:5:14: error: x isn't a type
2+ 5 | n := MAX(x);
3+ | ^
4+raz: 1 error

test/msg/MinReal.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+conform/err/MinReal.raz:2:19: error: MIN needs an integer type, found F64
2+ 2 | const Least = MIN(F64);
3+ | ^^^
4+raz: 1 error