Skip to content

Instantly share code, notes, and snippets.

@ttldtor
Created September 19, 2025 23:01
Show Gist options
  • Save ttldtor/759be9197dfd6b463ef7d1f9be3723ab to your computer and use it in GitHub Desktop.
Save ttldtor/759be9197dfd6b463ef7d1f9be3723ab to your computer and use it in GitHub Desktop.
meta C
#include <stdio.h>
typedef struct true_t {
char _;
} true_t;
true_t TRUE;
typedef struct false_t {
char _;
} false_t;
false_t FALSE;
int f_true() {
return 1;
}
int f_false() {
return 0;
}
#define bool_v(B) _Generic((B *) (0), true_t *: TRUE, false_t *: FALSE)
#define bool_v_to_int(B) _Generic(B, true_t: 1, false_t: 0)
#define v_to_bool(V) typeof(V)
#define is_string_v(X) _Generic((X), \
char *: 1, \
default: 0)
#define is_string_t(X) typeof(_Generic((X), \
char *: TRUE, \
default: FALSE))
#define has_signature_v(X, R, ...) \
_Generic((X), \
SignatureM(R, __VA_ARGS__): 1, \
default: 0)
#define has_signature_t(X, R, ...) \
typeof(_Generic((X), \
SignatureM(R, __VA_ARGS__): TRUE, \
default: FALSE))
#define to_string(X) _Generic((X), \
true_t: "true", \
false_t: "false")
#define has_same_type_v(X, Y) \
_Generic(has_same_type_t(X, Y), \
true_t: 1, \
false_t: 0)
#define has_same_type_t(X, Y) typeof(_Generic((X), \
typeof(Y): TRUE, \
default: FALSE))
#define SignatureM(R, ...) R (*)(__VA_ARGS__)
#define is_same_t(X, Y) \
typeof(_Generic(((X *) (0)), \
Y *: TRUE, \
default: FALSE))
#define and_t(X, Y) \
typeof(_Generic(bool_v(X), \
true_t: _Generic(bool_v(Y), true_t: TRUE, false_t: FALSE), \
false_t: _Generic(bool_v(Y), true_t: FALSE, false_t: FALSE)))
#define or_t(X, Y) \
typeof(_Generic(bool_v(X), \
true_t: _Generic(bool_v(Y), true_t: TRUE, false_t: TRUE), \
false_t: _Generic(bool_v(Y), true_t: TRUE, false_t: FALSE)))
#define not_t(X) \
typeof(_Generic(bool_v(X), \
true_t: FALSE, \
false_t: TRUE))
#define result_t(X, ...) typeof((X) (__VA_ARGS__))
#define id_sig(T) SignatureM(T, void)
#define true_sig id_sig(true_t)
#define false_sig id_sig(false_t)
#define _0 false_sig
#define _1 true_sig
#define bool_sig_v(S) _Generic((S) (0), true_sig: TRUE, false_sig: FALSE)
#define bool_sig_int(S) _Generic((S) (0), true_sig: 1, false_sig: 0)
#define bool_sig_t(S) typeof(bool_sig_v(S))
#define sig_apply_res_t(X) typeof(((X) (0))())
#define is_same_sig_t(SA, SB) typeof(_Generic((SA) (0), SB: TRUE, default: FALSE))
#define eq_sig(SA, SB) id_sig(is_same_sig_t(SA, SB))
#define not_sig(SA) id_sig(typeof(_Generic((SA) (0), true_sig: FALSE, false_sig: TRUE)))
#define and_sig(SA, SB) id_sig(typeof(_Generic((SA) (0), true_sig: _Generic((SB) (0), true_sig: TRUE, false_sig: FALSE), false_sig: FALSE)))
#define or_sig(SA, SB) id_sig(typeof(_Generic((SA) (0), true_sig: TRUE, false_sig: _Generic((SB) (0), true_sig: TRUE, false_sig: FALSE))))
#define xor_sig(SA, SB) not_sig(eq_sig(SA, SB))
#define select_sig(SD0, SD1, SI) typeof(_Generic((SI) (0), _0: (SD0) (0), _1: (SD1) (0)))
#define select2_sig(SD0, SD1, SD2, SD3, SI0, SI1) select_sig(select_sig(SD0, SD2, SI1), select_sig(SD1, SD3, SI1), SI0)
#define add_sig(SDA0, SDB0, SI) \
select_sig( \
xor_sig(SDA0, SDB0), \
and_sig(SDA0, SDB0), \
SI)
/**
* ```
* Result selection by index: (SI0, SI1)
*
* SDA0 ---------[ ]------------------------ S0 ( 0 , 0 )
* [ + ]
* SDA1 -. .-[ ]-. .--- S1 ( 0 , 1 )
* \ / \ /
* \ / "-[ ]--------"
* /. [ + ]
* / \ .--[ ]-.
* SDB0 --" "-[ ]." "-[ ] .-- C ( 1 , 0 )
* [ + ] [ V ]--"
* SDB1 ---------[ ]--------------[ ] - 0 ( 1 , 1 )
* ```
*/
#define add2_sig(SDA0, SDA1, SDB0, SDB1, SI0, SI1) \
select2_sig( \
add_sig(SDA0, SDB0, _0), \
add_sig(add_sig(SDA0, SDB0, _1), add_sig(SDA1, SDB1, _0), _0), \
or_sig(add_sig(add_sig(SDA0, SDB0, _1), add_sig(SDA1, SDB1, _0), _1), \
add_sig(SDA1, SDB1, _1)), \
false_sig, \
SI0, SI1)
void foo(int x) {}
void bar(void) {}
double bar2(int x, int y) { return 0.0; }
int main() {
printf("%d\n", is_string_v("123"));
printf("%d\n", is_string_v(123123));
printf("%d\n", has_signature_v(foo, void, int));
printf("%d\n", has_signature_v(bar, void, int));
printf("%d\n", has_signature_v(bar2, void, int));
printf("%d\n", has_signature_v(bar2, double, int, int));
has_signature_t(bar2, double, int, int) x;
printf("%s\n", to_string(x));
has_same_type_t(&bar2, &bar2) y1;
has_same_type_t(&bar2, &foo) y2;
has_same_type_t(1, 1) z;
is_same_t(typeof(&bar2), typeof(&bar2)) z1;
is_same_t(typeof(&bar2), typeof(&foo)) z2;
is_same_t(int, typeof(&foo)) z3;
is_same_t(true_t, true_t) z4;
and_t(true_t, false_t) aa;
printf("%s\n", to_string(bool_v(and_t(true_t, false_t))));
printf("%s\n", to_string(bool_v(and_t(true_t, true_t))));
auto xxx = (void (*)(int))(0);
auto xxxx = (SignatureM(void, int))(0);
auto xxxxx = bool_v(has_same_type_t(xxx, xxxx));
auto джигурда = bool_v(has_same_type_t((typeof(xxx(3)) *) (0), (void *) (0)));
auto джигурда2 = bool_v(has_same_type_t((typeof(((void (*)(int))(0))(3)) *) (0), (void *) (0)));
auto _ = bool_v(is_same_t(result_t(xxx, 42), result_t(xxxx, 4242)));
auto _______ = (true_sig) (0);
auto ________ = (false_sig) (0);
auto _________ = (not_sig(false_sig))(0);
auto __________ = bool_v(is_same_sig_t(not_sig(false_sig), true_sig));
auto ___________ = (and_sig(true_sig, false_sig))(0);
auto s1 = bool_sig_v(true_sig);
auto s2 = bool_sig_v(not_sig(false_sig));
auto s3 = bool_sig_v(and_sig(true_sig, false_sig));
auto select1 = bool_sig_v(select_sig(true_sig, false_sig, _1));
auto select2 = bool_v(is_same_sig_t(select_sig(true_sig, false_sig, _0), true_sig));
auto add1 = bool_sig_v(add_sig(true_sig, true_sig, false_sig));
auto add2 = bool_sig_v(add_sig(true_sig, true_sig, true_sig));
auto add30 = bool_sig_v(add2_sig(_1, _1, _1, _1, _0, _0));
auto add31 = bool_sig_v(add2_sig(_1, _1, _1, _1, _0, _1));
auto add32 = bool_sig_v(add2_sig(_1, _1, _1, _1, _1, _0));
auto add33 = bool_sig_v(add2_sig(_1, _1, _1, _1, _1, _1));
printf("%d %d %d %d\n", bool_v_to_int(add33), bool_v_to_int(add32), bool_v_to_int(add31), bool_v_to_int(add30));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment