about summary refs log tree commit diff stats
path: root/subx/011run.cc
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2018-08-08 23:13:37 -0700
committerKartik Agaram <vc@akkartik.com>2018-08-08 23:13:37 -0700
commit87f72beedeb8fbce95e0b9ac961eb4cb2ca99bcf (patch)
treede4a3d2bb3adbee9015d3ea958ca54fcdc3e7cc8 /subx/011run.cc
parentf8bd7ed527c600a5c022b039966c508d3adb0bc2 (diff)
downloadmu-87f72beedeb8fbce95e0b9ac961eb4cb2ca99bcf.tar.gz
4495 - nail down a few more error states
It would be confusing to use negative numbers in raw hex. But we'll rely
on programmer taste there.
Diffstat (limited to 'subx/011run.cc')
-rw-r--r--subx/011run.cc31
1 files changed, 22 insertions, 9 deletions
diff --git a/subx/011run.cc b/subx/011run.cc
index 3a57f055..6849daad 100644
--- a/subx/011run.cc
+++ b/subx/011run.cc
@@ -206,10 +206,6 @@ void load(const program& p) {
       const line& l = seg.lines.at(j);
       for (int k = 0;  k < SIZE(l.words);  ++k) {
         const word& w = l.words.at(k);
-        if (SIZE(w.data) != 2) {
-          raise << "token '" << w.data << "' is not a hex byte\n" << end();
-          return;
-        }
         uint8_t val = hex_byte(w.data);
         if (trace_contains_errors()) return;
         write_mem_u8(addr, val);
@@ -227,24 +223,41 @@ uint8_t hex_byte(const string& s) {
   istringstream in(s);
   int result = 0;
   in >> std::hex >> result;
-  if (!in) {
-    raise << "invalid hex " << s << '\n' << end();
+  if (!in || !in.eof()) {
+    raise << "token '" << s << "' is not a hex byte\n" << end();
     return '\0';
   }
-  if (result > 0xff) {
-    raise << "invalid hex byte " << std::hex << result << '\n' << end();
+  if (result > 0xff || result < -0x8f) {
+    raise << "token '" << s << "' is not a hex byte\n" << end();
     return '\0';
   }
   return static_cast<uint8_t>(result);
 }
 
 :(scenarios parse_and_load)
-:(scenario load_error)
+:(scenario number_too_large)
 % Hide_errors = true;
 == 0x1
 05 cab
 +error: token 'cab' is not a hex byte
 
+:(scenario invalid_hex)
+% Hide_errors = true;
+== 0x1
+05 cx
++error: token 'cx' is not a hex byte
+
+:(scenario negative_number)
+== 0x1
+05 -12
+$error: 0
+
+:(scenario negative_number_too_small)
+% Hide_errors = true;
+== 0x1
+05 -12345
++error: token '-12345' is not a hex byte
+
 //: helper for tests
 :(code)
 void parse_and_load(const string& text_bytes) {