feat: error handling, better prelude, double linked lists usage etc
This commit is contained in:
53
include/prelude/error.hpp
Normal file
53
include/prelude/error.hpp
Normal file
@@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
#include "prelude/string.hpp"
|
||||
#include <print>
|
||||
|
||||
struct Error
|
||||
{
|
||||
enum class Type
|
||||
{
|
||||
PARSE = 0,
|
||||
TYPE,
|
||||
COMPILE,
|
||||
COUNT_ERROR_TYPES,
|
||||
};
|
||||
|
||||
Type type;
|
||||
StringView message;
|
||||
StringView filename;
|
||||
long line = 0;
|
||||
long offset = 0;
|
||||
public:
|
||||
static Error CompileError(StringView filename, StringView message, long line = 0, long offset = 0)
|
||||
{
|
||||
return Error{Type::COMPILE, message, filename, line, offset};
|
||||
}
|
||||
|
||||
static Error ParseError(StringView filename, StringView message, long line = 0, long offset = 0)
|
||||
{
|
||||
return Error{Type::PARSE, message, filename, line, offset};
|
||||
}
|
||||
|
||||
static Error TypeError(StringView filename, StringView message, long line = 0, long offset = 0)
|
||||
{
|
||||
return Error{Type::TYPE, message, filename, line, offset};
|
||||
}
|
||||
};
|
||||
|
||||
constexpr const char* ErrorTypeNames[] = {
|
||||
"ParseError",
|
||||
"TypeError",
|
||||
"CompileError",
|
||||
};
|
||||
|
||||
class ErrorLogger
|
||||
{
|
||||
public:
|
||||
static void Raise(const Error &error)
|
||||
{
|
||||
StringBuilder sb;
|
||||
sb.AppendFormat("%s:%zu:%zu %s: %s", error.filename.c_str(), error.line, error.offset, ErrorTypeNames[static_cast<size_t>(error.type)], error.message.c_str());
|
||||
std::println("{}", sb.c_str());
|
||||
std::exit(1);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user