Jelajahi Sumber

showcase19: class init body + sibling method calls

- Showcase19 (exit 42): Accumulator sets a global scale in its class
  BEGIN body (run at startup); methods call each other by bare name
  (AddPair -> AddScaled -> Add, a sibling-call chain on THIS); the
  result is rendered with Conversions + CHAR-array concatenation.
- docs/summary_showcase19.md: session summary (CLASS finished +
  showcase).
- Suite 147/147; fixpoint OK (2,313,762 bytes).
Eric Streit 2 minggu lalu
induk
melakukan
9f1f2784c6
4 mengubah file dengan 133 tambahan dan 1 penghapusan
  1. 5 0
      compiler/run_tests.sh
  2. 58 0
      compiler/tests/showcase19.mod
  3. 1 1
      docs/features.md
  4. 69 0
      docs/summary_showcase19.md

+ 5 - 0
compiler/run_tests.sh

@@ -289,6 +289,11 @@ rm -f gen_ssa/_showcase11.txt
 # statements, 1-char strings, qualified type names.
 expect_run_files Showcase12 42 showcase12lib.def showcase12lib.mod showcase12.mod
 expect_run_files_out Showcase13 42 "Even(8)=T Odd(7)=T total=42" ../stdlib/sysio.def ../stdlib/sysio.mod showcase13.mod
+expect_run_files_out Showcase19 42 "showcase19 total=42" \
+  ../runtime/syslib/Utf8.def ../runtime/syslib/Utf8.mod \
+  ../stdlib/textio.def ../stdlib/textio.mod \
+  ../stdlib/conversions.def ../stdlib/conversions.mod \
+  showcase19.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 \

+ 58 - 0
compiler/tests/showcase19.mod

@@ -0,0 +1,58 @@
+MODULE Showcase19;
+// Session showcase: class init body + sibling method calls.
+//
+// The Accumulator class sets a global scale factor in its BEGIN body
+// (run once at startup); its methods call each other by bare name --
+// chains of sibling calls on THIS.
+// Expected ExitCode: 42.
+IMPORT TextIO, Conversions;
+
+VAR scale : INTEGER;               (* set by Accumulator's init body *)
+
+TYPE
+  CLASS Accumulator;
+    total : INTEGER;
+    PROCEDURE Init(v : INTEGER);
+    PROCEDURE Add(v : INTEGER);
+    PROCEDURE AddScaled(v : INTEGER);      (* sibling: Add *)
+    PROCEDURE AddPair(a, b : INTEGER);     (* sibling: AddScaled *)
+    PROCEDURE Value() : INTEGER;
+  END Accumulator;
+
+CLASS IMPLEMENTATION Accumulator;
+  PROCEDURE Init(v : INTEGER);
+  BEGIN total := v END Init;
+
+  PROCEDURE Add(v : INTEGER);
+  BEGIN total := total + v END Add;
+
+  PROCEDURE AddScaled(v : INTEGER);
+  BEGIN Add(v * scale) END AddScaled;      (* sibling call on THIS *)
+
+  PROCEDURE AddPair(a, b : INTEGER);
+  BEGIN AddScaled(a); AddScaled(b) END AddPair;
+
+  PROCEDURE Value() : INTEGER;
+  BEGIN RETURN total END Value;
+BEGIN
+  scale := 10                              (* class init body *)
+END Accumulator;
+
+VAR ExitCode : INTEGER;
+VAR acc : Accumulator;
+VAR line, num : ARRAY [0 .. 63] OF CHAR;
+
+BEGIN
+  ExitCode := 0;
+
+  acc.Init(0);
+  acc.AddPair(2, 2);            (* AddScaled -> Add: +20 +20 = 40 *)
+  acc.Add(2);                   (* +2 = 42 *)
+
+  Conversions.IntToStr(acc.Value(), num);
+  line := "showcase19 total=" + num;
+  TextIO.WriteString(line);
+  TextIO.WriteLn;
+
+  IF acc.Value() = 42 THEN ExitCode := 42 ELSE ExitCode := 1 END
+END Showcase19.

+ 1 - 1
docs/features.md

@@ -1,4 +1,4 @@
-# m2compiler-V3 — feature status (at `v3-class-finish`, 146/146 green)
+# m2compiler-V3 — feature status (at `v3-showcase19`, 147/147 green)
 
 Pipeline: Coco/R `M2.atg` (1748 lines, 73 productions) → `gm2`-built
 `M2` → QBE `.ssa` → `qbe` → `cc` → run. `SymTab.mod` 1623 lines,

+ 69 - 0
docs/summary_showcase19.md

@@ -0,0 +1,69 @@
+# Session summary — 2026-09-29 (c): CLASS finished + showcase19
+
+Suite **147/147**; self-hosting fixpoint **OK**
+(image **2,313,762 bytes**). `master`, tree clean except the user's
+uncommitted `compiler/toto.mod`.
+
+This session closed out the Clarion `CLASS` feature, then showcased
+it.
+
+## Steps, tags and headline commits
+
+| Step | Tag | Commit | What |
+| --- | --- | --- | --- |
+| CLASS finished | `v3-class-finish` | `3804256` | sibling method calls + init body |
+| Showcase | `v3-showcase19` | (this commit) | init body + sibling-call chains |
+
+## 1. CLASS finished
+
+Two `230`s removed (the third, multiple parents, stays by design):
+
+- **Sibling-method calls** — inside a `CLASS IMPLEMENTATION`, a bare
+  method name is a call on `THIS`.  `SymTab` tracks the innermost open
+  implementation (`implStk`/`CurImplClass`); `Design` binds an
+  otherwise-unresolved name to the class's method and arms `THIS` as
+  the receiver.  A virtual sibling still dispatches through the
+  vtable.
+- **Class `BEGIN … END` init body** — emitted as `<Class>_init`,
+  registered with the module init functions, and run **once at
+  startup**.  (A body touching instance fields still reports 230:
+  there is no receiver, and V3 has no class statics.)
+
+`docs/summary_class-finish.md`.
+
+With this, `CLASS` is ✅ for: fields, methods (value/`VAR` formals,
+results), single inheritance, `WITH`, hidden `THIS`, `VIRTUAL`
+dynamic dispatch (vtables) with subtype pointers, sibling calls, and
+init bodies.  ⏸ multiple parents only.
+
+## 2. Showcase
+
+`compiler/tests/showcase19.mod` (`Showcase19`, exit 42):
+
+- `Accumulator`'s `BEGIN` body sets a global `scale := 10` at startup.
+- `AddPair` → `AddScaled` → `Add` is a **chain of sibling calls** on
+  `THIS`; `AddScaled` uses the init-set global.
+- The result is rendered with `Conversions.IntToStr` + CHAR-array `+`.
+
+```
+showcase19 total=42
+```
+
+## Tests (147, was 146)
+
+New: `Showcase19` (42).  (The class-finish step added
+`t_classsibling` and `t_classinit` and promoted `t_class`.)
+
+## Resume
+
+```sh
+cd .../m2compiler-V3
+./bootstrap/fixpoint.sh                       # FIXPOINT OK
+cd compiler && ./build.sh && ./run_tests.sh   # 147/147
+```
+
+## Remaining polish
+
+- Multiple parents (deliberate).
+- `VAL(LONGINT|REAL, x)` and long→int narrowing.
+- More stdlib (`SysClock`, richer `Strings`/`Math`).