From 967be9e750221ab2ab783f95df79bb26d290a45e Mon Sep 17 00:00:00 2001 From: Martial Simon Date: Mon, 15 Sep 2025 01:07:58 +0200 Subject: add: added projects --- tiger-compiler/lib/misc/error.cc | 107 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 tiger-compiler/lib/misc/error.cc (limited to 'tiger-compiler/lib/misc/error.cc') diff --git a/tiger-compiler/lib/misc/error.cc b/tiger-compiler/lib/misc/error.cc new file mode 100644 index 0000000..565760b --- /dev/null +++ b/tiger-compiler/lib/misc/error.cc @@ -0,0 +1,107 @@ +/** + ** \file misc/error.cc + ** \brief Implement error. + */ + +#include +#include + +#include +#include + +namespace misc +{ + /*--------. + | error. | + `--------*/ + + error::error() + : status_(error_type::success) + {} + + error::error(const error& e) { *this = e; } + + error& error::operator=(const error& rhs) + { + status_ = rhs.status_get(); + stream_.str(rhs.stream_get().str()); + return *this; + } + + /*----------------------------. + | Filling the error handler. | + `----------------------------*/ + + error& error::operator<<(error_type e) + { + auto status_value = static_cast(status_); + auto e_value = static_cast(e); + if ((e_value && e_value < status_value) || (!status_value)) + status_ = e; + return *this; + } + + // Import errors. + error& error::operator<<(const error& rhs) + { + *this << rhs.status_get() << rhs.stream_get().str(); + return *this; + } + + error& error::operator<<(std::ostream& (*f)(std::ostream&)) + { + stream_ << f; + return *this; + } + + /*---------------. + | Manipulators. | + `---------------*/ + + void error::exit() const { throw *this; } + + void error::exit_on_error() const + { + if (static_cast(status_)) + exit(); + } + + void error::ice(const char* file, int line) const + { + std::cerr << stream_.str(); + __Terminate(file, line, "Internal Compiler error"); + } + + void error::ice_on_error(const char* file, int line) const + { + if (static_cast(status_)) + ice(file, line); + } + + void error::clear() + { + status_ = error_type::success; + stream_.clear(); + } + + /*------------. + | Accessors. | + `------------*/ + + error::operator bool() const { return status_ != error_type::success; } + + error::error_type error::status_get() const { return status_; } + + unsigned error::status_get_value() const + { + return static_cast(status_); + } + + const std::ostringstream& error::stream_get() const { return stream_; } + + std::ostream& operator<<(std::ostream& o, const error& e) + { + return o << e.stream_get().str(); + } + +} // namespace misc -- cgit v1.2.3