Parcourir la source

v3 step 5.5 — standard-library showcase, all six libs (95/95 tests green)

Eric Streit il y a 2 semaines
Parent
commit
b88367662e
3 fichiers modifiés avec 110 ajouts et 0 suppressions
  1. 9 0
      compiler/run_tests.sh
  2. 54 0
      compiler/tests/showcase11.mod
  3. 47 0
      docs/summary_step5.5.md

+ 9 - 0
compiler/run_tests.sh

@@ -230,6 +230,15 @@ expect_run_files_in ReadProg 0 "42" "20 22" ../stdlib/sysio.def ../stdlib/sysio.
 expect_run_files_in WholeIOProg 0 "42" "6 7" ../stdlib/sysio.def ../stdlib/sysio.mod ../stdlib/wholeio.def ../stdlib/wholeio.mod wholeio_prog.mod
 expect_run_files FilesProg 42 ../stdlib/files.def ../stdlib/files.mod files_prog.mod
 rm -f gen_ssa/_files_tmp.txt
+expect_run_files_in Showcase11 80 "Modula-2" "42" \
+  ../stdlib/sysio.def ../stdlib/sysio.mod \
+  ../stdlib/textio.def ../stdlib/textio.mod \
+  ../stdlib/wholeio.def ../stdlib/wholeio.mod \
+  ../stdlib/strings.def ../stdlib/strings.mod \
+  ../stdlib/math.def ../stdlib/math.mod \
+  ../stdlib/files.def ../stdlib/files.mod \
+  showcase11.mod
+rm -f gen_ssa/_showcase11.txt
 
 expect_fail_files() {
   # $1 = listing basename, $2 = message (may be empty), then files

+ 54 - 0
compiler/tests/showcase11.mod

@@ -0,0 +1,54 @@
+MODULE Showcase11;
+(* Standard-library tour: SysIO, TextIO, WholeIO, Strings, Math,
+   Files. Reads one CARDINAL from stdin (e.g. 42).
+   Prints: Modula-2 / 8 / 42 / 12 / 42 ; exit 80. *)
+
+IMPORT Files;
+FROM Files IMPORT File;
+FROM Math IMPORT sqrt, Pi;
+FROM Strings IMPORT Assign, Concat, Length;
+FROM SysIO IMPORT Write, WriteInt, WriteLn;
+FROM TextIO IMPORT WriteCard;
+FROM WholeIO IMPORT ReadCard;
+
+VAR ExitCode : INTEGER;
+VAR s, t, u : ARRAY [0..31] OF CHAR;
+VAR r : REAL;
+VAR f : File;
+VAR n : INTEGER;
+VAR card : CARDINAL;
+
+BEGIN
+  (* Strings: build "Modula-2" and report its length. *)
+  Assign("Modula", s);
+  Assign("-2", t);
+  Concat(s, t, u);
+  Write(u);
+  WriteLn;
+  WriteInt(Length(u));           (* 8 *)
+  WriteLn;
+
+  (* WholeIO input + TextIO output: echo the read card. *)
+  ReadCard(card);
+  WriteCard(card);               (* 42 *)
+  WriteLn;
+
+  (* Math: sqrt(144) = 12. *)
+  r := sqrt(144.0) + Pi - Pi;
+  IF r > 11.9 THEN n := 12 ELSE n := 0 END;
+  WriteInt(n);                   (* 12 *)
+  WriteLn;
+
+  (* Files: write two chars, read the integer back. *)
+  f := Files.OpenWrite("gen_ssa/_showcase11.txt");
+  Files.WriteChar(f, '4');
+  Files.WriteChar(f, '2');
+  Files.Close(f);
+  f := Files.OpenRead("gen_ssa/_showcase11.txt");
+  n := Files.ReadInt(f);
+  Files.Close(f);
+  WriteInt(n);                   (* 42 *)
+  WriteLn;
+
+  ExitCode := n - 12 + Length(u) + card
+END Showcase11.

+ 47 - 0
docs/summary_step5.5.md

@@ -0,0 +1,47 @@
+# V3 step 5.5 — library showcase (done 2026-09-22)
+
+`tests/showcase11.mod` tours every standard library in one
+separate-compilation session. Suite 95/95.
+
+## Session
+
+`showcase11.mod` + all six libraries
+(`sysio`, `textio`, `wholeio`, `strings`, `math`, `files`) built
+and linked into one image, run with stdin `42`.
+
+## Output
+
+```
+Modula-2
+8
+42
+12
+42
+```
+Exit **80**. Verified by `expect_run_files_in`
+(`Showcase11 → 80 [Modula-2]`, stdin `42`).
+
+## What it exercises
+
+- **Strings** — `Assign` ×2, `Concat` → `"Modula-2"`, `Length` → 8.
+- **WholeIO** — `ReadCard` from stdin (42).
+- **TextIO** — `WriteCard` echoes it.
+- **Math** — `sqrt(144.0) + Pi - Pi` → 12.
+- **Files** — `OpenWrite`, two `WriteChar`, `Close`, `OpenRead`,
+  `ReadInt` → 42.
+- **SysIO** — `Write` (string) + `WriteInt`/`WriteLn`.
+- **Exit code** — `n - 12 + Length(u) + card` = `42-12+8+42` = 80.
+
+## Constraint discovered while writing it
+
+Because qualified access flattens names into the client scope, two
+libraries exporting the same routine name (e.g. `TextIO.WriteInt`
+and `Files.WriteInt`) cannot both be *used* in one scope — the
+second resolves to the first. The showcase sidesteps this by using
+each library's **distinct** names (the materialized set is
+collision-free: `Write`/`WriteInt`/`WriteLn`/`WriteCard`/`ReadCard`/
+`OpenWrite`/`WriteChar`/`Close`/`OpenRead`/`ReadInt`/`Assign`/
+`Concat`/`Length`/`sqrt`/`Pi`). This is the concrete trigger for the
+symbol-naming / real-qualified-resolution pass recorded in
+`docs/session-2026-09-22-step5.md`; until then, callers must avoid
+same-named imports from two modules.