added the abs primitive function

This commit is contained in:
hyung-hwan 2018-04-07 02:28:38 +00:00
parent 9c23d72cec
commit 726b8026b3
4 changed files with 73 additions and 8 deletions

View File

@ -4031,6 +4031,32 @@ oops:
return HCL_NULL; return HCL_NULL;
} }
hcl_oop_t hcl_absint (hcl_t* hcl, hcl_oop_t x)
{
if (HCL_OOP_IS_SMOOI(x))
{
hcl_ooi_t v;
v = HCL_OOP_TO_SMOOI(x);
if (v < 0)
{
v = -v;
x = HCL_SMOOI_TO_OOP(v);
}
}
else if (HCL_IS_NBIGINT(hcl, x))
{
x = _clone_bigint(hcl, x, HCL_OBJ_GET_SIZE(x), HCL_BRAND_PBIGINT);
}
else if (!HCL_IS_PBIGINT(hcl, x))
{
hcl_seterrbfmt (hcl, HCL_EINVAL, "parameter not integer - %O", x);
return HCL_NULL;
}
return x;
}
hcl_oop_t hcl_inttostr (hcl_t* hcl, hcl_oop_t num, int radix, int ngc) hcl_oop_t hcl_inttostr (hcl_t* hcl, hcl_oop_t num, int radix, int ngc)
{ {
hcl_ooi_t v = 0; hcl_ooi_t v = 0;

View File

@ -946,6 +946,11 @@ hcl_oop_t hcl_sqrtint (
hcl_oop_t x hcl_oop_t x
); );
hcl_oop_t hcl_absint (
hcl_t* hcl,
hcl_oop_t x
);
hcl_oop_t hcl_strtoint ( hcl_oop_t hcl_strtoint (
hcl_t* hcl, hcl_t* hcl,
const hcl_ooch_t* str, const hcl_ooch_t* str,
@ -1050,13 +1055,9 @@ hcl_oop_t hcl_sqrtnum (
hcl_oop_t x hcl_oop_t x
); );
hcl_oop_t hcl_absnum (
/* ========================================================================= */ hcl_t* hcl,
/* comp.c */ hcl_oop_t x
/* ========================================================================= */
HCL_EXPORT int hcl_compile (
hcl_t* hcl,
hcl_oop_t expr
); );
/* ========================================================================= */ /* ========================================================================= */

View File

@ -374,3 +374,24 @@ hcl_oop_t hcl_sqrtnum (hcl_t* hcl, hcl_oop_t x)
return hcl_makefpdec(hcl, v, scale); return hcl_makefpdec(hcl, v, scale);
} }
} }
hcl_oop_t hcl_absnum (hcl_t* hcl, hcl_oop_t x)
{
if (!HCL_IS_FPDEC(hcl, x))
{
return hcl_absint(hcl, x);
}
else
{
hcl_oop_t v;
hcl_ooi_t scale;
scale = HCL_OOP_TO_SMOOI(((hcl_oop_fpdec_t)x)->scale);
v = ((hcl_oop_fpdec_t)x)->value;
v = hcl_absint(hcl, v);
if (!v) return HCL_NULL;
return hcl_makefpdec(hcl, v, scale);
}
}

View File

@ -502,7 +502,17 @@ static hcl_pfrc_t pf_number_rem (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t nargs)
static hcl_pfrc_t pf_number_sqrt (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t nargs) static hcl_pfrc_t pf_number_sqrt (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t nargs)
{ {
hcl_oop_t ret; hcl_oop_t ret;
ret = hcl_sqrtnum(hcl, HCL_STACK_GETARG(hcl, nargs, 0)); /*TODO: change to hcl_sqrtnum()*/ ret = hcl_sqrtnum(hcl, HCL_STACK_GETARG(hcl, nargs, 0));
if (!ret) return HCL_PF_FAILURE;
HCL_STACK_SETRET (hcl, nargs, ret);
return HCL_PF_SUCCESS;
}
static hcl_pfrc_t pf_number_abs (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t nargs)
{
hcl_oop_t ret;
ret = hcl_absnum(hcl, HCL_STACK_GETARG(hcl, nargs, 0));
if (!ret) return HCL_PF_FAILURE; if (!ret) return HCL_PF_FAILURE;
HCL_STACK_SETRET (hcl, nargs, ret); HCL_STACK_SETRET (hcl, nargs, ret);
@ -640,6 +650,12 @@ static pf_t builtin_prims[] =
{ 2, 2, pf_nql, 4, { 'n','q','l','?' } }, { 2, 2, pf_nql, 4, { 'n','q','l','?' } },
{ 2, 2, pf_nqk, 4, { 'n','q','k','?' } }, { 2, 2, pf_nqk, 4, { 'n','q','k','?' } },
/*
{ 1, 1, pf_is_null, 4, { 'n','u','l','l','?' } },
{ 1, 1, pf_is_integer, 8, { 'i','n','t','e','g','e','r','?' } },
{ 1, 1, pf_is_string, 7, { 's','t','r','i','n','g','?' } },
*/
{ 1, HCL_TYPE_MAX(hcl_oow_t), pf_number_add, 1, { '+' } }, { 1, HCL_TYPE_MAX(hcl_oow_t), pf_number_add, 1, { '+' } },
{ 1, HCL_TYPE_MAX(hcl_oow_t), pf_number_sub, 1, { '-' } }, { 1, HCL_TYPE_MAX(hcl_oow_t), pf_number_sub, 1, { '-' } },
{ 1, HCL_TYPE_MAX(hcl_oow_t), pf_number_mul, 1, { '*' } }, { 1, HCL_TYPE_MAX(hcl_oow_t), pf_number_mul, 1, { '*' } },
@ -648,6 +664,7 @@ static pf_t builtin_prims[] =
{ 1, HCL_TYPE_MAX(hcl_oow_t), pf_number_quo, 3, { 'q','u','o' } }, { 1, HCL_TYPE_MAX(hcl_oow_t), pf_number_quo, 3, { 'q','u','o' } },
{ 2, HCL_TYPE_MAX(hcl_oow_t), pf_number_rem, 3, { 'm','o','d' } }, { 2, HCL_TYPE_MAX(hcl_oow_t), pf_number_rem, 3, { 'm','o','d' } },
{ 1, 1, pf_number_sqrt, 4, { 's','q','r','t' } }, { 1, 1, pf_number_sqrt, 4, { 's','q','r','t' } },
{ 1, 1, pf_number_abs, 3, { 'a','b','s' } },
{ 2, 2, pf_number_gt, 1, { '>' } }, { 2, 2, pf_number_gt, 1, { '>' } },
{ 2, 2, pf_number_ge, 2, { '>','=' } }, { 2, 2, pf_number_ge, 2, { '>','=' } },