Files
except/include/except.h

104 lines
3.2 KiB
C
Raw Normal View History

2026-02-15 10:45:05 -06:00
#ifndef EXCEPT_INCLUDED
#define EXCEPT_INCLUDED
#include <setjmp.h>
struct Exception {
const char *reason;
};
typedef struct Exception Exception;
struct Except_Frame {
struct Except_Frame *prev;
jmp_buf env;
const char *file;
int line;
const struct Exception *exception;
};
typedef struct Except_Frame Except_Frame;
enum {
EXCEPT_STATE_ENTERED=0,
EXCEPT_STATE_RAISED,
EXCEPT_STATE_HANDLED,
EXCEPT_STATE_FINALIZED,
EXCEPT_STATE_COUNT };
extern struct Except_Frame *except_stack;
extern const struct Exception assert_failed;
void except_raise(const struct Exception *e, const char *file, int line);
// Try Catch MACROS - LINUX
#define RAISE(e) except_raise(&(e), __FILE__, __LINE__)
2026-02-15 11:49:14 -06:00
#define RERAISE except_raise(except_frame.exception, \
except_frame.file, except_frame.line)
2026-02-15 11:53:12 -06:00
#define RETURN switch (except_stack = except_stack->prev,0) default: return
2026-02-15 10:45:05 -06:00
#define TRY do { \
2026-02-15 11:53:12 -06:00
volatile int except_flag; \
2026-02-15 11:49:14 -06:00
Except_Frame except_frame; \
2026-02-15 11:53:12 -06:00
except_frame.prev = except_stack; \
except_stack = &except_frame; \
except_flag = setjmp(except_frame.env); \
if (except_flag == EXCEPT_STATE_ENTERED) {
2026-02-15 10:45:05 -06:00
#define EXCEPT(e) \
2026-02-15 11:53:12 -06:00
if (except_flag == EXCEPT_STATE_ENTERED) except_stack = except_stack->prev; \
2026-02-15 11:49:14 -06:00
} else if (except_frame.exception == &(e)) { \
2026-02-15 11:53:12 -06:00
except_flag = EXCEPT_STATE_HANDLED;
2026-02-15 10:45:05 -06:00
#define ELSE \
2026-02-15 11:53:12 -06:00
if (except_flag == EXCEPT_STATE_ENTERED) except_stack = except_stack->prev; \
2026-02-15 10:45:05 -06:00
} else { \
2026-02-15 11:53:12 -06:00
except_flag = EXCEPT_STATE_HANDLED;
2026-02-15 10:45:05 -06:00
#define FINALLY \
2026-02-15 11:53:12 -06:00
if (except_flag == EXCEPT_STATE_ENTERED) except_stack = except_stack->prev; \
2026-02-15 10:45:05 -06:00
} { \
2026-02-15 11:53:12 -06:00
if (except_flag == EXCEPT_STATE_ENTERED) \
except_flag = EXCEPT_STATE_FINALIZED;
2026-02-15 10:45:05 -06:00
#define END_TRY \
2026-02-15 11:53:12 -06:00
if (except_flag == EXCEPT_STATE_ENTERED) except_stack = except_stack->prev; \
} if (except_flag == EXCEPT_STATE_RAISED) RERAISE; \
2026-02-15 10:45:05 -06:00
} while (0)
// Try Catch MACROS - WIN32
#ifdef WIN32
#include <windows.h>
extern int except_index;
extern void except_init(void);
extern void except_push(Except_Frame *fp);
extern void except_pop(void);
#define RAISE(e) except_raise(&(e), __FILE__, __LINE__)
2026-02-15 11:49:14 -06:00
#define RERAISE except_raise(except_frame.exception, \
except_frame.file, except_frame.line)
2026-02-15 11:53:12 -06:00
#define RETURN switch (except_pop(),0) default: return
2026-02-15 10:45:05 -06:00
#define TRY do { \
2026-02-15 11:53:12 -06:00
volatile int except_flag; \
2026-02-15 11:49:14 -06:00
Except_Frame except_frame; \
2026-02-15 11:53:12 -06:00
if (except_index == -1) \
except_init(); \
except_push(&except_frame); \
except_flag = setjmp(except_frame.env); \
if (except_flag == EXCEPT_STATE_ENTERED) {
2026-02-15 10:45:05 -06:00
#define EXCEPT(e) \
2026-02-15 11:53:12 -06:00
if (except_flag == EXCEPT_STATE_ENTERED) except_pop(); \
2026-02-15 11:49:14 -06:00
} else if (except_frame.exception == &(e)) { \
2026-02-15 11:53:12 -06:00
except_flag = EXCEPT_STATE_HANDLED;
2026-02-15 10:45:05 -06:00
#define ELSE \
2026-02-15 11:53:12 -06:00
if (except_flag == EXCEPT_STATE_ENTERED) except_pop(); \
2026-02-15 10:45:05 -06:00
} else { \
2026-02-15 11:53:12 -06:00
except_flag = EXCEPT_STATE_HANDLED;
2026-02-15 10:45:05 -06:00
#define FINALLY \
2026-02-15 11:53:12 -06:00
if (except_flag == EXCEPT_STATE_ENTERED) except_pop(); \
2026-02-15 10:45:05 -06:00
} { \
2026-02-15 11:53:12 -06:00
if (except_flag == EXCEPT_STATE_ENTERED) \
except_flag = EXCEPT_STATE_FINALIZED;
2026-02-15 10:45:05 -06:00
#define END_TRY \
2026-02-15 11:53:12 -06:00
if (except_flag == EXCEPT_STATE_ENTERED) except_pop(); \
} if (except_flag == EXCEPT_STATE_RAISED) RERAISE; \
2026-02-15 10:45:05 -06:00
} while (0)
#endif
#endif // except.h