commit b8a3e2ab88cfe9d161718fcb789fc59dac9049ec
parent 20a2e94b95f262300a08b55f5eb7f389baaaaa9c
Author: Andres Navarro <canavarro82@gmail.com>
Date:   Sat, 23 Apr 2011 13:29:31 -0300
Added interface for creating bigrats.
Diffstat:
3 files changed, 42 insertions(+), 4 deletions(-)
diff --git a/src/kinteger.c b/src/kinteger.c
@@ -32,7 +32,7 @@ inline TValue kbigint_try_fixint(klisp_State *K, TValue n)
     }
 }
 
-/* for now used only for reading */
+/* It is used for reading and for creating temps and res in all operations */
 /* NOTE: is uint to allow INT32_MIN as positive argument in read */
 TValue kbigint_new(klisp_State *K, bool sign, uint32_t digit)
 {
diff --git a/src/krational.c b/src/krational.c
@@ -41,4 +41,42 @@ inline TValue kbigrat_try_integer(klisp_State *K, TValue n)
     return copy;
 }
 
+/* used for res & temps in operations */
+/* NOTE: This is to be called only with already reduced values */
+TValue kbigrat_new(klisp_State *K, bool sign, uint32_t num, 
+		   uint32_t den)
+{
+    Bigrat *new_bigrat = klispM_new(K, Bigrat);
+
+    /* header + gc_fields */
+    klispC_link(K, (GCObject *) new_bigrat, K_TBIGRAT, 0);
+
+    /* bigrat specific fields */
+    /* If later changed to alloc obj: 
+       GC: root bigint & put dummy value to work if garbage collections 
+       happens while allocating array */
+    new_bigrat->num.single = num;
+    new_bigrat->num.digits = &(new_bigrat->num.single);
+    new_bigrat->num.alloc = 1;
+    new_bigrat->num.used = 1;
+    new_bigrat->num.sign = sign? MP_NEG : MP_ZPOS;
+
+    new_bigrat->den.single = den;
+    new_bigrat->den.digits = &(new_bigrat->den.single);
+    new_bigrat->den.alloc = 1;
+    new_bigrat->den.used = 1;
+    new_bigrat->den.sign = MP_ZPOS;
+
+    return gc2bigrat(new_bigrat);
+}
+
+/* assumes src is rooted */
+TValue kbigrat_copy(klisp_State *K, TValue src)
+{
+    TValue copy = kbigrat_new(K, false, 0, 1);
+    /* arguments are in reverse order with respect to mp_rat_copy */
+    UNUSED(mp_int_init_copy(K, tv2bigrat(copy), tv2bigrat(src)));
+    return copy;
+}
 
+/* Interface for kgnumbers */
diff --git a/src/krational.h b/src/krational.h
@@ -18,9 +18,9 @@
 
 /* TEMP: for now we only implement bigrats (memory allocated) */
 
-/* TODO: how to alloc?? */
-/* TEMP: we'll see about these */
-/* for now used only for reading */
+/* TEMP: we'll see about reading & writing... */
+
+/* used in reading and for res & temps in operations */
 TValue kbigrat_new(klisp_State *K, bool sign, uint32_t num, 
 		   uint32_t den);