Просмотр исходного кода

v3 step 8.2/8.3 showcase — Showcase12: const folding, forward types, trailing ';', empty statements, 1-char strings, qualified types (106/106)

Eric Streit 2 недель назад
Родитель
Сommit
a1453d940b

+ 3 - 0
compiler/run_tests.sh

@@ -247,6 +247,9 @@ expect_run_files_in Showcase11 80 "Modula-2" "42" \
   ../stdlib/files.def ../stdlib/files.mod \
   showcase11.mod
 rm -f gen_ssa/_showcase11.txt
+# step 8.2/8.3: const folding, forward types, trailing ';', empty
+# statements, 1-char strings, qualified type names.
+expect_run_files Showcase12 42 showcase12lib.def showcase12lib.mod showcase12.mod
 expect_run_files_out MixProg 0 "7" ../stdlib/textio.def ../stdlib/textio.mod ../stdlib/sysio.def ../stdlib/sysio.mod ../stdlib/files.def ../stdlib/files.mod d_mix_prog.mod
 rm -f gen_ssa/_mix.txt
 

+ 41 - 0
compiler/tests/showcase12.mod

@@ -0,0 +1,41 @@
+MODULE Showcase12;
+// Step-8.2/8.3 showcase. Exercises, in one small session:
+//   - named-CONST folding in an array bound (M = N*2-1)
+//   - a forward pointer type (Link = POINTER TO Node; Node later)
+//   - a trailing ';' before a record's END
+//   - empty statements (';;' and a lone ';')
+//   - 1-character string literals passed to ARRAY OF CHAR
+//   - a qualified type name (Showcase12Lib.Point) and qualified call
+// Expected ExitCode: 42.
+IMPORT Showcase12Lib;
+
+CONST
+  N = 4;              // folded: N used below
+  M = N * 2 - 1;      // folds to 7 — the bound of Vec
+
+TYPE
+  Vec  = ARRAY [0 .. M] OF INTEGER;   // 8 elements
+  Link = POINTER TO Node;             // forward: Node is declared later
+  Node = RECORD
+    value : INTEGER;
+    next  : Link;      // trailing ';' before END is accepted
+  END;
+
+VAR
+  ExitCode : INTEGER;
+  v        : Vec;
+  head     : Link;
+  pt       : Showcase12Lib.Point;     // qualified type name
+  i, sum   : INTEGER;
+
+BEGIN
+  sum := 0;
+  FOR i := 0 TO M DO v[i] := i; sum := sum + v[i] END;;  // 0+..+7 = 28
+  ;                                                     // empty statement
+  NEW(head); head^.value := 9; head^.next := NIL;
+  sum := sum + head^.value;                             // 37
+  sum := sum + Showcase12Lib.Score("A")
+            + Showcase12Lib.Score("B");                 // +2+3 = 42
+  pt.x := 42;
+  IF sum = pt.x THEN ExitCode := sum ELSE ExitCode := 1 END
+END Showcase12.

+ 15 - 0
compiler/tests/showcase12lib.def

@@ -0,0 +1,15 @@
+DEFINITION MODULE Showcase12Lib;
+(* Step-8.2/8.3 showcase helper. Exports Point (used by the program
+   through the qualified type name Showcase12Lib.Point) and Score,
+   whose formal is ARRAY OF CHAR so 1-character string literals such
+   as "A" coerce to it. *)
+
+TYPE
+  Point = RECORD
+    x, y : INTEGER;
+  END;
+
+PROCEDURE Score (s : ARRAY OF CHAR) : INTEGER;
+(* "A" -> 2, "B" -> 3, anything else -> 0. *)
+
+END Showcase12Lib.

+ 10 - 0
compiler/tests/showcase12lib.mod

@@ -0,0 +1,10 @@
+IMPLEMENTATION MODULE Showcase12Lib;
+
+PROCEDURE Score (s : ARRAY OF CHAR) : INTEGER;
+BEGIN
+  IF s[0] = "A" THEN RETURN 2 END;
+  IF s[0] = "B" THEN RETURN 3 END;
+  RETURN 0
+END Score;
+
+END Showcase12Lib.