blog-static/code/compiler/13/error.cpp

42 lines
1.2 KiB
C++
Raw Permalink Normal View History

2020-09-08 18:38:05 -07:00
#include "error.hpp"
2020-09-11 21:29:49 -07:00
const char* compiler_error::what() const noexcept {
return "an error occured while compiling the program";
}
void compiler_error::print_about(std::ostream& to) {
to << what() << ": ";
to << description << std::endl;
}
void compiler_error::print_location(std::ostream& to, file_mgr& fm, bool highlight) {
2020-09-11 21:29:49 -07:00
if(!loc) return;
to << "occuring on line " << loc->begin.line << ":" << std::endl;
fm.print_location(to, *loc, highlight);
2020-09-11 21:29:49 -07:00
}
void compiler_error::pretty_print(std::ostream& to, file_mgr& fm) {
2020-09-11 21:29:49 -07:00
print_about(to);
print_location(to, fm);
2020-09-11 21:29:49 -07:00
}
2020-09-08 18:38:05 -07:00
const char* type_error::what() const noexcept {
return "an error occured while checking the types of the program";
}
2020-09-09 15:08:43 -07:00
void type_error::pretty_print(std::ostream& to, file_mgr& fm) {
2020-09-11 21:29:49 -07:00
print_about(to);
print_location(to, fm, true);
2020-09-09 15:08:43 -07:00
}
void unification_error::pretty_print(std::ostream& to, file_mgr& fm, type_mgr& mgr) {
type_error::pretty_print(to, fm);
2020-09-09 15:08:43 -07:00
to << "the expected type was:" << std::endl;
to << " \033[34m";
left->print(mgr, to);
to << std::endl << "\033[0mwhile the actual type was:" << std::endl;
to << " \033[32m";
right->print(mgr, to);
to << "\033[0m" << std::endl;
}