added some number primitives

This commit is contained in:
hyunghwan.chung 2019-01-10 07:00:17 +00:00
parent 05a3c4eefd
commit cd4d34c724
4 changed files with 141 additions and 13 deletions

View File

@ -143,7 +143,7 @@ class(#limited) Number(Magnitude)
method mdiv: aNumber method mdiv: aNumber
{ {
## integer division quotient ## integer division quotient
<primitive: #_integer_mdiv> <primitive: #_number_mdiv>
self primitiveFailed. self primitiveFailed.
} }
@ -173,31 +173,31 @@ class(#limited) Number(Magnitude)
method < aNumber method < aNumber
{ {
<primitive: #_integer_lt> <primitive: #_number_lt>
self primitiveFailed. self primitiveFailed.
} }
method > aNumber method > aNumber
{ {
<primitive: #_integer_gt> <primitive: #_number_gt>
self primitiveFailed. self primitiveFailed.
} }
method <= aNumber method <= aNumber
{ {
<primitive: #_integer_le> <primitive: #_number_le>
self primitiveFailed. self primitiveFailed.
} }
method >= aNumber method >= aNumber
{ {
<primitive: #_integer_ge> <primitive: #_number_ge>
self primitiveFailed. self primitiveFailed.
} }
method negated method negated
{ {
<primitive: #_integer_negated> <primitive: #_number_negated>
^0 - self. ^0 - self.
} }

View File

@ -2879,13 +2879,43 @@ static moo_pfrc_t pf_number_div (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
rcv = MOO_STACK_GETRCV(moo, nargs); rcv = MOO_STACK_GETRCV(moo, nargs);
arg = MOO_STACK_GETARG(moo, nargs, 0); arg = MOO_STACK_GETARG(moo, nargs, 0);
res = moo_divnums(moo, rcv, arg); res = moo_divnums(moo, rcv, arg, 0);
if (!res) return (moo->errnum == MOO_EINVAL? MOO_PF_FAILURE: MOO_PF_HARD_FAILURE); if (!res) return (moo->errnum == MOO_EINVAL? MOO_PF_FAILURE: MOO_PF_HARD_FAILURE);
MOO_STACK_SETRET (moo, nargs, res); MOO_STACK_SETRET (moo, nargs, res);
return MOO_PF_SUCCESS; return MOO_PF_SUCCESS;
} }
static moo_pfrc_t pf_number_mdiv (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
{
moo_oop_t rcv, arg, res;
MOO_ASSERT (moo, nargs == 1);
rcv = MOO_STACK_GETRCV(moo, nargs);
arg = MOO_STACK_GETARG(moo, nargs, 0);
res = moo_divnums(moo, rcv, arg, 1);
if (!res) return (moo->errnum == MOO_EINVAL? MOO_PF_FAILURE: MOO_PF_HARD_FAILURE);
MOO_STACK_SETRET (moo, nargs, res);
return MOO_PF_SUCCESS;
}
static moo_pfrc_t pf_number_negated (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
{
moo_oop_t rcv, res;
MOO_ASSERT (moo, nargs == 0);
rcv = MOO_STACK_GETRCV(moo, nargs);
res = moo_negatenum(moo, rcv);
if (!res) return (moo->errnum == MOO_EINVAL? MOO_PF_FAILURE: MOO_PF_HARD_FAILURE);
MOO_STACK_SETRET (moo, nargs, res);
return MOO_PF_SUCCESS;
}
static moo_pfrc_t pf_number_eq (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) static moo_pfrc_t pf_number_eq (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
{ {
@ -2919,6 +2949,70 @@ static moo_pfrc_t pf_number_ne (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
return MOO_PF_SUCCESS; return MOO_PF_SUCCESS;
} }
static moo_pfrc_t pf_number_lt (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
{
moo_oop_t rcv, arg, res;
MOO_ASSERT (moo, nargs == 1);
rcv = MOO_STACK_GETRCV(moo, nargs);
arg = MOO_STACK_GETARG(moo, nargs, 0);
res = moo_ltnums(moo, rcv, arg);
if (!res) return (moo->errnum == MOO_EINVAL? MOO_PF_FAILURE: MOO_PF_HARD_FAILURE);
MOO_STACK_SETRET (moo, nargs, res);
return MOO_PF_SUCCESS;
}
static moo_pfrc_t pf_number_gt (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
{
moo_oop_t rcv, arg, res;
MOO_ASSERT (moo, nargs == 1);
rcv = MOO_STACK_GETRCV(moo, nargs);
arg = MOO_STACK_GETARG(moo, nargs, 0);
res = moo_gtnums(moo, rcv, arg);
if (!res) return (moo->errnum == MOO_EINVAL? MOO_PF_FAILURE: MOO_PF_HARD_FAILURE);
MOO_STACK_SETRET (moo, nargs, res);
return MOO_PF_SUCCESS;
}
static moo_pfrc_t pf_number_le (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
{
moo_oop_t rcv, arg, res;
MOO_ASSERT (moo, nargs == 1);
rcv = MOO_STACK_GETRCV(moo, nargs);
arg = MOO_STACK_GETARG(moo, nargs, 0);
res = moo_lenums(moo, rcv, arg);
if (!res) return (moo->errnum == MOO_EINVAL? MOO_PF_FAILURE: MOO_PF_HARD_FAILURE);
MOO_STACK_SETRET (moo, nargs, res);
return MOO_PF_SUCCESS;
}
static moo_pfrc_t pf_number_ge (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
{
moo_oop_t rcv, arg, res;
MOO_ASSERT (moo, nargs == 1);
rcv = MOO_STACK_GETRCV(moo, nargs);
arg = MOO_STACK_GETARG(moo, nargs, 0);
res = moo_genums(moo, rcv, arg);
if (!res) return (moo->errnum == MOO_EINVAL? MOO_PF_FAILURE: MOO_PF_HARD_FAILURE);
MOO_STACK_SETRET (moo, nargs, res);
return MOO_PF_SUCCESS;
}
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
static moo_pfrc_t pf_integer_add (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) static moo_pfrc_t pf_integer_add (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
{ {
@ -3754,8 +3848,14 @@ static pf_t pftab[] =
{ "_number_add", { pf_number_add, 1, 1 } }, { "_number_add", { pf_number_add, 1, 1 } },
{ "_number_div", { pf_number_div, 1, 1 } }, { "_number_div", { pf_number_div, 1, 1 } },
{ "_number_eq", { pf_number_eq, 1, 1 } }, { "_number_eq", { pf_number_eq, 1, 1 } },
{ "_number_ge", { pf_number_ge, 1, 1 } },
{ "_number_gt", { pf_number_gt, 1, 1 } },
{ "_number_le", { pf_number_le, 1, 1 } },
{ "_number_lt", { pf_number_lt, 1, 1 } },
{ "_number_mdiv", { pf_number_mdiv, 1, 1 } },
{ "_number_mul", { pf_number_mul, 1, 1 } }, { "_number_mul", { pf_number_mul, 1, 1 } },
{ "_number_ne", { pf_number_ne, 1, 1 } }, { "_number_ne", { pf_number_ne, 1, 1 } },
{ "_number_negated", { pf_number_negated, 0, 0 } },
{ "_number_sub", { pf_number_sub, 1, 1 } }, { "_number_sub", { pf_number_sub, 1, 1 } },
{ "_utf8_seqlen", { moo_pf_utf8_seqlen, 0, 0 } }, { "_utf8_seqlen", { moo_pf_utf8_seqlen, 0, 0 } },

View File

@ -1405,7 +1405,8 @@ moo_oop_t moo_mltnums (
moo_oop_t moo_divnums ( moo_oop_t moo_divnums (
moo_t* moo, moo_t* moo,
moo_oop_t x, moo_oop_t x,
moo_oop_t y moo_oop_t y,
int modulo
); );
moo_oop_t moo_gtnums ( moo_oop_t moo_gtnums (
@ -1444,6 +1445,11 @@ moo_oop_t moo_nenums (
moo_oop_t y moo_oop_t y
); );
moo_oop_t moo_negatenum (
moo_t* moo,
moo_oop_t x
);
moo_oop_t moo_sqrtnum ( moo_oop_t moo_sqrtnum (
moo_t* moo, moo_t* moo,
moo_oop_t x moo_oop_t x

View File

@ -259,7 +259,7 @@ moo_oop_t moo_mltnums (moo_t* moo, moo_oop_t x, moo_oop_t y)
return mul_nums(moo, x, y, 1); return mul_nums(moo, x, y, 1);
} }
moo_oop_t moo_divnums (moo_t* moo, moo_oop_t x, moo_oop_t y) moo_oop_t moo_divnums (moo_t* moo, moo_oop_t x, moo_oop_t y, int modulo)
{ {
moo_ooi_t xs, ys, i; moo_ooi_t xs, ys, i;
moo_oop_t nv; moo_oop_t nv;
@ -304,7 +304,7 @@ moo_oop_t moo_divnums (moo_t* moo, moo_oop_t x, moo_oop_t y)
} }
} }
nv = moo_divints(moo, nv, yv, 0, MOO_NULL); nv = moo_divints(moo, nv, yv, modulo, MOO_NULL);
moo_popvolat (moo); moo_popvolat (moo);
if (!nv) return MOO_NULL; if (!nv) return MOO_NULL;
@ -367,6 +367,28 @@ moo_oop_t moo_nenums (moo_t* moo, moo_oop_t x, moo_oop_t y)
return comp_nums(moo, x, y, moo_neints); return comp_nums(moo, x, y, moo_neints);
} }
moo_oop_t moo_negatenum (moo_t* moo, moo_oop_t x)
{
if (!MOO_OOP_IS_FPDEC(moo, x))
{
return moo_negateint(moo, x);
}
else
{
moo_oop_t v;
moo_ooi_t scale;
scale = MOO_OOP_TO_SMOOI(((moo_oop_fpdec_t)x)->scale);
v = ((moo_oop_fpdec_t)x)->value;
v = moo_negateint(moo, v);
if (!v) return MOO_NULL;
return moo_makefpdec(moo, v, scale);
}
}
moo_oop_t moo_sqrtnum (moo_t* moo, moo_oop_t x) moo_oop_t moo_sqrtnum (moo_t* moo, moo_oop_t x)
{ {
if (!MOO_OOP_IS_FPDEC(moo, x)) if (!MOO_OOP_IS_FPDEC(moo, x))