summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2014-12-05 22:36:15 -0800
committerKaz Kylheku <kaz@kylheku.com>2014-12-05 22:36:15 -0800
commit462024f5d4cd7bc2c134eb732eabaea6f3bafc1e (patch)
tree552bf326425892de3127cca74c517643b10de89c
parent0bbef1e12870a4f749a112dfb4ef8a09736e8b29 (diff)
downloadtxr-462024f5d4cd7bc2c134eb732eabaea6f3bafc1e.tar.gz
txr-462024f5d4cd7bc2c134eb732eabaea6f3bafc1e.tar.bz2
txr-462024f5d4cd7bc2c134eb732eabaea6f3bafc1e.zip
* arith.c (tofloat, toint): Handle characters.
Fix error message in toint wrongly identifying itself as tofloat. * txr.1: Document handling of characters.
-rw-r--r--ChangeLog7
-rw-r--r--arith.c25
-rw-r--r--txr.15
3 files changed, 36 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index a39c45a3..9f1f3b3a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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.
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
@@ -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