PR modula2/112921 missing modules shortreal shortstr shortconv convstringshort

For completeness here are three SHORTREAL modules which match their
LONGREAL and REAL counterparts.  The datatype SHORTREAL is a GNU
extension and these modules were missing.

gcc/m2/ChangeLog:

	PR modula2/112921
	* gm2-libs-iso/ConvStringShort.def: New file.
	* gm2-libs-iso/ConvStringShort.mod: New file.
	* gm2-libs-iso/ShortConv.def: New file.
	* gm2-libs-iso/ShortConv.mod: New file.
	* gm2-libs-iso/ShortMath.def: New file.
	* gm2-libs-iso/ShortMath.mod: New file.
	* gm2-libs-iso/ShortStr.def: New file.
	* gm2-libs-iso/ShortStr.mod: New file.

libgm2/ChangeLog:

	PR modula2/112921
	* libm2iso/Makefile.am (M2DEFS): Add ConvStringShort.def,
	ShortConv.def, ShortMath.def and ShortStr.def.
	(M2MODS): Add ConvStringShort.mod,
	ShortConv.mod, ShortMath.mod and ShortStr.mod.
	* libm2iso/Makefile.in: Regenerate.

gcc/testsuite/ChangeLog:

	PR modula2/112921
	* gm2/iso/run/pass/shorttest.mod: New test.

Signed-off-by: Gaius Mulley <gaiusmod2@gmail.com>
This commit is contained in:
Gaius Mulley 2023-12-13 17:35:02 +00:00
parent 90bc2d09b5
commit 33a3f85ee4
11 changed files with 1020 additions and 15 deletions

View File

@ -0,0 +1,60 @@
(* ConvStringShort.def converts floating point numbers to Strings.
Copyright (C) 2009-2023 Free Software Foundation, Inc.
Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
This file is part of GNU Modula-2.
GNU Modula-2 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, or (at your option)
any later version.
GNU Modula-2 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.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. *)
DEFINITION MODULE ConvStringShort ;
FROM DynamicStrings IMPORT String ;
(*
RealToFloatString - converts a real with, sigFigs, into a string
and returns the result as a string.
*)
PROCEDURE RealToFloatString (real: SHORTREAL; sigFigs: CARDINAL) : String ;
(*
RealToEngString - converts the value of real to floating-point
string form, with sigFigs significant figures.
The number is scaled with one to three digits
in the whole number part and with an exponent
that is a multiple of three.
*)
PROCEDURE RealToEngString (real: SHORTREAL; sigFigs: CARDINAL) : String ;
(*
RealToFixedString - returns the number of characters in the fixed-point
string representation of real rounded to the given
place relative to the decimal point.
*)
PROCEDURE RealToFixedString (real: SHORTREAL; place: INTEGER) : String ;
END ConvStringShort.

View File

@ -0,0 +1,69 @@
(* ConvStringShort.mod converts floating point numbers to Strings.
Copyright (C) 2023 Free Software Foundation, Inc.
Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
This file is part of GNU Modula-2.
GNU Modula-2 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, or (at your option)
any later version.
GNU Modula-2 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.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. *)
IMPLEMENTATION MODULE ConvStringShort ;
IMPORT ConvStringReal ;
(*
RealToFloatString - converts a real with, sigFigs, into a string
and returns the result as a string.
*)
PROCEDURE RealToFloatString (real: SHORTREAL; sigFigs: CARDINAL) : String ;
BEGIN
RETURN ConvStringReal.RealToFloatString (real, sigFigs)
END RealToFloatString ;
(*
RealToEngString - converts the value of real to floating-point
string form, with sigFigs significant figures.
The number is scaled with one to three digits
in the whole number part and with an exponent
that is a multiple of three.
*)
PROCEDURE RealToEngString (real: SHORTREAL; sigFigs: CARDINAL) : String ;
BEGIN
RETURN ConvStringReal.RealToEngString (real, sigFigs)
END RealToEngString ;
(*
RealToFixedString - returns the number of characters in the fixed-point
string representation of real rounded to the given
place relative to the decimal point.
*)
PROCEDURE RealToFixedString (real: SHORTREAL; place: INTEGER) : String ;
BEGIN
RETURN ConvStringReal.RealToFixedString (real, place)
END RealToFixedString ;
END ConvStringShort.

View File

@ -0,0 +1,73 @@
(* ShortStr.mod provides low level SHORTREAL/string conversions.
Copyright (C) 2023 Free Software Foundation, Inc.
Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
This file is part of GNU Modula-2.
GNU Modula-2 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, or (at your option)
any later version.
GNU Modula-2 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.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. *)
DEFINITION MODULE ShortConv;
IMPORT
ConvTypes;
TYPE
ConvResults = ConvTypes.ConvResults; (* strAllRight, strOutOfRange,
strWrongFormat, strEmpty *)
PROCEDURE ScanReal (inputCh: CHAR; VAR chClass: ConvTypes.ScanClass;
VAR nextState: ConvTypes.ScanState);
(* Represents the start state of a finite state scanner for real
numbers - assigns class of inputCh to chClass and a procedure
representing the next state to nextState.
*)
PROCEDURE FormatReal (str: ARRAY OF CHAR): ConvResults;
(* Returns the format of the string value for conversion to LONGREAL. *)
PROCEDURE ValueReal (str: ARRAY OF CHAR): SHORTREAL;
(* Returns the value corresponding to the real number string value
str if str is well-formed; otherwise raises the ShortConv exception.
*)
PROCEDURE LengthFloatReal (real: SHORTREAL; sigFigs: CARDINAL): CARDINAL;
(* Returns the number of characters in the floating-point string
representation of real with sigFigs significant figures.
*)
PROCEDURE LengthEngReal (real: SHORTREAL; sigFigs: CARDINAL): CARDINAL;
(* Returns the number of characters in the floating-point engineering
string representation of real with sigFigs significant figures.
*)
PROCEDURE LengthFixedReal (real: SHORTREAL; place: INTEGER): CARDINAL;
(* Returns the number of characters in the fixed-point string
representation of real rounded to the given place relative to the
decimal point.
*)
PROCEDURE IsRConvException (): BOOLEAN;
(* Returns TRUE if the current coroutine is in the exceptional
execution state because of the raising of an exception in a
routine from this module; otherwise returns FALSE.
*)
END ShortConv.

View File

@ -0,0 +1,350 @@
(* ShortConv.mod implement the ISO ShortConv specification.
Copyright (C) 2009-2023 Free Software Foundation, Inc.
Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
This file is part of GNU Modula-2.
GNU Modula-2 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, or (at your option)
any later version.
GNU Modula-2 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.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. *)
IMPLEMENTATION MODULE ShortConv ;
FROM SYSTEM IMPORT ADDRESS ;
FROM ConvTypes IMPORT ScanClass ;
FROM CharClass IMPORT IsNumeric, IsWhiteSpace ;
FROM DynamicStrings IMPORT String, InitString, InitStringCharStar, KillString, Length, Slice, Mark, Index, string ;
FROM dtoa IMPORT strtod ;
FROM ConvStringShort IMPORT RealToFloatString, RealToEngString, RealToFixedString ;
FROM M2RTS IMPORT Halt ;
FROM libc IMPORT free ;
IMPORT EXCEPTIONS ;
TYPE
RealConvException = (noException, invalid, outofrange) ;
VAR
realConv: EXCEPTIONS.ExceptionSource ;
(* Low-level LONGREAL/string conversions. *)
(* Represents the start state of a finite state scanner for real
numbers - assigns class of inputCh to chClass and a procedure
representing the next state to nextState.
*)
PROCEDURE ScanReal (inputCh: CHAR; VAR chClass: ConvTypes.ScanClass;
VAR nextState: ConvTypes.ScanState) ;
BEGIN
IF IsNumeric(inputCh)
THEN
nextState := scanSecondDigit ;
chClass := valid
ELSIF (inputCh='+') OR (inputCh='-')
THEN
nextState := scanFirstDigit ;
chClass := valid
ELSIF IsWhiteSpace(inputCh)
THEN
nextState := ScanReal ;
chClass := padding
ELSE
nextState := ScanReal ;
chClass := invalid
END
END ScanReal ;
(*
scanFirstDigit -
*)
PROCEDURE scanFirstDigit (inputCh: CHAR; VAR chClass: ConvTypes.ScanClass;
VAR nextState: ConvTypes.ScanState) ;
BEGIN
IF IsNumeric(inputCh)
THEN
nextState := scanSecondDigit ;
chClass := valid
ELSE
nextState := scanFirstDigit ;
chClass := invalid
END
END scanFirstDigit ;
(*
scanSecondDigit -
*)
PROCEDURE scanSecondDigit (inputCh: CHAR; VAR chClass: ConvTypes.ScanClass;
VAR nextState: ConvTypes.ScanState) ;
BEGIN
IF IsNumeric(inputCh)
THEN
nextState := scanSecondDigit ;
chClass := valid
ELSIF inputCh='.'
THEN
nextState := scanFixed ;
chClass := valid
ELSIF inputCh='E'
THEN
nextState := scanScientific ;
chClass := valid
ELSE
nextState := noOpFinished ;
chClass := terminator
END
END scanSecondDigit ;
(*
scanFixed -
*)
PROCEDURE scanFixed (inputCh: CHAR; VAR chClass: ConvTypes.ScanClass;
VAR nextState: ConvTypes.ScanState) ;
BEGIN
IF IsNumeric(inputCh)
THEN
nextState := scanFixed ;
chClass := valid
ELSIF inputCh='E'
THEN
nextState := scanScientific ;
chClass := valid
ELSE
nextState := noOpFinished ;
chClass := terminator
END
END scanFixed ;
(*
scanScientific -
*)
PROCEDURE scanScientific (inputCh: CHAR; VAR chClass: ConvTypes.ScanClass;
VAR nextState: ConvTypes.ScanState) ;
BEGIN
IF IsNumeric(inputCh)
THEN
nextState := scanScientificSecond ;
chClass := valid
ELSIF (inputCh='-') OR (inputCh='+')
THEN
nextState := scanScientificSign ;
chClass := valid
ELSE
nextState := scanScientific ;
chClass := invalid
END
END scanScientific ;
(*
scanScientificSign -
*)
PROCEDURE scanScientificSign (inputCh: CHAR; VAR chClass: ConvTypes.ScanClass;
VAR nextState: ConvTypes.ScanState) ;
BEGIN
IF IsNumeric(inputCh)
THEN
nextState := scanScientificSecond ;
chClass := valid
ELSE
nextState := scanScientificSign ;
chClass := invalid
END
END scanScientificSign ;
(*
scanScientificSecond -
*)
PROCEDURE scanScientificSecond (inputCh: CHAR; VAR chClass: ConvTypes.ScanClass;
VAR nextState: ConvTypes.ScanState) ;
BEGIN
IF IsNumeric(inputCh)
THEN
nextState := scanScientificSecond ;
chClass := valid
ELSE
nextState := noOpFinished ;
chClass := terminator
END
END scanScientificSecond ;
(*
noOpFinished -
*)
PROCEDURE noOpFinished (inputCh: CHAR; VAR chClass: ConvTypes.ScanClass;
VAR nextState: ConvTypes.ScanState) ;
BEGIN
nextState := noOpFinished ;
chClass := terminator ;
(* should we raise an exception here? *)
END noOpFinished ;
(* Returns the format of the string value for conversion to LONGREAL. *)
PROCEDURE FormatReal (str: ARRAY OF CHAR) : ConvResults ;
VAR
proc : ConvTypes.ScanState ;
chClass: ConvTypes.ScanClass ;
i, h : CARDINAL ;
BEGIN
i := 1 ;
h := LENGTH(str) ;
ScanReal(str[0], chClass, proc) ;
WHILE (i<h) AND (chClass=padding) DO
proc(str[i], chClass, proc) ;
INC(i)
END ;
IF chClass=terminator
THEN
RETURN( strEmpty )
END ;
WHILE (i<h) AND (chClass=valid) DO
proc(str[i], chClass, proc) ;
INC(i)
END ;
CASE chClass OF
padding : RETURN( strWrongFormat ) |
terminator,
valid : RETURN( strAllRight ) |
invalid : RETURN( strWrongFormat )
END
END FormatReal ;
(* Returns the value corresponding to the real number string value
str if str is well-formed; otherwise raises the RealConv
exception.
*)
PROCEDURE ValueReal (str: ARRAY OF CHAR) : SHORTREAL ;
BEGIN
IF FormatReal(str)=strAllRight
THEN
RETURN( doValueReal(str) )
ELSE
EXCEPTIONS.RAISE(realConv, ORD(invalid),
'ShortConv.' + __FUNCTION__ + ': real number is invalid')
END
END ValueReal ;
(*
doValueReal - str, is a well-formed real number and its
value is returned.
*)
PROCEDURE doValueReal (str: ARRAY OF CHAR) : SHORTREAL ;
VAR
r : SHORTREAL ;
error: BOOLEAN ;
s : String ;
BEGIN
s := InitString(str) ;
r := strtod (string(s), error) ;
s := KillString(s) ;
IF error
THEN
EXCEPTIONS.RAISE (realConv, ORD(outofrange),
'ShortConv.' + __FUNCTION__ + ': real number is out of range')
END ;
RETURN r
END doValueReal ;
(* Returns the number of characters in the floating-point string
representation of real with sigFigs significant figures.
*)
PROCEDURE LengthFloatReal (real: SHORTREAL; sigFigs: CARDINAL) : CARDINAL ;
VAR
s: String ;
l: CARDINAL ;
BEGIN
s := RealToFloatString (real, sigFigs) ;
l := Length (s) ;
s := KillString (s) ;
RETURN l
END LengthFloatReal ;
(* Returns the number of characters in the floating-point engineering
string representation of real with sigFigs significant figures.
*)
PROCEDURE LengthEngReal (real: SHORTREAL; sigFigs: CARDINAL) : CARDINAL ;
VAR
s: String ;
l: CARDINAL ;
BEGIN
s := RealToEngString (real, sigFigs) ;
l := Length (s) ;
s := KillString (s) ;
RETURN l
END LengthEngReal ;
(* Returns the number of characters in the fixed-point string
representation of real rounded to the given place relative to the
decimal point.
*)
PROCEDURE LengthFixedReal (real: SHORTREAL; place: INTEGER) : CARDINAL ;
VAR
s: String ;
l: CARDINAL ;
BEGIN
s := RealToFixedString (real, place) ;
l := Length (s) ;
s := KillString (s) ;
RETURN l
END LengthFixedReal ;
(* Returns TRUE if the current coroutine is in the exceptional
execution state because of the raising of an exception in a
routine from this module; otherwise returns FALSE.
*)
PROCEDURE IsRConvException () : BOOLEAN ;
BEGIN
RETURN( EXCEPTIONS.IsCurrentSource(realConv) )
END IsRConvException ;
BEGIN
EXCEPTIONS.AllocateSource (realConv)
END ShortConv.

View File

@ -0,0 +1,76 @@
(* LongMath.def provides access to the ShortReal intrinsics.
Copyright (C) 2023 Free Software Foundation, Inc.
Contributed by Gaius Mulley <gaiusmod2@gmail.com>.
This file is part of GNU Modula-2.
GNU Modula-2 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, or (at your option)
any later version.
GNU Modula-2 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.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. *)
DEFINITION MODULE ShortMath;
(* Mathematical functions for the type LONGREAL *)
CONST
pi = 3.1415926535897932384626433832795028841972;
exp1 = 2.7182818284590452353602874713526624977572;
PROCEDURE __BUILTIN__ sqrt (x: SHORTREAL): SHORTREAL;
(* Returns the positive square root of x *)
PROCEDURE __BUILTIN__ exp (x: SHORTREAL): SHORTREAL;
(* Returns the exponential of x *)
PROCEDURE __BUILTIN__ ln (x: SHORTREAL): SHORTREAL;
(* Returns the natural logarithm of x *)
(* The angle in all trigonometric functions is measured in radians *)
PROCEDURE __BUILTIN__ sin (x: SHORTREAL): SHORTREAL;
(* Returns the sine of x *)
PROCEDURE __BUILTIN__ cos (x: SHORTREAL): SHORTREAL;
(* Returns the cosine of x *)
PROCEDURE tan (x: SHORTREAL): SHORTREAL;
(* Returns the tangent of x *)
PROCEDURE arcsin (x: SHORTREAL): SHORTREAL;
(* Returns the arcsine of x *)
PROCEDURE arccos (x: SHORTREAL): SHORTREAL;
(* Returns the arccosine of x *)
PROCEDURE arctan (x: SHORTREAL): SHORTREAL;
(* Returns the arctangent of x *)
PROCEDURE power (base, exponent: SHORTREAL): SHORTREAL;
(* Returns the value of the number base raised to the power exponent *)
PROCEDURE round (x: SHORTREAL): INTEGER;
(* Returns the value of x rounded to the nearest integer *)
PROCEDURE IsRMathException (): BOOLEAN;
(* Returns TRUE if the current coroutine is in the exceptional
execution state because of the raising of an exception in a
routine from this module; otherwise returns FALSE.
*)
END ShortMath.

View File

@ -0,0 +1,110 @@
(* LongMath.mod implement the ISO LongMath specification.
Copyright (C) 2023 Free Software Foundation, Inc.
Contributed by Gaius Mulley <gaiusmod2@gmail.com>.
This file is part of GNU Modula-2.
GNU Modula-2 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, or (at your option)
any later version.
GNU Modula-2 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.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. *)
IMPLEMENTATION MODULE ShortMath ;
IMPORT libm ;
IMPORT cbuiltin ;
PROCEDURE __ATTRIBUTE__ __BUILTIN__ ((__builtin_sqrtf)) sqrt (x: SHORTREAL): SHORTREAL;
(* Returns the positive square root of x *)
BEGIN
RETURN cbuiltin.sqrtf (x)
END sqrt ;
PROCEDURE __ATTRIBUTE__ __BUILTIN__ ((__builtin_expf)) exp (x: SHORTREAL): SHORTREAL;
(* Returns the exponential of x *)
BEGIN
RETURN cbuiltin.expf (x)
END exp ;
PROCEDURE __ATTRIBUTE__ __BUILTIN__ ((__builtin_logf)) ln (x: SHORTREAL): SHORTREAL;
(* Returns the natural logarithm of x *)
BEGIN
RETURN cbuiltin.logf (x)
END ln ;
(* The angle in all trigonometric functions is measured in radians *)
PROCEDURE __ATTRIBUTE__ __BUILTIN__ ((__builtin_sinf)) sin (x: SHORTREAL): SHORTREAL;
(* Returns the sine of x *)
BEGIN
RETURN cbuiltin.sinf (x)
END sin ;
PROCEDURE __ATTRIBUTE__ __BUILTIN__ ((__builtin_cosf)) cos (x: SHORTREAL): SHORTREAL;
(* Returns the cosine of x *)
BEGIN
RETURN cbuiltin.cosf (x)
END cos ;
PROCEDURE tan (x: SHORTREAL): SHORTREAL;
(* Returns the tangent of x *)
BEGIN
RETURN libm.tanf (x)
END tan ;
PROCEDURE arcsin (x: SHORTREAL): SHORTREAL;
(* Returns the arcsine of x *)
BEGIN
RETURN libm.asinf (x)
END arcsin ;
PROCEDURE arccos (x: SHORTREAL): SHORTREAL;
(* Returns the arccosine of x *)
BEGIN
RETURN libm.acosf (x)
END arccos ;
PROCEDURE arctan (x: SHORTREAL): SHORTREAL;
(* Returns the arctangent of x *)
BEGIN
RETURN libm.atanf (x)
END arctan ;
PROCEDURE power (base, exponent: SHORTREAL): SHORTREAL;
(* Returns the value of the number base raised to the power exponent *)
BEGIN
RETURN libm.powf (base, exponent)
END power ;
PROCEDURE round (x: SHORTREAL) : INTEGER;
(* Returns the value of x rounded to the nearest integer *)
BEGIN
RETURN TRUNC (x)
END round ;
PROCEDURE IsRMathException (): BOOLEAN;
(* Returns TRUE if the current coroutine is in the
exceptional execution state because of the raising
of an exception in a routine from this module; otherwise
returns FALSE.
*)
BEGIN
RETURN FALSE
END IsRMathException ;
END ShortMath.

View File

@ -0,0 +1,87 @@
(* ShortStr.def provides conversion between shortreal and strings.
Copyright (C) 2023 Free Software Foundation, Inc.
Contributed by Gaius Mulley <gaiusmod2@gmail.com>.
This file is part of GNU Modula-2.
GNU Modula-2 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, or (at your option)
any later version.
GNU Modula-2 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.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. *)
DEFINITION MODULE ShortStr;
(* SHORTREAL/string conversions *)
IMPORT
ConvTypes;
TYPE
(* strAllRight, strOutOfRange, strWrongFormat, strEmpty *)
ConvResults = ConvTypes.ConvResults;
(* the string form of a signed fixed-point real number is
["+" | "-"], decimal digit, {decimal digit}, [".",
{decimal digit}]
*)
(* the string form of a signed floating-point real number is
signed fixed-point real number, "E", ["+" | "-"],
decimal digit, {decimal digit}
*)
PROCEDURE StrToReal (str: ARRAY OF CHAR; VAR real: SHORTREAL;
VAR res: ConvResults);
(* Ignores any leading spaces in str. If the subsequent characters
in str are in the format of a signed real number, assigns a
corresponding value to real. Assigns a value indicating the
format of str to res.
*)
PROCEDURE RealToFloat (real: SHORTREAL; sigFigs: CARDINAL;
VAR str: ARRAY OF CHAR);
(* Converts the value of real to floating-point string form, with
sigFigs significant figures, and copies the possibly truncated
result to str.
*)
PROCEDURE RealToEng (real: SHORTREAL; sigFigs: CARDINAL;
VAR str: ARRAY OF CHAR);
(* Converts the value of real to floating-point string form, with
sigFigs significant figures, and copies the possibly truncated
result to str. The number is scaled with one to three digits
in the whole number part and with an exponent that is a
multiple of three.
*)
PROCEDURE RealToFixed (real: SHORTREAL; place: INTEGER;
VAR str: ARRAY OF CHAR);
(* Converts the value of real to fixed-point string form, rounded
to the given place relative to the decimal point, and copies
the possibly truncated result to str.
*)
PROCEDURE RealToStr (real: SHORTREAL; VAR str: ARRAY OF CHAR);
(* Converts the value of real as RealToFixed if the sign and
magnitude can be shown within the capacity of str, or
otherwise as RealToFloat, and copies the possibly truncated
result to str. The number of places or significant digits
depend on the capacity of str.
*)
END ShortStr.

View File

@ -0,0 +1,150 @@
(* ShortStr.mod implement the ISO ShortStr specification.
Copyright (C) 2009-2023 Free Software Foundation, Inc.
Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
This file is part of GNU Modula-2.
GNU Modula-2 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, or (at your option)
any later version.
GNU Modula-2 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.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. *)
IMPLEMENTATION MODULE ShortStr;
(* REAL/string conversions *)
IMPORT ShortConv ;
FROM DynamicStrings IMPORT String, InitString, KillString, Length, CopyOut ;
FROM ConvStringShort IMPORT RealToFixedString, RealToFloatString,
RealToEngString ;
(* the string form of a signed fixed-point real number is
["+" | "-"], decimal digit, {decimal digit}, [".",
{decimal digit}]
*)
(* the string form of a signed floating-point real number is
signed fixed-point real number, "E", ["+" | "-"],
decimal digit, {decimal digit}
*)
PROCEDURE StrToReal (str: ARRAY OF CHAR; VAR real: SHORTREAL;
VAR res: ConvResults) ;
(* Ignores any leading spaces in str. If the subsequent characters
in str are in the format of a signed real number, assigns a
corresponding value to real. Assigns a value indicating the
format of str to res.
*)
BEGIN
res := ShortConv.FormatReal(str) ;
IF res=strAllRight
THEN
real := ShortConv.ValueReal(str)
END
END StrToReal ;
PROCEDURE RealToFloat (real: SHORTREAL; sigFigs: CARDINAL;
VAR str: ARRAY OF CHAR) ;
(* Converts the value of real to floating-point string form, with
sigFigs significant figures, and copies the possibly truncated
result to str.
*)
VAR
s: String ;
BEGIN
s := RealToFloatString(real, sigFigs) ;
CopyOut(str, s) ;
s := KillString(s)
END RealToFloat ;
PROCEDURE RealToEng (real: SHORTREAL; sigFigs: CARDINAL;
VAR str: ARRAY OF CHAR) ;
(* Converts the value of real to floating-point string form, with
sigFigs significant figures, and copies the possibly truncated
result to str. The number is scaled with one to three digits
in the whole number part and with an exponent that is a multiple
of three.
*)
VAR
s: String ;
BEGIN
s := RealToEngString(real, sigFigs) ;
CopyOut(str, s) ;
s := KillString(s)
END RealToEng ;
PROCEDURE RealToFixed (real: SHORTREAL; place: INTEGER;
VAR str: ARRAY OF CHAR) ;
(* Converts the value of real to fixed-point string form, rounded
to the given place relative to the decimal point, and copies
the possibly truncated result to str.
*)
VAR
s: String ;
BEGIN
s := RealToFixedString(real, place) ;
CopyOut(str, s) ;
s := KillString(s)
END RealToFixed ;
PROCEDURE RealToStr (real: SHORTREAL; VAR str: ARRAY OF CHAR) ;
(* Converts the value of real as RealToFixed if the sign and
magnitude can be shown within the capacity of str, or
otherwise as RealToFloat, and copies the possibly truncated
result to str. The number of places or significant digits
are implementation-defined.
*)
VAR
s : String ;
sigFigs: CARDINAL ;
BEGIN
sigFigs := HIGH(str) ;
WHILE sigFigs>1 DO
s := RealToFixedString(real, sigFigs) ;
IF Length(s)<=HIGH(str)
THEN
CopyOut(str, s) ;
s := KillString(s) ;
RETURN
END ;
s := KillString(s) ;
DEC(sigFigs)
END ;
sigFigs := HIGH(str) ;
WHILE sigFigs#0 DO
s := RealToFloatString(real, sigFigs) ;
IF Length(s)<=HIGH(str)
THEN
CopyOut(str, s) ;
s := KillString(s) ;
RETURN
END ;
s := KillString(s) ;
DEC(sigFigs)
END
END RealToStr ;
END ShortStr.

View File

@ -0,0 +1,13 @@
MODULE shorttest ;
FROM ShortStr IMPORT RealToStr ;
FROM STextIO IMPORT WriteString, WriteLn ;
FROM ShortMath IMPORT pi ;
VAR
buf: ARRAY [0..30] OF CHAR ;
BEGIN
WriteString ("pi = ") ;
RealToStr (pi, buf) ;
WriteString (buf) ; WriteLn
END shorttest.

View File

@ -101,6 +101,7 @@ if BUILD_ISOLIB
M2DEFS = ChanConsts.def CharClass.def \
ClientSocket.def ComplexMath.def \
ConvStringLong.def ConvStringReal.def \
ConvStringShort.def \
ConvTypes.def COROUTINES.def \
ErrnoCategory.def EXCEPTIONS.def \
GeneralUserExceptions.def IOChan.def \
@ -124,7 +125,10 @@ M2DEFS = ChanConsts.def CharClass.def \
RTgenif.def RTio.def \
Semaphores.def SeqFile.def \
ShortComplexMath.def \
ShortIO.def ShortWholeIO.def \
ShortConv.def \
ShortIO.def \
ShortMath.def ShortStr.def \
ShortWholeIO.def \
SimpleCipher.def SIOResult.def \
SLongIO.def SLongWholeIO.def \
SRawIO.def SRealIO.def \
@ -143,6 +147,7 @@ M2DEFS = ChanConsts.def CharClass.def \
M2MODS = ChanConsts.mod CharClass.mod \
ClientSocket.mod ComplexMath.mod \
ConvStringLong.mod ConvStringReal.mod \
ConvStringShort.mod \
ConvTypes.mod COROUTINES.mod \
EXCEPTIONS.mod GeneralUserExceptions.mod \
IOChan.mod IOConsts.mod \
@ -164,7 +169,10 @@ M2MODS = ChanConsts.mod CharClass.mod \
RTgen.mod RTio.mod \
Semaphores.mod SeqFile.mod \
ShortComplexMath.mod \
ShortIO.mod ShortWholeIO.mod \
ShortConv.mod \
ShortIO.mod \
ShortMath.mod ShortStr.mod \
ShortWholeIO.mod \
SimpleCipher.mod SIOResult.mod \
SLongIO.mod SLongWholeIO.mod \
SRawIO.mod SRealIO.mod \

View File

@ -160,7 +160,8 @@ libm2iso_la_LIBADD =
@BUILD_ISOLIB_TRUE@am__objects_1 = ChanConsts.lo CharClass.lo \
@BUILD_ISOLIB_TRUE@ ClientSocket.lo ComplexMath.lo \
@BUILD_ISOLIB_TRUE@ ConvStringLong.lo ConvStringReal.lo \
@BUILD_ISOLIB_TRUE@ ConvTypes.lo COROUTINES.lo EXCEPTIONS.lo \
@BUILD_ISOLIB_TRUE@ ConvStringShort.lo ConvTypes.lo \
@BUILD_ISOLIB_TRUE@ COROUTINES.lo EXCEPTIONS.lo \
@BUILD_ISOLIB_TRUE@ GeneralUserExceptions.lo IOChan.lo \
@BUILD_ISOLIB_TRUE@ IOConsts.lo IOLink.lo IOResult.lo \
@BUILD_ISOLIB_TRUE@ LongComplexMath.lo LongConv.lo LongIO.lo \
@ -172,16 +173,16 @@ libm2iso_la_LIBADD =
@BUILD_ISOLIB_TRUE@ RealIO.lo RealMath.lo RealStr.lo RndFile.lo \
@BUILD_ISOLIB_TRUE@ RTdata.lo RTentity.lo RTfio.lo RTgenif.lo \
@BUILD_ISOLIB_TRUE@ RTgen.lo RTio.lo Semaphores.lo SeqFile.lo \
@BUILD_ISOLIB_TRUE@ ShortComplexMath.lo ShortIO.lo \
@BUILD_ISOLIB_TRUE@ ShortWholeIO.lo SimpleCipher.lo \
@BUILD_ISOLIB_TRUE@ SIOResult.lo SLongIO.lo SLongWholeIO.lo \
@BUILD_ISOLIB_TRUE@ SRawIO.lo SRealIO.lo SShortIO.lo \
@BUILD_ISOLIB_TRUE@ SShortWholeIO.lo StdChans.lo STextIO.lo \
@BUILD_ISOLIB_TRUE@ Storage.lo StreamFile.lo StringChan.lo \
@BUILD_ISOLIB_TRUE@ Strings.lo SWholeIO.lo SysClock.lo \
@BUILD_ISOLIB_TRUE@ SYSTEM.lo TermFile.lo TERMINATION.lo \
@BUILD_ISOLIB_TRUE@ TextIO.lo TextUtil.lo WholeConv.lo \
@BUILD_ISOLIB_TRUE@ WholeIO.lo WholeStr.lo
@BUILD_ISOLIB_TRUE@ ShortComplexMath.lo ShortConv.lo ShortIO.lo \
@BUILD_ISOLIB_TRUE@ ShortMath.lo ShortStr.lo ShortWholeIO.lo \
@BUILD_ISOLIB_TRUE@ SimpleCipher.lo SIOResult.lo SLongIO.lo \
@BUILD_ISOLIB_TRUE@ SLongWholeIO.lo SRawIO.lo SRealIO.lo \
@BUILD_ISOLIB_TRUE@ SShortIO.lo SShortWholeIO.lo StdChans.lo \
@BUILD_ISOLIB_TRUE@ STextIO.lo Storage.lo StreamFile.lo \
@BUILD_ISOLIB_TRUE@ StringChan.lo Strings.lo SWholeIO.lo \
@BUILD_ISOLIB_TRUE@ SysClock.lo SYSTEM.lo TermFile.lo \
@BUILD_ISOLIB_TRUE@ TERMINATION.lo TextIO.lo TextUtil.lo \
@BUILD_ISOLIB_TRUE@ WholeConv.lo WholeIO.lo WholeStr.lo
@BUILD_ISOLIB_TRUE@am_libm2iso_la_OBJECTS = $(am__objects_1) \
@BUILD_ISOLIB_TRUE@ ErrnoCategory.lo RTco.lo wrapclock.lo \
@BUILD_ISOLIB_TRUE@ wraptime.lo libm2iso_la-wrapsock.lo
@ -488,6 +489,7 @@ FLAGS_TO_PASS = $(AM_MAKEFLAGS)
@BUILD_ISOLIB_TRUE@M2DEFS = ChanConsts.def CharClass.def \
@BUILD_ISOLIB_TRUE@ ClientSocket.def ComplexMath.def \
@BUILD_ISOLIB_TRUE@ ConvStringLong.def ConvStringReal.def \
@BUILD_ISOLIB_TRUE@ ConvStringShort.def \
@BUILD_ISOLIB_TRUE@ ConvTypes.def COROUTINES.def \
@BUILD_ISOLIB_TRUE@ ErrnoCategory.def EXCEPTIONS.def \
@BUILD_ISOLIB_TRUE@ GeneralUserExceptions.def IOChan.def \
@ -511,7 +513,10 @@ FLAGS_TO_PASS = $(AM_MAKEFLAGS)
@BUILD_ISOLIB_TRUE@ RTgenif.def RTio.def \
@BUILD_ISOLIB_TRUE@ Semaphores.def SeqFile.def \
@BUILD_ISOLIB_TRUE@ ShortComplexMath.def \
@BUILD_ISOLIB_TRUE@ ShortIO.def ShortWholeIO.def \
@BUILD_ISOLIB_TRUE@ ShortConv.def \
@BUILD_ISOLIB_TRUE@ ShortIO.def \
@BUILD_ISOLIB_TRUE@ ShortMath.def ShortStr.def \
@BUILD_ISOLIB_TRUE@ ShortWholeIO.def \
@BUILD_ISOLIB_TRUE@ SimpleCipher.def SIOResult.def \
@BUILD_ISOLIB_TRUE@ SLongIO.def SLongWholeIO.def \
@BUILD_ISOLIB_TRUE@ SRawIO.def SRealIO.def \
@ -530,6 +535,7 @@ FLAGS_TO_PASS = $(AM_MAKEFLAGS)
@BUILD_ISOLIB_TRUE@M2MODS = ChanConsts.mod CharClass.mod \
@BUILD_ISOLIB_TRUE@ ClientSocket.mod ComplexMath.mod \
@BUILD_ISOLIB_TRUE@ ConvStringLong.mod ConvStringReal.mod \
@BUILD_ISOLIB_TRUE@ ConvStringShort.mod \
@BUILD_ISOLIB_TRUE@ ConvTypes.mod COROUTINES.mod \
@BUILD_ISOLIB_TRUE@ EXCEPTIONS.mod GeneralUserExceptions.mod \
@BUILD_ISOLIB_TRUE@ IOChan.mod IOConsts.mod \
@ -551,7 +557,10 @@ FLAGS_TO_PASS = $(AM_MAKEFLAGS)
@BUILD_ISOLIB_TRUE@ RTgen.mod RTio.mod \
@BUILD_ISOLIB_TRUE@ Semaphores.mod SeqFile.mod \
@BUILD_ISOLIB_TRUE@ ShortComplexMath.mod \
@BUILD_ISOLIB_TRUE@ ShortIO.mod ShortWholeIO.mod \
@BUILD_ISOLIB_TRUE@ ShortConv.mod \
@BUILD_ISOLIB_TRUE@ ShortIO.mod \
@BUILD_ISOLIB_TRUE@ ShortMath.mod ShortStr.mod \
@BUILD_ISOLIB_TRUE@ ShortWholeIO.mod \
@BUILD_ISOLIB_TRUE@ SimpleCipher.mod SIOResult.mod \
@BUILD_ISOLIB_TRUE@ SLongIO.mod SLongWholeIO.mod \
@BUILD_ISOLIB_TRUE@ SRawIO.mod SRealIO.mod \