simplified CHAR_TO_NUM() and renamed it to ZDIGIT_TO_NUM() in comp.c

This commit is contained in:
hyunghwan.chung 2020-05-27 09:52:06 +00:00
parent f40a4a5ee1
commit 1ac58605a3
2 changed files with 16 additions and 14 deletions

View File

@ -11,6 +11,6 @@
#include 'Stream.moo'. #include 'Stream.moo'.
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
#include 'FFI.moo'. //#include 'FFI.moo'.
#include 'Stdio.moo'. #include 'Stdio.moo'.
// #include 'Console.moo'. #include 'Console.moo'.

View File

@ -626,10 +626,10 @@ static int add_oop_to_oopbuf_nodup (moo_t* moo, moo_oopbuf_t* oopbuf, moo_oop_t
return 1; /* added a new item */ return 1; /* added a new item */
} }
#define CHAR_TO_NUM(c,base) \ #define ZDIGIT_TO_NUM(c,base) \
((c >= '0' && c <= '9')? ((c - '0' < base)? (c - '0'): base): \ ((c >= '0' && c <= '9')? (c - '0'): \
(c >= 'A' && c <= 'Z')? ((c - 'A' + 10 < base)? (c - 'A' + 10): base): \ (c >= 'A' && c <= 'Z')? (c - 'A' + 10): \
(c >= 'a' && c <= 'z')? ((c - 'a' + 10 < base)? (c - 'a' + 10): base): base) (c >= 'a' && c <= 'z')? (c - 'a' + 10): base)
static int string_to_smooi (moo_t* moo, moo_oocs_t* str, int radixed, moo_ooi_t* num) static int string_to_smooi (moo_t* moo, moo_oocs_t* str, int radixed, moo_ooi_t* num)
{ {
@ -657,10 +657,11 @@ static int string_to_smooi (moo_t* moo, moo_oocs_t* str, int radixed, moo_ooi_t*
{ {
MOO_ASSERT (moo, ptr < end); MOO_ASSERT (moo, ptr < end);
/* the caller must ensure that the radix part is composed of decimal digits only */
base = 0; base = 0;
do do
{ {
base = base * 10 + CHAR_TO_NUM(*ptr, 10); base = base * 10 + ZDIGIT_TO_NUM(*ptr, 10);
ptr++; ptr++;
} }
while (*ptr != 'r'); while (*ptr != 'r');
@ -672,7 +673,7 @@ static int string_to_smooi (moo_t* moo, moo_oocs_t* str, int radixed, moo_ooi_t*
MOO_ASSERT (moo, ptr < end); MOO_ASSERT (moo, ptr < end);
value = old_value = 0; value = old_value = 0;
while (ptr < end && (v = CHAR_TO_NUM(*ptr, base)) < base) while (ptr < end && (v = ZDIGIT_TO_NUM(*ptr, base)) < base)
{ {
value = value * base + v; value = value * base + v;
if (value < old_value) if (value < old_value)
@ -726,10 +727,11 @@ static moo_oop_t string_to_int (moo_t* moo, moo_oocs_t* str, int radixed)
{ {
MOO_ASSERT (moo, ptr < end); MOO_ASSERT (moo, ptr < end);
/* the caller must ensure that the radix part is composed of decimal digits only */
base = 0; base = 0;
do do
{ {
base = base * 10 + CHAR_TO_NUM(*ptr, 10); base = base * 10 + ZDIGIT_TO_NUM(*ptr, 10);
ptr++; ptr++;
} }
while (*ptr != 'r'); while (*ptr != 'r');
@ -777,7 +779,7 @@ static moo_oop_t string_to_fpdec (moo_t* moo, moo_oocs_t* str, int prescaled)
{ {
do do
{ {
xscale = xscale * 10 + CHAR_TO_NUM(str->ptr[pos], 10); xscale = xscale * 10 + ZDIGIT_TO_NUM(str->ptr[pos], 10);
pos++; pos++;
len--; len--;
} }
@ -1468,7 +1470,7 @@ static int get_numlit (moo_t* moo, int negated)
int r; int r;
moo_oow_t rv; moo_oow_t rv;
r = CHAR_TO_NUM(c, 10); r = ZDIGIT_TO_NUM(c, 10);
MOO_ASSERT (moo, r < 10); MOO_ASSERT (moo, r < 10);
rv = radix * 10 + r; rv = radix * 10 + r;
if (rv < radix) radix_overflowed = 1; if (rv < radix) radix_overflowed = 1;
@ -1525,7 +1527,7 @@ static int get_numlit (moo_t* moo, int negated)
ADD_TOKEN_CHAR (moo, c); ADD_TOKEN_CHAR (moo, c);
GET_CHAR_TO (moo, c); GET_CHAR_TO (moo, c);
if (CHAR_TO_NUM(c, radix) >= radix) if (ZDIGIT_TO_NUM(c, radix) >= radix)
{ {
/* no digit after the radix specifier */ /* no digit after the radix specifier */
moo_setsynerr (moo, (xscale > 0? MOO_SYNERR_FPDECLITINVAL: MOO_SYNERR_RADINTLITINVAL), TOKEN_LOC(moo), TOKEN_NAME(moo)); moo_setsynerr (moo, (xscale > 0? MOO_SYNERR_FPDECLITINVAL: MOO_SYNERR_RADINTLITINVAL), TOKEN_LOC(moo), TOKEN_NAME(moo));
@ -1543,7 +1545,7 @@ static int get_numlit (moo_t* moo, int negated)
moo_iolxc_t underscore; moo_iolxc_t underscore;
underscore = moo->c->lxc; underscore = moo->c->lxc;
GET_CHAR_TO(moo, c); GET_CHAR_TO(moo, c);
if (CHAR_TO_NUM(c, radix) >= radix) if (ZDIGIT_TO_NUM(c, radix) >= radix)
{ {
unget_char (moo, &moo->c->lxc); unget_char (moo, &moo->c->lxc);
unget_char (moo, &underscore); unget_char (moo, &underscore);
@ -1552,7 +1554,7 @@ static int get_numlit (moo_t* moo, int negated)
else continue; else continue;
} }
} }
while (CHAR_TO_NUM(c, radix) < radix); while (ZDIGIT_TO_NUM(c, radix) < radix);
SET_TOKEN_TYPE (moo, (xscale > 0? MOO_IOTOK_SCALEDFPDECLIT: MOO_IOTOK_RADINTLIT)); SET_TOKEN_TYPE (moo, (xscale > 0? MOO_IOTOK_SCALEDFPDECLIT: MOO_IOTOK_RADINTLIT));
unget_char (moo, &moo->c->lxc); unget_char (moo, &moo->c->lxc);