raz / conform / run / BoolInt.raz

41 lines · 1.5 KiB · raw · history · last change eaa6cf3c

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. *)
5module BoolInt;
6import Out;
7
8const
9 One = INT(true) + I8(false); (* 1 *)
10 Mask = 0 - U64(1 < 2); (* all ones *)
11
12var t, f: BOOL;
13
14(* x where a = b, else y, by a mask and no jump *)
15proc choose(a, b, x, y: U64): U64;
16var m: U64;
17begin
18 m := 0 - U64(a = b);
19 return (x & m) | (y & ~m);
20end;
21
22proc main*;
23var a, b: INT; u8: U8; i16: I16; u32: U32; i64: I64; uint: UINT;
24begin
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;
39end;
40
41end BoolInt.