Support files with non empty last line

This commit is contained in:
augustin64 2024-01-12 16:33:05 +01:00
parent ac872a64c9
commit 2c0f8af34c
4 changed files with 18 additions and 14 deletions

View File

@ -76,7 +76,7 @@ void print_error_position(vector<string> history, CodePosition pos) {
}
void print_error_stack_trace(vector<string> 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)

View File

@ -8,6 +8,6 @@ using namespace std;
/*
Retrieves user input
*/
vector<string> get_input(vector<string> history);
vector<string> get_input(vector<string> history, bool &received_eof);
#endif

View File

@ -28,7 +28,7 @@ void show_prompt(int line_number) {
}
string get_line_with_hist(vector<string> history, int line_num) {
string get_line_with_hist(vector<string> history, int line_num, bool &received_eof) {
show_prompt(line_num);
string input;
@ -40,8 +40,8 @@ string get_line_with_hist(vector<string> 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<string> history, int line_num) {
}
vector<string> get_input(vector<string> input) {
vector<string> get_input(vector<string> 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] == ';') {

View File

@ -79,11 +79,12 @@ int main(int argc, char* argv[]) {
vector<string> 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;
}