[LINT68][COMMITTED] Add operator L from LongInt to string
Jose E. Marchesi
jemarch@gnu.org
Sat Jun 13 13:53:16 GMT 2026
The original code from Baszenski features read routines to be used
with the transput ("read" for Lints). Since we don't have the general
transput yet, I have added a new operator L that, given a LongInt,
yields the textual representation. It is the reverse of longwhole.
---
src/liblint68.a68 | 36 ++++++++++++++++++++++++++++++++++--
testsuite/Makefile.am | 7 +++++--
testsuite/test-l-string.a68 | 25 +++++++++++++++++++++++++
3 files changed, 64 insertions(+), 4 deletions(-)
create mode 100644 testsuite/test-l-string.a68
diff --git a/src/liblint68.a68 b/src/liblint68.a68
index dc3db8c..3b6bb2f 100644
--- a/src/liblint68.a68
+++ b/src/liblint68.a68
@@ -12,7 +12,9 @@
1. Adapt from the point stropping used in the published work to
modern stropping supported by ga68.
2. Added some additional commentary.
- 3. Several code improvements.
+ 3. The input routines has been replaced with an operator L
+ that gets a string.
+ 4. Several other code improvements.
Note that the origin form of this code is Copyright 1983
Rechenzentrum der Ruhr-Universitaet, published in the form of a book
@@ -134,7 +136,9 @@ def
Note that 0:-1 bounds are used instead of the customary 1:0 so
that checking for non-zero can be done by UPB abs'u > 0 }
- LongInt zero = (plus_sign, heap[0:-1]ShortInt);
+ LongInt zero; {XXX GCC bug = (plus_sign, heap[0:-1]ShortInt);}
+ sign'zero := plus_sign;
+ abs'zero := heap[0:-1]ShortInt;
{ The algorithms for arithmetic with LongInt values are
essentially those described in D.E. Knuth: The Art of Computing
@@ -769,6 +773,34 @@ def
{ Input and conversion routines
───────────────────────────── }
+ pub op L = (string s) LongInt:
+ begin
+ LongInt result := zero;
+ bool gotsign := false, negative := false, strange := false;
+
+ for i to UPB s while ~strange
+ do if s[i] = "-"
+ then if gotsign
+ then strange := true
+ else negative := true;
+ gotsign := true
+ fi
+ elif s[i] = "+"
+ then if gotsign
+ then strange := true
+ else negative := false;
+ gotsign := true
+ fi
+ elif s[i] = blank
+ then skip
+ elif ABS s[i] >= ABS "0" AND ABS s[i] <= ABS "9"
+ then result *:= 10 +:= (ABS s[i] - ABS "0")
+ else strange := true
+ fi
+ od;
+ (negative | -result | result)
+ end;
+
pub proc longwhole = (LongInt x, int width) string:
begin
LongInt absx := ABS x;
diff --git a/testsuite/Makefile.am b/testsuite/Makefile.am
index edd6bbe..99150f3 100644
--- a/testsuite/Makefile.am
+++ b/testsuite/Makefile.am
@@ -18,12 +18,15 @@
AM_A68FLAGS = -I$(top_builddir)/src/.libs -std=gnu68 -fcheck=nil
-TESTS = test-rel-ops test-fac
+TESTS = test-rel-ops test-fac test-l-string
-noinst_PROGRAMS = test-rel-ops test-fac
+noinst_PROGRAMS = test-rel-ops test-fac test-l-string
test_rel_ops_SOURCES = test-rel-ops.a68
test_rel_ops_LDADD = $(top_builddir)/src/liblint68.la
test_fac_SOURCES = test-fac.a68
test_fac_LDADD = $(top_builddir)/src/liblint68.la
+
+test_l_string_SOURCES = test-l-string.a68
+test_l_string_LDADD = $(top_builddir)/src/liblint68.la
diff --git a/testsuite/test-l-string.a68 b/testsuite/test-l-string.a68
new file mode 100644
index 0000000..5b5e2bc
--- /dev/null
+++ b/testsuite/test-l-string.a68
@@ -0,0 +1,25 @@
+{ test-l-string - Unit tests for liblint68 - from string to lint
+
+ Copyright (C) 2026 Jose E. Marchesi
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+}
+
+access Lint68
+begin
+ assert (L "2432902008176640000" = FAC 20);
+ assert (L "+2432902008176640000" = FAC 20);
+ assert (L "815915283247897734345611269596115894272000000000" = FAC 40)
+
+end
--
2.39.5
More information about the Algol68
mailing list