From 726b8026b3b29f1756afef15a61e20da70aa7f48 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sat, 7 Apr 2018 02:28:38 +0000 Subject: [PATCH] added the abs primitive function --- lib/bigint.c | 26 ++++++++++++++++++++++++++ lib/hcl-prv.h | 15 ++++++++------- lib/number.c | 21 +++++++++++++++++++++ lib/prim.c | 19 ++++++++++++++++++- 4 files changed, 73 insertions(+), 8 deletions(-) diff --git a/lib/bigint.c b/lib/bigint.c index 8ee9d40..75ef5d0 100644 --- a/lib/bigint.c +++ b/lib/bigint.c @@ -4031,6 +4031,32 @@ oops: 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_ooi_t v = 0; diff --git a/lib/hcl-prv.h b/lib/hcl-prv.h index 91021c5..124aa6f 100644 --- a/lib/hcl-prv.h +++ b/lib/hcl-prv.h @@ -946,6 +946,11 @@ hcl_oop_t hcl_sqrtint ( hcl_oop_t x ); +hcl_oop_t hcl_absint ( + hcl_t* hcl, + hcl_oop_t x +); + hcl_oop_t hcl_strtoint ( hcl_t* hcl, const hcl_ooch_t* str, @@ -1050,13 +1055,9 @@ hcl_oop_t hcl_sqrtnum ( hcl_oop_t x ); - -/* ========================================================================= */ -/* comp.c */ -/* ========================================================================= */ -HCL_EXPORT int hcl_compile ( - hcl_t* hcl, - hcl_oop_t expr +hcl_oop_t hcl_absnum ( + hcl_t* hcl, + hcl_oop_t x ); /* ========================================================================= */ diff --git a/lib/number.c b/lib/number.c index df2da0f..1753f8e 100644 --- a/lib/number.c +++ b/lib/number.c @@ -374,3 +374,24 @@ hcl_oop_t hcl_sqrtnum (hcl_t* hcl, hcl_oop_t x) 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); + } +} diff --git a/lib/prim.c b/lib/prim.c index 661353d..3c80a61 100644 --- a/lib/prim.c +++ b/lib/prim.c @@ -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) { 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; 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_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_sub, 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' } }, { 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_abs, 3, { 'a','b','s' } }, { 2, 2, pf_number_gt, 1, { '>' } }, { 2, 2, pf_number_ge, 2, { '>','=' } },