klisp

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

commit c2befd746ede5e81fd8764a5f335ec1b9d2be1c7
parent dafc7af18fb444be19f5a649196fa4e957f267e7
Author: Andres Navarro <canavarro82@gmail.com>
Date:   Sat, 23 Apr 2011 14:39:56 -0300

Changed code in write to print strings. It now uses imath directly.

Diffstat:
Msrc/kinteger.c | 10++++++++++
Msrc/kinteger.h | 4++++
Msrc/kwrite.c | 34+++++++++-------------------------
3 files changed, 23 insertions(+), 25 deletions(-)

diff --git a/src/kinteger.c b/src/kinteger.c @@ -111,6 +111,16 @@ int32_t kbigint_print_size(TValue tv_bigint, int32_t base) return mp_int_string_len(tv2bigint(tv_bigint), base); } +/* this is used by write */ +void kbigint_print_string(klisp_State *K, TValue tv_bigint, int32_t base, + char *buf, int32_t limit) +{ + mp_result res = mp_int_to_string(K, tv2bigint(tv_bigint), base, buf, + limit); + /* only possible error is truncation */ + klisp_assert(res == MP_OK); +} + /* Interface for kgnumbers */ bool kbigint_eqp(TValue tv_bigint1, TValue tv_bigint2) { diff --git a/src/kinteger.h b/src/kinteger.h @@ -75,6 +75,10 @@ void kbigint_invert_sign(klisp_State *K, TValue tv_bigint); print the number */ int32_t kbigint_print_size(TValue tv_bigint, int32_t base); +/* this is used by write */ +void kbigint_print_string(klisp_State *K, TValue tv_bigint, int32_t base, + char *buf, int32_t limit); + /* Interface for kgnumbers */ bool kbigint_eqp(TValue bigint1, TValue bigint2); diff --git a/src/kwrite.c b/src/kwrite.c @@ -12,6 +12,7 @@ #include "kwrite.h" #include "kobject.h" #include "kinteger.h" +#include "krational.h" #include "kpair.h" #include "kstring.h" #include "ksymbol.h" @@ -52,40 +53,23 @@ void kwrite_error(klisp_State *K, char *msg) } /* TODO: check for return codes and throw error if necessary */ - +#define KDEFAULT_NUMBER_RADIX 10 void kw_print_bigint(klisp_State *K, TValue bigint) { - int32_t size = kbigint_print_size(bigint, 10) + - ((kbigint_negativep(bigint))? 1 : 0); - + int32_t radix = KDEFAULT_NUMBER_RADIX; + int32_t size = kbigint_print_size(bigint, radix); krooted_tvs_push(K, bigint); + /* here we are using 1 byte extra, because size already includes + 1 for the terminator, but better be safe than sorry */ TValue buf_str = kstring_new_s(K, size); krooted_tvs_push(K, buf_str); - /* write backwards so we can use printf later */ - char *buf = kstring_buf(buf_str) + size - 1; - - TValue copy = kbigint_copy(K, bigint); - krooted_vars_push(K, &copy); - - /* must work with positive bigint to get the digits */ - if (kbigint_negativep(bigint)) - kbigint_invert_sign(K, copy); - - while(kbigint_has_digits(K, copy)) { - int32_t digit = kbigint_remove_digit(K, copy, 10); - /* write backwards so we can use printf later */ - /* XXX: use to_digit function */ - *buf-- = '0' + digit; - } - if (kbigint_negativep(bigint)) - *buf-- = '-'; - - kw_printf(K, "%s", buf+1); + char *buf = kstring_buf(buf_str); + kbigint_print_string(K, bigint, radix, buf, size); + kw_printf(K, "%s", buf); krooted_tvs_pop(K); krooted_tvs_pop(K); - krooted_vars_pop(K); } /*