File: print.c

package info (click to toggle)
datatype99 1.6.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 476 kB
  • sloc: ansic: 1,071; sh: 43; makefile: 6
file content (51 lines) | stat: -rw-r--r-- 1,794 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <datatype99.h>

#include <stdio.h>

// Deriver implementation {
#define DATATYPE99_DERIVE_Print_IMPL(name, variants)                                               \
    ML99_prefixedBlock(                                                                            \
        v(inline static void name##_print(name self, FILE *stream)),                               \
        ML99_prefixedBlock(                                                                        \
            v(match(self)),                                                                        \
            ML99_listMapInPlace(ML99_compose(v(genArm), v(ML99_untuple)), v(variants))))

#define genArm_IMPL(tag, sig)                                                                      \
    ML99_TERMS(                                                                                    \
        DATATYPE99_assertAttrIsPresent(v(tag##_Print_fmt)),                                        \
        ML99_prefixedBlock(                                                                        \
            DATATYPE99_of(v(tag), ML99_indexedArgs(ML99_listLen(v(sig)))),                         \
            ML99_invokeStmt(v(fprintf), v(stream), DATATYPE99_attrValue(v(tag##_Print_fmt)))))
#define genArm_ARITY 1
// (Deriver implementation)

#define Foo_Print_fmt attr("Foo(\"%s\")", *_0)
#define Bar_Print_fmt attr("Bar(%d, %d)", *_0, *_1)

// clang-format off
datatype(
    derive(Print),
    MyType,
    (Foo, const char *),
    (Bar, int, int)
);
// clang-format on

#undef Foo_Print_fmt
#undef Bar_Print_fmt

/*
 * Output:
 *
 * Foo("hello world")
 * Bar(3, 5)
 */
int main(void) {
    MyType_print(Foo("hello world"), stdout);
    puts("");

    MyType_print(Bar(3, 5), stdout);
    puts("");

    return 0;
}