diff --git a/ase/awk/parse.c b/ase/awk/parse.c index 3c53ebe5..335d8f94 100644 --- a/ase/awk/parse.c +++ b/ase/awk/parse.c @@ -1,5 +1,5 @@ /* - * $Id: parse.c,v 1.182 2006-09-27 14:11:31 bacon Exp $ + * $Id: parse.c,v 1.183 2006-09-27 14:13:33 bacon Exp $ */ #include diff --git a/ase/awk/run.c b/ase/awk/run.c index a6a84bb9..9a151908 100644 --- a/ase/awk/run.c +++ b/ase/awk/run.c @@ -1,5 +1,5 @@ /* - * $Id: run.c,v 1.206 2006-09-25 14:53:10 bacon Exp $ + * $Id: run.c,v 1.207 2006-09-27 14:11:31 bacon Exp $ */ #include @@ -2642,7 +2642,7 @@ static xp_awk_val_t* __eval_binop_land ( static xp_awk_val_t* __eval_binop_in ( xp_awk_run_t* run, xp_awk_nde_t* left, xp_awk_nde_t* right) { - xp_awk_val_t* rv, * res; + xp_awk_val_t* rv; xp_char_t* str; xp_size_t len; @@ -2676,21 +2676,14 @@ static xp_awk_val_t* __eval_binop_in ( if (rv->type == XP_AWK_VAL_NIL) { - res = xp_awk_makeintval (run, 0); - if (res == XP_NULL) - { - XP_AWK_FREE (run->awk, str); - xp_awk_refdownval (run, rv); - PANIC (run, XP_AWK_ENOMEM); - } - XP_AWK_FREE (run->awk, str); xp_awk_refdownval (run, rv); - return res; + return xp_awk_val_zero; } else if (rv->type == XP_AWK_VAL_MAP) { xp_long_t r; + xp_awk_val_t* res; r = (xp_long_t)(xp_awk_map_get(((xp_awk_val_map_t*)rv)->map,str,len) != XP_NULL); @@ -4480,18 +4473,22 @@ static xp_awk_val_t** __get_reference_indexed ( static xp_awk_val_t* __eval_int (xp_awk_run_t* run, xp_awk_nde_t* nde) { xp_awk_val_t* val; - val = xp_awk_makeintval ( - run, ((xp_awk_nde_int_t*)nde)->val); + + val = xp_awk_makeintval (run, ((xp_awk_nde_int_t*)nde)->val); if (val == XP_NULL) PANIC (run, XP_AWK_ENOMEM); + ((xp_awk_val_int_t*)val)->nde = (xp_awk_nde_int_t*)nde; + return val; } static xp_awk_val_t* __eval_real (xp_awk_run_t* run, xp_awk_nde_t* nde) { xp_awk_val_t* val; - val = xp_awk_makerealval ( - run, ((xp_awk_nde_real_t*)nde)->val); + + val = xp_awk_makerealval (run, ((xp_awk_nde_real_t*)nde)->val); if (val == XP_NULL) PANIC (run, XP_AWK_ENOMEM); + ((xp_awk_val_real_t*)val)->nde = (xp_awk_nde_real_t*)nde; + return val; } @@ -4499,8 +4496,7 @@ static xp_awk_val_t* __eval_str (xp_awk_run_t* run, xp_awk_nde_t* nde) { xp_awk_val_t* val; - val = xp_awk_makestrval ( - run, + val = xp_awk_makestrval (run, ((xp_awk_nde_str_t*)nde)->buf, ((xp_awk_nde_str_t*)nde)->len); if (val == XP_NULL) PANIC (run, XP_AWK_ENOMEM); @@ -4512,8 +4508,7 @@ static xp_awk_val_t* __eval_rex (xp_awk_run_t* run, xp_awk_nde_t* nde) { xp_awk_val_t* val; - val = xp_awk_makerexval ( - run, + val = xp_awk_makerexval (run, ((xp_awk_nde_rex_t*)nde)->buf, ((xp_awk_nde_rex_t*)nde)->len, ((xp_awk_nde_rex_t*)nde)->code); diff --git a/ase/awk/val.c b/ase/awk/val.c index 8d58a879..3f9e0e9c 100644 --- a/ase/awk/val.c +++ b/ase/awk/val.c @@ -1,5 +1,5 @@ /* - * $Id: val.c,v 1.61 2006-09-25 06:17:19 bacon Exp $ + * $Id: val.c,v 1.62 2006-09-27 14:11:32 bacon Exp $ */ #include @@ -8,6 +8,16 @@ #include #endif +static xp_char_t* __str_to_str ( + xp_awk_run_t* run, const xp_char_t* str, xp_size_t str_len, + xp_bool_t clear_buf, xp_awk_str_t* buf, xp_size_t* len); +static xp_char_t* __val_int_to_str ( + xp_awk_run_t* run, xp_awk_val_int_t* v, + xp_bool_t clear_buf, xp_awk_str_t* buf, xp_size_t* len); +static xp_char_t* __val_real_to_str ( + xp_awk_run_t* run, xp_awk_val_real_t* v, + xp_bool_t clear_buf, xp_awk_str_t* buf, xp_size_t* len); + static xp_awk_val_nil_t __awk_nil = { XP_AWK_VAL_NIL, 0 }; xp_awk_val_t* xp_awk_val_nil = (xp_awk_val_t*)&__awk_nil; @@ -56,6 +66,7 @@ xp_awk_val_t* xp_awk_makeintval (xp_awk_run_t* run, xp_long_t v) val->type = XP_AWK_VAL_INT; val->ref = 0; val->val = v; + val->nde = XP_NULL; /*xp_printf (XP_T("makeintval => %p\n"), val);*/ return (xp_awk_val_t*)val; @@ -79,6 +90,7 @@ xp_awk_val_t* xp_awk_makerealval (xp_awk_run_t* run, xp_real_t v) val->type = XP_AWK_VAL_REAL; val->ref = 0; val->val = v; + val->nde = XP_NULL; /*xp_printf (XP_T("makerealval => %p\n"), val);*/ return (xp_awk_val_t*)val; @@ -369,200 +381,47 @@ xp_char_t* xp_awk_valtostr ( { if (v->type == XP_AWK_VAL_NIL) { - if (buf == XP_NULL) - { - xp_char_t* tmp; - tmp = xp_awk_strdup (run->awk, XP_T("")); - if (tmp == XP_NULL) - { - run->errnum = XP_AWK_ENOMEM; - return XP_NULL; - } - - if (len != XP_NULL) *len = 0; - return tmp; - } - else - { - if (clear_buf) xp_awk_str_clear (buf); - - if (len != XP_NULL) *len = XP_AWK_STR_LEN(buf); - return XP_AWK_STR_BUF(buf); - } + return __str_to_str (run, XP_T(""), 0, clear_buf, buf, len); } if (v->type == XP_AWK_VAL_INT) { - xp_char_t* tmp; - xp_long_t t; - xp_size_t l = 0; + xp_awk_val_int_t* vi = (xp_awk_val_int_t*)v; - t = ((xp_awk_val_int_t*)v)->val; - if (t == 0) + if (vi->nde != XP_NULL) { - /* handle zero */ - if (buf == XP_NULL) - { - tmp = XP_AWK_MALLOC ( - run->awk, 2 * xp_sizeof(xp_char_t)); - if (tmp == XP_NULL) - { - run->errnum = XP_AWK_ENOMEM; - return XP_NULL; - } - - tmp[0] = XP_T('0'); - tmp[1] = XP_T('\0'); - if (len != XP_NULL) *len = 1; - return tmp; - } - else - { - if (clear_buf) xp_awk_str_clear (buf); - if (xp_awk_str_cat (buf, XP_T("0")) == (xp_size_t)-1) - { - run->errnum = XP_AWK_ENOMEM; - return XP_NULL; - } - - if (len != XP_NULL) *len = XP_AWK_STR_LEN(buf); - return XP_AWK_STR_BUF(buf); - } - } - - /* non-zero values */ - if (t < 0) { t = -t; l++; } - while (t > 0) { l++; t /= 10; } - - if (buf == XP_NULL) - { - tmp = XP_AWK_MALLOC ( - run->awk, (l + 1) * xp_sizeof(xp_char_t)); - if (tmp == XP_NULL) - { - run->errnum = XP_AWK_ENOMEM; - return XP_NULL; - } - - tmp[l] = XP_T('\0'); - if (len != XP_NULL) *len = l; + return __str_to_str ( + run, vi->nde->str, vi->nde->len, + clear_buf, buf, len); } else { - /* clear the buffer */ - if (clear_buf) xp_awk_str_clear (buf); - - tmp = XP_AWK_STR_BUF(buf) + XP_AWK_STR_LEN(buf); - - /* extend the buffer */ - if (xp_awk_str_nccat ( - buf, XP_T(' '), l) == (xp_size_t)-1) - { - run->errnum = XP_AWK_ENOMEM; - return XP_NULL; - } + return __val_int_to_str (run, vi, clear_buf, buf, len); } - - t = ((xp_awk_val_int_t*)v)->val; - if (t < 0) t = -t; - - while (t > 0) - { - tmp[--l] = (xp_char_t)(t % 10) + XP_T('0'); - t /= 10; - } - - if (((xp_awk_val_int_t*)v)->val < 0) tmp[--l] = XP_T('-'); - - if (buf != XP_NULL) - { - tmp = XP_AWK_STR_BUF(buf); - if (len != XP_NULL) *len = XP_AWK_STR_LEN(buf); - } - - return tmp; } if (v->type == XP_AWK_VAL_REAL) { -/* TODO: change the code */ - xp_char_t tbuf[256], * tmp; + xp_awk_val_real_t* vr = (xp_awk_val_real_t*)v; - #if (XP_SIZEOF_LONG_DOUBLE != 0) - run->awk->syscas->sprintf ( - tbuf, xp_countof(tbuf), XP_T("%Lf"), - (long double)((xp_awk_val_real_t*)v)->val); - #elif (XP_SIZEOF_DOUBLE != 0) - run->awk->syscas->sprintf ( - tbuf, xp_countof(tbuf), XP_T("%f"), - (double)((xp_awk_val_real_t*)v)->val); - #else - #error unsupported floating-point data type - #endif - - if (buf == XP_NULL) + if (vr->nde != XP_NULL) { - tmp = xp_awk_strdup (run->awk, tbuf); - if (tmp == XP_NULL) - { - run->errnum = XP_AWK_ENOMEM; - return XP_NULL; - } - - if (len != XP_NULL) *len = xp_awk_strlen(tmp); + return __str_to_str ( + run, vr->nde->str, vr->nde->len, + clear_buf, buf, len); } else { - if (clear_buf) xp_awk_str_clear (buf); - - if (xp_awk_str_cat (buf, tbuf) == (xp_size_t)-1) - { - run->errnum = XP_AWK_ENOMEM; - return XP_NULL; - } - - tmp = XP_AWK_STR_BUF(buf); - if (len != XP_NULL) *len = XP_AWK_STR_LEN(buf); + return __val_real_to_str (run, vr, clear_buf, buf, len); } - - return tmp; } if (v->type == XP_AWK_VAL_STR) { - xp_char_t* tmp; + xp_awk_val_str_t* vs = (xp_awk_val_str_t*)v; - if (buf == XP_NULL) - { - tmp = xp_awk_strxdup ( - run->awk, - ((xp_awk_val_str_t*)v)->buf, - ((xp_awk_val_str_t*)v)->len); - if (tmp == XP_NULL) - { - run->errnum = XP_AWK_ENOMEM; - return XP_NULL; - } - - if (len != XP_NULL) *len = ((xp_awk_val_str_t*)v)->len; - } - else - { - if (clear_buf) xp_awk_str_clear (buf); - - if (xp_awk_str_ncat (buf, - ((xp_awk_val_str_t*)v)->buf, - ((xp_awk_val_str_t*)v)->len) == (xp_size_t)-1) - { - run->errnum = XP_AWK_ENOMEM; - return XP_NULL; - } - - tmp = XP_AWK_STR_BUF(buf); - if (len != XP_NULL) *len = XP_AWK_STR_LEN(buf); - } - - return tmp; + return __str_to_str ( + run, vs->buf, vs->len, clear_buf, buf, len); } /* TODO: process more value types */ @@ -572,6 +431,180 @@ xp_printf (XP_T("*** ERROR: WRONG VALUE TYPE [%d] in xp_awk_valtostr v=> %p***\n return XP_NULL; } +static xp_char_t* __str_to_str ( + xp_awk_run_t* run, const xp_char_t* str, xp_size_t str_len, + xp_bool_t clear_buf, xp_awk_str_t* buf, xp_size_t* len) +{ + if (buf == XP_NULL) + { + xp_char_t* tmp; + tmp = xp_awk_strxdup (run->awk, str, str_len); + if (tmp == XP_NULL) + { + run->errnum = XP_AWK_ENOMEM; + return XP_NULL; + } + + if (len != XP_NULL) *len = str_len; + return tmp; + } + else + { + xp_size_t n; + + if (clear_buf) xp_awk_str_clear (buf); + n = xp_awk_str_ncat (buf, str, str_len); + if (n == (xp_size_t)-1) + { + run->errnum = XP_AWK_ENOMEM; + return XP_NULL; + } + + if (len != XP_NULL) *len = XP_AWK_STR_LEN(buf); + return XP_AWK_STR_BUF(buf); + } +} + +static xp_char_t* __val_int_to_str ( + xp_awk_run_t* run, xp_awk_val_int_t* v, + xp_bool_t clear_buf, xp_awk_str_t* buf, xp_size_t* len) +{ + xp_char_t* tmp; + xp_long_t t; + xp_size_t l = 0; + + t = v->val; + if (t == 0) + { + /* handle zero */ + if (buf == XP_NULL) + { + tmp = XP_AWK_MALLOC ( + run->awk, 2 * xp_sizeof(xp_char_t)); + if (tmp == XP_NULL) + { + run->errnum = XP_AWK_ENOMEM; + return XP_NULL; + } + + tmp[0] = XP_T('0'); + tmp[1] = XP_T('\0'); + if (len != XP_NULL) *len = 1; + return tmp; + } + else + { + if (clear_buf) xp_awk_str_clear (buf); + if (xp_awk_str_cat (buf, XP_T("0")) == (xp_size_t)-1) + { + run->errnum = XP_AWK_ENOMEM; + return XP_NULL; + } + + if (len != XP_NULL) *len = XP_AWK_STR_LEN(buf); + return XP_AWK_STR_BUF(buf); + } + } + + /* non-zero values */ + if (t < 0) { t = -t; l++; } + while (t > 0) { l++; t /= 10; } + + if (buf == XP_NULL) + { + tmp = XP_AWK_MALLOC ( + run->awk, (l + 1) * xp_sizeof(xp_char_t)); + if (tmp == XP_NULL) + { + run->errnum = XP_AWK_ENOMEM; + return XP_NULL; + } + + tmp[l] = XP_T('\0'); + if (len != XP_NULL) *len = l; + } + else + { + /* clear the buffer */ + if (clear_buf) xp_awk_str_clear (buf); + + tmp = XP_AWK_STR_BUF(buf) + XP_AWK_STR_LEN(buf); + + /* extend the buffer */ + if (xp_awk_str_nccat ( + buf, XP_T(' '), l) == (xp_size_t)-1) + { + run->errnum = XP_AWK_ENOMEM; + return XP_NULL; + } + } + + t = v->val; + if (t < 0) t = -t; + + while (t > 0) + { + tmp[--l] = (xp_char_t)(t % 10) + XP_T('0'); + t /= 10; + } + + if (v->val < 0) tmp[--l] = XP_T('-'); + + if (buf != XP_NULL) + { + tmp = XP_AWK_STR_BUF(buf); + if (len != XP_NULL) *len = XP_AWK_STR_LEN(buf); + } + + return tmp; +} + +static xp_char_t* __val_real_to_str ( + xp_awk_run_t* run, xp_awk_val_real_t* v, + xp_bool_t clear_buf, xp_awk_str_t* buf, xp_size_t* len) +{ +/* TODO: change the code */ + xp_char_t tbuf[256], * tmp; + +#if (XP_SIZEOF_LONG_DOUBLE != 0) + run->awk->syscas->sprintf ( + tbuf, xp_countof(tbuf), XP_T("%Lf"), (long double)v->val); +#elif (XP_SIZEOF_DOUBLE != 0) + run->awk->syscas->sprintf ( + tbuf, xp_countof(tbuf), XP_T("%f"), (double)v->val); +#else + #error unsupported floating-point data type +#endif + + if (buf == XP_NULL) + { + tmp = xp_awk_strdup (run->awk, tbuf); + if (tmp == XP_NULL) + { + run->errnum = XP_AWK_ENOMEM; + return XP_NULL; + } + + if (len != XP_NULL) *len = xp_awk_strlen(tmp); + } + else + { + if (clear_buf) xp_awk_str_clear (buf); + + if (xp_awk_str_cat (buf, tbuf) == (xp_size_t)-1) + { + run->errnum = XP_AWK_ENOMEM; + return XP_NULL; + } + + tmp = XP_AWK_STR_BUF(buf); + if (len != XP_NULL) *len = XP_AWK_STR_LEN(buf); + } + + return tmp; +} + + int xp_awk_valtonum ( xp_awk_run_t* run, xp_awk_val_t* v, xp_long_t* l, xp_real_t* r) { diff --git a/ase/awk/val.h b/ase/awk/val.h index b113094f..1969358d 100644 --- a/ase/awk/val.h +++ b/ase/awk/val.h @@ -1,5 +1,5 @@ /* - * $Id: val.h,v 1.40 2006-09-01 06:22:13 bacon Exp $ + * $Id: val.h,v 1.41 2006-09-27 14:11:32 bacon Exp $ */ #ifndef _XP_AWK_VAL_H_ @@ -68,6 +68,7 @@ struct xp_awk_val_int_t { XP_AWK_VAL_HDR; xp_long_t val; + xp_awk_nde_int_t* nde; }; /* XP_AWK_VAL_REAL */ @@ -75,6 +76,7 @@ struct xp_awk_val_real_t { XP_AWK_VAL_HDR; xp_real_t val; + xp_awk_nde_real_t* nde; }; /* XP_AWK_VAL_STR */ diff --git a/ase/test/awk/t47.awk b/ase/test/awk/t47.awk index 943d9f61..15831ab4 100644 --- a/ase/test/awk/t47.awk +++ b/ase/test/awk/t47.awk @@ -7,8 +7,8 @@ BEGIN { for (i in a) print "a[" i "]=" a[i]; #SUBSEP=",,,"; - #SUBSEP=4.5; - SUBSEP=555; + SUBSEP=4.5; + #SUBSEP=555; print "------------------------"; diff --git a/ase/test/awk/t48.awk b/ase/test/awk/t48.awk new file mode 100644 index 00000000..7ac01c7a --- /dev/null +++ b/ase/test/awk/t48.awk @@ -0,0 +1,3 @@ +BEGIN { + print ("xx" in j); +} diff --git a/ase/types.h b/ase/types.h index 544ebb21..e2ecc275 100644 --- a/ase/types.h +++ b/ase/types.h @@ -1,5 +1,5 @@ /* - * $Id: types.h,v 1.55 2006-09-08 11:27:01 bacon Exp $ + * $Id: types.h,v 1.56 2006-09-27 14:14:29 bacon Exp $ */ #ifndef _XP_TYPES_H_ @@ -88,17 +88,20 @@ typedef int xp_tri_t; #define xp_dead -1 /* integer that can hold a pointer */ -#if XP_SIZEOF_VOID_P == XP_SIZEOF_LONG_LONG - typedef long long xp_int_t; - typedef unsigned long long xp_uint_t; +#if XP_SIZEOF_VOID_P == XP_SIZEOF_INT + typedef int xp_int_t; + typedef unsigned int xp_uint_t; #elif XP_SIZEOF_VOID_P == XP_SIZEOF_LONG typedef long xp_int_t; typedef unsigned long xp_uint_t; +#elif XP_SIZEOF_VOID_P == XP_SIZEOF_LONG_LONG + typedef long long xp_int_t; + typedef unsigned long long xp_uint_t; #else - typedef int xp_int_t; - typedef unsigned int xp_uint_t; + #error Unsupported pointer size #endif + /* the largest integer supported by the system */ #if XP_SIZEOF_LONG_LONG != 0 typedef long long xp_long_t;