diff --git a/runtime.c b/runtime.c
index 6959a83..329c213 100644
a
|
b
|
C_regparm C_word C_fcall convert_string_to_number(C_char *str, int radix, C_word |
7215 | 7215 | } |
7216 | 7216 | } |
7217 | 7217 | |
7218 | | |
7219 | | static char *to_binary(C_uword num) |
| 7218 | static char *to_n_nary(C_uword num, C_uword base) |
7220 | 7219 | { |
7221 | 7220 | char *p; |
7222 | | |
7223 | | buffer[ 66 ] = '\0'; |
| 7221 | char digits[] ={ '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' }; |
| 7222 | buffer [ 66 ] = '\0'; |
7224 | 7223 | p = buffer + 66; |
7225 | | |
| 7224 | |
7226 | 7225 | do { |
7227 | | *(--p) = (num & 1) ? '1' : '0'; |
7228 | | num /= 2; |
7229 | | } while(num); |
| 7226 | *(--p) = digits [ num % base ]; |
| 7227 | num /= base; |
| 7228 | } while (num); |
7230 | 7229 | |
7231 | 7230 | return p; |
7232 | 7231 | } |
… |
… |
void C_ccall C_number_to_string(C_word c, C_word closure, C_word k, C_word num, |
7259 | 7258 | num = -num; |
7260 | 7259 | } |
7261 | 7260 | |
| 7261 | if((radix < 2) || (radix > 16)){ |
| 7262 | barf(C_BAD_ARGUMENT_TYPE_ERROR, "number->string", C_fix(radix)); |
| 7263 | } |
| 7264 | |
7262 | 7265 | switch(radix) { |
7263 | | case 2: |
7264 | | p = to_binary(num); |
7265 | | break; |
7266 | | |
7267 | 7266 | #ifdef C_SIXTY_FOUR |
7268 | 7267 | case 8: C_sprintf(p = buffer + 1, C_text("%lo"), num); break; |
7269 | 7268 | case 10: C_sprintf(p = buffer + 1, C_text("%ld"), num); break; |
… |
… |
void C_ccall C_number_to_string(C_word c, C_word closure, C_word k, C_word num, |
7273 | 7272 | case 10: C_sprintf(p = buffer + 1, C_text("%d"), num); break; |
7274 | 7273 | case 16: C_sprintf(p = buffer + 1, C_text("%x"), num); break; |
7275 | 7274 | #endif |
7276 | | |
7277 | | default: barf(C_BAD_ARGUMENT_TYPE_ERROR, "number->string", C_fix(radix)); |
| 7275 | default: |
| 7276 | p = to_n_nary(num, radix); |
7278 | 7277 | } |
7279 | 7278 | } |
7280 | 7279 | else if(!C_immediatep(num) && C_block_header(num) == C_FLONUM_TAG) { |
… |
… |
void C_ccall C_number_to_string(C_word c, C_word closure, C_word k, C_word num, |
7286 | 7285 | f = -f; |
7287 | 7286 | } |
7288 | 7287 | |
7289 | | switch(radix) { |
7290 | | case 2: |
7291 | | p = to_binary((unsigned int)f); |
7292 | | goto fini; |
| 7288 | if((radix < 2) || (radix > 16)){ |
| 7289 | barf(C_BAD_ARGUMENT_TYPE_ERROR, "number->string", C_fix(radix)); |
| 7290 | } |
7293 | 7291 | |
| 7292 | switch(radix) { |
7294 | 7293 | case 8: |
7295 | 7294 | C_sprintf(p = buffer, "%o", (unsigned int)f); |
7296 | 7295 | goto fini; |
… |
… |
void C_ccall C_number_to_string(C_word c, C_word closure, C_word k, C_word num, |
7298 | 7297 | case 16: |
7299 | 7298 | C_sprintf(p = buffer, "%x", (unsigned int)f); |
7300 | 7299 | goto fini; |
| 7300 | |
| 7301 | default: |
| 7302 | p = to_n_nary((unsigned int)f, radix); |
| 7303 | goto fini; |
| 7304 | |
7301 | 7305 | } |
7302 | 7306 | } |
7303 | 7307 | |