about summary refs log tree commit diff stats
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
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.
-rw-r--r--subx/003trace.cc1
-rw-r--r--subx/011run.cc31
2 files changed, 23 insertions, 9 deletions
diff --git a/subx/003trace.cc b/subx/003trace.cc
index 81594a7d..b442cbdf 100644
--- a/subx/003trace.cc
+++ b/subx/003trace.cc
@@ -204,6 +204,7 @@ Hide_warnings = true;
 // If we aren't yet sure how to deal with some corner case, use assert_for_now
 // to indicate that it isn't an inviolable invariant.
 #define assert_for_now assert
+#define raise_for_now raise
 
 // Inside tests, fail any tests that displayed (unexpected) errors.
 // Expected errors in tests should always be hidden and silently checked for.
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) {