Separate errors to own header-file

This commit is contained in:
Sofia 2026-04-13 00:24:18 +03:00
parent 85644457d0
commit 5706a81098
5 changed files with 39 additions and 11 deletions

View File

@ -1,6 +1,7 @@
#include "codegen.h" #include "codegen.h"
#include "ast.h" #include "ast.h"
#include "types.h" #include "types.h"
#include "errors.h"
#include <llvm/IR/Module.h> #include <llvm/IR/Module.h>
#include <llvm/IR/Verifier.h> #include <llvm/IR/Verifier.h>
@ -53,7 +54,7 @@ namespace AST {
} }
} }
else { else {
throw codegen::Error("Value " + this->m_name + " not found", this->m_meta); throw CompileError("Value " + this->m_name + " not found", this->m_meta);
} }
} }
@ -87,11 +88,11 @@ namespace AST {
std::make_shared<types::FundamentalType>(types::FundamentalTypeKind::Bool), std::make_shared<types::FundamentalType>(types::FundamentalTypeKind::Bool),
}; };
default: default:
throw codegen::Error("invalid binop", this->m_meta); throw CompileError("invalid binop", this->m_meta);
} }
} }
catch (std::runtime_error& error) { catch (std::runtime_error& error) {
throw codegen::Error(error.what(), this->m_meta); throw CompileError(error.what(), this->m_meta);
} }
} }

View File

@ -11,13 +11,6 @@
#include "tokens.h" #include "tokens.h"
namespace codegen { namespace codegen {
class Error : public std::runtime_error {
public:
token::Metadata m_meta;
Error(std::string text, token::Metadata meta)
: std::runtime_error{ text }, m_meta{ meta } {}
};
struct StackValue { struct StackValue {
llvm::Value* value = nullptr; llvm::Value* value = nullptr;
std::shared_ptr<types::Type> ty = nullptr; std::shared_ptr<types::Type> ty = nullptr;

15
src/errors.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef ERRORS_H
#define ERRORS_H
#include <memory>
#include "tokens.h"
class CompileError : public std::runtime_error {
public:
token::Metadata m_meta;
CompileError(std::string text, token::Metadata meta)
: std::runtime_error{ text }, m_meta{ meta } {}
};
#endif

View File

@ -19,6 +19,7 @@
#include "tokens.h" #include "tokens.h"
#include "parsing.h" #include "parsing.h"
#include "errors.h"
std::string read_file(std::string_view filepath) { std::string read_file(std::string_view filepath) {
std::ifstream input{ std::string{filepath} }; std::ifstream input{ std::string{filepath} };
@ -103,7 +104,7 @@ std::optional<CompileOutput> compile(std::string_view in_filename) {
tls->codegen(builder, scope); tls->codegen(builder, scope);
} }
} }
catch (codegen::Error& error) { catch (CompileError& error) {
std::cerr << "FATAL: " << error.what() << std::endl; std::cerr << "FATAL: " << error.what() << std::endl;
std::cerr << " at " << error.m_meta.start.line + 1 << ":" << error.m_meta.start.col + 1; std::cerr << " at " << error.m_meta.start.line + 1 << ":" << error.m_meta.start.col + 1;
std::cerr << " to " << error.m_meta.end.line + 1 << ":" << error.m_meta.end.col + 1 << std::endl; std::cerr << " to " << error.m_meta.end.line + 1 << ":" << error.m_meta.end.col + 1 << std::endl;

18
src/typechecker.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef TYPECHECK_H
#define TYPECHECK_H
#include <map>
#include "types.h"
namespace typecheck {
struct Scope {
std::map<std::string, types::Type> symbols;
};
struct State {
};
}
#endif