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

View file

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

View file

@ -28,6 +28,7 @@ void __ungetc_init ();
void __ungetc_clear (int filedes); void __ungetc_clear (int filedes);
void __ungetc_set (int filedes, int c); void __ungetc_set (int filedes, int c);
int __ungetc_p (int filedes); int __ungetc_p (int filedes);
double abtod (char const **p, int base);
long abtol (char const **p, int base); long abtol (char const **p, int base);
char *itoa (int number); char *itoa (int number);
char *ltoa (long 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; char const *s = *p;
int i = 0; int i = 0;
int sign = 1; int sign_p = 0;
if (!base) if (!base)
base = 10; base = 10;
while (isspace (*s))
s++;
if (*s && *s == '+')
s++;
if (*s && *s == '-') if (*s && *s == '-')
{ {
sign = -1; sign_p = 1;
s++; s++;
} }
while (isnumber (*s, base)) while (isnumber (*s, base))
@ -42,5 +46,5 @@ abtol (char const **p, int base)
s++; s++;
} }
*p = 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:""-*- /* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software * 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. * This file is part of GNU Mes.
* *
@ -21,12 +21,13 @@
#include <mes/lib.h> #include <mes/lib.h>
#include <stdlib.h> #include <stdlib.h>
double int
strtod (char const *string, char **tailptr) main ()
{ {
static int stub = 0; char *s = "1.2e3";
if (__mes_debug () && !stub) char *p = s;
eputs ("strtod stub\n"); double d = abtod (&p, 0);
stub = 1; printf ("%f\n", d);
return 0; return 0;
} }

View file

@ -0,0 +1 @@
1200.000000