about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2017-06-10 14:36:38 -0700
committerKartik K. Agaram <vc@akkartik.com>2017-06-10 15:41:42 -0700
commit909adb27f9df2ba58e703562e3cf8f948fc58b56 (patch)
treed85bf7767b9d38479e66de96a4fddebd27e98ef7
parentf0f077661c1f5d587602343a1a0bc82f0c99cf59 (diff)
downloadmu-909adb27f9df2ba58e703562e3cf8f948fc58b56.tar.gz
3905
Standardize exit paths. Most layers now don't need to know about termbox.

We can't really use `assert` in console-mode apps; it can't just exit because
we want to be able to check assertion failures in tests.
-rw-r--r--003trace.cc2
-rw-r--r--030container.cc3
-rw-r--r--034address.cc3
-rw-r--r--036refcount.cc3
-rw-r--r--038new_text.cc3
-rw-r--r--080display.cc36
-rw-r--r--console.mu1
7 files changed, 27 insertions, 24 deletions
diff --git a/003trace.cc b/003trace.cc
index 7b046f89..eb4513d5 100644
--- a/003trace.cc
+++ b/003trace.cc
@@ -165,7 +165,7 @@ int Trace_errors = 0;  // used only when Trace_stream is NULL
 #define DUMP(label)  if (Trace_stream) cerr << Trace_stream->readable_contents(label);
 
 // Errors are a special layer.
-#define raise  (!Trace_stream ? (tb_shutdown(),++Trace_errors,cerr) /*do print*/ : Trace_stream->stream(Error_depth, "error"))
+#define raise  (!Trace_stream ? (++Trace_errors,cerr) /*do print*/ : Trace_stream->stream(Error_depth, "error"))
 // 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
diff --git a/030container.cc b/030container.cc
index b189435f..2e5da42e 100644
--- a/030container.cc
+++ b/030container.cc
@@ -248,9 +248,8 @@ container_metadata& get(vector<pair<type_tree*, container_metadata> >& all, cons
     if (matches(all.at(i).first, key))
       return all.at(i).second;
   }
-  tb_shutdown();
   raise << "unknown size for type '" << to_string(key) << "'\n" << end();
-  assert(false);
+  exit(1);
 }
 
 bool contains_key(const vector<pair<type_tree*, container_metadata> >& all, const type_tree* key) {
diff --git a/034address.cc b/034address.cc
index 65b97dc0..4b955050 100644
--- a/034address.cc
+++ b/034address.cc
@@ -367,9 +367,8 @@ int allocate(int size) {
 :(code)
 void ensure_space(int size) {
   if (size > Initial_memory_per_routine) {
-    tb_shutdown();
     cerr << "can't allocate " << size << " locations, that's too much compared to " << Initial_memory_per_routine << ".\n";
-    exit(0);
+    exit(1);
   }
   if (Current_routine->alloc + size > Current_routine->alloc_max) {
     // waste the remaining space and create a new chunk
diff --git a/036refcount.cc b/036refcount.cc
index d545e270..1b0a8d8c 100644
--- a/036refcount.cc
+++ b/036refcount.cc
@@ -77,7 +77,6 @@ void decrement_refcount(int old_address, const type_tree* payload_type, int payl
   --old_refcount;
   put(Memory, old_address, old_refcount);
   if (old_refcount < 0) {
-    tb_shutdown();
     cerr << "Negative refcount!!! " << old_address << ' ' << old_refcount << '\n';
     if (Trace_stream) {
       cerr << "Saving trace to last_trace.\n";
@@ -85,7 +84,7 @@ void decrement_refcount(int old_address, const type_tree* payload_type, int payl
       fout << Trace_stream->readable_contents("");
       fout.close();
     }
-    exit(0);
+    exit(1);
   }
   // End Decrement Refcount(old_address, payload_type, payload_size)
 }
diff --git a/038new_text.cc b/038new_text.cc
index cf7c01b5..7f71230d 100644
--- a/038new_text.cc
+++ b/038new_text.cc
@@ -186,15 +186,16 @@ case ASSERT: {
 :(before "End Primitive Recipe Implementations")
 case ASSERT: {
   if (!ingredients.at(0).at(0)) {
+    // Begin ASSERT in Run
     if (is_literal_text(current_instruction().ingredients.at(1)))
       raise << current_instruction().ingredients.at(1).name << '\n' << end();
     else
       raise << read_mu_text(ingredients.at(1).at(0)) << '\n' << end();
+    if (!Hide_errors) exit(1);
   }
   break;
 }
 
-
 //: 'cheating' by using the host system
 
 :(before "End Primitive Recipe Declarations")
diff --git a/080display.cc b/080display.cc
index ebde9130..14c2b2f2 100644
--- a/080display.cc
+++ b/080display.cc
@@ -41,11 +41,13 @@ case OPEN_CONSOLE: {
   Display_row = Display_column = 0;
   int width = tb_width();
   int height = tb_height();
-  if (width > 222 || height > 222) tb_shutdown();
-  if (width > 222)
-    raise << "sorry, Mu doesn't support windows wider than 222 characters in console mode. Please resize your window.\n" << end();
-  if (height > 222)
-    raise << "sorry, Mu doesn't support windows taller than 222 characters in console mode. Please resize your window.\n" << end();
+  if (width > 222 || height > 222) {
+    if (width > 222)
+      raise << "sorry, Mu doesn't support windows wider than 222 characters in console mode. Please resize your window.\n" << end();
+    if (height > 222)
+      raise << "sorry, Mu doesn't support windows taller than 222 characters in console mode. Please resize your window.\n" << end();
+    exit(1);
+  }
   break;
 }
 
@@ -59,13 +61,23 @@ case CLOSE_CONSOLE: {
 }
 :(before "End Primitive Recipe Implementations")
 case CLOSE_CONSOLE: {
-  tb_clear();
   tb_shutdown();
   break;
 }
 
-:(before "End Teardown")
-tb_shutdown();
+//: Automatically close the console in some situations.
+:(before "End One-time Setup")
+atexit(close_console_and_scroll_to_bottom);
+:(after "Begin ASSERT in Run")
+if (tb_is_active()) close_console_and_scroll_to_bottom();
+:(code)
+void close_console_and_scroll_to_bottom() {
+  if (!tb_is_active()) return;
+  // leave the screen in a relatively clean state
+  tb_set_cursor(tb_width()-1, tb_height()-1);
+  cout << "\r\n";
+  tb_shutdown();
+}
 
 :(before "End Primitive Recipe Declarations")
 CLEAR_DISPLAY,
@@ -358,13 +370,7 @@ case CHECK_FOR_INTERACTION: {
   // treat keys within ascii as unicode characters
   if (event_type == TB_EVENT_KEY && event.key < 0xff) {
     products.at(0).push_back(/*text event*/0);
-    if (event.key == TB_KEY_CTRL_C) {
-      // leave the screen in a relatively clean state
-      tb_set_cursor(tb_width()-1, tb_height()-1);
-      cout << "\r\n";
-      tb_shutdown();
-      exit(1);
-    }
+    if (event.key == TB_KEY_CTRL_C) exit(1);
     if (event.key == TB_KEY_BACKSPACE2) event.key = TB_KEY_BACKSPACE;
     if (event.key == TB_KEY_CARRIAGE_RETURN) event.key = TB_KEY_NEWLINE;
     products.at(0).push_back(event.key);
diff --git a/console.mu b/console.mu
index 062ca3b0..cc81c232 100644
--- a/console.mu
+++ b/console.mu
@@ -5,7 +5,6 @@
 def main [
   local-scope
   open-console
-  clear-screen 0/screen  # non-scrolling app
   {
     e:event, found?:bool <- check-for-interaction
     break-if found?