diff options
author | Ori Bernstein <ori@eigenstate.org> | 2013-08-07 17:26:38 -0400 |
---|---|---|
committer | Ori Bernstein <ori@eigenstate.org> | 2013-08-07 17:26:38 -0400 |
commit | 7ea39f801622858865e2fb06768a9f44f81b5bf8 (patch) | |
tree | f2583bc784c4e76d513e832d050d3a33ddccae3f /libstd/intparse.myr | |
parent | c2cd468bc6ebefa681b47472e2a2df2fbbf0fe55 (diff) | |
download | mc-7ea39f801622858865e2fb06768a9f44f81b5bf8.tar.gz |
Add support for negative numbers when parsing integers.
Diffstat (limited to 'libstd/intparse.myr')
-rw-r--r-- | libstd/intparse.myr | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/libstd/intparse.myr b/libstd/intparse.myr index 2e04f42..29c519a 100644 --- a/libstd/intparse.myr +++ b/libstd/intparse.myr @@ -12,18 +12,38 @@ pkg std = ;; generic intparse = {s + var isneg + + isneg = 0 + if hasprefix(s, "-") + s = s[1:] + isneg = 1 + ;; + if hasprefix(s, "0x") - -> intparsebase(s[2:], 16) + -> doparse(s[2:], isneg, 16) elif hasprefix(s, "0o") - -> intparsebase(s[2:], 8) + -> doparse(s[2:], isneg, 8) elif hasprefix(s, "0b") - -> intparsebase(s[2:], 2) + -> doparse(s[2:], isneg, 2) else - -> intparsebase(s[2:], 10) + -> doparse(s[2:], isneg, 10) ;; } generic intparsebase = {s, base -> @a::(tcint,tcnum,tctest) + var isneg + + isneg = 0 + if hasprefix(s, "-") + s = s[1:] + isneg = 1 + ;; + + -> doparse(s, isneg, base) +} + +generic doparse = {s, isneg, base -> @a::(tcint,tcnum,tctest) var v : @a::(tcint,tcnum,tctest) var c @@ -34,7 +54,12 @@ generic intparsebase = {s, base -> @a::(tcint,tcnum,tctest) v *= base castto(@a::(tcint,tcnum,tctest)) v += charval(c, base) ;; - -> v + + if isneg + -> v + else + -> -v + ;; } generic charval = {c, base -> @a :: (tcint,tcnum,tctest) |