klisp

an open source interpreter for the Kernel Programming Language.
git clone http://git.hanabi.in/repos/klisp.git
Log | Files | Refs | README

commit d3e64f1fc858dbd22132f8010034658d0e904789
parent 2fb4419de3975d0dfd4034b8dd1011c17c56cae7
Author: Andres Navarro <canavarro82@gmail.com>
Date:   Sun, 10 Apr 2011 19:37:51 -0300

Added support for bigints in abs applicative.

Diffstat:
Msrc/kgnumbers.c | 6++++++
Msrc/kinteger.c | 11+++++++++++
Msrc/kinteger.h | 3+++
3 files changed, 20 insertions(+), 0 deletions(-)

diff --git a/src/kgnumbers.c b/src/kgnumbers.c @@ -689,9 +689,15 @@ void kabs(klisp_State *K, TValue *xparams, TValue ptree, TValue denv) case K_TFIXINT: { int32_t i = ivalue(n); kapply_cc(K, i < 0? i2tv(-i) : n); + return; + } + case K_TBIGINT: { + kapply_cc(K, kbigint_abs(K, n)); + return; } case K_TEINF: kapply_cc(K, KEPINF); + return; default: /* shouldn't happen */ assert(0); diff --git a/src/kinteger.c b/src/kinteger.c @@ -178,6 +178,17 @@ bool kbigint_evenp(TValue tv_bigint) return ((bigint->last->digit) & 1) == 0; } +TValue kbigint_abs(klisp_State *K, TValue tv_bigint) +{ + if (kbigint_positivep(tv_bigint)) { + return tv_bigint; + } else { + TValue res = kbigint_copy(K, tv_bigint); + kbigint_invert_sign(res); + return res; + } +} + /* Mutate the bigint to have the opposite sign, used in read */ void kbigint_invert_sign(TValue tv_bigint) { diff --git a/src/kinteger.h b/src/kinteger.h @@ -54,6 +54,9 @@ bool kbigint_positivep(TValue tv_bigint); bool kbigint_oddp(TValue tv_bigint); bool kbigint_evenp(TValue tv_bigint); +/* needs the state to create a copy if negative */ +TValue kbigint_abs(klisp_State *K, TValue tv_bigint); + /* Mutate the bigint to have the opposite sign, used in read & write */ void kbigint_invert_sign(TValue tv_bigint);