Browse Source

showcase21: result suffixes + coroutines + standard library

compiler/tests/showcase21.mod tours:
- result suffixes (GetRec()^.field, GetArr()^[i]);
- PIM coroutines (Ping/Pong ping-pong via TRANSFER);
- the new ISO/gm2 library (RealStr, LongMath, WholeStr, DynamicStrings),
  building the label with DynamicStrings and printing
  "showcase21 total=42"; exit 42.
Suite 174/174; adds docs/summary_showcase21.md.
Eric Streit 1 week ago
parent
commit
678f20e835
3 changed files with 188 additions and 0 deletions
  1. 10 0
      compiler/run_tests.sh
  2. 99 0
      compiler/tests/showcase21.mod
  3. 79 0
      docs/summary_showcase21.md

+ 10 - 0
compiler/run_tests.sh

@@ -347,6 +347,16 @@ expect_run_files_out Showcase20 42 "showcase20 total=42" \
   ../stdlib/textio.def ../stdlib/textio.mod \
   ../stdlib/conversions.def ../stdlib/conversions.mod \
   showcase20.mod
+expect_run_files_out Showcase21 42 "showcase21 total=42" \
+  ../stdlib/convtypes.def ../stdlib/convtypes.mod \
+  ../stdlib/conversions.def ../stdlib/conversions.mod \
+  ../stdlib/math.def ../stdlib/math.mod \
+  ../stdlib/wholestr.def ../stdlib/wholestr.mod \
+  ../stdlib/realstr.def ../stdlib/realstr.mod \
+  ../stdlib/longmath.def ../stdlib/longmath.mod \
+  ../stdlib/dynamicstrings.def ../stdlib/dynamicstrings.mod \
+  ../stdlib/strio.def ../stdlib/strio.mod \
+  showcase21.mod
 expect_run_files_out Showcase18 42 "showcase18 areas sum=139" \
   ../runtime/syslib/Utf8.def ../runtime/syslib/Utf8.mod \
   ../stdlib/textio.def ../stdlib/textio.mod \

+ 99 - 0
compiler/tests/showcase21.mod

@@ -0,0 +1,99 @@
+MODULE Showcase21;
+// Tour of three features added this step:
+//
+//   * result suffixes — components applied to a function-call result
+//     (GetRec()^.field, GetArr()^[i]);
+//   * PIM coroutines from SYSTEM — PROCESS / NEWPROCESS / TRANSFER,
+//     two coroutines ping-pong five times and hand control back;
+//   * ISO/gm2 standard library — RealStr (RealToFixed), LongMath
+//     (sqrt/round), WholeStr (CardToStr) and DynamicStrings
+//     (InitString/ConCat/CopyOut) build the output label.
+//
+// Writes "showcase21 total=42"; expected ExitCode: 42.
+IMPORT StrIO, WholeStr, RealStr, LongMath, DynamicStrings;
+FROM DynamicStrings IMPORT String;
+FROM SYSTEM IMPORT ADDRESS, PROCESS, NEWPROCESS, TRANSFER, ADR;
+
+TYPE
+  Rec    = RECORD a, b : INTEGER END;
+  RecPtr = POINTER TO Rec;
+  Arr    = ARRAY [0..3] OF INTEGER;
+  ArrPtr = POINTER TO Arr;
+
+VAR ExitCode : INTEGER;
+VAR r : RecPtr;
+VAR s : ArrPtr;
+VAR total : INTEGER;
+VAR x : REAL;
+VAR hits : CARDINAL;
+
+VAR ping, pong, exits : PROCESS;
+VAR ws1, ws2 : ARRAY [0..65535] OF CHAR;
+
+VAR num, label, out : ARRAY [0..63] OF CHAR;
+VAR ds : String;
+
+PROCEDURE GetRec () : RecPtr;
+BEGIN RETURN r END GetRec;
+
+PROCEDURE GetArr () : ArrPtr;
+BEGIN RETURN s END GetArr;
+
+PROCEDURE Ping;
+BEGIN
+  LOOP
+    TRANSFER(ping, pong)
+  END
+END Ping;
+
+PROCEDURE Pong;
+BEGIN
+  LOOP
+    INC(hits);
+    IF hits >= 5 THEN TRANSFER(pong, exits) END;
+    TRANSFER(pong, ping)
+  END
+END Pong;
+
+BEGIN
+  ExitCode := 0;
+
+  (* --- result suffixes --- *)
+  NEW(r); NEW(s);
+  r^.a := 40; r^.b := 2;
+  s^[0] := 1; s^[1] := 2; s^[2] := 2; s^[3] := 4;
+  IF GetRec()^.a + GetRec()^.b = 42 THEN
+    ExitCode := ExitCode + 1
+  ELSE ExitCode := 100 END;
+  IF GetArr()^[1] + GetArr()^[2] = 4 THEN
+    ExitCode := ExitCode + 2
+  ELSE ExitCode := 100 END;
+
+  total := 40;
+  total := total + GetArr()^[1] + GetArr()^[2];   (* 44 *)
+  total := total - GetRec()^.b;                    (* 42 *)
+
+  (* --- ISO/gm2 library --- *)
+  RealStr.RealToFixed(42.0, 0, num);               (* "42" *)
+  x := LongMath.sqrt(1764.0);                      (* 42.0 *)
+  IF (num[0] = "4") AND (num[1] = "2") AND
+     (LongMath.round(x) = 42) THEN
+    ExitCode := ExitCode + 4
+  ELSE ExitCode := 100 END;
+
+  (* --- coroutines --- *)
+  NEWPROCESS(Ping, ADR(ws1), 65536, ping);
+  NEWPROCESS(Pong, ADR(ws2), 65536, pong);
+  TRANSFER(exits, ping);
+  IF hits = 5 THEN ExitCode := ExitCode + 8 ELSE ExitCode := 100 END;
+
+  (* --- output line via DynamicStrings + WholeStr --- *)
+  WholeStr.CardToStr(VAL(CARDINAL, total), num);
+  ds := DynamicStrings.InitString("showcase21 total=");
+  ds := DynamicStrings.ConCat(ds, DynamicStrings.InitString(num));
+  DynamicStrings.CopyOut(out, ds);
+  StrIO.WriteString(out);
+  StrIO.WriteLn;
+
+  IF ExitCode = 15 THEN ExitCode := 42 END
+END Showcase21.

+ 79 - 0
docs/summary_showcase21.md

@@ -0,0 +1,79 @@
+# Session summary — 2026-10-01 (b): showcase21 — suffixes, coroutines, library
+
+Suite **174/174**; self-hosting fixpoint **OK** (image
+**2,785,569 bytes**).  `master`, tree clean except the user's
+uncommitted `compiler/toto.mod`.
+
+This step tours the three features landed this session on items 2, 4
+and 6 of the gaps list (1 `GOTO`, 3 generics, 5 multiple inheritance
+were deliberately skipped).
+
+## Steps, tags and headline commits
+
+| Step | Tag | Commit | What |
+| --- | --- | --- | --- |
+| Language + library | `v3-resultsfx-coroutines-lib` | `31adfc3` | suffixes, coroutines, core library |
+| Notes | — | `e844223` | session summary |
+| `LongIO` | — | `b1a52db` | channel-based LONGREAL I/O |
+| Showcase | `v3-showcase21` | (this commit) | showcase21 end to end |
+
+## 1. Result suffixes
+
+`F()^`, `F()[i]`, `F().field` — components applied to a function-call
+result (`ResultComp` in `M2.atg`, inside the call group so the Coco/R
+grammar stays LL(1)-clean).  A final scalar is loaded with `ElemLoad`;
+aggregates stay as their descriptor address.  Expression context only.
+
+## 2. PIM coroutines
+
+`PROCESS` plus `NEWPROCESS`/`TRANSFER`/`IOTRANSFER` are predefined in
+`SYSTEM` (with a new parameterless `PROC` type) and bind to
+`ucontext`-backed helpers in `runtime/syslib/shim.c`.  Coroutine bodies
+are called with a 0 static link (module-level procedures).
+
+## 3. Standard library
+
+gm2/ISO names added under `stdlib/`: `ConvTypes`, `IOConsts`,
+`SIOResult`, `TERMINATION`, `StrIO`, `InOut`, `StdIO`, `WholeStr`,
+`RealStr`, `LongStr`, `LongMath`, `LongIO`, `STextIO`, `SWholeIO`,
+`SRealIO`, `DynamicStrings` (core) and `SysStorage`.  Backend fixes
+alongside: procedure values / `LONGINT` are 8-byte in array load/store
+and the parameter prologue; driver session file cap 32 → 128;
+`Math.ln`/`arctan` wrap `log`/`atan`.
+
+## 4. Showcase
+
+`compiler/tests/showcase21.mod` (`Showcase21`, exit 42) exercises:
+
+- **result suffixes** — `GetRec()^.a + GetRec()^.b = 42`,
+  `GetArr()^[i]` sums to 4, then `40 + 2 + 2 - 2 = 42`;
+- **coroutines** — `Ping`/`Pong` ping-pong via `TRANSFER`, hand control
+  back to the main process after five hits;
+- **library** — `RealStr.RealToFixed(42.0, 0, num)` gives `"42"`,
+  `LongMath.sqrt(1764.0)` round-trips to 42, and `WholeStr.CardToStr`
+  + `DynamicStrings` build the label.
+
+```
+showcase21 total=42
+```
+
+## Tests (174, was 171)
+
+New this session: `t_resultsfx`, `t_coroutine`, `t_dynstr`, `library1`
+(16-module session) and `Showcase21`.  Full suite green.
+
+## Resume
+
+```sh
+cd .../m2compiler-V3
+./bootstrap/fixpoint.sh                       # FIXPOINT OK
+cd compiler && ./build.sh && ./run_tests.sh   # 174/174
+```
+
+## Remaining
+
+`GOTO`/labels, generics, multiple inheritance (deliberate), full
+forward variables, statement-context result suffixes, and the
+channel/device library holes (`LongWholeIO`, `wrapclock`/`wraptime`/
+`wrapc`, `TimeString`, `ChanConsts`/`GenDevIF`/`DeviceTablePtr`/
+`StdChans`/`StreamFile`/`FIO`).