Make slightly better error raporting at codegen

This commit is contained in:
Sofia 2026-04-11 00:07:29 +03:00
parent 6a315711cf
commit 344b7588d8
4 changed files with 45 additions and 29 deletions

View File

@ -41,7 +41,7 @@ namespace AST {
} }
} }
else { else {
throw std::runtime_error("Value " + this->m_name + " not found"); throw codegen::Error("Value " + this->m_name + " not found", this->m_meta);
} }
} }
@ -49,6 +49,7 @@ namespace AST {
auto lvalued = scope.with_lvalue(); auto lvalued = scope.with_lvalue();
auto lhs = this->m_lhs->codegen(builder, this->m_binop == types::BinOp::Assignment ? lvalued : scope); auto lhs = this->m_lhs->codegen(builder, this->m_binop == types::BinOp::Assignment ? lvalued : scope);
auto rhs = this->m_rhs->codegen(builder, scope); auto rhs = this->m_rhs->codegen(builder, scope);
try {
switch (this->m_binop) { switch (this->m_binop) {
case types::BinOp::Assignment: case types::BinOp::Assignment:
builder.builder->CreateStore(rhs.value, lhs.value, false); builder.builder->CreateStore(rhs.value, lhs.value, false);
@ -74,7 +75,11 @@ namespace AST {
std::make_shared<types::FundamentalType>(types::FundamentalTypeKind::Bool), std::make_shared<types::FundamentalType>(types::FundamentalTypeKind::Bool),
}; };
default: default:
throw std::runtime_error("invalid binop"); throw codegen::Error("invalid binop", this->m_meta);
}
}
catch (std::runtime_error& error) {
throw codegen::Error(error.what(), this->m_meta);
} }
} }

View File

@ -8,8 +8,16 @@
#include "builder.h" #include "builder.h"
#include "types.h" #include "types.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;

View File

@ -88,8 +88,10 @@ int main() {
tls->codegen(builder, scope); tls->codegen(builder, scope);
} }
} }
catch (std::runtime_error& error) { catch (codegen::Error& 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 << " to " << error.m_meta.end.line + 1 << ":" << error.m_meta.end.col + 1 << std::endl;
return 1; return 1;
} }

View File

@ -1,7 +1,8 @@
#include "types.h"
#include <sstream> #include <sstream>
#include "types.h"
namespace types { namespace types {
int operator_precedence(BinOp& op) { int operator_precedence(BinOp& op) {
switch (op) { switch (op) {