Skip to content

Instantly share code, notes, and snippets.

@potetisensei
Created October 27, 2013 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save potetisensei/7184271 to your computer and use it in GitHub Desktop.
Save potetisensei/7184271 to your computer and use it in GitHub Desktop.
動くか知らん
#include <setjmp.h>
#include "stack.h"
#include "queue.h"
#include "template.h"
#define _tmp_var(name) \
__tmp_var(name, __LINE__)
#define __tmp_var(name, line) \
___tmp_var(name, line)
#define ___tmp_var(name, line) \
_ ## name ## _ ## line
enum ExceptionID { SUCCESS = 0, NULL_POINTER_EXCEPTION, MEMORY_EXCEPTION, EMPTY_EXCEPTION};
struct _exception_object {
ExceptionID id;
char *message;
};
typedef struct _exception_object* ExceptionObject;
ExceptionObject GlobalExceptionInMemoryCrisis;
UsingQueue(ExceptionObject);
UsingStack(jmp_buf);
Queue(ExceptionObject) GlobalExceptionQueue;
Stack(jmp_buf) GlobalTryContextStack;
char set_exception(ExceptionObject obj, ExceptionID id, char *message) {
obj = malloc(sizeof(struct _exception_object));
if (obj == NULL) {
GlobalExceptionInMemoryCrisis->id = MEMORY_EXCEPTION;
GlobalExceptionInMemoryCrisis->message = "[*]Error: Out of memory.";
return -1;
}
obj->id = id;
obj->message = message;
return register_alloc(obj, NULL);
}
#define try \
char _tmp_var(will_eval) = 1;\
jmp_buf _tmp_var(try_context);\
\
if (setjmp(_tmp_var(try_context)) == SUCCESS) {\
if (invoke(Stack, jmp_buf, push(&GlobalTryContextStack, _tmp_var(try_context) ) ) == -1) {\
_tmp_var(will_eval) = 0;\
}\
} else {\
_tmp_var(will_eval) = 0;\
}\
\
if (_tmp_var(will_eval))
#define catch(exception_id) \
char _tmp_var(will_eval) = 1;\
\
if (invoke(Queue, ExceptionObject, empty(&GlobalExceptionQueue)) == 1) {\
_tmp_var(will_eval) = 0;\
} else {\
ExceptionObject _tmp_var(caused);\
\
invoke(Queue, ExceptionObject, top(&GlobalExceptionQueue, &_tmp_var(caused)) );\
if (_tmp_var(caused)->id == exception_id) {\
invoke(Queue, ExceptionObject, pop(&GlobalExceptionQueue) );\
} else {\
_tmp_var(will_eval) = 0;\
}\
}\
\
if (_tmp_var(will_eval))
void NULLPointerException() {
ExceptionObject nullpo;
if (set_exception(nullpo, NULL_POINTER_EXCEPTION, "[*]Error: NULL pointer exception.") == -1) {
invoke(Queue, ExceptionObject, push(&GlobalExceptionQueue, GlobalExceptionInMemoryCrisis));
} else {
invoke(Queue, ExceptionObject, push(&GlobalExceptionQueue, nullpo));
}
if (invoke(Stack, jmp_buf, empty(&GlobalTryContextStack)) == 0) {
jmp_buf context;
invoke(Stack, jmp_buf, top(&GlobalTryContextStack), &context);
longjmp(context, NULL_POINTER_EXCEPTION);
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment