| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- MODULE Showcase20;
- // Session showcase: array and record constructors (GNU Modula-2).
- //
- // * a CHAR-array CONST from a constructor;
- // * a CARDINAL-array CONST using the `expr BY n` repeat form;
- // * a static record constructor (a nested array-from-string field
- // and an inlined nested record field);
- // * a runtime record constructor (fields from variables);
- // * a runtime array constructor (elements from variables).
- //
- // Expected ExitCode: 42.
- IMPORT TextIO, Conversions;
- TYPE
- Name = ARRAY [0..7] OF CHAR;
- Triple = ARRAY [0..2] OF CARDINAL;
- Date = RECORD y, m, d : CARDINAL END;
- Person = RECORD name : Name; born : Date END;
- CONST
- Greet = Name { 'H','e','l','l','o','!','!','!' };
- Base = Triple { 7, 7 BY 2 }; (* the repeat form: 7, 7, 7 *)
- VAR
- p, q : Person;
- a, b : Triple;
- i : CARDINAL;
- total : CARDINAL;
- msg : ARRAY [0 .. 63] OF CHAR;
- num : ARRAY [0 .. 15] OF CHAR;
- ExitCode : INTEGER;
- BEGIN
- (* static record constructor: string field + inlined nested record *)
- p := Person { "Modula", Date { 1980, 5, 3 } };
- (* runtime record constructor: every field is a runtime value *)
- i := p.born.m; (* 5 *)
- q := Person { "Runtime", Date { i, i + 1, i + 2 } };
- (* runtime array constructor: elements from variables *)
- a := Triple { i, i + 1, i + 2 }; (* 5, 6, 7 *)
- b := Triple { Base[0], Base[1], Base[2] };
- total := 0;
- total := total + Base[0] + Base[1] + Base[2]; (* 7+7+7 = 21 *)
- total := total + a[0] + a[1] + a[2]; (* 5+6+7 = 18 *)
- total := total + p.born.d; (* 3 = 42 *)
- Conversions.CardToStr(total, num);
- msg := "showcase20 total=" + num;
- TextIO.WriteString(msg);
- TextIO.WriteLn;
- IF (total = 42)
- AND (Greet[0] = 'H') AND (Greet[7] = '!')
- AND (p.name[0] = 'M') AND (p.born.y = 1980)
- AND (q.name[0] = 'R') AND (q.born.d = 7)
- AND (b[0] = 7) AND (b[2] = 7)
- THEN ExitCode := 42 ELSE ExitCode := 1 END
- END Showcase20.
|