Skip to content
Snippets Groups Projects
Verified Commit a98da705 authored by Kaelan Carlos's avatar Kaelan Carlos :egg:
Browse files

made error messages nicer

parent ee64f1ab
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -57,25 +57,52 @@ int main(int argc, char *argv[]) {
return EXIT_SUCCESS;
}
else if (flag == "-v"sv || flag == "--verbose"sv) {
verbose = true;
summary = true;
}
else if (flag == "-s"sv || flag == "--summary"sv) {
summary = true;
else if (flag == "--messages"sv) {
msgs::note(
"k32c",
"hello, world!",
" | This compiler is greeting the world.\n | Say 'hello' :)"
);
msgs::warn(
"k3.14c",
"you're dangerously awesome",
" | You are incredibly cool.\n | Keep being your amazing self."
);
msgs::error(
"k2.71c",
"i'm very grateful to you",
" | Thanks for using this small project of mine.\n | Have a cookie for your troubles 🍪\n (this is 100\% an error, what do you mean?)"
);
return EXIT_SUCCESS;
}
else if (flag == "-o"sv || flag == "--output") {
i++;
if (i == argc) {
msgs::error("missing filename after '\033[1m-o\033[0m'");
msgs::error(
"k32c",
"missing file name after '\033[1m" + flag + "\033[0m'",
" | The " + flag + " option is used to change the name of the output binary.\n | Please specify a name for the output binary."
);
}
out_file_name = argv[i];
}
else if (flag == "-s"sv || flag == "--summary"sv) {
summary = true;
}
else if (flag == "-v"sv || flag == "--verbose"sv) {
verbose = true;
summary = true;
}
else {
msgs::warn("unknown option '\033[1m" + flag + "\033[0m' has been ignored");
msgs::warn(
"k32c",
"unknown option '\033[1m" + flag + "\033[0m' has been ignored",
" | The " + flag + " option doesn't exist.\n | See all options with --help or -h."
);
}
}
......@@ -84,7 +111,10 @@ int main(int argc, char *argv[]) {
std::string arg(argv[i]);
if (in_file_name != ""sv)
msgs::warn("extraneous argument '\033[1m" + arg + "\033[0m' has been ignored");
msgs::warn(
"k32c",
"extraneous argument '\033[1m" + arg + "\033[0m' has been ignored",
" | There should only be one file name.\n | Check that every option is in the right place.");
in_file_name = arg;
}
......@@ -92,14 +122,22 @@ int main(int argc, char *argv[]) {
// Sanitise and validate file name
if (in_file_name == ""sv) {
msgs::fatal_error("no input files");
msgs::fatal_error(
"k32c",
"no input file",
" | This compiler is unable to compile nothing.\n | Please specify a file to compile."
);
} else if (!std::filesystem::exists(in_file_name)) {
msgs::fatal_error(in_file_name + ": No such file or directory");
msgs::fatal_error(
"k32c",
"unable to locate '" + in_file_name + "'",
" | This compiler is unable to compile nothing.\n | Please check the specified file name is correct."
);
}
if (summary) {
std::cout << in_file_name << " found, starting compilation." << std::endl;
std::cout << out_file_name << " will be the final binary." << std::endl;
std::cout << out_file_name << " will be the output binary." << std::endl;
}
return EXIT_SUCCESS;
......
......@@ -49,33 +49,46 @@ std::string help = R"(Usage: k32c [options] file...
Options:
--copyright, or -c Display this program's copyright information.
--help, or -h Display this information.
--messages Display test messages.
--output, or -o Change the name of the output binary.
--summary, or -s Display summarised output during compilation.
--verbose, or -v Display more thorough output during compilation.)";
std::string warning_prefix = "\033[1mk32c: \033[35mwarning: \033[0m";
std::string error_prefix = "\033[1mk32c: \033[31merror: \033[0m";
std::string fatal_error_prefix = "\033[1mk32c: \033[31mfatal error: \033[0m";
std::string note_prefix = "\033[36mnote: \033[0m";
std::string warning_prefix = "\033[35mwarning: \033[0m";
std::string error_prefix = "\033[31merror: \033[0m";
std::string fatal_error_prefix = "\033[31mfatal error: \033[0m";
// HELPER FUNCTIONS
void warn(std::string error_msg) {
void note(std::string file, std::string summary, std::string message) {
std::cout << "\033[1m" << file << ": ";
std::cout << note_prefix;
std::cout << summary << std::endl;
std::cout << message << std::endl;
}
void warn(std::string file, std::string summary, std::string message) {
std::cout << "\033[1m" << file << ": ";
std::cout << warning_prefix;
std::cout << error_msg;
std::cout << std::endl;
std::cout << summary << std::endl;
std::cout << message << std::endl;
}
void error(std::string error_msg) {
void error(std::string file, std::string summary, std::string message) {
std::cout << "\033[1m" << file << ": ";
std::cout << error_prefix;
std::cout << error_msg;
std::cout << std::endl;
std::cout << summary << std::endl;
std::cout << message << std::endl;
std::cout << "Compilation terminated." << std::endl;
exit(EXIT_FAILURE);
}
void fatal_error(std::string error_msg) {
void fatal_error(std::string file, std::string summary, std::string message) {
std::cout << "\033[1m" << file << ": ";
std::cout << fatal_error_prefix;
std::cout << error_msg;
std::cout << std::endl;
std::cout << summary << std::endl;
std::cout << message << std::endl;
std::cout << "Compilation terminated." << std::endl;
exit(EXIT_FAILURE);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment