raz / conform / run / Fill.raz

90 lines · 2.5 KiB · raw · history · last change 264716cb

1module Fill;
2import Out, FillKinds, U8s := Filled(U8), U16s := Filled(U16), U32s := Filled(U32),
3 U64s := Filled(U64), I8s := Filled(I8), Bools := Filled(BOOL), Ptrs := Filled(^I64),
4 Sets := Filled(FillKinds.Colours), Colours := Filled(FillKinds.Colour),
5 Rgbs := Filled(FillKinds.Rgb), Spans := Filled(FillKinds.Span);
6import SYSTEM;
7
8var
9 calls: INT; (* how many times next has been called *)
10 a: [9]U32; big: [100]U64; s: []U32; ps: [3]^I64; spans: [5]FillKinds.Span;
11 x, y: I64;
12
13(* 7, counting the call *)
14proc next(): U32;
15begin
16 INC(calls);
17 return 7;
18end;
19
20(* A span of n, counting the call *)
21proc span(n: I64): FillKinds.Span;
22begin
23 INC(calls);
24 return {lo: n, hi: -n, tag: "spanspan"};
25end;
26
27(* The sum of the elements of s *)
28proc sum(s: []U32): I64;
29var i: INT; t: I64;
30begin
31 t := 0;
32 for i := 0 to LEN(s) - 1 do INC(t, I64(s[i])); end;
33 return t;
34end;
35
36(* An array a var parameter gives *)
37proc fives(var b: [9]U32);
38begin
39 FILL(b, 5);
40end;
41
42proc main*;
43var i: INT; t: I64; p: ^[100]U64;
44begin
45 U8s.check("U8", 0A5H, 1);
46 U16s.check("U16", 0A55AH, 1);
47 U32s.check("U32", 0DEADBEEFH, 1);
48 U64s.check("U64", 0123456789ABCDEFH, 1);
49 I8s.check("I8", -2, 3);
50 Bools.check("BOOL", true, false);
51 Ptrs.check("pointer", SYSTEM.ADR(x), SYSTEM.ADR(y));
52 Sets.check("set", {FillKinds.red, FillKinds.blue}, {FillKinds.green});
53 Colours.check("enumeration", FillKinds.blue, FillKinds.green);
54 Rgbs.check("3-byte record", {r: 1, g: 2, b: 3}, {r: 9, g: 9, b: 9});
55 Spans.check("24-byte record", {lo: 1, hi: 2, tag: "abcdefgh"}, {lo: -1, hi: -2, tag: "zzzzzzzz"});
56
57 (* x is evaluated once, before any element is written *)
58 FILL(a, next());
59 s := a;
60 FILL(s[2..5], next() + 1);
61 Out.int(calls); Out.char(' '); Out.int(sum(a)); Out.ln;
62 FILL(spans, span(4));
63 Out.int(calls); Out.char(' '); Out.int(spans[0].lo + spans[4].hi); Out.char(' ');
64 Out.str(spans[4].tag[0..8]); Out.ln;
65 a[3] := 100;
66 FILL(a, a[3]); (* read once, though FILL writes a[3] *)
67 Out.int(sum(a)); Out.ln;
68 fives(a);
69 Out.int(sum(a)); Out.ln;
70
71 (* zeros *)
72 FILL(a, 0);
73 Out.int(sum(a)); Out.char(' ');
74 FILL(a, 1);
75 FILL(s[1..8], 0);
76 Out.int(sum(a)); Out.char(' ');
77 for i := 0 to 99 do big[i] := U64(i); end;
78 p := SYSTEM.ADR(big);
79 FILL(p^[10..100], 0);
80 t := 0;
81 for i := 0 to 99 do INC(t, I64(big[i])); end;
82 Out.int(t); Out.char(' ');
83 FILL(p^, 0);
84 FILL(ps, SYSTEM.ADR(x));
85 FILL(ps, nil);
86 if (big[1] = 0) and (ps[2] = nil) then Out.str("zero"); end;
87 Out.ln;
88end;
89
90end Fill.