From 5706a8109873c001cae11c42b6d98a2d883b8109 Mon Sep 17 00:00:00 2001 From: Sofia Date: Mon, 13 Apr 2026 00:24:18 +0300 Subject: [PATCH] Separate errors to own header-file --- src/codegen.cpp | 7 ++++--- src/codegen.h | 7 ------- src/errors.h | 15 +++++++++++++++ src/main.cpp | 3 ++- src/typechecker.h | 18 ++++++++++++++++++ 5 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 src/errors.h create mode 100644 src/typechecker.h diff --git a/src/codegen.cpp b/src/codegen.cpp index 844f7e8..4ea381a 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -1,6 +1,7 @@ #include "codegen.h" #include "ast.h" #include "types.h" +#include "errors.h" #include #include @@ -53,7 +54,7 @@ namespace AST { } } 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::FundamentalTypeKind::Bool), }; default: - throw codegen::Error("invalid binop", this->m_meta); + throw CompileError("invalid binop", this->m_meta); } } catch (std::runtime_error& error) { - throw codegen::Error(error.what(), this->m_meta); + throw CompileError(error.what(), this->m_meta); } } diff --git a/src/codegen.h b/src/codegen.h index c5eb939..84552f8 100644 --- a/src/codegen.h +++ b/src/codegen.h @@ -11,13 +11,6 @@ #include "tokens.h" 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 { llvm::Value* value = nullptr; std::shared_ptr ty = nullptr; diff --git a/src/errors.h b/src/errors.h new file mode 100644 index 0000000..9a3a46a --- /dev/null +++ b/src/errors.h @@ -0,0 +1,15 @@ +#ifndef ERRORS_H +#define ERRORS_H + +#include + +#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 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index fe5f909..5534955 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,6 +19,7 @@ #include "tokens.h" #include "parsing.h" +#include "errors.h" std::string read_file(std::string_view filepath) { std::ifstream input{ std::string{filepath} }; @@ -103,7 +104,7 @@ std::optional compile(std::string_view in_filename) { tls->codegen(builder, scope); } } - catch (codegen::Error& error) { + catch (CompileError& error) { 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; diff --git a/src/typechecker.h b/src/typechecker.h new file mode 100644 index 0000000..5319f25 --- /dev/null +++ b/src/typechecker.h @@ -0,0 +1,18 @@ +#ifndef TYPECHECK_H +#define TYPECHECK_H + +#include + +#include "types.h" + +namespace typecheck { + struct Scope { + std::map symbols; + }; + + struct State { + + }; +} + +#endif \ No newline at end of file