diff options
author | Kartik Agaram <vc@akkartik.com> | 2018-08-13 16:50:31 -0700 |
---|---|---|
committer | Kartik Agaram <vc@akkartik.com> | 2018-08-13 16:50:31 -0700 |
commit | 5e8ef005d8387b3b7977cf55e6032bd7b9052470 (patch) | |
tree | e1ffc4b889e920821d3bb2f30eb65909db39aaac /subx | |
parent | 31338623d1d44e422c04cf886429cfa0f479d947 (diff) | |
download | mu-5e8ef005d8387b3b7977cf55e6032bd7b9052470.tar.gz |
4518
Support both signed and unsigned numbers when parsing strings.
Diffstat (limited to 'subx')
-rw-r--r-- | subx/030---operands.cc | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/subx/030---operands.cc b/subx/030---operands.cc index 68a37324..3b3b286c 100644 --- a/subx/030---operands.cc +++ b/subx/030---operands.cc @@ -430,16 +430,37 @@ bool is_hex_int(const string& s) { } int32_t parse_int(const string& s) { + if (s.empty()) return 0; istringstream in(s); - int32_t result = 0; - in >> std::hex >> result; + in >> std::hex; + if (s.at(0) == '-') { + int32_t result = 0; + in >> result; + if (!in || !in.eof()) { + raise << "not a number: " << s << '\n' << end(); + return 0; + } + return result; + } + uint32_t uresult = 0; + in >> uresult; if (!in || !in.eof()) { raise << "not a number: " << s << '\n' << end(); return 0; } - return result; + return static_cast<int32_t>(uresult); +} +:(before "End Unit Tests") +void test_parse_int() { + CHECK_EQ(0, parse_int("0")); + CHECK_EQ(0, parse_int("0x0")); + CHECK_EQ(0, parse_int("0x0")); + CHECK_EQ(16, parse_int("10")); // hex always + CHECK_EQ(-1, parse_int("-1")); + CHECK_EQ(-1, parse_int("0xffffffff")); } +:(code) string to_string(const line& inst) { ostringstream out; for (int i = 0; i < SIZE(inst.words); ++i) { |