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.
This commit is contained in:
Kartik K. Agaram 2017-06-10 14:36:38 -07:00
parent f0f077661c
commit 909adb27f9
7 changed files with 27 additions and 24 deletions

View File

@ -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

View File

@ -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) {

View File

@ -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

View File

@ -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)
}

View File

@ -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")

View File

@ -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);

View File

@ -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?