diff --git a/README.md b/README.md index 865cad4..7e196e3 100644 --- a/README.md +++ b/README.md @@ -9,12 +9,12 @@ #define WITH99_resource_intptr_IMPL(var, init) \ ML99_tuple(\ ML99_just(v(int *var)),\ - ML99_just(v((var = malloc(sizeof(int)),printf("Allocated %p(%s)\n", var, #var), *var = init, 0))),\ + ML99_just(v((var = malloc(sizeof(int)),printf("Allocated %p(%s)\n", var, #var), *var = init, 1))),\ ML99_just(v((printf("Freed %p(%s)\n", var, #var), free(var))))\ ) #define With WITH99_With -#define Never while(0) +#define Never while(0); int main(void) { With( @@ -23,6 +23,6 @@ int main(void) { ) do { // All resources are allocated here. *a += *b; printf("%d\n", *a); - } Never; // And deallocated here. + } Never // And deallocated here. } ``` diff --git a/include/with99.h b/include/with99.h index fccb5ea..3166452 100644 --- a/include/with99.h +++ b/include/with99.h @@ -13,7 +13,7 @@ #error Your Metalang99 is a bit mouldy, update to v1.13.2 or later. #endif -#define WITH99_MAJOR 1 +#define WITH99_MAJOR 2 #define WITH99_MINOR 0 #define WITH99_PATCH 0 @@ -21,17 +21,17 @@ #define WITH99_VERSION_EQ(x, y, z) (WITH99_MAJOR == (x) && WITH99_MINOR == (y) && WITH99_PATCH == (z)) #define WITH99_PRIV_IF_ERROR(code)\ - if (!(code)) + if ((code)) #define WITH99_priv_res2stmt(f, maybe) ML99_call(v(WITH99_priv_res2stmt), f, maybe) #define WITH99_priv_res2stmt_IMPL(f, maybe)\ - ML99_if(\ - ML99_isJust(v(maybe)),\ - ML99_appl(\ - ML99_reify(v(f)),\ - ML99_maybeUnwrap(v(maybe))\ + ML99_appl(\ + ML99_if(\ + ML99_isJust(v(maybe)),\ + ML99_compose(ML99_reify(v(f)), v(ML99_maybeUnwrap)),\ + ML99_appl(v(ML99_const), v())\ ),\ - v()\ + v(maybe)\ ) diff --git a/include/with99/defres.h b/include/with99/defres.h index c99d695..6aeec60 100644 --- a/include/with99/defres.h +++ b/include/with99/defres.h @@ -1,25 +1,46 @@ #ifndef WITH99_DEFRES_H #define WITH99_DEFRES_H -#define WITH99_resource_Void_IMPL(var, init) \ - ML99_tuple(\ - ML99_just(v(void *var)),\ - ML99_just(v(var = init, 0)),\ - ML99_just(v(free(var)))\ - ) - -#define WITH99_resource_Memory_IMPL(var, type, init) \ - ML99_tuple(\ - ML99_just(v(type *var)),\ - ML99_just(v(var = init, 0)),\ - ML99_just(v(free(var)))\ - ) - -#define WITH99_resource_Adhoc_IMPL(var, type, init) \ +#define WITH99_resource_Adhoc_IMPL(var, type, init, deinit) \ ML99_tuple(\ ML99_just(v(type var)),\ - ML99_just(v(var = init, 0)),\ - ML99_just(v(free(var)))\ + ML99_just(v((init, 1))),\ + ML99_just(v((deinit)))\ + ) + +#define WITH99_resource_AdhocThrows_IMPL(var, type, init, deinit) \ + ML99_tuple(\ + ML99_just(v(type var)),\ + ML99_just(v((init))),\ + ML99_just(v((deinit)))\ + ) + +#define WITH99_resource_Call_IMPL(init, deinit) \ + ML99_tuple(\ + ML99_nothing(),\ + ML99_just(v((init, 1))),\ + ML99_just(v((deinit)))\ + ) + +#define WITH99_resource_CallThrows_IMPL(init, deinit) \ + ML99_tuple(\ + ML99_nothing(),\ + ML99_just(v((init))),\ + ML99_just(v((deinit)))\ + ) + +#define WITH99_resource_File_IMPL(var, filename, mode) \ + ML99_tuple(\ + ML99_just(v(FILE* var)),\ + ML99_just(v(var = fopen(filename, mode))),\ + ML99_just(v(fclose(var)))\ + ) + +#define WITH99_resource_TmpFile_IMPL(var) \ + ML99_tuple(\ + ML99_just(v(FILE* var)),\ + ML99_just(v(var = tmpfile())),\ + ML99_just(v(fclose(var)))\ ) #endif