feat: error handling, better prelude, double linked lists usage etc

This commit is contained in:
2026-01-02 22:06:56 +01:00
parent a453603b9b
commit 6176d549c1
10 changed files with 148 additions and 65 deletions

53
include/prelude/error.hpp Normal file
View 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);
}
};