浏览代码

stdlib: add LongIO (channel-based) and document the library step

Eric Streit 1 周之前
父节点
当前提交
b1a52dbd5c
共有 7 个文件被更改,包括 96 次插入 和 10 次删除
  1. 2 0
      compiler/run_tests.sh
  2. 3 1
      compiler/tests/library1.mod
  3. 1 1
      docs/analysis_gaps.md
  4. 2 2
      docs/features.md
  5. 5 6
      docs/summary_resultsfx-coroutines-lib.md
  6. 17 0
      stdlib/longio.def
  7. 66 0
      stdlib/longio.mod

+ 2 - 0
compiler/run_tests.sh

@@ -267,6 +267,8 @@ expect_run_files Library1 42 \
   ../stdlib/realstr.def ../stdlib/realstr.mod \
   ../stdlib/longstr.def ../stdlib/longstr.mod \
   ../stdlib/longmath.def ../stdlib/longmath.mod \
+  ../stdlib/iochan.def ../stdlib/iochan.mod \
+  ../stdlib/longio.def ../stdlib/longio.mod \
   ../stdlib/strio.def ../stdlib/strio.mod \
   ../stdlib/inout.def ../stdlib/inout.mod \
   ../stdlib/stdio.def ../stdlib/stdio.mod \

+ 3 - 1
compiler/tests/library1.mod

@@ -5,7 +5,7 @@ MODULE Library1;
 // Exit 42.
 IMPORT ConvTypes, WholeStr, RealStr, LongStr, LongMath,
        StrIO, InOut, STextIO, SWholeIO, SRealIO, SIOResult, IOConsts,
-       TERMINATION, StdIO;
+       TERMINATION, StdIO, IOChan, LongIO;
 FROM ConvTypes IMPORT ConvResults, strAllRight;
 FROM IOConsts IMPORT ReadResults, notKnown;
 
@@ -39,6 +39,8 @@ BEGIN
   STextIO.WriteLn;
   SRealIO.WriteFixed(2.5, 1, 0);
   STextIO.WriteLn;
+  LongIO.WriteReal(IOChan.StdOutChan(), 3.5, 0);
+  STextIO.WriteLn;
   StrIO.WriteString("strio");
   StrIO.WriteLn;
   InOut.WriteString("oct ");

+ 1 - 1
docs/analysis_gaps.md

@@ -121,7 +121,7 @@ channel/device library holes in §2.
   (ucontext-backed).
 - **Core text/ISO library** — `ConvTypes`, `IOConsts`, `SIOResult`,
   `TERMINATION`, `StrIO`, `InOut`, `StdIO`, `WholeStr`, `RealStr`,
-  `LongStr`, `LongMath`, `STextIO`, `SWholeIO`, `SRealIO`,
+  `LongStr`, `LongMath`, `LongIO`, `STextIO`, `SWholeIO`, `SRealIO`,
   `DynamicStrings` (core), `SysStorage`.
 - **Backend** — procedure values / LONGINT are 8-byte in array load/
   store and in the parameter prologue (were 32-bit); driver session

+ 2 - 2
docs/features.md

@@ -139,8 +139,8 @@ Legend: ✅ done · 🔄 partial · ⏸ not started / deferred.
   `IOChan` (streams + files; `Position`/`Seek`/`Rewind`), `Storage`.
 - ✅ gm2/ISO text + conversion library added: `ConvTypes`, `IOConsts`,
   `SIOResult`, `TERMINATION`, `StrIO`, `InOut`, `StdIO`, `WholeStr`,
-  `RealStr`, `LongStr`, `LongMath`, `STextIO`, `SWholeIO`, `SRealIO`,
-  `DynamicStrings` (core subset), `SysStorage`.
+  `RealStr`, `LongStr`, `LongMath`, `LongIO`, `STextIO`, `SWholeIO`,
+  `SRealIO`, `DynamicStrings` (core subset), `SysStorage`.
 
 ## Coroutines
 - ✅ PIM/SYSTEM coroutines: `PROCESS`, `NEWPROCESS`, `TRANSFER`,

+ 5 - 6
docs/summary_resultsfx-coroutines-lib.md

@@ -44,17 +44,16 @@ not accepted.
 
 Added: `ConvTypes`, `IOConsts`, `SIOResult`, `TERMINATION`, `StrIO`,
 `InOut`, `StdIO`, `WholeStr`, `RealStr`, `LongStr`, `LongMath`,
-`STextIO`, `SWholeIO`, `SRealIO`, `DynamicStrings` (core subset),
-`SysStorage`.  `TERMINATION.def` is uppercase (gm2 spelling).
+`STextIO`, `SWholeIO`, `SRealIO`, `LongIO`, `DynamicStrings` (core
+subset), `SysStorage`.  `TERMINATION.def` is uppercase (gm2 spelling).
 
 New shim helpers: `m2readline`, `m2writeintwidth`, `m2realconv`,
 `m2terminating`, `m2hashalted`, and the `m2ds*` DynamicStrings core
 (heap C strings; `String` is an opaque type completed to `ADDRESS`).
 
-Still missing from the §2 list: `LongIO`/`LongWholeIO` (channel-based),
-`wrapclock`/`wraptime`/`wrapc`, `TimeString`, and the gm2 device
-framework (`ChanConsts`/`GenDevIF`/`DeviceTablePtr`/`StdChans`/
-`StreamFile`/`FIO`).
+Still missing from the §2 list: `LongWholeIO`, `wrapclock`/`wraptime`/
+`wrapc`, `TimeString`, and the gm2 device framework (`ChanConsts`/
+`GenDevIF`/`DeviceTablePtr`/`StdChans`/`StreamFile`/`FIO`).
 
 ## Tests (173, was 171)
 

+ 17 - 0
stdlib/longio.def

@@ -0,0 +1,17 @@
+DEFINITION MODULE LongIO;
+(* ISO 10514-1: LONGREAL I/O over an IOChan channel.  V3's IOChan is
+   the channel abstraction; the text form is built with LongStr. *)
+
+IMPORT IOChan;
+
+PROCEDURE ReadReal (cid : IOChan.ChanId; VAR real : LONGREAL);
+PROCEDURE WriteFloat (cid : IOChan.ChanId; real : LONGREAL;
+                      sigFigs : CARDINAL; width : CARDINAL);
+PROCEDURE WriteEng (cid : IOChan.ChanId; real : LONGREAL;
+                    sigFigs : CARDINAL; width : CARDINAL);
+PROCEDURE WriteFixed (cid : IOChan.ChanId; real : LONGREAL;
+                      place : INTEGER; width : CARDINAL);
+PROCEDURE WriteReal (cid : IOChan.ChanId; real : LONGREAL;
+                     width : CARDINAL);
+
+END LongIO.

+ 66 - 0
stdlib/longio.mod

@@ -0,0 +1,66 @@
+IMPLEMENTATION MODULE LongIO;
+(* Builds the text with LongStr and writes it through IOChan. *)
+
+IMPORT IOChan, LongStr, ConvTypes;
+
+PROCEDURE WritePadded (cid : IOChan.ChanId; VAR s : ARRAY OF CHAR;
+                       width : CARDINAL);
+  VAR n, i : CARDINAL;
+    pad : ARRAY [0 .. 127] OF CHAR;
+BEGIN
+  n := 0;
+  WHILE (n <= HIGH(s)) AND (s[n] # CHR(0)) DO INC(n) END;
+  IF width > n THEN
+    i := 0;
+    WHILE (i < width - n) AND (i <= HIGH(pad)) DO
+      pad[i] := " "; INC(i)
+    END;
+    pad[i] := CHR(0);
+    IOChan.WriteString(cid, pad)
+  END;
+  IOChan.WriteString(cid, s)
+END WritePadded;
+
+PROCEDURE WriteFloat (cid : IOChan.ChanId; real : LONGREAL;
+                      sigFigs : CARDINAL; width : CARDINAL);
+  VAR s : ARRAY [0 .. 127] OF CHAR;
+BEGIN
+  LongStr.RealToFloat(real, sigFigs, s);
+  WritePadded(cid, s, width)
+END WriteFloat;
+
+PROCEDURE WriteEng (cid : IOChan.ChanId; real : LONGREAL;
+                    sigFigs : CARDINAL; width : CARDINAL);
+  VAR s : ARRAY [0 .. 127] OF CHAR;
+BEGIN
+  LongStr.RealToEng(real, sigFigs, s);
+  WritePadded(cid, s, width)
+END WriteEng;
+
+PROCEDURE WriteFixed (cid : IOChan.ChanId; real : LONGREAL;
+                      place : INTEGER; width : CARDINAL);
+  VAR s : ARRAY [0 .. 127] OF CHAR;
+BEGIN
+  LongStr.RealToFixed(real, place, s);
+  WritePadded(cid, s, width)
+END WriteFixed;
+
+PROCEDURE WriteReal (cid : IOChan.ChanId; real : LONGREAL;
+                     width : CARDINAL);
+BEGIN
+  IF width <= 16 THEN
+    WriteFixed(cid, real, 6, width)
+  ELSE
+    WriteFloat(cid, real, 6, width)
+  END
+END WriteReal;
+
+PROCEDURE ReadReal (cid : IOChan.ChanId; VAR real : LONGREAL);
+  VAR s : ARRAY [0 .. 255] OF CHAR;
+    res : ConvTypes.ConvResults;
+BEGIN
+  IOChan.ReadString(cid, s);
+  LongStr.StrToReal(s, real, res)
+END ReadReal;
+
+END LongIO.