diff options
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | arith.c | 25 | ||||
-rw-r--r-- | txr.1 | 5 |
3 files changed, 36 insertions, 1 deletions
@@ -1,5 +1,12 @@ 2014-12-05 Kaz Kylheku <kaz@kylheku.com> + * 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 <kaz@kylheku.com> + * eval.c (eval_init): Register in function as intrinsic. * lib.c (in): New function. @@ -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); } } @@ -18884,6 +18884,11 @@ 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 .code tofloat as if by the function |