Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ project(pxkorka
)


set(CMAKE_CXX_STANDARD 26)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

Expand Down
3 changes: 2 additions & 1 deletion include/korka/compiler/compiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,8 @@ struct unique_type{};
};

if constexpr (not expected()) {
report_error<[]{return expected.error();}>();
report_error<[]{return expected().error();}>();
return expected().error();
} else {
return compilation_result_to_const<[] constexpr { return expected().value(); }>();
}
Expand Down
15 changes: 15 additions & 0 deletions include/korka/shared/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,23 @@ namespace korka {
struct redeclaration {
std::string_view identifier;
};

constexpr auto report(const redeclaration &err) -> std::string {
return korka::format("Compiler Error: ~ was redeclared", err.identifier);
}

struct unknown_type {
std::string_view identifier;
};

constexpr auto report(const unknown_type &err) -> std::string {
return korka::format("Compiler Error: unknown type `~`", err.identifier);
}

struct undefined_symbol {
std::string_view identifier;
};

constexpr auto report(const undefined_symbol &err) -> std::string {
return korka::format("Compiler Error: symbol `~` not defined", err.identifier);
}
Expand All @@ -72,6 +75,7 @@ namespace korka {
std::string_view return_type;
std::string_view actual_type;
};

constexpr auto report(const function_return_type_mismatch &err) -> std::string {
return korka::format("Compiler Error: expected ~ type to be returned, got ~", err.return_type, err.actual_type);
}
Expand Down Expand Up @@ -110,8 +114,19 @@ namespace korka {
}, err);
}

template<const_string Msg>
struct ErrorMessage {
static_assert(false, "Check the template parameter for details");
};

template<auto err_getter>
consteval auto report_error() -> void {
// Idk, __cpp_static_assert check is not enough for clang
#if __cplusplus >= 202400L && __cpp_static_assert >= 202306L
static_assert(false, to_string(err_getter()));
#else
constexpr auto msg = const_string_from_string_view<[]{return to_string(err_getter());}>();
std::ignore = ErrorMessage<msg>{};
#endif
}
}
30 changes: 28 additions & 2 deletions include/korka/utils/const_format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,32 @@

namespace korka {
namespace detail {
// // Custom std::to_chars, since it's not constexpr in C++ 20
// template<typename T>
// constexpr auto internal_to_chars(char* first, char* last, T value) -> char* {
// if (value == 0) {
// *first = '0';
// return first + 1;
// }
//
// if constexpr (std::is_signed_v<T>) {
// if (value < 0) {
// *first++ = '-';
// // Handle potential overflow for minimum values
// unsigned long long v = static_cast<unsigned long long>(-(value + 1)) + 1;
// return internal_to_chars(first, last, v);
// }
// }
//
// char* start = first;
// while (value > 0) {
// *first++ = static_cast<char>('0' + (value % 10));
// value /= 10;
// }
// std::reverse(start, first);
// return first;
// }

constexpr auto to_string_helper(const auto &value) -> std::string {
using T = std::decay_t<decltype(value)>;

Expand All @@ -18,8 +44,8 @@ namespace korka {
return std::string(1, value);
} else if constexpr (requires { std::to_chars(std::declval<char *>(), std::declval<char *>(), value); }) {
std::array<char, 64> buffer{};
auto [ptr, ec] = std::to_chars(buffer.data(), buffer.data() + buffer.size(), value);
return std::string(buffer.data(), ptr);
auto [end, _] = std::to_chars(buffer.data(), buffer.data() + buffer.size(), value);
return std::string(buffer.data(), end );
} else {
return "?";
}
Expand Down
2 changes: 1 addition & 1 deletion include/korka/utils/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace korka {

template<auto sv_getter>
constexpr auto const_string_from_string_view() {
const_string<sv_getter().length> str;
const_string<sv_getter().length()> str;
std::copy_n(sv_getter().data(), str.length, str.value);
return str;
}
Expand Down
2 changes: 1 addition & 1 deletion include/korka/utils/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace korka {
template<auto data_getter>
constexpr auto to_array() {
using value_type = typename decltype(data_getter())::value_type;
constexpr static std::size_t size = data_getter().size();
constexpr std::size_t size = data_getter().size();

std::array<value_type, size> out;
auto in = data_getter();
Expand Down