mirror of
https://github.com/sigmasternchen/crap-libs
synced 2025-03-15 07:38:56 +00:00
first version of try-catch
This commit is contained in:
parent
6562e34a27
commit
c3886fb0b4
4 changed files with 170 additions and 0 deletions
30
try/Makefile
Normal file
30
try/Makefile
Normal file
|
@ -0,0 +1,30 @@
|
|||
#
|
||||
# Makefile for crap-libs/try
|
||||
#
|
||||
# Author: overflowerror
|
||||
#
|
||||
|
||||
CC = gcc
|
||||
DEFS = -D_XOPEN_SOURCE=500 -D_BSD_SOURCE
|
||||
CFLAGS = -Wall -Wextra -g -std=c99 -pedantic -DENDEBUG $(DEFS) -fPIC
|
||||
LDFLAGS = $(DEFS)
|
||||
LIBFLAGS = -shared $(DEFS)
|
||||
|
||||
.PHONY: all clean
|
||||
|
||||
all: example libtry.so
|
||||
|
||||
%.o: %.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
example: example.o try.o
|
||||
$(CC) $(LDFLAGS) -o $@ $^
|
||||
|
||||
libtry.so: try.o
|
||||
$(CC) $(LIBFLAGS) -o $@ $^
|
||||
|
||||
try.o: try.c try.h
|
||||
example.o: example.c try.h
|
||||
|
||||
clean:
|
||||
rm -f *.o example libtry.so
|
54
try/example.c
Normal file
54
try/example.c
Normal file
|
@ -0,0 +1,54 @@
|
|||
#include "try.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
void function_with_throw(const char* e) {
|
||||
subtry();
|
||||
|
||||
throw(e);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
// simple try-catch
|
||||
try {
|
||||
throw("Hello World 1");
|
||||
} catch(const char* e) {
|
||||
printf("Catch 1: %s\n", e);
|
||||
} endtry;
|
||||
|
||||
// try-catch from function
|
||||
try {
|
||||
trycall function_with_throw("Hello World 2");
|
||||
} catch(const char* e) {
|
||||
printf("Catch 2: %s\n", e);
|
||||
} endtry;
|
||||
|
||||
// nested try-catch
|
||||
try {
|
||||
const char* string;
|
||||
|
||||
try {
|
||||
throw("Hello World 3");
|
||||
} catch(const char* e) {
|
||||
string = e;
|
||||
} endtry;
|
||||
|
||||
throw(string);
|
||||
} catch(const char* e) {
|
||||
printf("Catch 3: %s\n", e);
|
||||
} endtry;
|
||||
|
||||
// nested try-catch with throw in catch-clause
|
||||
try {
|
||||
tpush();
|
||||
try {
|
||||
throw("Hello World 4");
|
||||
} catch(const char* e) {
|
||||
cthrow(e);
|
||||
} endtry;
|
||||
} catch(const char* e) {
|
||||
printf("Catch 4: %s\n", e);
|
||||
} endtry;
|
||||
|
||||
return 0;
|
||||
}
|
60
try/try.c
Normal file
60
try/try.c
Normal file
|
@ -0,0 +1,60 @@
|
|||
#include "try.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
struct tryc {
|
||||
bool used;
|
||||
void* exception;
|
||||
};
|
||||
|
||||
#define MAX_TRY 10
|
||||
|
||||
int trynr = 0;
|
||||
|
||||
struct tryc trys[MAX_TRY] = {0};
|
||||
|
||||
int stack_position = -1;
|
||||
try_t stack[MAX_TRY] = {-1};
|
||||
|
||||
try_t find_free_id(void) {
|
||||
for (int i = 0; i < MAX_TRY; i++) {
|
||||
if (!trys[i].used)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
try_t try_new(void) {
|
||||
if (trynr >= MAX_TRY)
|
||||
return -1;
|
||||
try_t t = find_free_id();
|
||||
trys[t].used = true;
|
||||
trys[t].exception = NULL;
|
||||
return t;
|
||||
}
|
||||
|
||||
bool try_has_catch(try_t id) {
|
||||
return trys[id].exception != NULL;
|
||||
}
|
||||
|
||||
void* try_catch(try_t id) {
|
||||
return trys[id].exception;
|
||||
}
|
||||
|
||||
|
||||
void try_remove(try_t id) {
|
||||
trys[id].used = false;
|
||||
trynr--;
|
||||
}
|
||||
|
||||
try_t try_pop(void) {
|
||||
return stack[stack_position--];
|
||||
}
|
||||
|
||||
void try_push(try_t id) {
|
||||
stack[++stack_position] = id;
|
||||
}
|
||||
|
||||
void try_throw(try_t id, void* exception) {
|
||||
trys[id].exception = exception;
|
||||
}
|
26
try/try.h
Normal file
26
try/try.h
Normal file
|
@ -0,0 +1,26 @@
|
|||
#ifndef TRY_H
|
||||
#define TRY_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef int try_t;
|
||||
|
||||
#define try do { try_t _try_id = try_new(); void _try_f(void)
|
||||
#define catch(e) ; _try_f(); if (try_has_catch(_try_id)) { e = try_catch(_try_id); try_remove(_try_id); do
|
||||
#define endtry while(false); } } while(false);
|
||||
#define throw(e) try_throw(_try_id, (void*) e); return;
|
||||
#define subtry() try_t _try_id = try_pop();
|
||||
#define tpush() try_push(_try_id)
|
||||
#define cthrow(e) _try_id = try_pop(); throw(e);
|
||||
#define trycall try_push(_try_id);
|
||||
|
||||
|
||||
try_t try_new(void);
|
||||
bool try_has_catch(try_t);
|
||||
void* try_catch(try_t);
|
||||
void try_remove(try_t);
|
||||
void try_throw(try_t, void*);
|
||||
try_t try_pop(void);
|
||||
void try_push(try_t);
|
||||
|
||||
#endif
|
Loading…
Reference in a new issue