This repository has been archived by the owner on Jul 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathterminate.hpp
59 lines (44 loc) · 1.67 KB
/
terminate.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2023 The Jule Programming Language.
// Use of this source code is governed by a BSD 3-Clause
// license that can be found in the LICENSE file.
#ifndef __JULE_TERMINATE_HPP
#define __JULE_TERMINATE_HPP
#include <cstdlib>
#include "trait.hpp"
#include "types.hpp"
#include "str.hpp"
#include "error.hpp"
#include "builtin.hpp"
namespace jule {
// Error mask for terminations.
// It's also built-in Error trait.
struct Error {
virtual jule::Str error(void) { return jule::Str(); }
virtual ~Error(void) noexcept {}
jule::Bool operator==(const Error&) { return false; }
jule::Bool operator!=(const Error &src) { return !this->operator==(src); }
friend std::ostream &operator<<(std::ostream &stream, Error error) noexcept
{ return stream << error.error(); }
};
// JuleC terminate handler.
void terminate_handler(void) noexcept;
jule::Trait<Error> exception_to_error(const jule::Exception &exception);
void terminate_handler(void) noexcept {
try { std::rethrow_exception(std::current_exception()); }
catch (const jule::Exception &e) {
jule::outln(std::string("panic: ") + std::string(e.what()));
std::exit(jule::EXIT_PANIC);
}
}
jule::Trait<Error> exception_to_error(const jule::Exception &exception) {
struct PanicError: public Error {
jule::Str message;
jule::Str error(void)
{ return this->message; }
};
struct PanicError error;
error.message = jule::to_str(exception.what());
return jule::Trait<Error>(error);
}
} // namespace jule
#endif // #ifndef __JULE_TERMINATE_HPP