From 462024f5d4cd7bc2c134eb732eabaea6f3bafc1e Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Fri, 5 Dec 2014 22:36:15 -0800 Subject: * arith.c (tofloat, toint): Handle characters. Fix error message in toint wrongly identifying itself as tofloat. * txr.1: Document handling of characters. --- ChangeLog | 7 +++++++ arith.c | 25 ++++++++++++++++++++++++- txr.1 | 5 +++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index a39c45a3..9f1f3b3a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2014-12-05 Kaz Kylheku + + * arith.c (tofloat, toint): Handle characters. + Fix error message in toint wrongly identifying itself as tofloat. + + * txr.1: Document handling of characters. + 2014-12-05 Kaz Kylheku * eval.c (eval_init): Register in function as intrinsic. diff --git a/arith.c b/arith.c index 00621a5d..058a4e1d 100644 --- a/arith.c +++ b/arith.c @@ -2083,6 +2083,13 @@ val tofloat(val obj) switch (tag(obj)) { case TAG_NUM: return flo_int(obj); + case TAG_CHR: + { + cnum ch = c_num(obj); + if (isdigit(ch)) + return flo(ch - '0'); + return nil; + } case TAG_LIT: return flo_str(obj); case TAG_PTR: @@ -2111,6 +2118,22 @@ val toint(val obj, val base) return obj; case TAG_LIT: return int_str(obj, base); + case TAG_CHR: + { + cnum ch = c_num(obj); + + if (ch >= '0' && ch <= '9') + return num(ch - '0'); + + if (isalpha(ch)) { + cnum n = 10 + toupper(ch) - 'A'; + cnum b = c_num(default_arg(base, num_fast(10))); + + if (n < b) + return num(n); + } + return nil; + } case TAG_PTR: switch (type(obj)) { case BGNUM: @@ -2126,7 +2149,7 @@ val toint(val obj, val base) } /* fallthrough */ default: - uw_throwf(error_s, lit("tofloat: ~s is not convertible to float"), obj, nao); + uw_throwf(error_s, lit("toint: ~s is not convertible to integer"), obj, nao); } } diff --git a/txr.1 b/txr.1 index a68cc498..0c3ab384 100644 --- a/txr.1 +++ b/txr.1 @@ -18882,6 +18882,11 @@ to floating-point or integer, respectively. If a floating-point value is passed into tofloat, or an integer value into toint, then the value is simply returned. +If +.meta value +is a character, then it is treated as a string of length one +containing that character. + If .meta value is a string, then it is converted by -- cgit v1.2.3