summaryrefslogtreecommitdiff
path: root/libstd/intparse.myr
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2013-12-20 20:58:20 -0500
committerOri Bernstein <ori@eigenstate.org>2013-12-20 20:58:20 -0500
commitcfb191b44373a070f378a6655a69214cf6dbda9b (patch)
treeb3a324d70e476c4d1265f4e682d675c1da0f14f5 /libstd/intparse.myr
parentd438cbf5a395ddb1eb383afb44389832dda6ee5a (diff)
downloadmc-cfb191b44373a070f378a6655a69214cf6dbda9b.tar.gz
Make integer parsing return an option
Diffstat (limited to 'libstd/intparse.myr')
-rw-r--r--libstd/intparse.myr34
1 files changed, 20 insertions, 14 deletions
diff --git a/libstd/intparse.myr b/libstd/intparse.myr
index 74f5002..d763854 100644
--- a/libstd/intparse.myr
+++ b/libstd/intparse.myr
@@ -1,16 +1,17 @@
use "die.use"
use "strcmp.use"
+use "option.use"
use "types.use"
use "utf.use"
use "fmt.use"
pkg std =
- generic intparsebase : (s : byte[:], base : int -> @a::(tcint,tcnum,tctest))
- generic intparse : (s : byte[:] -> @a::(tcint,tcnum,tctest))
+ generic intparsebase : (s : byte[:], base : int -> option(@a::(tcint,tcnum,tctest)))
+ generic intparse : (s : byte[:] -> option(@a::(tcint,tcnum,tctest)))
/* FIXME: fix hidden exports */
- generic charval
- generic doparse
+ generic charval : (c : char, base : int -> option(@a::(tcnum,tctest,tcint)))
+ generic doparse : (s : byte[:], isneg : bool, base : int -> option(@a::(tcnum,tctest,tcint)))
;;
generic intparse = {s
@@ -33,7 +34,7 @@ generic intparse = {s
;;
}
-generic intparsebase = {s, base -> @a::(tcint,tcnum,tctest)
+generic intparsebase = {s, base
var isneg
isneg = false
@@ -45,25 +46,30 @@ generic intparsebase = {s, base -> @a::(tcint,tcnum,tctest)
-> doparse(s, isneg, base)
}
-generic doparse = {s, isneg, base -> @a::(tcint,tcnum,tctest)
- var v : @a::(tcint,tcnum,tctest)
+generic doparse = {s, isneg, base
var c
+ var v
v = 0
while s.len != 0
(c, s) = striter(s)
- v *= base castto(@a::(tcint,tcnum,tctest))
- v += charval(c, base)
+ match charval(c, base)
+ | `Some cv:
+ v *= (base castto(@a::(tcint,tcnum,tctest)))
+ v += cv
+ | `None:
+ -> `None
+ ;;
;;
if isneg
- -> -v
+ -> `Some -v
else
- -> v
+ -> `Some v
;;
}
-generic charval = {c, base -> @a :: (tcint,tcnum,tctest)
+generic charval = {c, base
var v = -1
if c >= '0' && c <= '9'
@@ -75,7 +81,7 @@ generic charval = {c, base -> @a :: (tcint,tcnum,tctest)
;;
if v < 0 || v > (base castto(@a::(tcint,tcnum,tctest)))
- fatal(1, "Character %c out of range", c)
+ -> `None
;;
- -> v
+ -> `Some v
}