From bb4e176e42f5079ee81fa682f482f37de153d154 Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Thu, 16 Nov 2023 19:37:36 -0800 Subject: [PATCH] WIP, read rationals as inexact nums --- CHANGELOG.md | 1 + runtime.c | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64fc54c0..5be088ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ Features - Arthur Maciel added `make-opaque` to `(cyclone foreign)`. - Add `memory-streams` to the list of symbols that `(features)` can return, indicating that the current installation supports in-memory streams. +- Enhanced the reader to load rationals as inexact numbers. Bug Fixes diff --git a/runtime.c b/runtime.c index 11fcf9db..716e8075 100644 --- a/runtime.c +++ b/runtime.c @@ -2617,14 +2617,20 @@ int str_is_bignum(str2int_errno errnum, char *c) return 1; } -float string2rational(char *s){ +double string2rational(char *s) { - // TODO: this is terrible, needs work: char *nom = _strdup(s); - char *denom = strchr(s, '\'); + char *denom = strchr(nom, '/'); + if (denom == NULL) { + // Should never happen since we check for '/' elsewhere + return 0.0; + } denom[0] = '\0'; denom++; - return strtol(nom, NULL, 10) / strtol(denom, NULL, 10); +// TODO: check return values from strtol + double x = strtol(nom, NULL, 10); + double y = strtol(denom, NULL, 10); + return x / y; } object Cyc_string2number_(void *data, object cont, object str) @@ -2640,10 +2646,9 @@ object Cyc_string2number_(void *data, object cont, object str) if (rv == STR2INT_SUCCESS) { _return_closcall1(data, cont, obj_int2obj(result)); } else if (rv == STR2INT_RATIONAL) { - // TODO: function to handle this: - // - find / symbol - // - parse int on either side (could be bignums!) - // - for now, perform division and return float. longer-term, return rational + double d = string2rational(s); + make_double(result, d); + _return_closcall1(data, cont, &result); } else if (str_is_bignum(rv, s)) { alloc_bignum(data, bn); if (MP_OKAY != mp_read_radix(&(bignum_value(bn)), s, 10)) {