simplified the way to get the first integer index in idxnde_to_str()

This commit is contained in:
hyung-hwan 2020-05-03 06:17:44 +00:00
parent 6709839f96
commit 84d869606f

View File

@ -193,14 +193,7 @@ static hawk_val_t* eval_printf (hawk_rtx_t* rtx, hawk_nde_t* nde);
static int read_record (hawk_rtx_t* rtx); static int read_record (hawk_rtx_t* rtx);
struct idxnde_to_str_idxint_t static hawk_ooch_t* idxnde_to_str (hawk_rtx_t* rtx, hawk_nde_t* nde, hawk_ooch_t* buf, hawk_oow_t* len, hawk_nde_t** remidx, hawk_int_t* firstidxint);
{
int ok;
hawk_int_t v;
};
typedef struct idxnde_to_str_idxint_t idxnde_to_str_idxint_t;
static hawk_ooch_t* idxnde_to_str (hawk_rtx_t* rtx, hawk_nde_t* nde, hawk_ooch_t* buf, hawk_oow_t* len, hawk_nde_t** remidx, idxnde_to_str_idxint_t* firstidxint);
static hawk_ooi_t idxnde_to_int (hawk_rtx_t* rtx, hawk_nde_t* nde, hawk_nde_t** remidx); static hawk_ooi_t idxnde_to_int (hawk_rtx_t* rtx, hawk_nde_t* nde, hawk_nde_t** remidx);
typedef hawk_val_t* (*binop_func_t) (hawk_rtx_t* rtx, hawk_val_t* left, hawk_val_t* right); typedef hawk_val_t* (*binop_func_t) (hawk_rtx_t* rtx, hawk_val_t* left, hawk_val_t* right);
@ -4424,7 +4417,7 @@ static hawk_val_t* eval_binop_in (hawk_rtx_t* rtx, hawk_nde_t* left, hawk_nde_t*
hawk_oow_t len; hawk_oow_t len;
hawk_ooch_t idxbuf[IDXBUFSIZE]; hawk_ooch_t idxbuf[IDXBUFSIZE];
hawk_nde_t* remidx; hawk_nde_t* remidx;
idxnde_to_str_idxint_t idxint; hawk_int_t idxint;
#if defined(HAWK_ENABLE_GC) #if defined(HAWK_ENABLE_GC)
if (right->type < HAWK_NDE_NAMED || right->type > HAWK_NDE_ARGIDX) if (right->type < HAWK_NDE_NAMED || right->type > HAWK_NDE_ARGIDX)
@ -4441,7 +4434,7 @@ static hawk_val_t* eval_binop_in (hawk_rtx_t* rtx, hawk_nde_t* left, hawk_nde_t*
/* evaluate the left-hand side of the operator */ /* evaluate the left-hand side of the operator */
len = HAWK_COUNTOF(idxbuf); len = HAWK_COUNTOF(idxbuf);
str = (left->type == HAWK_NDE_GRP)? /* it is inefficinet to call idxnde_to_str() for an array. but i don't know if the right hand side is a map or an array yet */ str = (left->type == HAWK_NDE_GRP)? /* it is inefficinet to call idxnde_to_str() for an array. but i don't know if the right hand side is a map or an array yet */
idxnde_to_str(rtx, ((hawk_nde_grp_t*)left)->body, idxbuf, &len, &remidx, &idxint): idxnde_to_str(rtx, ((hawk_nde_grp_t*)left)->body, idxbuf, &len, &remidx, HAWK_NULL):
idxnde_to_str(rtx, left, idxbuf, &len, &remidx, &idxint); idxnde_to_str(rtx, left, idxbuf, &len, &remidx, &idxint);
if (HAWK_UNLIKELY(!str)) return HAWK_NULL; if (HAWK_UNLIKELY(!str)) return HAWK_NULL;
@ -4487,14 +4480,14 @@ static hawk_val_t* eval_binop_in (hawk_rtx_t* rtx, hawk_nde_t* left, hawk_nde_t*
{ {
hawk_arr_t* arr; hawk_arr_t* arr;
if (!idxint.ok) if (left->type == HAWK_NDE_GRP)
{ {
hawk_rtx_seterrnum (rtx, &left->loc, HAWK_EARRIDXMULTI); hawk_rtx_seterrnum (rtx, &left->loc, HAWK_EARRIDXMULTI);
goto oops; goto oops;
} }
arr = ((hawk_val_arr_t*)ropv)->arr; arr = ((hawk_val_arr_t*)ropv)->arr;
res = (idxint.v < 0 || idxint.v >= HAWK_ARR_SIZE(arr) || !HAWK_ARR_SLOT(arr, idxint.v))? HAWK_VAL_ZERO: HAWK_VAL_ONE; res = (idxint < 0 || idxint >= HAWK_ARR_SIZE(arr) || !HAWK_ARR_SLOT(arr, idxint))? HAWK_VAL_ZERO: HAWK_VAL_ONE;
break; break;
} }
@ -4503,7 +4496,6 @@ static hawk_val_t* eval_binop_in (hawk_rtx_t* rtx, hawk_nde_t* left, hawk_nde_t*
goto oops; goto oops;
} }
done:
if (str != idxbuf) hawk_rtx_freemem (rtx, str); if (str != idxbuf) hawk_rtx_freemem (rtx, str);
hawk_rtx_refdownval (rtx, ropv); hawk_rtx_refdownval (rtx, ropv);
return res; return res;
@ -7469,7 +7461,7 @@ read_again:
return 1; return 1;
} }
static hawk_ooch_t* idxnde_to_str (hawk_rtx_t* rtx, hawk_nde_t* nde, hawk_ooch_t* buf, hawk_oow_t* len, hawk_nde_t** remidx, idxnde_to_str_idxint_t* firstidxint) static hawk_ooch_t* idxnde_to_str (hawk_rtx_t* rtx, hawk_nde_t* nde, hawk_ooch_t* buf, hawk_oow_t* len, hawk_nde_t** remidx, hawk_int_t* firstidxint)
{ {
hawk_ooch_t* str; hawk_ooch_t* str;
hawk_val_t* idx; hawk_val_t* idx;
@ -7532,11 +7524,6 @@ static hawk_ooch_t* idxnde_to_str (hawk_rtx_t* rtx, hawk_nde_t* nde, hawk_ooch_t
hawk_rtx_refdownval (rtx, idx); hawk_rtx_refdownval (rtx, idx);
*remidx = HAWK_NULL; *remidx = HAWK_NULL;
if (firstidxint)
{
firstidxint->ok = 1;
firstidxint->v = idxint;
}
} }
else else
{ {
@ -7545,6 +7532,7 @@ static hawk_ooch_t* idxnde_to_str (hawk_rtx_t* rtx, hawk_nde_t* nde, hawk_ooch_t
hawk_oocs_t tmp; hawk_oocs_t tmp;
hawk_rtx_valtostr_out_t out; hawk_rtx_valtostr_out_t out;
hawk_nde_t* xnde; hawk_nde_t* xnde;
int first = 1;
out.type = HAWK_RTX_VALTOSTR_STRPCAT; out.type = HAWK_RTX_VALTOSTR_STRPCAT;
out.u.strpcat = &idxstr; out.u.strpcat = &idxstr;
@ -7571,6 +7559,19 @@ static hawk_ooch_t* idxnde_to_str (hawk_rtx_t* rtx, hawk_nde_t* nde, hawk_ooch_t
hawk_rtx_refupval (rtx, idx); hawk_rtx_refupval (rtx, idx);
if (firstidxint && first)
{
if (hawk_rtx_valtoint(rtx, idx, &idxint) <= -1)
{
hawk_rtx_refdownval (rtx, idx);
hawk_ooecs_fini (&idxstr);
ADJERR_LOC (rtx, &nde->loc);
return HAWK_NULL;
}
first = 0;
}
if (xnde != nde && hawk_ooecs_ncat(&idxstr, rtx->gbl.subsep.ptr, rtx->gbl.subsep.len) == (hawk_oow_t)-1) if (xnde != nde && hawk_ooecs_ncat(&idxstr, rtx->gbl.subsep.ptr, rtx->gbl.subsep.len) == (hawk_oow_t)-1)
{ {
hawk_rtx_refdownval (rtx, idx); hawk_rtx_refdownval (rtx, idx);
@ -7599,10 +7600,9 @@ static hawk_ooch_t* idxnde_to_str (hawk_rtx_t* rtx, hawk_nde_t* nde, hawk_ooch_t
/* if nde is not HAWK_NULL, it should be of the HAWK_NDE_NULL type */ /* if nde is not HAWK_NULL, it should be of the HAWK_NDE_NULL type */
*remidx = nde? nde->next: nde; *remidx = nde? nde->next: nde;
if (firstidxint) firstidxint->ok = 0;
} }
if (firstidxint) *firstidxint = idxint;
return str; return str;
} }