diff options
author | Ori Bernstein <ori@eigenstate.org> | 2013-08-07 17:50:54 -0400 |
---|---|---|
committer | Ori Bernstein <ori@eigenstate.org> | 2013-08-07 17:50:54 -0400 |
commit | 4b77bda908dec8620abde1e452a1f2bac29f1252 (patch) | |
tree | 5c84b2a3245b9ff4b04aeec96958f8b874cb312d /libstd/intparse.myr | |
parent | 7ea39f801622858865e2fb06768a9f44f81b5bf8 (diff) | |
download | mc-4b77bda908dec8620abde1e452a1f2bac29f1252.tar.gz |
Parse negative numbers correctly.
We seem to munge them when returning. Oops.
Diffstat (limited to 'libstd/intparse.myr')
-rw-r--r-- | libstd/intparse.myr | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/libstd/intparse.myr b/libstd/intparse.myr index 29c519a..74f5002 100644 --- a/libstd/intparse.myr +++ b/libstd/intparse.myr @@ -2,6 +2,7 @@ use "die.use" use "strcmp.use" use "types.use" use "utf.use" +use "fmt.use" pkg std = generic intparsebase : (s : byte[:], base : int -> @a::(tcint,tcnum,tctest)) @@ -9,15 +10,16 @@ pkg std = /* FIXME: fix hidden exports */ generic charval + generic doparse ;; generic intparse = {s var isneg - isneg = 0 + isneg = false if hasprefix(s, "-") s = s[1:] - isneg = 1 + isneg = true ;; if hasprefix(s, "0x") @@ -27,17 +29,17 @@ generic intparse = {s elif hasprefix(s, "0b") -> doparse(s[2:], isneg, 2) else - -> doparse(s[2:], isneg, 10) + -> doparse(s, isneg, 10) ;; } generic intparsebase = {s, base -> @a::(tcint,tcnum,tctest) var isneg - isneg = 0 + isneg = false if hasprefix(s, "-") s = s[1:] - isneg = 1 + isneg = true ;; -> doparse(s, isneg, base) @@ -47,7 +49,6 @@ generic doparse = {s, isneg, base -> @a::(tcint,tcnum,tctest) var v : @a::(tcint,tcnum,tctest) var c - assert(base <= 36, "Base for parsing values is too big") v = 0 while s.len != 0 (c, s) = striter(s) @@ -56,9 +57,9 @@ generic doparse = {s, isneg, base -> @a::(tcint,tcnum,tctest) ;; if isneg - -> v - else -> -v + else + -> v ;; } @@ -74,7 +75,7 @@ generic charval = {c, base -> @a :: (tcint,tcnum,tctest) ;; if v < 0 || v > (base castto(@a::(tcint,tcnum,tctest))) - die("Character out of range for base when parsing integer") + fatal(1, "Character %c out of range", c) ;; -> v } |