mirror of
https://codeberg.org/ProgramSnail/lang.git
synced 2025-12-06 06:58:46 +00:00
part of type checker, type heck result type
This commit is contained in:
parent
48c9e200be
commit
17ff590048
13 changed files with 1022 additions and 321 deletions
74
include/error_log.hpp
Normal file
74
include/error_log.hpp
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
#pragma once
|
||||
|
||||
#include "basic_nodes.hpp"
|
||||
#include "basic_printers.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace error_handling {
|
||||
|
||||
enum class ErrorType {
|
||||
GENERAL,
|
||||
TYPE_CHECK,
|
||||
};
|
||||
|
||||
inline std::string to_string(ErrorType error_type) {
|
||||
switch (error_type) {
|
||||
case ErrorType::GENERAL:
|
||||
return "general error";
|
||||
case ErrorType::TYPE_CHECK:
|
||||
return "type check error";
|
||||
}
|
||||
}
|
||||
|
||||
class ErrorLog {
|
||||
public:
|
||||
struct ErrorMessage {
|
||||
public:
|
||||
ErrorMessage(nodes::Node node, const std::string &message,
|
||||
ErrorType error_type = ErrorType::GENERAL)
|
||||
: node_(node), message_(message), error_type_(error_type) {}
|
||||
|
||||
void print(printers::Printer &printer) {
|
||||
printer.print("Error at ");
|
||||
print_position(printer, node_.get_start_position(),
|
||||
node_.get_start_position());
|
||||
printer.print(" : ");
|
||||
printer.print(message_);
|
||||
printer.print(" (" + to_string(error_type_) + ").");
|
||||
}
|
||||
|
||||
private:
|
||||
inline void print_position(printers::Printer &printer,
|
||||
std::pair<size_t, size_t> start_position,
|
||||
std::pair<size_t, size_t> end_position) {
|
||||
printer.print("[" + std::to_string(start_position.first + 1) + ", " +
|
||||
std::to_string(start_position.second + 1) + "] - [" +
|
||||
std::to_string(end_position.first + 1) + ", " +
|
||||
std::to_string(end_position.second + 1) + "]");
|
||||
}
|
||||
|
||||
public:
|
||||
nodes::Node node_;
|
||||
std::string message_;
|
||||
ErrorType error_type_ = ErrorType::GENERAL;
|
||||
};
|
||||
|
||||
void add_error(ErrorMessage &&error) {
|
||||
messages_.push_back(std::move(error));
|
||||
}
|
||||
|
||||
void add_error(const ErrorMessage &error) { messages_.push_back(error); }
|
||||
|
||||
void print_all_errors(printers::Printer &printer) {
|
||||
for (auto &message : messages_) {
|
||||
message.print(printer);
|
||||
printer.new_indent_line();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<ErrorMessage> messages_;
|
||||
};
|
||||
|
||||
} // namespace error_handling
|
||||
Loading…
Add table
Add a link
Reference in a new issue