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.
| @@ -197,7 +197,10 @@ | ||
| 197 | 197 | is an alias. A hex literal above high(I64) is a `U64`. |
| 198 | 198 | - `p^.f` and `p^[i]` are explicit; `p[i]` on a pointer to an array is |
| 199 | 199 | an error. A statement starts with a name, so `^T(x)^ := v` isn't one. |
| 200 | -- No `BOOL` to integer conversion (left out until needed). | |
| 200 | +- A `BOOL` converts to every integer type, 0 or 1 (report 2026-10.36), | |
| 201 | + in `convert`'s integer branch with no code of its own, as a `BOOL`'s | |
| 202 | + value is already 0 or 1; of a constant it folds. No integer to | |
| 203 | + `BOOL`: the error's help says `x # 0`. | |
| 201 | 204 | - `for`: the step is a constant; the loop never steps past the limit, so |
| 202 | 205 | it can't wrap; afterwards the variable holds the last value it took; |
| 203 | 206 | the body may assign it. Leave as is. |
| @@ -2,6 +2,6 @@ | ||
| 2 | 2 | proc main*; |
| 3 | 3 | var b: BOOL; x: I64; |
| 4 | 4 | begin |
| 5 | - x := I64(b); | |
| 5 | + b := BOOL(x); | |
| 6 | 6 | end; |
| 7 | 7 | end AtConvert. |
| @@ -0,0 +1,3 @@ | ||
| 1 | +module BoolOfConst; | |
| 2 | +const Yes = BOOL(1); | |
| 3 | +end BoolOfConst. |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module BoolToReal; | |
| 2 | +proc main*; | |
| 3 | +var b: BOOL; r: F64; | |
| 4 | +begin | |
| 5 | + r := F64(b); | |
| 6 | +end; | |
| 7 | +end BoolToReal. |
| @@ -0,0 +1,7 @@ | ||
| 1 | +1 0 1 1 0 | |
| 2 | +1 0 1 | |
| 3 | +1 0 0 | |
| 4 | +1 FFFFFFFFFFFFFFFF | |
| 5 | +0000000000000000 FFFFFFFFFFFFFFFF | |
| 6 | +10 20 | |
| 7 | +2 |
| @@ -0,0 +1,41 @@ | ||
| 1 | +(* A BOOL converts to each integer type, false to 0 and true to 1 | |
| 2 | + (report section 6.7): a variable, a comparison, a condition made of | |
| 3 | + and and or, and a constant, in a constant too; and 0 - U64(a = b), | |
| 4 | + a mask of all ones or none, chooses with no if. *) | |
| 5 | +module BoolInt; | |
| 6 | +import Out; | |
| 7 | + | |
| 8 | +const | |
| 9 | + One = INT(true) + I8(false); (* 1 *) | |
| 10 | + Mask = 0 - U64(1 < 2); (* all ones *) | |
| 11 | + | |
| 12 | +var t, f: BOOL; | |
| 13 | + | |
| 14 | +(* x where a = b, else y, by a mask and no jump *) | |
| 15 | +proc choose(a, b, x, y: U64): U64; | |
| 16 | +var m: U64; | |
| 17 | +begin | |
| 18 | + m := 0 - U64(a = b); | |
| 19 | + return (x & m) | (y & ~m); | |
| 20 | +end; | |
| 21 | + | |
| 22 | +proc main*; | |
| 23 | +var a, b: INT; u8: U8; i16: I16; u32: U32; i64: I64; uint: UINT; | |
| 24 | +begin | |
| 25 | + t := true; | |
| 26 | + a := 3; b := 4; | |
| 27 | + u8 := U8(t); i16 := I16(f); u32 := U32(t); i64 := I64(t); uint := UINT(f); | |
| 28 | + Out.int(I64(u8)); Out.str(" "); Out.int(I64(i16)); Out.str(" "); Out.int(I64(u32)); Out.str(" "); | |
| 29 | + Out.int(i64); Out.str(" "); Out.int(I64(uint)); Out.ln; | |
| 30 | + Out.int(INT(a < b)); Out.str(" "); Out.int(INT(a = b)); Out.str(" "); | |
| 31 | + Out.int(I64(I8(a < b) - I8(a > b))); Out.ln; | |
| 32 | + Out.int(INT((a < b) and (b < 10))); Out.str(" "); Out.int(INT((a > b) or f)); Out.str(" "); | |
| 33 | + Out.int(INT(not t)); Out.ln; | |
| 34 | + Out.int(One); Out.str(" "); Out.hex(Mask, 16); Out.ln; | |
| 35 | + Out.hex(0 - U64(a = b), 16); Out.str(" "); Out.hex(0 - U64(a # b), 16); Out.ln; | |
| 36 | + Out.int(I64(choose(5, 5, 10, 20))); Out.str(" "); Out.int(I64(choose(5, 6, 10, 20))); Out.ln; | |
| 37 | + (* counting what holds *) | |
| 38 | + Out.int(INT(a > 0) + INT(b > 0) + INT(a > b)); Out.ln; | |
| 39 | +end; | |
| 40 | + | |
| 41 | +end BoolInt. |
| @@ -22,7 +22,7 @@ | ||
| 22 | 22 | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | |
| 23 | 23 | | `src/X64.raz` | 1,605 | 1,318 | The instruction encoder, jumps within a procedure, the assembler | |
| 24 | 24 | | `src/Gen.raz` | 2,666 | 1,885 | The stack machine, for x86-64: values, operations, checks, calls, frames | |
| 25 | -| `src/Item.raz` | 2,293 | 1,841 | Items: types, constants, variables, operators, conversions, calls | | |
| 25 | +| `src/Item.raz` | 2,297 | 1,844 | Items: types, constants, variables, operators, conversions, calls | | |
| 26 | 26 | | `src/Parse.raz` | 4,403 | 3,803 | Declarations, statements, expressions, built-ins, recovery, warnings | |
| 27 | 27 | | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places | |
| 28 | 28 | | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | |
| @@ -1395,6 +1395,12 @@ | ||
| 1395 | 1395 | results on as they are, so `resultsFit` takes only `INT` to `I64`. |
| 1396 | 1396 | `assign` stores, or |
| 1397 | 1397 | copies records and arrays. `convert` is the written conversion, `T(x)`. |
| 1398 | +A `BOOL` converts to an integer (report 2026-10.36) as an integer | |
| 1399 | +does, with no code of its own: its value is already 0 or 1, in a | |
| 1400 | +variable's byte and where `value` makes a condition's (by `setcc` and | |
| 1401 | +`movzx`, or 1 and 0 where `and` and `or` jump to), so `U64(a = b)` is | |
| 1402 | +the compare and `setcc`, and a constant `BOOL`'s is folded. An integer to `BOOL` stays an error, with help | |
| 1403 | +that says `x # 0`. | |
| 1398 | 1404 | |
| 1399 | 1405 | Views, `v.(T)` (report section 6.9): a view is the item of `v` with |
| 1400 | 1406 | `T` for its type. It is at the same place, so nothing is copied or |
| @@ -1640,7 +1640,8 @@ | ||
| 1640 | 1640 | end; |
| 1641 | 1641 | x.made := false; (* what a conversion gives widens, U8(a + b) too *) |
| 1642 | 1642 | if Sym.same(s, t) then return nil; end; |
| 1643 | - if (Sym.isInt(s) or (s^.form in {Sym.fEnum, Sym.fSet})) and (t^.form = Sym.fInt) then | |
| 1643 | + (* a BOOL is 0 or 1, as a comparison's value is, so it converts as an integer does *) | |
| 1644 | + if (Sym.isInt(s) or (s^.form in {Sym.fEnum, Sym.fSet, Sym.fBool})) and (t^.form = Sym.fInt) then | |
| 1644 | 1645 | if x.mode = mConst then x.val := cut(x.val, t); |
| 1645 | 1646 | else try value(x); Gen.narrow(INT(t^.size), t^.signed); end; |
| 1646 | 1647 | elsif Sym.isInt(s) and (t^.form = Sym.fEnum) then |
| @@ -1687,6 +1688,9 @@ | ||
| 1687 | 1688 | if x.mode # mConst then try value(x); end; |
| 1688 | 1689 | elsif (s^.form in {Sym.fPtr, Sym.fProc}) and isWord(t) then |
| 1689 | 1690 | try value(x); |
| 1691 | + elsif Sym.isInt(s) and (t^.form = Sym.fBool) then | |
| 1692 | + return Scan.markHelp(x.pos, say1("can't convert {} to BOOL", Sym.typeName(s)), | |
| 1693 | + say1("a comparison says it: {} # 0", Diag.source(x.pos))); | |
| 1690 | 1694 | else |
| 1691 | 1695 | return Scan.markAt(x.pos, say2("can't convert {} to {}", Sym.typeName(s), Sym.typeName(t))); |
| 1692 | 1696 | end; |
| @@ -1,4 +1,5 @@ | ||
| 1 | -conform/err/AtConvert.raz:5:14: error: can't convert BOOL to I64 | |
| 2 | - 5 | x := I64(b); | |
| 3 | - | ^ | |
| 1 | +conform/err/AtConvert.raz:5:15: error: can't convert I64 to BOOL | |
| 2 | + 5 | b := BOOL(x); | |
| 3 | + | ^ | |
| 4 | +help: a comparison says it: x # 0 | |
| 4 | 5 | raz: 1 error |
| @@ -0,0 +1,5 @@ | ||
| 1 | +conform/err/BoolOfConst.raz:2:18: error: can't convert an integer constant to BOOL | |
| 2 | + 2 | const Yes = BOOL(1); | |
| 3 | + | ^ | |
| 4 | +help: a comparison says it: 1 # 0 | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/BoolToReal.raz:5:14: error: can't convert BOOL to F64 | |
| 2 | + 5 | r := F64(b); | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |