with99/include/with99.h
2025-07-28 22:07:49 +05:00

90 lines
2.9 KiB
C

/*
* Copyright (C) 2025 by bedohswe <bedohswe@firemail.cc> <root@bedohswe.eu.org>
* https://github.com/bedohswe/ https://gitea.bedohswe.eu.org/bedohswe/
*
* Permission to use, copy, modify, and/or distribute this software for any purpose
* with or without fee is hereby granted.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef WITH99_H
#define WITH99_H
#include <metalang99.h>
#include <metalang99/tuple.h>
#include <metalang99/variadics.h>
#include <metalang99/util.h>
#include <metalang99/lang.h>
#include <metalang99/bool.h>
#include <metalang99/maybe.h>
#if !ML99_VERSION_COMPATIBLE(1, 13, 2)
#error Your Metalang99 is a bit mouldy, update to v1.13.2 or later.
#endif
#define WITH99_MAJOR 3
#define WITH99_MINOR 2
#define WITH99_PATCH 1
#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_IF_ERROR(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_appl(\
ML99_if(\
ML99_isJust(v(maybe)),\
ML99_compose(ML99_reify(v(f)), v(ML99_maybeUnwrap)),\
ML99_appl(v(ML99_const), v())\
),\
v(maybe)\
)
#define WITH99_priv_res2stmts_IMPL(tDeclInitDeinit)\
ML99_TERMS(\
WITH99_priv_res2stmt(v(ML99_INTRODUCE_VAR_TO_STMT), ML99_tupleGet(0)(v(tDeclInitDeinit))),\
WITH99_priv_res2stmt(v(WITH99_PRIV_IF_ERROR), ML99_tupleGet(1)(v(tDeclInitDeinit))),\
WITH99_priv_res2stmt(v(ML99_CHAIN_EXPR_STMT_AFTER), ML99_tupleGet(2)(v(tDeclInitDeinit))),\
v(ML99_CHAIN_EXPR_STMT((void)0))\
)
#define WITH99_priv_create_stmt_IMPL(tResNameArgs)\
ML99_call(\
WITH99_priv_res2stmts,\
ML99_call(\
ML99_cat(v(WITH99_resource_), ML99_tupleGet(0)(v(tResNameArgs))),\
ML99_tupleTail(v(tResNameArgs))\
)\
)
#define WITH99_priv_create_stmt_ARITY 1
#define WITH99_With(...) ML99_EVAL(WITH99_with(v(__VA_ARGS__)))
#define WITH99_priv_multiple(...) ML99_variadicsForEach(v(WITH99_priv_create_stmt), v(__VA_ARGS__))
#define WITH99_with(...) ML99_call(WITH99_with, __VA_ARGS__)
#define WITH99_with_IMPL(...) \
ML99_appl(\
ML99_if(\
ML99_isTuple(ML99_variadicsGet(0)(v(__VA_ARGS__))),\
ML99_compose(ML99_appl(v(ML99_variadicsForEach), v(WITH99_priv_create_stmt)), v(ML99_untuple)),\
v(WITH99_priv_create_stmt)\
),\
ML99_tuple(v(__VA_ARGS__))\
)
#endif