From eaa6cf3c9384f698fe6d9c8a6ac37a468188aefd Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 23 Oct 2026 12:23:40 +0100 Subject: [PATCH] INT(b): a BOOL as an integer, as report 2026-10.36 has it Item's convert takes a BOOL in its integer branch, beside enumerations and sets: false is 0 and true 1, which a BOOL's value already is, in a variable's byte and where value makes a condition's, so the conversion makes no code of its own, and of a constant it folds. U64(a = b) is the compare and its setcc, and 0 - U64(a = b) a mask. An integer to BOOL stays an error, now with help: "a comparison says it: x # 0". conform/err/AtConvert, which converted a BOOL to an I64, converts an I64 to a BOOL instead. Tests: conform/run/BoolInt (a variable to U8, I16, U32, I64 and UINT, comparisons, and and or, constants, the mask choosing with no if); conform/err BoolOfConst and BoolToReal, their messages in test/msg. compiler.md and CLAUDE.md's readings say so. Code lines 11,997 -> 12,000. --- diff --git a/CLAUDE.md b/CLAUDE.md index ed138aa..155183c 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -197,7 +197,10 @@ is an alias. A hex literal above high(I64) is a `U64`. - `p^.f` and `p^[i]` are explicit; `p[i]` on a pointer to an array is an error. A statement starts with a name, so `^T(x)^ := v` isn't one. -- No `BOOL` to integer conversion (left out until needed). +- A `BOOL` converts to every integer type, 0 or 1 (report 2026-10.36), + in `convert`'s integer branch with no code of its own, as a `BOOL`'s + value is already 0 or 1; of a constant it folds. No integer to + `BOOL`: the error's help says `x # 0`. - `for`: the step is a constant; the loop never steps past the limit, so it can't wrap; afterwards the variable holds the last value it took; the body may assign it. Leave as is. diff --git a/conform/err/AtConvert.raz b/conform/err/AtConvert.raz index 7f9d912..d17de02 100644 --- a/conform/err/AtConvert.raz +++ b/conform/err/AtConvert.raz @@ -2,6 +2,6 @@ proc main*; var b: BOOL; x: I64; begin - x := I64(b); + b := BOOL(x); end; end AtConvert. diff --git a/conform/err/BoolOfConst.raz b/conform/err/BoolOfConst.raz new file mode 100644 index 0000000..7cb6ad0 --- /dev/null +++ b/conform/err/BoolOfConst.raz @@ -0,0 +1,3 @@ +module BoolOfConst; +const Yes = BOOL(1); +end BoolOfConst. diff --git a/conform/err/BoolToReal.raz b/conform/err/BoolToReal.raz new file mode 100644 index 0000000..298c4b9 --- /dev/null +++ b/conform/err/BoolToReal.raz @@ -0,0 +1,7 @@ +module BoolToReal; +proc main*; +var b: BOOL; r: F64; +begin + r := F64(b); +end; +end BoolToReal. diff --git a/conform/run/BoolInt.expect b/conform/run/BoolInt.expect new file mode 100644 index 0000000..461c1f1 --- /dev/null +++ b/conform/run/BoolInt.expect @@ -0,0 +1,7 @@ +1 0 1 1 0 +1 0 1 +1 0 0 +1 FFFFFFFFFFFFFFFF +0000000000000000 FFFFFFFFFFFFFFFF +10 20 +2 diff --git a/conform/run/BoolInt.raz b/conform/run/BoolInt.raz new file mode 100644 index 0000000..bc860cf --- /dev/null +++ b/conform/run/BoolInt.raz @@ -0,0 +1,41 @@ +(* A BOOL converts to each integer type, false to 0 and true to 1 + (report section 6.7): a variable, a comparison, a condition made of + and and or, and a constant, in a constant too; and 0 - U64(a = b), + a mask of all ones or none, chooses with no if. *) +module BoolInt; +import Out; + +const + One = INT(true) + I8(false); (* 1 *) + Mask = 0 - U64(1 < 2); (* all ones *) + +var t, f: BOOL; + +(* x where a = b, else y, by a mask and no jump *) +proc choose(a, b, x, y: U64): U64; +var m: U64; +begin + m := 0 - U64(a = b); + return (x & m) | (y & ~m); +end; + +proc main*; +var a, b: INT; u8: U8; i16: I16; u32: U32; i64: I64; uint: UINT; +begin + t := true; + a := 3; b := 4; + u8 := U8(t); i16 := I16(f); u32 := U32(t); i64 := I64(t); uint := UINT(f); + Out.int(I64(u8)); Out.str(" "); Out.int(I64(i16)); Out.str(" "); Out.int(I64(u32)); Out.str(" "); + Out.int(i64); Out.str(" "); Out.int(I64(uint)); Out.ln; + Out.int(INT(a < b)); Out.str(" "); Out.int(INT(a = b)); Out.str(" "); + Out.int(I64(I8(a < b) - I8(a > b))); Out.ln; + Out.int(INT((a < b) and (b < 10))); Out.str(" "); Out.int(INT((a > b) or f)); Out.str(" "); + Out.int(INT(not t)); Out.ln; + Out.int(One); Out.str(" "); Out.hex(Mask, 16); Out.ln; + Out.hex(0 - U64(a = b), 16); Out.str(" "); Out.hex(0 - U64(a # b), 16); Out.ln; + Out.int(I64(choose(5, 5, 10, 20))); Out.str(" "); Out.int(I64(choose(5, 6, 10, 20))); Out.ln; + (* counting what holds *) + Out.int(INT(a > 0) + INT(b > 0) + INT(a > b)); Out.ln; +end; + +end BoolInt. diff --git a/doc/compiler.md b/doc/compiler.md index 1941b27..2e1aec1 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -22,7 +22,7 @@ | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | | `src/X64.raz` | 1,605 | 1,318 | The instruction encoder, jumps within a procedure, the assembler | | `src/Gen.raz` | 2,666 | 1,885 | The stack machine, for x86-64: values, operations, checks, calls, frames | -| `src/Item.raz` | 2,293 | 1,841 | Items: types, constants, variables, operators, conversions, calls | +| `src/Item.raz` | 2,297 | 1,844 | Items: types, constants, variables, operators, conversions, calls | | `src/Parse.raz` | 4,403 | 3,803 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | @@ -1395,6 +1395,12 @@ results on as they are, so `resultsFit` takes only `INT` to `I64`. `assign` stores, or copies records and arrays. `convert` is the written conversion, `T(x)`. +A `BOOL` converts to an integer (report 2026-10.36) as an integer +does, with no code of its own: its value is already 0 or 1, in a +variable's byte and where `value` makes a condition's (by `setcc` and +`movzx`, or 1 and 0 where `and` and `or` jump to), so `U64(a = b)` is +the compare and `setcc`, and a constant `BOOL`'s is folded. An integer to `BOOL` stays an error, with help +that says `x # 0`. Views, `v.(T)` (report section 6.9): a view is the item of `v` with `T` for its type. It is at the same place, so nothing is copied or diff --git a/src/Item.raz b/src/Item.raz index d9296ac..7e3f683 100644 --- a/src/Item.raz +++ b/src/Item.raz @@ -1640,7 +1640,8 @@ end; x.made := false; (* what a conversion gives widens, U8(a + b) too *) if Sym.same(s, t) then return nil; end; - if (Sym.isInt(s) or (s^.form in {Sym.fEnum, Sym.fSet})) and (t^.form = Sym.fInt) then + (* a BOOL is 0 or 1, as a comparison's value is, so it converts as an integer does *) + if (Sym.isInt(s) or (s^.form in {Sym.fEnum, Sym.fSet, Sym.fBool})) and (t^.form = Sym.fInt) then if x.mode = mConst then x.val := cut(x.val, t); else try value(x); Gen.narrow(INT(t^.size), t^.signed); end; elsif Sym.isInt(s) and (t^.form = Sym.fEnum) then @@ -1687,6 +1688,9 @@ if x.mode # mConst then try value(x); end; elsif (s^.form in {Sym.fPtr, Sym.fProc}) and isWord(t) then try value(x); + elsif Sym.isInt(s) and (t^.form = Sym.fBool) then + return Scan.markHelp(x.pos, say1("can't convert {} to BOOL", Sym.typeName(s)), + say1("a comparison says it: {} # 0", Diag.source(x.pos))); else return Scan.markAt(x.pos, say2("can't convert {} to {}", Sym.typeName(s), Sym.typeName(t))); end; diff --git a/test/msg/AtConvert.err b/test/msg/AtConvert.err index 28aa2d8..f362041 100644 --- a/test/msg/AtConvert.err +++ b/test/msg/AtConvert.err @@ -1,4 +1,5 @@ -conform/err/AtConvert.raz:5:14: error: can't convert BOOL to I64 - 5 | x := I64(b); - | ^ +conform/err/AtConvert.raz:5:15: error: can't convert I64 to BOOL + 5 | b := BOOL(x); + | ^ +help: a comparison says it: x # 0 raz: 1 error diff --git a/test/msg/BoolOfConst.err b/test/msg/BoolOfConst.err new file mode 100644 index 0000000..919c55b --- /dev/null +++ b/test/msg/BoolOfConst.err @@ -0,0 +1,5 @@ +conform/err/BoolOfConst.raz:2:18: error: can't convert an integer constant to BOOL + 2 | const Yes = BOOL(1); + | ^ +help: a comparison says it: 1 # 0 +raz: 1 error diff --git a/test/msg/BoolToReal.err b/test/msg/BoolToReal.err new file mode 100644 index 0000000..2052e3d --- /dev/null +++ b/test/msg/BoolToReal.err @@ -0,0 +1,4 @@ +conform/err/BoolToReal.raz:5:14: error: can't convert BOOL to F64 + 5 | r := F64(b); + | ^ +raz: 1 error