From: Gaius Mulley Date: Wed, 13 Dec 2023 17:35:02 +0000 (+0000) Subject: PR modula2/112921 missing modules shortreal shortstr shortconv convstringshort X-Git-Tag: basepoints/gcc-15~3630 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=33a3f85ee4b5bc562a84c6896294278e0a5ab160;p=thirdparty%2Fgcc.git 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 --- diff --git a/gcc/m2/gm2-libs-iso/ConvStringShort.def b/gcc/m2/gm2-libs-iso/ConvStringShort.def new file mode 100644 index 000000000000..a6b485c1d996 --- /dev/null +++ b/gcc/m2/gm2-libs-iso/ConvStringShort.def @@ -0,0 +1,60 @@ +(* ConvStringShort.def converts floating point numbers to Strings. + +Copyright (C) 2009-2023 Free Software Foundation, Inc. +Contributed by Gaius Mulley . + +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 +. *) + +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. diff --git a/gcc/m2/gm2-libs-iso/ConvStringShort.mod b/gcc/m2/gm2-libs-iso/ConvStringShort.mod new file mode 100644 index 000000000000..064027c017c0 --- /dev/null +++ b/gcc/m2/gm2-libs-iso/ConvStringShort.mod @@ -0,0 +1,69 @@ +(* ConvStringShort.mod converts floating point numbers to Strings. + +Copyright (C) 2023 Free Software Foundation, Inc. +Contributed by Gaius Mulley . + +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 +. *) + +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. diff --git a/gcc/m2/gm2-libs-iso/ShortConv.def b/gcc/m2/gm2-libs-iso/ShortConv.def new file mode 100644 index 000000000000..2373c7b0b83e --- /dev/null +++ b/gcc/m2/gm2-libs-iso/ShortConv.def @@ -0,0 +1,73 @@ +(* ShortStr.mod provides low level SHORTREAL/string conversions. + +Copyright (C) 2023 Free Software Foundation, Inc. +Contributed by Gaius Mulley . + +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 +. *) + +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. diff --git a/gcc/m2/gm2-libs-iso/ShortConv.mod b/gcc/m2/gm2-libs-iso/ShortConv.mod new file mode 100644 index 000000000000..66f31b51960a --- /dev/null +++ b/gcc/m2/gm2-libs-iso/ShortConv.mod @@ -0,0 +1,350 @@ +(* ShortConv.mod implement the ISO ShortConv specification. + +Copyright (C) 2009-2023 Free Software Foundation, Inc. +Contributed by Gaius Mulley . + +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 +. *) + +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. + +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 +. *) + +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. diff --git a/gcc/m2/gm2-libs-iso/ShortMath.mod b/gcc/m2/gm2-libs-iso/ShortMath.mod new file mode 100644 index 000000000000..3776b3b99d7d --- /dev/null +++ b/gcc/m2/gm2-libs-iso/ShortMath.mod @@ -0,0 +1,110 @@ +(* LongMath.mod implement the ISO LongMath specification. + +Copyright (C) 2023 Free Software Foundation, Inc. +Contributed by Gaius Mulley . + +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 +. *) + +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. diff --git a/gcc/m2/gm2-libs-iso/ShortStr.def b/gcc/m2/gm2-libs-iso/ShortStr.def new file mode 100644 index 000000000000..42fa9dd2e849 --- /dev/null +++ b/gcc/m2/gm2-libs-iso/ShortStr.def @@ -0,0 +1,87 @@ +(* ShortStr.def provides conversion between shortreal and strings. + +Copyright (C) 2023 Free Software Foundation, Inc. +Contributed by Gaius Mulley . + +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 +. *) + +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. diff --git a/gcc/m2/gm2-libs-iso/ShortStr.mod b/gcc/m2/gm2-libs-iso/ShortStr.mod new file mode 100644 index 000000000000..946d8ae681ef --- /dev/null +++ b/gcc/m2/gm2-libs-iso/ShortStr.mod @@ -0,0 +1,150 @@ +(* ShortStr.mod implement the ISO ShortStr specification. + +Copyright (C) 2009-2023 Free Software Foundation, Inc. +Contributed by Gaius Mulley . + +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 +. *) + +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. diff --git a/gcc/testsuite/gm2/iso/run/pass/shorttest.mod b/gcc/testsuite/gm2/iso/run/pass/shorttest.mod new file mode 100644 index 000000000000..f6c343c21c82 --- /dev/null +++ b/gcc/testsuite/gm2/iso/run/pass/shorttest.mod @@ -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. diff --git a/libgm2/libm2iso/Makefile.am b/libgm2/libm2iso/Makefile.am index 8e774c5ea195..01b5dc7001a3 100644 --- a/libgm2/libm2iso/Makefile.am +++ b/libgm2/libm2iso/Makefile.am @@ -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 \ diff --git a/libgm2/libm2iso/Makefile.in b/libgm2/libm2iso/Makefile.in index a82aa0c408bc..1d04835fbf09 100644 --- a/libgm2/libm2iso/Makefile.in +++ b/libgm2/libm2iso/Makefile.in @@ -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 \