diff options
author | Kartik Agaram <vc@akkartik.com> | 2019-07-25 11:44:25 -0700 |
---|---|---|
committer | Kartik Agaram <vc@akkartik.com> | 2019-07-25 11:44:25 -0700 |
commit | 08b2b92fd2e90c59a5c0a8112f824c5d361d7c22 (patch) | |
tree | 2b49e691fdf6d4b9b43abbda0cd6125c99df93ee /subx/030---operands.cc | |
parent | b6d51014e7aaaef5bd4cf3e1a78040f77e676df5 (diff) | |
download | mu-08b2b92fd2e90c59a5c0a8112f824c5d361d7c22.tar.gz |
5474
Diffstat (limited to 'subx/030---operands.cc')
-rw-r--r-- | subx/030---operands.cc | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/subx/030---operands.cc b/subx/030---operands.cc index 5e6d6432..ec621017 100644 --- a/subx/030---operands.cc +++ b/subx/030---operands.cc @@ -489,3 +489,34 @@ string to_string(const line& inst) { } return out.str(); } + +int32_t parse_int(const string& s) { + if (s.empty()) return 0; + istringstream in(s); + 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 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")); +} |