From 052144cabb126efe925a96f8a0597a0f2005d206 Mon Sep 17 00:00:00 2001 From: lemon Date: Mon, 23 Feb 2026 20:36:05 +0100 Subject: add metalang99 testsuite (preprocessor stress testing) --- test/external/metalang99/examples/demo.c | 48 ++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 test/external/metalang99/examples/demo.c (limited to 'test/external/metalang99/examples/demo.c') diff --git a/test/external/metalang99/examples/demo.c b/test/external/metalang99/examples/demo.c new file mode 100644 index 0000000..4a209d9 --- /dev/null +++ b/test/external/metalang99/examples/demo.c @@ -0,0 +1,48 @@ +// `...` is sometimes used to workaround a TCC bug, see +// . + +#include + +// Compile-time list manipulation: + +// 3, 3, 3, 3, 3 +static int five_threes[] = { + ML99_LIST_EVAL_COMMA_SEP(ML99_listReplicate(v(5), v(3))), +}; + +// 5, 4, 3, 2, 1 +static int from_5_to_1[] = { + ML99_LIST_EVAL_COMMA_SEP(ML99_listReverse(ML99_list(v(1, 2, 3, 4, 5)))), +}; + +// 9, 2, 5 +static int lesser_than_10[] = { + ML99_LIST_EVAL_COMMA_SEP( + ML99_listFilter(ML99_appl(v(ML99_greater), v(10)), ML99_list(v(9, 2, 11, 13, 5)))), +}; + +// Macro recursion: +#define factorial(n) ML99_natMatch(n, v(factorial_)) +#define factorial_Z_IMPL(...) v(1) // `...` due to the TCC's bug. +#define factorial_S_IMPL(n) ML99_mul(ML99_inc(v(n)), factorial(v(n))) + +ML99_ASSERT_EQ(factorial(v(4)), v(24)); + +// Overloading on a number of arguments: +typedef struct { + double width, height; +} Rect; + +#define Rect_new(...) ML99_OVERLOAD(Rect_new_, __VA_ARGS__) +#define Rect_new_1(x) \ + { x, x } +#define Rect_new_2(x, y) \ + { x, y } + +static Rect _7x8 = Rect_new(7, 8), _10x10 = Rect_new(10); + +// ... and more! + +int main(void) { + // Yeah. All is done at compile time. +} -- cgit v1.2.3