mescc: Mes C Library: Support GNU Awk: Add abtod, implementing strtod.

* include/libmes.h (abtod): Declare.
* lib/mes/abtod.c: New file.
* build-aux/configure-lib.sh (libmes_SOURCES, libc_tcc_SOURCES): Add
it.
* lib/mes/abtol.c: Update.
* lib/stdlib/strtod.c: Use it to implement; move from stub/strtod.
* lib/tests/mes/90-abtod.c: Test it.
* lib/tests/mes/90-abtod.stdout: Baseline.
* build-aux/check-mescc.sh (tests): Run it.
This commit is contained in:
Jan Nieuwenhuizen 2019-06-25 12:51:44 +02:00
parent cd50beca3a
commit 742e88abd5
No known key found for this signature in database
GPG Key ID: F3C1A0D9C1D65273
8 changed files with 117 additions and 11 deletions

View File

@ -200,6 +200,7 @@ if test -z "$bootstrap"; then
TESTS="$TESTS
lib/tests/dirent/90-readdir.c
lib/tests/io/90-stat.c
lib/tests/mes/90-abtod.c
lib/tests/posix/90-execlp.c
lib/tests/posix/90-unsetenv.c
lib/tests/signal/90-signal.c
@ -223,6 +224,7 @@ lib/tests/scaffold/a1-global-no-clobber.c
fi
XFAIL_TESTS="
lib/tests/mes/90-abtod.c
lib/tests/stdio/90-sprintf.c
lib/tests/stdio/90-sprintf.c
"
@ -262,6 +264,7 @@ fi
if test $compiler = gcc; then
XFAIL_TESTS="$XFAIL_TESTS
lib/tests/mes/90-abtod.c
"
if test $mes_cpu = x86; then

View File

@ -86,6 +86,7 @@ lib/linux/lseek.c
fi
else
libmes_SOURCES="$libmes_SOURCES
lib/mes/abtod.c
"
fi
@ -156,6 +157,7 @@ lib/ctype/islower.c
lib/ctype/isupper.c
lib/ctype/tolower.c
lib/ctype/toupper.c
lib/mes/abtod.c
lib/mes/search-path.c
lib/posix/execvp.c
lib/stdio/fclose.c
@ -180,6 +182,7 @@ lib/stdio/vsprintf.c
lib/stdio/vsscanf.c
lib/stdlib/calloc.c
lib/stdlib/qsort.c
lib/stdlib/strtod.c
lib/stdlib/strtof.c
lib/stdlib/strtol.c
lib/stdlib/strtold.c
@ -199,7 +202,6 @@ lib/stub/ldexp.c
lib/stub/mprotect.c
lib/stub/localtime.c
lib/stub/sigemptyset.c
lib/stub/strtod.c
lib/$mes_cpu-mes-$compiler/setjmp.c
"

View File

@ -28,6 +28,7 @@ void __ungetc_init ();
void __ungetc_clear (int filedes);
void __ungetc_set (int filedes, int c);
int __ungetc_p (int filedes);
double abtod (char const **p, int base);
long abtol (char const **p, int base);
char *itoa (int number);
char *ltoa (long number);

53
lib/mes/abtod.c Normal file
View File

@ -0,0 +1,53 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of GNU Mes.
*
* GNU Mes 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.
*
* GNU Mes 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 GNU Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <mes/lib.h>
double
abtod (char const **p, int base)
{
char const *s = *p;
double d = 0;
int sign_p = 0;
if (!base)
base = 10;
double dbase = base;
long i = abtol (&s, base);
long f = 0;
long e = 0;
if (*s == '.')
{
s++;
f = abtol (&s, base);
}
if (*s == 'e')
{
s++;
e = abtol (&s, base);
}
d = i + f / dbase;
if (e < 0)
while (e++)
d = d / dbase;
while (e--)
d = d * dbase;
*p = s;
return sign_p ? -d : d;
}

View File

@ -26,12 +26,16 @@ abtol (char const **p, int base)
{
char const *s = *p;
int i = 0;
int sign = 1;
int sign_p = 0;
if (!base)
base = 10;
while (isspace (*s))
s++;
if (*s && *s == '+')
s++;
if (*s && *s == '-')
{
sign = -1;
sign_p = 1;
s++;
}
while (isnumber (*s, base))
@ -42,5 +46,5 @@ abtol (char const **p, int base)
s++;
}
*p = s;
return i * sign;
return sign_p ? -i : i;
}

41
lib/stdlib/strtod.c Normal file
View File

@ -0,0 +1,41 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2017,2018,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of GNU Mes.
*
* GNU Mes 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.
*
* GNU Mes 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 GNU Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <mes/lib.h>
#include <stdlib.h>
#include <string.h>
double
strtod (char const *string, char **tailptr)
{
int base = 10;
if (!strncmp (string, "0x", 2))
{
string += 2;
base = 16;
}
if (tailptr)
{
*tailptr = (char *) string;
return abtod ((char const **) tailptr, base);
}
char **p = (char **) &string;
return abtod ((char const **) p, base);
}

View File

@ -1,6 +1,6 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
* Copyright © 2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of GNU Mes.
*
@ -21,12 +21,13 @@
#include <mes/lib.h>
#include <stdlib.h>
double
strtod (char const *string, char **tailptr)
int
main ()
{
static int stub = 0;
if (__mes_debug () && !stub)
eputs ("strtod stub\n");
stub = 1;
char *s = "1.2e3";
char *p = s;
double d = abtod (&p, 0);
printf ("%f\n", d);
return 0;
}

View File

@ -0,0 +1 @@
1200.000000