added the hawk::type() builtin function
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
hyung-hwan 2024-04-16 12:33:13 +09:00
parent 0407dd648c
commit a7d3432c11
3 changed files with 40 additions and 2 deletions

View File

@ -1404,7 +1404,8 @@ enum hawk_val_type_t
/* - the enumerators between HAWK_VAL_NIL and HAWK_VAL_ARR inclusive /* - the enumerators between HAWK_VAL_NIL and HAWK_VAL_ARR inclusive
* must be synchronized with an internal table of the __cmp_val * must be synchronized with an internal table of the __cmp_val
* function in run.c. * function in run.c.
* - all enumerators must be in sync with __val_type_name in val.c */ * - all enumerators must be in sync with __val_type_name in val.c
* - all enumerators must be in sync with VAL_XXX defintion in mod-hawk.c */
HAWK_VAL_NIL = 0, /**< nil */ HAWK_VAL_NIL = 0, /**< nil */
HAWK_VAL_CHAR = 1, /**< character */ HAWK_VAL_CHAR = 1, /**< character */
HAWK_VAL_BCHR = 2, /**< byte character */ HAWK_VAL_BCHR = 2, /**< byte character */

View File

@ -449,6 +449,22 @@ static int fnc_modlibdirs (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi)
return 0; return 0;
} }
static int fnc_type (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi)
{
hawk_val_t* a0;
hawk_val_t* r;
int _type;
a0 = hawk_rtx_getarg(rtx, 0);
_type = hawk_rtx_getvaltype(rtx, a0);
r = hawk_rtx_makeintval(rtx, _type);
if (HAWK_UNLIKELY(!r)) return -1;
hawk_rtx_setretval (rtx, r);
return 0;
}
static int fnc_typename (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) static int fnc_typename (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi)
{ {
hawk_val_t* a0; hawk_val_t* a0;
@ -500,13 +516,30 @@ static hawk_mod_fnc_tab_t fnctab[] =
{ HAWK_T("isnil"), { { 1, 1, HAWK_NULL }, fnc_isnil, 0 } }, { HAWK_T("isnil"), { { 1, 1, HAWK_NULL }, fnc_isnil, 0 } },
{ HAWK_T("map"), { { 0, A_MAX, HAWK_NULL }, fnc_map, 0 } }, { HAWK_T("map"), { { 0, A_MAX, HAWK_NULL }, fnc_map, 0 } },
{ HAWK_T("modlibdirs"), { { 0, 0, HAWK_NULL }, fnc_modlibdirs, 0 } }, { HAWK_T("modlibdirs"), { { 0, 0, HAWK_NULL }, fnc_modlibdirs, 0 } },
{ HAWK_T("type"), { { 1, 1, HAWK_NULL }, fnc_type, 0 } },
{ HAWK_T("typename"), { { 1, 1, HAWK_NULL }, fnc_typename, 0 } } { HAWK_T("typename"), { { 1, 1, HAWK_NULL }, fnc_typename, 0 } }
}; };
static hawk_mod_int_tab_t inttab[] = static hawk_mod_int_tab_t inttab[] =
{ {
/* keep this table sorted for binary search in query(). */ /* keep this table sorted for binary search in query(). */
{ HAWK_T("GC_NUM_GENS"), { HAWK_GC_NUM_GENS } } { HAWK_T("GC_NUM_GENS"), { HAWK_GC_NUM_GENS } },
/* synchronize this table with enum hawk_val_type_t in hawk.h */
/* the names follow the val_type_name table in val.c */
{ HAWK_T("VAL_ARRAY"), { HAWK_VAL_ARR } },
{ HAWK_T("VAL_BCHAR"), { HAWK_VAL_BCHR } },
{ HAWK_T("VAL_CHAR"), { HAWK_VAL_CHAR } },
{ HAWK_T("VAL_FLT"), { HAWK_VAL_FLT } },
{ HAWK_T("VAL_FUN"), { HAWK_VAL_FUN } },
{ HAWK_T("VAL_INT"), { HAWK_VAL_INT } },
{ HAWK_T("VAL_MAP"), { HAWK_VAL_MAP } },
{ HAWK_T("VAL_MBS"), { HAWK_VAL_MBS } },
{ HAWK_T("VAL_NIL"), { HAWK_VAL_NIL } },
{ HAWK_T("VAL_STR"), { HAWK_VAL_STR } },
{ HAWK_T("VAL_REF"), { HAWK_VAL_REF } },
{ HAWK_T("VAL_REX"), { HAWK_VAL_REX } }
}; };
static int query (hawk_mod_t* mod, hawk_t* hawk, const hawk_ooch_t* name, hawk_mod_sym_t* sym) static int query (hawk_mod_t* mod, hawk_t* hawk, const hawk_ooch_t* name, hawk_mod_sym_t* sym)

View File

@ -22,6 +22,10 @@ function main()
tap_ensure (foo2[1], 1, @SCRIPTNAME, @SCRIPTLINE); tap_ensure (foo2[1], 1, @SCRIPTNAME, @SCRIPTLINE);
tap_ensure (foo3[1], ini, @SCRIPTNAME, @SCRIPTLINE); tap_ensure (foo3[1], ini, @SCRIPTNAME, @SCRIPTLINE);
tap_ensure (foo4[1], ini, @SCRIPTNAME, @SCRIPTLINE); tap_ensure (foo4[1], ini, @SCRIPTNAME, @SCRIPTLINE);
tap_ensure (hawk::type('a'), hawk::VAL_CHAR, @SCRIPTNAME, @SCRIPTLINE);
tap_ensure (hawk::type(@b'b'), hawk::VAL_BCHAR, @SCRIPTNAME, @SCRIPTLINE);
tap_ensure (hawk::type("a"), hawk::VAL_STR, @SCRIPTNAME, @SCRIPTLINE);
tap_ensure (hawk::type(@b"a"), hawk::VAL_MBS, @SCRIPTNAME, @SCRIPTLINE);
} }
## gsub ## gsub