diff --git a/README.md b/README.md index d17798d..1ea3623 100644 --- a/README.md +++ b/README.md @@ -6,20 +6,18 @@ #include -#define WITH99_RESOURCE_TYPE_intptr(...) int* -#define WITH99_RESOURCE_INIT_intptr(res, ...) (printf("Allocated to %p\n", res), *res = malloc(sizeof(int)), 0) -#define WITH99_RESOURCE_FREE_intptr(res, ...) (printf("Freed %p at %p\n", res, *res), free(*res)) -#define WITH99_RESOURCE_THROWS_intptr(res, ...) 0 +#define WITH99_resource_type_intptr_IMPL(...) v(int*) +#define WITH99_resource_init_intptr_IMPL(var, init, ...) v((printf("Allocated to %p\n", var), var = malloc(sizeof(int)), *var = init, 0)) +#define WITH99_resource_free_intptr_IMPL(var, ...) v((printf("Freed %p at %p\n", var, var), free(var))) +#define WITH99_resource_throws_intptr_IMPL(...) ML99_false() #define With WITH99_With int main(void) { With( - (intptr, a), - (intptr, b) + (intptr, a, 1), + (intptr, b, 3) ) { - *a = 1; - *b = 2; *a += *b; printf("%d\n", *a); } diff --git a/include/with99.h b/include/with99.h index defbad9..87d21c5 100644 --- a/include/with99.h +++ b/include/with99.h @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -11,22 +12,34 @@ #error Your Metalang99 is a bit mouldy, update to v1.13.2 or later. #endif -#define WITH99_VERSION 2 +#define WITH99_MAJOR 0 +#define WITH99_MINOR 1 +#define WITH99_PATCH 1 -#define WITH99_priv_restype_IMPL(res, ...) ML99_call(ML99_cat(v(WITH99_resource_type_ ), v(res)), v(__VA_ARGS__)) -#define WITH99_priv_resinit_IMPL(res, ...) ML99_call(ML99_cat(v(WITH99_resource_init_ ), v(res)), v(__VA_ARGS__)) -#define WITH99_priv_resfree_IMPL(res, ...) ML99_call(ML99_cat(v(WITH99_resource_free_ ), v(res)), v(__VA_ARGS__)) -#define WITH99_priv_resthrows_IMPL(res, ...) ML99_call(ML99_cat(v(WITH99_resource_throws_), v(res)), v(__VA_ARGS__)) +#define WITH99_VERSION_COMPATIBLE(x, y, z) (WITH99_MAJOR == (x) && ((WITH99_MINOR == (y) && WITH99_PATCH >= (z)) || (WITH99_MINOR > (y)))) +#define WITH99_VERSION_EQ(x, y, z) (WITH99_MAJOR == (x) && WITH99_MINOR == (y) && WITH99_PATCH == (z)) -#define WITH99_priv_declare_resource_IMPL(res, name, ...) ML99_TERMS(ML99_call(v(WITH99_priv_restype), v(res), v(__VA_ARGS__)), v(name)) -#define WITH99_priv_init_resource_IMPL(res, name, ...) ML99_call(v(WITH99_priv_resinit), v(res), v(&name), v(__VA_ARGS__)) -#define WITH99_priv_free_resource_IMPL(res, name, ...) ML99_call(v(WITH99_priv_resfree), v(res), v(&name), v(__VA_ARGS__)) -#define WITH99_priv_throws_resource_IMPL(res, name, ...) ML99_call(v(WITH99_priv_resthrows), v(res), v(__VA_ARGS__)) +#define WITH99_priv_restype_IMPL(tResNameArgs) ML99_call(ML99_cat(v(WITH99_resource_type_ ), ML99_tupleGet(0)(v(tResNameArgs))), ML99_tupleTail(v(tResNameArgs))) +#define WITH99_priv_resinit_IMPL(tResNameArgs) ML99_call(ML99_cat(v(WITH99_resource_init_ ), ML99_tupleGet(0)(v(tResNameArgs))), ML99_tupleTail(v(tResNameArgs))) +#define WITH99_priv_resfree_IMPL(tResNameArgs) ML99_call(ML99_cat(v(WITH99_resource_free_ ), ML99_tupleGet(0)(v(tResNameArgs))), ML99_tupleTail(v(tResNameArgs))) +#define WITH99_priv_resthrows_IMPL(tResNameArgs) ML99_call(ML99_cat(v(WITH99_resource_throws_), ML99_tupleGet(0)(v(tResNameArgs))), ML99_tupleTail(v(tResNameArgs))) + + +#define WITH99_priv_uncomma(...) ML99_call(v(WITH99_priv_uncomma), v(__VA_ARGS__)) + +// ASDIOJSAOIdmnlaskdjasoid;asjl +// ML99_uncomma breaks shit +#define WITH99_priv_uncomma_IMPL(...) ML99_abort(ML99_variadicsForEach(v(ML99_id), __VA_ARGS__)) + +#define WITH99_priv_declare_resource_IMPL(tResNameArgs) ML99_call(v(WITH99_priv_restype), v(tResNameArgs)), ML99_tupleGet(1)(v(tResNameArgs)) +#define WITH99_priv_init_resource_IMPL(tResNameArgs) ML99_call(v(WITH99_priv_resinit), v(tResNameArgs)) +#define WITH99_priv_free_resource_IMPL(tResNameArgs) ML99_call(v(WITH99_priv_resfree), v(tResNameArgs)) +#define WITH99_priv_throws_resource_IMPL(tResNameArgs) ML99_call(v(WITH99_priv_resthrows), v(tResNameArgs)) #define WITH99_priv_throw_match_0_IMPL(tResNameArgs)\ ML99_appl(ML99_reify(v(ML99_CHAIN_EXPR_STMT)),\ ML99_call(v(WITH99_priv_init_resource),\ - ML99_untuple(v(tResNameArgs))\ + v(tResNameArgs)\ )\ ) @@ -36,27 +49,29 @@ #define WITH99_priv_throw_match_1_IMPL(tResNameArgs)\ ML99_appl(ML99_reify(v(WITH99_PRIV_IF_ERROR)),\ ML99_call(v(WITH99_priv_init_resource),\ - ML99_untuple(v(tResNameArgs))\ + v(tResNameArgs)\ )\ ) #define WITH99_priv_create_stmt_IMPL(tResNameArgs)\ - ML99_appl(ML99_reify(v(ML99_INTRODUCE_VAR_TO_STMT)),\ - ML99_call(v(WITH99_priv_declare_resource),\ - ML99_untuple(v(tResNameArgs))\ - )\ - ),\ - ML99_boolMatchWithArgs(\ - ML99_call(\ - v(WITH99_priv_throws_resource),\ - ML99_untuple(v(tResNameArgs))\ + ML99_TERMS(\ + ML99_appl(ML99_reify(v(ML99_INTRODUCE_VAR_TO_STMT)),\ + ML99_call(v(WITH99_priv_declare_resource),\ + v(tResNameArgs)\ + )\ ),\ - v(WITH99_priv_throw_match_),\ - v(tResNameArgs)\ - ),\ - ML99_appl(ML99_reify(v(ML99_CHAIN_EXPR_STMT_AFTER)),\ - ML99_call(v(WITH99_priv_free_resource),\ - ML99_untuple(v(tResNameArgs))\ + ML99_boolMatchWithArgs(\ + ML99_call(\ + v(WITH99_priv_throws_resource),\ + v(tResNameArgs)\ + ),\ + v(WITH99_priv_throw_match_),\ + v(tResNameArgs)\ + ),\ + ML99_appl(ML99_reify(v(ML99_CHAIN_EXPR_STMT_AFTER)),\ + ML99_call(v(WITH99_priv_free_resource),\ + v(tResNameArgs)\ + )\ )\ )