From 2c0f8af34cc3adcbadb02216a3c49ab2c72de78f Mon Sep 17 00:00:00 2001 From: augustin64 Date: Fri, 12 Jan 2024 16:33:05 +0100 Subject: [PATCH] Support files with non empty last line --- src/errors.cpp | 2 +- src/include/input.hpp | 2 +- src/input.cpp | 12 ++++++------ src/main.cpp | 16 ++++++++++------ 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/errors.cpp b/src/errors.cpp index 18a3be7..be96e10 100644 --- a/src/errors.cpp +++ b/src/errors.cpp @@ -76,7 +76,7 @@ void print_error_position(vector history, CodePosition pos) { } void print_error_stack_trace(vector history, const RuntimeError& error) { - cout << "\n" BOLD "Traceback" RESET " (most recent call last)" << endl; + cout << endl << BOLD "Traceback" RESET " (most recent call last)" << endl; for (StackTraceEntry e : error.trace) { cout << BOLD << setw(8) << setfill(' ') << to_string(get<1>(e).line+1) + ":" + to_string(get<1>(e).column+1) diff --git a/src/include/input.hpp b/src/include/input.hpp index 390b11b..c2e5684 100644 --- a/src/include/input.hpp +++ b/src/include/input.hpp @@ -8,6 +8,6 @@ using namespace std; /* Retrieves user input */ -vector get_input(vector history); +vector get_input(vector history, bool &received_eof); #endif \ No newline at end of file diff --git a/src/input.cpp b/src/input.cpp index d508e06..d58920a 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -28,7 +28,7 @@ void show_prompt(int line_number) { } -string get_line_with_hist(vector history, int line_num) { +string get_line_with_hist(vector history, int line_num, bool &received_eof) { show_prompt(line_num); string input; @@ -40,8 +40,8 @@ string get_line_with_hist(vector history, int line_num) { ch = silent_get_char(); if (ch == 4 || ch == EOF) { // 4 is End Of Transmission which can also be received - cout << "\rReceived EOF\033[K" << endl; - exit(0); + received_eof = true; + return input; } if (ch == 27) { // Special sequences @@ -131,11 +131,11 @@ string get_line_with_hist(vector history, int line_num) { } -vector get_input(vector input) { +vector get_input(vector input, bool &received_eof) { int line_num = input.size(); - while (1) { + while (!received_eof) { line_num++; - input.push_back(get_line_with_hist(input, line_num)); + input.push_back(get_line_with_hist(input, line_num, received_eof)); int n = input.back().length(); if (n >= 2 && input.back()[n-1] == ';' && input.back()[n-2] == ';') { diff --git a/src/main.cpp b/src/main.cpp index ee9eedd..cdb6163 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -79,11 +79,12 @@ int main(int argc, char* argv[]) { vector input; Memory memory; + bool received_eof = false; - while (true) { + while (!received_eof) { try { int initial_line = input.size(); - input = get_input(input); + input = get_input(input, received_eof); ExecArgs args = { print_tokens=print_tokens, @@ -108,19 +109,22 @@ int main(int argc, char* argv[]) { } catch (const SyntaxError& e) { print_error_position(input, e.pos); - cout << BOLD RED "Syntax Error: " RESET << e.get_message() << endl; + cout << BOLD RED "Syntax Error: " RESET << e.get_message() << endl << endl; } catch (const ControlError& e) { print_error_position(input, e.pos); - cout << BOLD RED "Control Error: " RESET << e.get_message() << endl; + cout << BOLD RED "Control Error: " RESET << e.get_message() << endl << endl; } catch (const TypeError& e) { print_error_position(input, e.pos); - cout << BOLD RED "Type Error: " RESET << e.get_message() << endl; + cout << BOLD RED "Type Error: " RESET << e.get_message() << endl << endl; } catch (const RuntimeError& e) { print_error_stack_trace(input, e); + cout << endl; } - cout << endl; } + + cout << "\rReceived EOF\033[K" << endl; + return 0; } \ No newline at end of file