raz / conform / run / SmallWiden.raz

52 lines · 1.4 KiB · raw · history · last change 264716cb

1(* A U8, I8 or I16 widens to INT, and a U8 or U16 to UINT, where it is
2 moved: assigned, passed, returned, as a bound or a step, and where it
3 meets an INT in an operator; a conversion's result too (report
4 2026-10.31, sections 5.10 and 6.1). *)
5module SmallWiden;
6import Out;
7
8var g: U8; h: I16;
9
10proc show(n: INT);
11begin
12 Out.int(n); Out.str(" ");
13end;
14
15proc byte(i: INT): U8;
16begin
17 return U8(i);
18end;
19
20proc twice(n: INT): INT;
21begin
22 return 2 * n;
23end;
24
25proc wide(b: U8): INT;
26begin
27 return b; (* returned *)
28end;
29
30proc main*;
31var s: []const U8; a, b: U8; c: I8; w: U16; n, i: INT; u: UINT;
32begin
33 s := "7319";
34 for i := 0 to LEN(s) - 1 do n := n * 10 + s[i] - '0'; end;
35 show(n); (* an element, met by an INT *)
36 a := 200; b := 100;
37 show(INT(a) + b); (* 300: the sum made in INT *)
38 show(U8(a + b)); (* 44: made in U8, then converted *)
39 c := -5; n := c; show(n); (* a signed one keeps its sign *)
40 g := 255; h := -30000;
41 show(g); show(h); (* passed *)
42 show(twice(byte(300))); (* a call's result: 88 *)
43 show(wide(a));
44 if (a > n) and (c < n) then Out.str("compared "); end;
45 n := 0; INC(n, a); DEC(n, c); show(n); (* 205 *)
46 for i := c to c do show(i); end; (* bounds *)
47 w := 65535; u := w; show(INT(u));
48 u := a; show(INT(u));
49 Out.ln;
50end;
51
52end SmallWiden.