ref: cfb191b44373a070f378a6655a69214cf6dbda9b
parent: d438cbf5a395ddb1eb383afb44389832dda6ee5a
author: Ori Bernstein <[email protected]>
date: Fri Dec 20 15:58:20 EST 2013
Make integer parsing return an option
--- 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 intparsebase = {s, base -> @a::(tcint,tcnum,tctest)
+generic intparsebase = {s, base
var isneg
isneg = false
@@ -45,25 +46,30 @@
-> 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 @@
;;
if v < 0 || v > (base castto(@a::(tcint,tcnum,tctest)))
- fatal(1, "Character %c out of range", c)
+ -> `None
;;
- -> v
+ -> `Some v
}
--- a/libstd/ipparse.myr
+++ b/libstd/ipparse.myr
@@ -24,20 +24,29 @@
for j = 0; j < ip.len; j++
if ip[j] == '.' castto(byte)
put("seg[%z..%z] = %s\n", last, j, ip[last:j])
- val = intparsebase(ip[last:j], 10)
- if val < 0 || val > 255
+ match intparsebase(ip[last:j], 10)
+ | `Some v:
+ val = v
+ if val < 0 || val > 255
+ -> `None
+ ;;
+ addr[i++] = val castto(byte)
+ last = j + 1
+ | `None:
-> `None
;;
- addr[i++] = val castto(byte)
- last = j + 1
;;
;;
- put("seg[%z..%z] = %s\n", last, j, ip[last:j])
- val = intparsebase(ip[last:j], 10)
- if val < 0 || val > 255
+ match intparsebase(ip[last:j], 10)
+ | `Some v:
+ val = v
+ if val < 0 || val > 255
+ -> `None
+ ;;
+ addr[i] = val castto(byte)
+ | `None:
-> `None
;;
- addr[i] = val castto(byte)
if j != ip.len
-> `None
;;
--- a/muse/muse.c
+++ b/muse/muse.c
@@ -127,6 +127,7 @@
file->file.exports = mkstab();
file->file.globls = mkstab();
updatens(file->file.exports, outfile);
+ tyinit(file->file.globls);
for (i = optind; i < argc; i++)
mergeuse(argv[i]);
f = fopen(outfile, "w");