Changes & fixes

This commit is contained in:
bʰedoh₂ swé 2025-07-26 12:00:08 +05:00
parent 3b4e555649
commit 40f67b4bd7
3 changed files with 49 additions and 28 deletions

View File

@ -9,12 +9,12 @@
#define WITH99_resource_intptr_IMPL(var, init) \ #define WITH99_resource_intptr_IMPL(var, init) \
ML99_tuple(\ ML99_tuple(\
ML99_just(v(int *var)),\ 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))))\ ML99_just(v((printf("Freed %p(%s)\n", var, #var), free(var))))\
) )
#define With WITH99_With #define With WITH99_With
#define Never while(0) #define Never while(0);
int main(void) { int main(void) {
With( With(
@ -23,6 +23,6 @@ int main(void) {
) do { // All resources are allocated here. ) do { // All resources are allocated here.
*a += *b; *a += *b;
printf("%d\n", *a); printf("%d\n", *a);
} Never; // And deallocated here. } Never // And deallocated here.
} }
``` ```

View File

@ -13,7 +13,7 @@
#error Your Metalang99 is a bit mouldy, update to v1.13.2 or later. #error Your Metalang99 is a bit mouldy, update to v1.13.2 or later.
#endif #endif
#define WITH99_MAJOR 1 #define WITH99_MAJOR 2
#define WITH99_MINOR 0 #define WITH99_MINOR 0
#define WITH99_PATCH 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_VERSION_EQ(x, y, z) (WITH99_MAJOR == (x) && WITH99_MINOR == (y) && WITH99_PATCH == (z))
#define WITH99_PRIV_IF_ERROR(code)\ #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(f, maybe) ML99_call(v(WITH99_priv_res2stmt), f, maybe)
#define WITH99_priv_res2stmt_IMPL(f, maybe)\ #define WITH99_priv_res2stmt_IMPL(f, maybe)\
ML99_appl(\
ML99_if(\ ML99_if(\
ML99_isJust(v(maybe)),\ ML99_isJust(v(maybe)),\
ML99_appl(\ ML99_compose(ML99_reify(v(f)), v(ML99_maybeUnwrap)),\
ML99_reify(v(f)),\ ML99_appl(v(ML99_const), v())\
ML99_maybeUnwrap(v(maybe))\
),\ ),\
v()\ v(maybe)\
) )

View File

@ -1,25 +1,46 @@
#ifndef WITH99_DEFRES_H #ifndef WITH99_DEFRES_H
#define WITH99_DEFRES_H #define WITH99_DEFRES_H
#define WITH99_resource_Void_IMPL(var, init) \ #define WITH99_resource_Adhoc_IMPL(var, type, init, deinit) \
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) \
ML99_tuple(\ ML99_tuple(\
ML99_just(v(type var)),\ ML99_just(v(type var)),\
ML99_just(v(var = init, 0)),\ ML99_just(v((init, 1))),\
ML99_just(v(free(var)))\ 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 #endif