*** empty log message ***
This commit is contained in:
parent
9ca4d7128a
commit
99579addf1
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: awk.h,v 1.120 2006-10-03 14:38:26 bacon Exp $
|
||||
* $Id: awk.h,v 1.121 2006-10-05 14:20:57 bacon Exp $
|
||||
*/
|
||||
|
||||
#ifndef _XP_AWK_AWK_H_
|
||||
@ -349,8 +349,8 @@ int xp_awk_clrrec (xp_awk_run_t* run, xp_bool_t skip_inrec_line);
|
||||
int xp_awk_setrec (xp_awk_run_t* run, xp_size_t idx, const xp_char_t* str, xp_size_t len);
|
||||
|
||||
/* utility functions exported by awk.h */
|
||||
xp_long_t xp_awk_strtolong (
|
||||
xp_awk_t* awk, const xp_char_t* str,
|
||||
xp_long_t xp_awk_strxtolong (
|
||||
xp_awk_t* awk, const xp_char_t* str, xp_size_t len,
|
||||
int base, const xp_char_t** endptr);
|
||||
xp_real_t xp_awk_strtoreal (
|
||||
xp_awk_t* awk, const xp_char_t* str);
|
||||
|
105
ase/awk/misc.c
105
ase/awk/misc.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: misc.c,v 1.22 2006-09-25 06:17:19 bacon Exp $
|
||||
* $Id: misc.c,v 1.23 2006-10-05 14:20:57 bacon Exp $
|
||||
*/
|
||||
|
||||
#include <xp/awk/awk_i.h>
|
||||
@ -33,19 +33,25 @@ void* xp_awk_memset (void* dst, int val, xp_size_t n)
|
||||
return dst;
|
||||
}
|
||||
|
||||
xp_long_t xp_awk_strtolong (
|
||||
xp_awk_t* awk, const xp_char_t* str,
|
||||
xp_long_t xp_awk_strxtolong (
|
||||
xp_awk_t* awk, const xp_char_t* str, xp_size_t len,
|
||||
int base, const xp_char_t** endptr)
|
||||
{
|
||||
xp_long_t n = 0;
|
||||
const xp_char_t* p;
|
||||
const xp_char_t* end;
|
||||
xp_size_t rem;
|
||||
int digit, negative = 0;
|
||||
|
||||
xp_assert (base < 37);
|
||||
|
||||
p = str; while (XP_AWK_ISSPACE(awk,*p)) p++;
|
||||
end = str + len;
|
||||
p = str;
|
||||
|
||||
/*while (XP_AWK_ISSPACE(awk,*p)) p++;*/
|
||||
|
||||
while (*p != XP_T('\0'))
|
||||
/*while (*p != XP_T('\0')) */
|
||||
while (p < end)
|
||||
{
|
||||
if (*p == XP_T('-'))
|
||||
{
|
||||
@ -56,12 +62,15 @@ xp_long_t xp_awk_strtolong (
|
||||
else break;
|
||||
}
|
||||
|
||||
rem = end - p;
|
||||
if (base == 0)
|
||||
{
|
||||
if (*p == XP_T('0'))
|
||||
if (rem >= 1 && *p == XP_T('0'))
|
||||
{
|
||||
p++;
|
||||
if (*p == XP_T('x') || *p == XP_T('X'))
|
||||
|
||||
if (rem == 1) base = 8;
|
||||
else if (*p == XP_T('x') || *p == XP_T('X'))
|
||||
{
|
||||
p++; base = 16;
|
||||
}
|
||||
@ -73,18 +82,19 @@ xp_long_t xp_awk_strtolong (
|
||||
}
|
||||
else base = 10;
|
||||
}
|
||||
else if (base == 16)
|
||||
else if (rem >= 2 && base == 16)
|
||||
{
|
||||
if (*p == XP_T('0') &&
|
||||
(*(p+1) == XP_T('x') || *(p+1) == XP_T('X'))) p += 2;
|
||||
}
|
||||
else if (base == 2)
|
||||
else if (rem >= 2 && base == 2)
|
||||
{
|
||||
if (*p == XP_T('0') &&
|
||||
(*(p+1) == XP_T('b') || *(p+1) == XP_T('B'))) p += 2;
|
||||
}
|
||||
|
||||
while (*p != XP_T('\0'))
|
||||
/*while (*p != XP_T('\0'))*/
|
||||
while (p < end)
|
||||
{
|
||||
if (*p >= XP_T('0') && *p <= XP_T('9'))
|
||||
digit = *p - XP_T('0');
|
||||
@ -132,12 +142,13 @@ xp_real_t xp_awk_strtoreal (xp_awk_t* awk, const xp_char_t* str)
|
||||
* Table giving binary powers of 10. Entry is 10^2^i.
|
||||
* Used to convert decimal exponents into floating-point numbers.
|
||||
*/
|
||||
static xp_real_t powersOf10[] = {
|
||||
static xp_real_t powers_of_10[] =
|
||||
{
|
||||
10., 100., 1.0e4, 1.0e8, 1.0e16,
|
||||
1.0e32, 1.0e64, 1.0e128, 1.0e256
|
||||
};
|
||||
|
||||
xp_real_t fraction, dblExp, * d;
|
||||
xp_real_t fraction, dbl_exp, * d;
|
||||
const xp_char_t* p;
|
||||
xp_cint_t c;
|
||||
int exp = 0; /* Exponent read from "EX" field */
|
||||
@ -152,15 +163,15 @@ xp_real_t xp_awk_strtoreal (xp_awk_t* awk, const xp_char_t* str)
|
||||
*/
|
||||
|
||||
int frac_exp;
|
||||
int mantSize; /* Number of digits in mantissa. */
|
||||
int decPt; /* Number of mantissa digits BEFORE decimal point */
|
||||
const xp_char_t *pExp; /* Temporarily holds location of exponent in string */
|
||||
int sign = 0, expSign = 0;
|
||||
int mant_size; /* Number of digits in mantissa. */
|
||||
int dec_pt; /* Number of mantissa digits BEFORE decimal point */
|
||||
const xp_char_t *pexp; /* Temporarily holds location of exponent in string */
|
||||
int sign = 0, exp_sign = 0;
|
||||
|
||||
p = str;
|
||||
|
||||
/* Strip off leading blanks and check for a sign */
|
||||
while (XP_AWK_ISSPACE(awk,*p)) p++;
|
||||
/*while (XP_AWK_ISSPACE(awk,*p)) p++;*/
|
||||
|
||||
while (*p != XP_T('\0'))
|
||||
{
|
||||
@ -175,12 +186,14 @@ xp_real_t xp_awk_strtoreal (xp_awk_t* awk, const xp_char_t* str)
|
||||
|
||||
/* Count the number of digits in the mantissa (including the decimal
|
||||
* point), and also locate the decimal point. */
|
||||
decPt = -1;
|
||||
for (mantSize = 0; ; mantSize++) {
|
||||
dec_pt = -1;
|
||||
for (mant_size = 0; ; mant_size++)
|
||||
{
|
||||
c = *p;
|
||||
if (!XP_AWK_ISDIGIT (awk, c)) {
|
||||
if ((c != XP_T('.')) || (decPt >= 0)) break;
|
||||
decPt = mantSize;
|
||||
if (!XP_AWK_ISDIGIT (awk, c))
|
||||
{
|
||||
if ((c != XP_T('.')) || (dec_pt >= 0)) break;
|
||||
dec_pt = mant_size;
|
||||
}
|
||||
p++;
|
||||
}
|
||||
@ -191,28 +204,28 @@ xp_real_t xp_awk_strtoreal (xp_awk_t* awk, const xp_char_t* str)
|
||||
* If the mantissa has more than 18 digits, ignore the extras, since
|
||||
* they can't affect the value anyway.
|
||||
*/
|
||||
pExp = p;
|
||||
p -= mantSize;
|
||||
if (decPt < 0)
|
||||
pexp = p;
|
||||
p -= mant_size;
|
||||
if (dec_pt < 0)
|
||||
{
|
||||
decPt = mantSize;
|
||||
dec_pt = mant_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
mantSize -= 1; /* One of the digits was the point */
|
||||
mant_size--; /* One of the digits was the point */
|
||||
}
|
||||
|
||||
if (mantSize > 18)
|
||||
if (mant_size > 18)
|
||||
{
|
||||
frac_exp = decPt - 18;
|
||||
mantSize = 18;
|
||||
frac_exp = dec_pt - 18;
|
||||
mant_size = 18;
|
||||
}
|
||||
else
|
||||
{
|
||||
frac_exp = decPt - mantSize;
|
||||
frac_exp = dec_pt - mant_size;
|
||||
}
|
||||
|
||||
if (mantSize == 0)
|
||||
if (mant_size == 0)
|
||||
{
|
||||
fraction = 0.0;
|
||||
/*p = str;*/
|
||||
@ -222,7 +235,7 @@ xp_real_t xp_awk_strtoreal (xp_awk_t* awk, const xp_char_t* str)
|
||||
{
|
||||
int frac1, frac2;
|
||||
frac1 = 0;
|
||||
for ( ; mantSize > 9; mantSize -= 1)
|
||||
for ( ; mant_size > 9; mant_size--)
|
||||
{
|
||||
c = *p;
|
||||
p++;
|
||||
@ -234,7 +247,7 @@ xp_real_t xp_awk_strtoreal (xp_awk_t* awk, const xp_char_t* str)
|
||||
frac1 = 10 * frac1 + (c - XP_T('0'));
|
||||
}
|
||||
frac2 = 0;
|
||||
for (; mantSize > 0; mantSize -= 1) {
|
||||
for (; mant_size > 0; mant_size--) {
|
||||
c = *p;
|
||||
p++;
|
||||
if (c == XP_T('.'))
|
||||
@ -248,23 +261,23 @@ xp_real_t xp_awk_strtoreal (xp_awk_t* awk, const xp_char_t* str)
|
||||
}
|
||||
|
||||
/* Skim off the exponent */
|
||||
p = pExp;
|
||||
p = pexp;
|
||||
if ((*p == XP_T('E')) || (*p == XP_T('e')))
|
||||
{
|
||||
p++;
|
||||
if (*p == XP_T('-'))
|
||||
{
|
||||
expSign = 1;
|
||||
exp_sign = 1;
|
||||
p++;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (*p == XP_T('+')) p++;
|
||||
expSign = 0;
|
||||
exp_sign = 0;
|
||||
}
|
||||
if (!XP_AWK_ISDIGIT (awk, *p))
|
||||
{
|
||||
/* p = pExp; */
|
||||
/* p = pexp; */
|
||||
goto done;
|
||||
}
|
||||
while (XP_AWK_ISDIGIT (awk, *p))
|
||||
@ -274,7 +287,7 @@ xp_real_t xp_awk_strtoreal (xp_awk_t* awk, const xp_char_t* str)
|
||||
}
|
||||
}
|
||||
|
||||
if (expSign) exp = frac_exp - exp;
|
||||
if (exp_sign) exp = frac_exp - exp;
|
||||
else exp = frac_exp + exp;
|
||||
|
||||
/*
|
||||
@ -285,22 +298,22 @@ xp_real_t xp_awk_strtoreal (xp_awk_t* awk, const xp_char_t* str)
|
||||
*/
|
||||
if (exp < 0)
|
||||
{
|
||||
expSign = 1;
|
||||
exp_sign = 1;
|
||||
exp = -exp;
|
||||
}
|
||||
else expSign = 0;
|
||||
else exp_sign = 0;
|
||||
|
||||
if (exp > MAX_EXPONENT) exp = MAX_EXPONENT;
|
||||
|
||||
dblExp = 1.0;
|
||||
dbl_exp = 1.0;
|
||||
|
||||
for (d = powersOf10; exp != 0; exp >>= 1, d++)
|
||||
for (d = powers_of_10; exp != 0; exp >>= 1, d++)
|
||||
{
|
||||
if (exp & 01) dblExp *= *d;
|
||||
if (exp & 01) dbl_exp *= *d;
|
||||
}
|
||||
|
||||
if (expSign) fraction /= dblExp;
|
||||
else fraction *= dblExp;
|
||||
if (exp_sign) fraction /= dbl_exp;
|
||||
else fraction *= dbl_exp;
|
||||
|
||||
done:
|
||||
return (sign)? -fraction: fraction;
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: parse.c,v 1.184 2006-09-28 14:21:23 bacon Exp $
|
||||
* $Id: parse.c,v 1.185 2006-10-05 14:20:57 bacon Exp $
|
||||
*/
|
||||
|
||||
#include <xp/awk/awk_i.h>
|
||||
@ -1950,8 +1950,9 @@ static xp_awk_nde_t* __parse_primary (xp_awk_t* awk)
|
||||
|
||||
nde->type = XP_AWK_NDE_INT;
|
||||
nde->next = XP_NULL;
|
||||
nde->val = xp_awk_strtolong (
|
||||
awk, XP_AWK_STR_BUF(&awk->token.name), 0, XP_NULL);
|
||||
nde->val = xp_awk_strxtolong (awk,
|
||||
XP_AWK_STR_BUF(&awk->token.name),
|
||||
XP_AWK_STR_LEN(&awk->token.name), 0, XP_NULL);
|
||||
nde->str = xp_awk_strxdup (awk,
|
||||
XP_AWK_STR_BUF(&awk->token.name),
|
||||
XP_AWK_STR_LEN(&awk->token.name));
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: run.c,v 1.222 2006-10-04 14:51:20 bacon Exp $
|
||||
* $Id: run.c,v 1.223 2006-10-05 14:20:57 bacon Exp $
|
||||
*/
|
||||
|
||||
#include <xp/awk/awk_i.h>
|
||||
@ -2837,8 +2837,34 @@ static int __cmp_int_str (
|
||||
{
|
||||
xp_char_t* str;
|
||||
xp_size_t len;
|
||||
xp_long_t r;
|
||||
int n;
|
||||
|
||||
r = xp_awk_strxtolong (run->awk,
|
||||
((xp_awk_val_str_t*)right)->buf,
|
||||
((xp_awk_val_str_t*)right)->len, 0, &str);
|
||||
if (str == ((xp_awk_val_str_t*)right)->buf +
|
||||
((xp_awk_val_str_t*)right)->len)
|
||||
{
|
||||
if (((xp_awk_val_int_t*)left)->val > r) return 1;
|
||||
if (((xp_awk_val_int_t*)left)->val < r) return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* TODO: */
|
||||
/*
|
||||
r = xp_awk_strxtoreal (run->awk,
|
||||
((xp_awk_val_str_t*)right)->buf,
|
||||
((xp_awk_val_str_t*)right)->len, &str);
|
||||
if (str == ((xp_awk_val_str_t*)right)->buf +
|
||||
((xp_awk_val_str_t*)right)->len)
|
||||
{
|
||||
if (((xp_awk_val_int_t*)left)->val > rr) return 1;
|
||||
if (((xp_awk_val_int_t*)left)->val < rr) return -1;
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
|
||||
str = xp_awk_valtostr (run, left, xp_true, XP_NULL, &len);
|
||||
if (str == XP_NULL)
|
||||
{
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: val.c,v 1.66 2006-10-04 14:51:21 bacon Exp $
|
||||
* $Id: val.c,v 1.67 2006-10-05 14:20:57 bacon Exp $
|
||||
*/
|
||||
|
||||
#include <xp/awk/awk_i.h>
|
||||
@ -384,6 +384,7 @@ xp_char_t* xp_awk_valtostr (
|
||||
{
|
||||
xp_awk_val_int_t* vi = (xp_awk_val_int_t*)v;
|
||||
|
||||
/*
|
||||
if (vi->nde != XP_NULL && vi->nde->str != XP_NULL)
|
||||
{
|
||||
return __str_to_str (
|
||||
@ -392,8 +393,9 @@ xp_char_t* xp_awk_valtostr (
|
||||
}
|
||||
else
|
||||
{
|
||||
*/
|
||||
return __val_int_to_str (run, vi, clear_buf, buf, len);
|
||||
}
|
||||
/*}*/
|
||||
}
|
||||
|
||||
if (v->type == XP_AWK_VAL_REAL)
|
||||
@ -635,9 +637,9 @@ int xp_awk_valtonum (
|
||||
const xp_char_t* endptr;
|
||||
|
||||
/* don't care about val->len */
|
||||
*l = xp_awk_strtolong (
|
||||
run->awk, ((xp_awk_val_str_t*)v)->buf, 0, &endptr);
|
||||
|
||||
*l = xp_awk_strxtolong (run->awk,
|
||||
((xp_awk_val_str_t*)v)->buf,
|
||||
((xp_awk_val_str_t*)v)->len, 0, &endptr);
|
||||
if (*endptr == XP_T('.') ||
|
||||
*endptr == XP_T('E') ||
|
||||
*endptr == XP_T('e'))
|
||||
|
@ -65,6 +65,14 @@ BEGIN {
|
||||
print "11 > \"10\"", (11 > "10");
|
||||
print "11 < \"10\"", (11 < "10");
|
||||
|
||||
print "--------------------------";
|
||||
print "010 == \"8\"", (010 == "8");
|
||||
print "010 != \"8\"", (010 != "8");
|
||||
print "010 >= \"8\"", (010 >= "8");
|
||||
print "010 <= \"8\"", (010 <= "8");
|
||||
print "010 > \"8\"", (010 > "8");
|
||||
print "010 < \"8\"", (010 < "8");
|
||||
|
||||
print "--------------------------";
|
||||
print "10.0 == \"10\"", (10.0 == "10");
|
||||
print "10.0 != \"10\"", (10.0 != "10");
|
||||
|
27
ase/test/awk/num.awk
Normal file
27
ase/test/awk/num.awk
Normal file
@ -0,0 +1,27 @@
|
||||
BEGIN {
|
||||
print 1 + 0;
|
||||
print 0B11111111 + 0;
|
||||
print 10 + 0;
|
||||
print 0x10 + 0;
|
||||
print 0b00000010 + 0;
|
||||
print 0b + 0;
|
||||
print 0x + 0;
|
||||
|
||||
print "-----------------------";
|
||||
print +1 + 0;
|
||||
print +0B11111111 + 0;
|
||||
print +10 + 0;
|
||||
print +0x10 + 0;
|
||||
print +0b00000010 + 0;
|
||||
print +0b + 0;
|
||||
print +0x + 0;
|
||||
|
||||
print "-----------------------";
|
||||
print -1 + 0;
|
||||
print -0B11111111 + 0;
|
||||
print -10 + 0;
|
||||
print -0x10 + 0;
|
||||
print -0b00000010 + 0;
|
||||
print -0b + 0;
|
||||
print -0x + 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user