TXR:一点点正则表达式,一点点类型系统。如果令牌看起来像一个数字,那么让我们尝试将其从字符串转换为数字对象num-str
。如果失败,则一定是范围错误。该typeof
函数为我们提供了对象的类型fixnum
:bignum
或float
。
@(freeform)
@(coll)@{token /[^\s]+/}@(end)
@(output)
@ (repeat)
@token @(if (eql (match-regex token #/[+\-]?\d+([.]\d+)?([Ee][+\-]?\d+)?/)
(length token))
(let ((x (num-str token)))
(if x (typeof x) "out-of-range"))
"non-numeric")
@ (end)
@(end)
跑:
$ txr verify.txr -
hello world
1.5E900 1.43 52 5A 12341234123412341234 12341234123412341234243.42 42
[Ctrl-D]
hello non-numeric
world non-numeric
1.5E900 out-of-range
1.43 float
52 fixnum
5A non-numeric
12341234123412341234 bignum
12341234123412341234243.42 float
42 fixnum