Jelajahi Sumber

cleaning some folders

Eric Streit 3 minggu lalu
induk
melakukan
de2f549488
8 mengubah file dengan 624 tambahan dan 0 penghapusan
  1. 270 0
      Showcase.LST
  2. TEMPAT SAMPAH
      Showcase.MC4
  3. 264 0
      Showcase.mod
  4. TEMPAT SAMPAH
      TME.MC4
  5. TEMPAT SAMPAH
      TMF.MC4
  6. TEMPAT SAMPAH
      TMG.MC4
  7. 49 0
      docs/goal_step4.md
  8. 41 0
      docs/goal_step5.md

+ 270 - 0
Showcase.LST

@@ -0,0 +1,270 @@
+Listing:
+
+    1  MODULE Showcase;
+    2  
+    3  (* Tour of everything M2c + mcint can do so far (steps 1-6):
+    4     scalars, control flow, procedures, composites, sets,
+    5     pointers, strings, open arrays, I/O and a local MODULE. *)
+    6  
+    7    MODULE Stats 
+    8    
+    9    EXPORT count, Bump, Limit;
+   10  
+   11    VAR 
+   12      count : INTEGER;
+   13  
+   14    CONST 
+   15      Limit = 100;
+   16      
+   17      PROCEDURE Bump;
+   18      BEGIN
+   19        count := count + 1
+   20      END Bump;
+   21  
+   22    END Stats;
+   23  
+   24  TYPE 
+   25    Color = (Red, Green, Blue);
+   26    Inner = RECORD u, v : INTEGER END;
+   27    Pair = RECORD a : INTEGER; b : Inner END;
+   28    Node = RECORD val : INTEGER; next : POINTER TO Node END;
+   29    PNode = POINTER TO Node;
+   30  
+   31  VAR 
+   32    total, x, y, i, f, w, k : INTEGER;
+   33    flag : BOOLEAN;
+   34    ch : CHAR;
+   35    col : Color;
+   36    r : REAL;
+   37    a, b : ARRAY [1 .. 5] OF INTEGER;
+   38    m1, m2, m3 : ARRAY [1 .. 2], [1 .. 2] OF INTEGER;
+   39    rec : Pair;
+   40    s1, s2, u, n, d : SET OF [0 .. 15];
+   41    head, spare : PNode;
+   42    s, t, s1c : ARRAY [1 .. 10] OF CHAR;
+   43    ExitCode : INTEGER;
+   44  
+   45  PROCEDURE Double(x : INTEGER) : INTEGER;
+   46  BEGIN
+   47    RETURN x * 2
+   48  END Double;
+   49  
+   50  PROCEDURE Incr(VAR x : INTEGER);
+   51  BEGIN
+   52    x := x + 1
+   53  END Incr;
+   54  
+   55  PROCEDURE Fact(n : INTEGER) : INTEGER;
+   56  BEGIN
+   57    IF n <= 1 THEN RETURN 1 ELSE RETURN n * Fact(n - 1) END
+   58  END Fact;
+   59  
+   60  PROCEDURE IsEven(n : INTEGER) : BOOLEAN;
+   61  FORWARD;
+   62  
+   63  PROCEDURE IsOdd(n : INTEGER) : BOOLEAN;
+   64  BEGIN
+   65    IF n = 0 THEN RETURN FALSE ELSE RETURN IsEven(n - 1) END
+   66  END IsOdd;
+   67  
+   68  PROCEDURE IsEven(n : INTEGER) : BOOLEAN;
+   69  BEGIN
+   70    IF n = 0 THEN RETURN TRUE ELSE RETURN IsOdd(n - 1) END
+   71  END IsEven;
+   72  
+   73  PROCEDURE Outer : INTEGER;
+   74    PROCEDURE Inner : INTEGER;
+   75    BEGIN
+   76      RETURN 6
+   77    END Inner;
+   78  BEGIN
+   79    RETURN Inner() + 1
+   80  END Outer;
+   81  
+   82  PROCEDURE SumOpen(x : ARRAY OF INTEGER) : INTEGER;
+   83  VAR i, s : INTEGER;
+   84  BEGIN
+   85    s := 0;
+   86    FOR i := 0 TO HIGH(x) DO 
+   87      s := s + x[i] 
+   88    END;
+   89    RETURN s
+   90  END SumOpen;
+   91  
+   92  PROCEDURE Append(v : INTEGER);
+   93  VAR q, r : PNode;
+   94  BEGIN
+   95    NEW(q);
+   96    q^.val := v;
+   97    q^.next := NIL;
+   98    IF head = NIL THEN
+   99      head := q
+  100    ELSE
+  101      r := head;
+  102      WHILE r^.next # NIL DO
+  103        r := r^.next
+  104      END;
+  105      r^.next := q
+  106    END
+  107  END Append;
+  108  
+  109  PROCEDURE ListSum(p : PNode) : INTEGER;
+  110  BEGIN
+  111    IF p = NIL THEN
+  112       RETURN 0 
+  113    ELSE 
+  114      RETURN p^.val + ListSum(p^.next) 
+  115    END
+  116  END ListSum;
+  117  
+  118  BEGIN
+  119    total := 0;
+  120    WriteString("tour");
+  121    Stats.count := 0;
+  122    Stats.Bump();
+  123    Stats.Bump();
+  124    Stats.Bump();
+  125    total := total + Stats.count;
+  126    IF Stats.Limit = 100 THEN 
+  127      total := total + 1 
+  128    END;
+  129    x := 6 * 7 - 2;
+  130    total := total + x DIV 10;
+  131    y := x MOD 7;
+  132    total := total + y;
+  133    r := 3.5 + 2.25;
+  134    IF (r > 5.0) AND (r < 6.0) THEN 
+  135      total := total + 2 
+  136    END;
+  137    ch := "A";
+  138    IF ch = "A" THEN 
+  139      total := total + 1 
+  140    END;
+  141    IF ch < "B" THEN 
+  142      total := total + 1 
+  143    END;
+  144    flag := (x > y) AND (y > 0);
+  145    IF flag THEN 
+  146      total := total + 1 
+  147    END;
+  148    flag := NOT flag OR FALSE;
+  149    IF ~flag THEN 
+  150      total := total + 1 
+  151    END;
+  152    col := Green;
+  153    CASE col OF
+  154      Red : total := total + 0 |
+  155      Green : total := total + 3 |
+  156      Blue : total := total + 5
+  157    END;
+  158    k := 2;
+  159    CASE k OF
+  160      1 : total := total + 0 |
+  161      2 : total := total + 4
+  162    ELSE
+  163      total := total + 0
+  164    END;
+  165    i := 0; w := 0;
+  166    WHILE i < 4 DO
+  167      i := i + 1;
+  168      w := w + i
+  169    END;
+  170    total := total + w DIV 5;
+  171    i := 0;
+  172    REPEAT
+  173      i := i + 1
+  174    UNTIL i >= 3;
+  175    total := total + i;
+  176    i := 0;
+  177    LOOP
+  178      i := i + 1;
+  179      IF i = 3 THEN 
+  180        EXIT 
+  181      END
+  182    END;
+  183    total := total + i;
+  184    f := 0;
+  185    FOR i := 1 TO 5 DO 
+  186      f := f + i 
+  187    END;
+  188    total := total + f MOD 10;
+  189    IF x > 100 THEN
+  190      total := total + 0
+  191    ELSIF x > 10 THEN
+  192      total := total + 2
+  193    ELSE
+  194      total := total + 0
+  195    END;
+  196    total := total + Double(21) DIV 10;
+  197    Incr(x);
+  198    total := total + x MOD 10;
+  199    total := total + Fact(5) DIV 100;
+  200    IF IsEven(10) THEN 
+  201      total := total + 1 
+  202    END;
+  203    total := total + Outer();
+  204    a[1] := 5; a[2] := 4; a[3] := 3; a[4] := 2; a[5] := 1;
+  205    b := a;
+  206    total := total + b[1];
+  207    total := total + HIGH(a);
+  208    total := total + SumOpen(a);
+  209    m1[1,1] := 1; m1[1,2] := 2; m1[2,1] := 3; m1[2,2] := 4;
+  210    m2[1,1] := 5; m2[1,2] := 6; m2[2,1] := 7; m2[2,2] := 8;
+  211    m3[1,1] := m1[1,1]*m2[1,1] + m1[1,2]*m2[2,1];
+  212    m3[1,2] := m1[1,1]*m2[1,2] + m1[1,2]*m2[2,2];
+  213    m3[2,1] := m1[2,1]*m2[1,1] + m1[2,2]*m2[2,1];
+  214    m3[2,2] := m1[2,1]*m2[1,2] + m1[2,2]*m2[2,2];
+  215    total := total + (m3[1,1] + m3[2,2]) MOD 20;
+  216    rec.a := 1;
+  217    rec.b.u := 10;
+  218    rec.b.v := 20;
+  219    WITH rec DO
+  220      a := a + 1;
+  221      WITH b DO
+  222        u := u + 2;
+  223        v := v + 3
+  224      END
+  225    END;
+  226    total := total + rec.a + rec.b.u + rec.b.v;
+  227    s1 := {1 .. 5};
+  228    s2 := {4 .. 8};
+  229    u := s1 + s2;
+  230    n := s1 * s2;
+  231    d := s1 - s2;
+  232    IF (1 IN u) AND (5 IN n) AND (2 IN d) THEN
+  233      total := total + 5
+  234    END;
+  235    IF u = u THEN 
+  236      total := total + 1 
+  237    END;
+  238    head := NIL;
+  239    Append(4);
+  240    Append(7);
+  241    Append(9);
+  242    total := total + ListSum(head);
+  243    NEW(spare);
+  244    DISPOSE(spare);
+  245    s := "ok";
+  246    t := "ok";
+  247    IF s = t THEN 
+  248      total := total + 2 
+  249    END;
+  250    WriteString(s);
+  251    t := "abc";
+  252    IF t # s THEN 
+  253      total := total + 1 
+  254    END;
+  255    IF s > t THEN 
+  256      total := total + 1 
+  257    END;
+  258    s1c := "okx";
+  259    IF s1c > s THEN 
+  260      total := total + 1 
+  261    END;
+  262    WriteInt(total);
+  263    ExitCode := total
+  264  END Showcase.
+
+    0 errors
+
+

TEMPAT SAMPAH
Showcase.MC4


+ 264 - 0
Showcase.mod

@@ -0,0 +1,264 @@
+MODULE Showcase;
+
+(* Tour of everything M2c + mcint can do so far (steps 1-6):
+   scalars, control flow, procedures, composites, sets,
+   pointers, strings, open arrays, I/O and a local MODULE. *)
+
+  MODULE Stats 
+  
+  EXPORT count, Bump, Limit;
+
+  VAR 
+    count : INTEGER;
+
+  CONST 
+    Limit = 100;
+    
+    PROCEDURE Bump;
+    BEGIN
+      count := count + 1
+    END Bump;
+
+  END Stats;
+
+TYPE 
+  Color = (Red, Green, Blue);
+  Inner = RECORD u, v : INTEGER END;
+  Pair = RECORD a : INTEGER; b : Inner END;
+  Node = RECORD val : INTEGER; next : POINTER TO Node END;
+  PNode = POINTER TO Node;
+
+VAR 
+  total, x, y, i, f, w, k : INTEGER;
+  flag : BOOLEAN;
+  ch : CHAR;
+  col : Color;
+  r : REAL;
+  a, b : ARRAY [1 .. 5] OF INTEGER;
+  m1, m2, m3 : ARRAY [1 .. 2], [1 .. 2] OF INTEGER;
+  rec : Pair;
+  s1, s2, u, n, d : SET OF [0 .. 15];
+  head, spare : PNode;
+  s, t, s1c : ARRAY [1 .. 10] OF CHAR;
+  ExitCode : INTEGER;
+
+PROCEDURE Double(x : INTEGER) : INTEGER;
+BEGIN
+  RETURN x * 2
+END Double;
+
+PROCEDURE Incr(VAR x : INTEGER);
+BEGIN
+  x := x + 1
+END Incr;
+
+PROCEDURE Fact(n : INTEGER) : INTEGER;
+BEGIN
+  IF n <= 1 THEN RETURN 1 ELSE RETURN n * Fact(n - 1) END
+END Fact;
+
+PROCEDURE IsEven(n : INTEGER) : BOOLEAN;
+FORWARD;
+
+PROCEDURE IsOdd(n : INTEGER) : BOOLEAN;
+BEGIN
+  IF n = 0 THEN RETURN FALSE ELSE RETURN IsEven(n - 1) END
+END IsOdd;
+
+PROCEDURE IsEven(n : INTEGER) : BOOLEAN;
+BEGIN
+  IF n = 0 THEN RETURN TRUE ELSE RETURN IsOdd(n - 1) END
+END IsEven;
+
+PROCEDURE Outer : INTEGER;
+  PROCEDURE Inner : INTEGER;
+  BEGIN
+    RETURN 6
+  END Inner;
+BEGIN
+  RETURN Inner() + 1
+END Outer;
+
+PROCEDURE SumOpen(x : ARRAY OF INTEGER) : INTEGER;
+VAR i, s : INTEGER;
+BEGIN
+  s := 0;
+  FOR i := 0 TO HIGH(x) DO 
+    s := s + x[i] 
+  END;
+  RETURN s
+END SumOpen;
+
+PROCEDURE Append(v : INTEGER);
+VAR q, r : PNode;
+BEGIN
+  NEW(q);
+  q^.val := v;
+  q^.next := NIL;
+  IF head = NIL THEN
+    head := q
+  ELSE
+    r := head;
+    WHILE r^.next # NIL DO
+      r := r^.next
+    END;
+    r^.next := q
+  END
+END Append;
+
+PROCEDURE ListSum(p : PNode) : INTEGER;
+BEGIN
+  IF p = NIL THEN
+     RETURN 0 
+  ELSE 
+    RETURN p^.val + ListSum(p^.next) 
+  END
+END ListSum;
+
+BEGIN
+  total := 0;
+  WriteString("tour");
+  Stats.count := 0;
+  Stats.Bump();
+  Stats.Bump();
+  Stats.Bump();
+  total := total + Stats.count;
+  IF Stats.Limit = 100 THEN 
+    total := total + 1 
+  END;
+  x := 6 * 7 - 2;
+  total := total + x DIV 10;
+  y := x MOD 7;
+  total := total + y;
+  r := 3.5 + 2.25;
+  IF (r > 5.0) AND (r < 6.0) THEN 
+    total := total + 2 
+  END;
+  ch := "A";
+  IF ch = "A" THEN 
+    total := total + 1 
+  END;
+  IF ch < "B" THEN 
+    total := total + 1 
+  END;
+  flag := (x > y) AND (y > 0);
+  IF flag THEN 
+    total := total + 1 
+  END;
+  flag := NOT flag OR FALSE;
+  IF ~flag THEN 
+    total := total + 1 
+  END;
+  col := Green;
+  CASE col OF
+    Red : total := total + 0 |
+    Green : total := total + 3 |
+    Blue : total := total + 5
+  END;
+  k := 2;
+  CASE k OF
+    1 : total := total + 0 |
+    2 : total := total + 4
+  ELSE
+    total := total + 0
+  END;
+  i := 0; w := 0;
+  WHILE i < 4 DO
+    i := i + 1;
+    w := w + i
+  END;
+  total := total + w DIV 5;
+  i := 0;
+  REPEAT
+    i := i + 1
+  UNTIL i >= 3;
+  total := total + i;
+  i := 0;
+  LOOP
+    i := i + 1;
+    IF i = 3 THEN 
+      EXIT 
+    END
+  END;
+  total := total + i;
+  f := 0;
+  FOR i := 1 TO 5 DO 
+    f := f + i 
+  END;
+  total := total + f MOD 10;
+  IF x > 100 THEN
+    total := total + 0
+  ELSIF x > 10 THEN
+    total := total + 2
+  ELSE
+    total := total + 0
+  END;
+  total := total + Double(21) DIV 10;
+  Incr(x);
+  total := total + x MOD 10;
+  total := total + Fact(5) DIV 100;
+  IF IsEven(10) THEN 
+    total := total + 1 
+  END;
+  total := total + Outer();
+  a[1] := 5; a[2] := 4; a[3] := 3; a[4] := 2; a[5] := 1;
+  b := a;
+  total := total + b[1];
+  total := total + HIGH(a);
+  total := total + SumOpen(a);
+  m1[1,1] := 1; m1[1,2] := 2; m1[2,1] := 3; m1[2,2] := 4;
+  m2[1,1] := 5; m2[1,2] := 6; m2[2,1] := 7; m2[2,2] := 8;
+  m3[1,1] := m1[1,1]*m2[1,1] + m1[1,2]*m2[2,1];
+  m3[1,2] := m1[1,1]*m2[1,2] + m1[1,2]*m2[2,2];
+  m3[2,1] := m1[2,1]*m2[1,1] + m1[2,2]*m2[2,1];
+  m3[2,2] := m1[2,1]*m2[1,2] + m1[2,2]*m2[2,2];
+  total := total + (m3[1,1] + m3[2,2]) MOD 20;
+  rec.a := 1;
+  rec.b.u := 10;
+  rec.b.v := 20;
+  WITH rec DO
+    a := a + 1;
+    WITH b DO
+      u := u + 2;
+      v := v + 3
+    END
+  END;
+  total := total + rec.a + rec.b.u + rec.b.v;
+  s1 := {1 .. 5};
+  s2 := {4 .. 8};
+  u := s1 + s2;
+  n := s1 * s2;
+  d := s1 - s2;
+  IF (1 IN u) AND (5 IN n) AND (2 IN d) THEN
+    total := total + 5
+  END;
+  IF u = u THEN 
+    total := total + 1 
+  END;
+  head := NIL;
+  Append(4);
+  Append(7);
+  Append(9);
+  total := total + ListSum(head);
+  NEW(spare);
+  DISPOSE(spare);
+  s := "ok";
+  t := "ok";
+  IF s = t THEN 
+    total := total + 2 
+  END;
+  WriteString(s);
+  t := "abc";
+  IF t # s THEN 
+    total := total + 1 
+  END;
+  IF s > t THEN 
+    total := total + 1 
+  END;
+  s1c := "okx";
+  IF s1c > s THEN 
+    total := total + 1 
+  END;
+  WriteInt(total);
+  ExitCode := total
+END Showcase.

TEMPAT SAMPAH
TME.MC4


TEMPAT SAMPAH
TMF.MC4


TEMPAT SAMPAH
TMG.MC4


+ 49 - 0
docs/goal_step4.md

@@ -0,0 +1,49 @@
+# Step 4 goal — Composite types (saved, to implement tomorrow)
+
+## Scope (Phase 3 of the plan)
+
+- `ARRAY`: bounds, indexing (incl. multi-dimensional), scaled address
+  arithmetic, indexed loads/stores (`0EH`/`1EH`), open-array parameters.
+- `RECORD`: field layout with offsets, `.field` selection, `WITH`.
+- `SET`: add operators (`+` union, `-` difference, `*` intersection);
+  literals/`IN`/`=`/`#` already work since step 2.
+- `POINTER`: `NEW`/`DISPOSE`, dereference, `NIL` handling.
+- Strings: string variables as `ARRAY OF CHAR`.
+
+## Design deltas (vs step 3)
+
+- SymTab: byte sizes on descriptors, field/element offsets,
+  per-variable sizes (frame + globals); new queries (`TypeSize`,
+  `FieldOffset`, …).
+- MGen: scaled indexing, `copy_block` for composite assignment.
+- Policy to fix at start: range checking (checked vs unchecked),
+  `NIL` dereference (runtime trap, no static check).
+- `NEW` semantics: **recommended** — VM `ALLOCATE` (`0x40 05`),
+  already implemented by the interpreter. Alternatives (e.g.
+  compiler-managed bump region) to be decided if ALLOCATE
+  misbehaves under test.
+
+## Acceptance (same bar)
+
+~15 run + ~10 rejection tests, all green, no cascades; full
+49-test regression still passing (→ ~74 total).
+
+## Suggested first tests
+
+Matrix multiply or sieve (arrays); nested records + `WITH`;
+set ops with ranges; linked list via `NEW`/`DISPOSE` (combines
+pointers with step-3 recursion).
+
+---
+
+# Session checkpoint (saved, not committed)
+
+- Tags `step1`, `step2`, `step2.1`, `step3` exist locally
+  (`step3` = `c0f1924`, suite 49/49 + mc64 24/24 green).
+- Pushing (commits + tags) is done by the user (credentials).
+- Uncommitted on disk: `summary_step1/2/2.1/3.md` (docs commit
+  `0e07f06` was undone per user request), this `goal_step4.md`,
+  plus pre-existing leftovers never touched (`m2comp/coco/`
+  deletions, `gnu-m2-grammar.txt`, chatgpt folder, instructions
+  file, `STAK`/`DEP.MC4`).
+- Next action: implement the step-4 goal above.

+ 41 - 0
docs/goal_step5.md

@@ -0,0 +1,41 @@
+# Step 5 goal — Hardening + modules (saved, to implement next)
+
+## Scope (Phase 4 of the plan)
+
+- **Finish composites:** open-array parameters (`ARRAY OF T` + `HIGH`),
+  whole array/record copy (`copy_block`), value composite
+  parameters/returns, composite `=`/`#` (`string_comp` for `CHAR`
+  arrays, `213` otherwise), VAR actuals with tails (`a[i]`, `p^`,
+  fields — currently `233`), `CHAR` slot-packing cleanup (currently
+  8x over-allocated).
+- **Checking:** opt-in range checks (`DA`/`DB`/`DC`, unchecked stays
+  default), `NIL` dereference VM trap (currently reads 0).
+- **Modules:** `DEFINITION`/`IMPLEMENTATION` split, `IMPORT`/`EXPORT`,
+  qualified identifiers (`M.x` — currently `QualIdent` kills the
+  tail), separate compilation to one `.MC4` (loader already handles
+  `depCount`, currently rejected).
+- **Explicitly deferred:** `LONGINT`/`LONGCARD` 64-bit (`0C5–CC`,
+  `0x40 14–1C`), `LONGREAL` quads (`0x12`), procedure
+  types/variables, `cones`.
+
+## Design deltas (vs step 4)
+
+- SymTab: open-array descriptors (isOpen + elem), module
+  scopes/symbols, export tables.
+- MGen: open-array hidden length temps, `copy_block` for value
+  params/returns + whole assignment, `string_comp` (`0C4`) for
+  `CHAR`-array comparison.
+- Policy to fix at start: one-file vs separate-`.MC4` linkage;
+  `SYSTEM` services vs language I/O (replace the `ExitCode`
+  convention?).
+
+## Acceptance (same bar)
+
+~15 run + ~10 rejection tests, all green, no cascades; full
+74-test regression still passing (→ ~99 total).
+
+## Suggested first tests
+
+Open-array sum/`HIGH` (value + VAR); whole-array copy; string
+`=`/`#`; multi-module import (exported procedure + variable);
+`NIL` trap; `HIGH` on a fixed array (must fail cleanly).