Browse Source

showcase17: CLASS lowering + CHAR-array + + ConvResults

- Showcase17 (exit 42): a Bounded counter class (single inheritance
  from Counter) driven through its THIS receiver, a value parsed via
  the ISO ConvResults API, and a label built by CHAR-array
  concatenation; prints 'showcase17 total=42'.
- docs/summary_showcase17.md: session summary for the three features.
- Suite 142/142; fixpoint OK (2,188,065 bytes).
Eric Streit 1 week ago
parent
commit
807ff9c990
3 changed files with 169 additions and 0 deletions
  1. 5 0
      compiler/run_tests.sh
  2. 74 0
      compiler/tests/showcase17.mod
  3. 90 0
      docs/summary_showcase17.md

+ 5 - 0
compiler/run_tests.sh

@@ -286,6 +286,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 Showcase17 42 "showcase17 total=42" \
+  ../runtime/syslib/Utf8.def ../runtime/syslib/Utf8.mod \
+  ../stdlib/textio.def ../stdlib/textio.mod \
+  ../stdlib/conversions.def ../stdlib/conversions.mod \
+  showcase17.mod
 rm -f gen_ssa/_showcase16.txt
 expect_run_files_out Showcase16 42 "showcase16: id=3 total=42" \
   ../runtime/syslib/SysShim.def ../runtime/syslib/SysShim.mod \

+ 74 - 0
compiler/tests/showcase17.mod

@@ -0,0 +1,74 @@
+MODULE Showcase17;
+// Session showcase: Clarion CLASS lowering, CHAR-array string
+// concatenation, and ISO ConvResults.
+//
+// A Bounded counter class (single inheritance from Counter) is driven
+// through its hidden THIS receiver; a value is parsed with the
+// ConvResults API, and a label is built by CHAR-array concatenation
+// for the output line.
+// Expected ExitCode: 42.
+IMPORT TextIO, Conversions;
+FROM Conversions IMPORT strAllRight, strWrongFormat, strOutOfRange;
+
+TYPE
+  CLASS Counter;
+    total : INTEGER;
+    PROCEDURE Bump(by : INTEGER);
+    PROCEDURE Value() : INTEGER;
+  END Counter;
+
+  CLASS Bounded (Counter);        (* single inheritance *)
+    limit : INTEGER;
+    PROCEDURE SetLimit(v : INTEGER);
+    PROCEDURE Over() : BOOLEAN;
+  END Bounded;
+
+CLASS IMPLEMENTATION Counter;
+  PROCEDURE Bump(by : INTEGER);
+  BEGIN total := total + by END Bump;
+  PROCEDURE Value() : INTEGER;
+  BEGIN RETURN total END Value;
+END Counter;
+
+CLASS IMPLEMENTATION Bounded;
+  PROCEDURE SetLimit(v : INTEGER);
+  BEGIN limit := v END SetLimit;
+  PROCEDURE Over() : BOOLEAN;
+  BEGIN RETURN total > limit END Over;
+END Bounded;
+
+VAR ExitCode : INTEGER;
+VAR c : Bounded;
+VAR n : INTEGER;
+VAR label : ARRAY [0 .. 31] OF CHAR;
+VAR msg : ARRAY [0 .. 63] OF CHAR;
+VAR r : Conversions.ConvResults;
+
+BEGIN
+  ExitCode := 0;
+
+  (* class methods: own + inherited, through the receiver *)
+  c.SetLimit(100);
+  c.Bump(15);
+  c.Bump(27);                       (* total = 42 *)
+  IF c.Value() = 42 THEN ExitCode := ExitCode + 1 ELSE ExitCode := 100 END;
+  IF NOT c.Over() THEN ExitCode := ExitCode + 2 ELSE ExitCode := 100 END;
+
+  (* ISO ConvResults *)
+  r := Conversions.StrToInt("-42", n);
+  IF (r = strAllRight) AND (n = -42) THEN
+    ExitCode := ExitCode + 4
+  ELSE ExitCode := 100 END;
+  r := Conversions.StrToInt("nope", n);
+  IF r = strWrongFormat THEN ExitCode := ExitCode + 8 ELSE ExitCode := 100 END;
+  r := Conversions.StrToInt("99999999999", n);
+  IF r = strOutOfRange THEN ExitCode := ExitCode + 16 ELSE ExitCode := 100 END;
+
+  (* CHAR-array concatenation *)
+  Conversions.IntToStr(c.Value(), label);
+  msg := "showcase17 total=" + label;
+  TextIO.WriteString(msg);
+  TextIO.WriteLn;
+
+  IF ExitCode = 31 THEN ExitCode := c.Value() END   (* 42 *)
+END Showcase17.

+ 90 - 0
docs/summary_showcase17.md

@@ -0,0 +1,90 @@
+# Session summary — 2026-09-29: CHAR-array `+`, ConvResults, CLASS lowering
+
+Suite **142/142**; self-hosting fixpoint **OK**
+(`bootstrap/fixpoint.sh`, image **2,188,065 bytes**). `master`, tree
+clean except the user's uncommitted `compiler/toto.mod`.
+
+Three features landed this session, each its own tagged step.
+
+## Steps, tags and headline commits
+
+| Step | Tag | Commit | What |
+| --- | --- | --- | --- |
+| CHAR-array `+` + ConvResults | `v3-charcat-convresults` | `5e2056f` | `ARRAY OF CHAR` concatenation test; ISO `ConvResults` |
+| Clarion CLASS lowering | `v3-class-lowering` | `ed02804` | fields, methods, `THIS`, single inheritance |
+| Showcase | `v3-showcase17` | `de27462` | the three together |
+
+## 1. CHAR-array `+` (verified + tested)
+
+String concatenation for `ARRAY OF CHAR` already worked end-to-end
+from the earlier string work; it just had no test. `t_charcat.mod`
+(15) now locks in `a + b`, `a + "c"`, `"x" + a` and `a + a + a`.
+
+(Classic note: a single-character *string* literal is a `CHAR`, so
+`b := "z"` into a CHAR array is 210 — use `b := "zz"`; the `+`
+operator accepts the `CHAR` form directly.)
+
+## 2. ISO `ConvResults`
+
+`Conversions` now reports a status enumeration:
+
+```modula-2
+TYPE ConvResults = (strAllRight, strOutOfRange, strWrongFormat,
+                    strEmpty);
+PROCEDURE StrToInt (s : ARRAY OF CHAR; VAR n : INTEGER) : ConvResults;
+PROCEDURE StrToCard(s : ARRAY OF CHAR; VAR n : CARDINAL): ConvResults;
+PROCEDURE StrToReal(VAR s : ARRAY OF CHAR; VAR x : REAL): ConvResults;
+```
+
+Overflow is detected before the 32-bit accumulate wraps (V3 has no
+`VAL(LONGINT, int)` or long→int narrowing). The BOOLEAN shortcuts
+live on as `IntVal`/`CardVal`/`RealVal`.
+
+## 3. Clarion CLASS lowering
+
+Classes now **execute** (see `docs/summary_class-lowering.md` for the
+full design):
+
+- A class is a record for layout; single inheritance lays a child's
+  fields after the parent's, and field lookups walk the parent chain.
+- Each method has a hidden `THIS` `VAR` parameter, pushed as the
+  current `WITH` base so bare field names mean `THIS^.field`.
+- `obj.M(args)` binds statically with the receiver as the hidden
+  first argument; a `CLASS IMPLEMENTATION` reuses the declared method
+  symbol (matching call-site and definition uids).
+- `VIRTUAL` is accepted but dispatches statically for now; bare
+  sibling-method calls and a class `BEGIN` init body stay 230.
+
+## 4. Showcase
+
+`compiler/tests/showcase17.mod` (`Showcase17`, exit 42) ties them
+together: a `Bounded` counter class (inheriting from `Counter`) driven
+through its receiver, a value parsed with `ConvResults`, and a label
+built by CHAR-array concatenation.
+
+```
+showcase17 total=42
+```
+
+## Tests (142, was 139)
+
+New: `t_charcat` (15), `convresults_prog` (127), `t_classmethod` (7),
+`t_classinherit` (7), `TClassDefProg` (8), `Showcase17` (42).
+`conv_prog`/`showcase14` moved to `IntVal`/`RealVal`;
+`t_classdef.mod` became `t_classdef.def` (a definition used by
+`TClassDefProg`).
+
+## Resume
+
+```sh
+cd .../m2compiler-V3
+./bootstrap/fixpoint.sh                       # FIXPOINT OK
+cd compiler && ./build.sh && ./run_tests.sh   # 142/142
+```
+
+## Remaining polish
+
+- CLASS: `VIRTUAL` vtable dispatch, sibling-method calls, class init
+  bodies.
+- `VAL(LONGINT|REAL, x)` and long→int narrowing.
+- `IOChan`-over-`Files` extras; deeper `Strings`/`Math`.