commit f47f4092ca541473c32028e03c60309b9ee79db5
parent 75df3df6ed5874c9c41baae8ee284cf6373a7b51
Author: Andres Navarro <canavarro82@gmail.com>
Date: Sun, 10 Apr 2011 13:53:30 -0300
Added bigint support for positive? and negative? applicatives.
Diffstat:
4 files changed, 36 insertions(+), 4 deletions(-)
diff --git a/src/Makefile b/src/Makefile
@@ -58,7 +58,7 @@ kwrite.o: kwrite.c kwrite.h kobject.h kpair.h kstring.h kstate.h kerror.h \
kstate.o: kstate.c kstate.h klisp.h kobject.h kmem.h kstring.h klisp.h \
kground.h kenvironment.h kpair.h keval.h koperative.h kground.h \
krepl.h kcontinuation.h kapplicative.h kport.h ksymbol.h kport.h \
- kstring.h
+ kstring.h kinteger.h
kmem.o: kmem.c kmem.h klisp.h kerror.h klisp.h kstate.h
kerror.o: kerror.c kerror.h klisp.h kstate.h klisp.h kmem.h kstring.h
kauxlib.o: kauxlib.c kauxlib.h klisp.h kstate.h klisp.h
@@ -135,7 +135,7 @@ kgchars.o: kgchars.c kgchars.h kghelpers.h kstate.h klisp.h \
kobject.h kerror.h kapplicative.h koperative.h kcontinuation.h
kgnumbers.o: kgnumbers.c kgnumbers.h kghelpers.h kstate.h klisp.h \
kobject.h kerror.h kapplicative.h koperative.h kcontinuation.h \
- ksymbol.h
+ ksymbol.h kinteger.h
kgstrings.o: kgstrings.c kgstrings.h kghelpers.h kstate.h klisp.h \
kobject.h kerror.h kapplicative.h koperative.h kcontinuation.h \
ksymbol.h
diff --git a/src/kgnumbers.c b/src/kgnumbers.c
@@ -17,6 +17,7 @@
#include "kcontinuation.h"
#include "kerror.h"
#include "ksymbol.h"
+#include "kinteger.h"
#include "kghelpers.h"
#include "kgnumbers.h"
@@ -617,8 +618,36 @@ void kdiv_mod(klisp_State *K, TValue *xparams, TValue ptree, TValue denv)
/* use ftyped_predp */
/* Helpers for positive?, negative?, odd? & even? */
-bool kpositivep(TValue n) { return ivalue(n) > 0; }
-bool knegativep(TValue n) { return ivalue(n) < 0; }
+bool kpositivep(TValue n)
+{
+ switch (ttype(n)) {
+ case K_TFIXINT:
+ case K_TEINF:
+ return ivalue(n) > 0;
+ case K_TBIGINT:
+ return kbigint_positivep(n);
+ default:
+ /* shouldn't happen */
+ assert(0);
+ return false;
+ }
+}
+
+bool knegativep(TValue n)
+{
+ switch (ttype(n)) {
+ case K_TFIXINT:
+ case K_TEINF:
+ return ivalue(n) < 0;
+ case K_TBIGINT:
+ return kbigint_negativep(n);
+ default:
+ /* shouldn't happen */
+ assert(0);
+ return false;
+ }
+}
+
bool koddp(TValue n) { return (ivalue(n) & 1) != 0; }
bool kevenp(TValue n) { return (ivalue(n) & 1) == 0; }
diff --git a/src/kgnumbers.h b/src/kgnumbers.h
@@ -110,6 +110,7 @@ void kgcd(klisp_State *K, TValue *xparams, TValue ptree, TValue denv);
void klcm(klisp_State *K, TValue *xparams, TValue ptree, TValue denv);
/* Misc Helpers */
+/* TEMP: only infinities, fixints and bigints for now */
inline bool kfast_zerop(TValue n) { return ttisfixint(n) && ivalue(n) == 0; }
inline bool kfast_onep(TValue n) { return ttisfixint(n) && ivalue(n) == 1; }
/* TEMP: only exact infinties */
diff --git a/src/kinteger.c b/src/kinteger.c
@@ -159,6 +159,8 @@ bool kbigint_negativep(TValue tv_bigint)
return kbigint_negp(tv2bigint(tv_bigint));
}
+/* unlike the positive? applicative this would return true on zero,
+ but zero is never represented as a bigint so there is no problem */
bool kbigint_positivep(TValue tv_bigint)
{
return kbigint_posp(tv2bigint(tv_bigint));