aboutsummaryrefslogtreecommitdiff
path: root/builtins/pointer.c
blob: 0d89e8a6d8ef9535e8d76aaf06c106a373d8b64d (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Type infos and methods for Pointer types
#include <ctype.h>
#include <err.h>
#include <gc.h>
#include <gc/cord.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/param.h>

#include "functions.h"
#include "text.h"
#include "types.h"
#include "util.h"

typedef struct recursion_s {
    const void *ptr;
    struct recursion_s *next;
} recursion_t;

public Text_t Pointer$as_text(const void *x, bool colorize, const TypeInfo *type) {
    auto ptr_info = type->PointerInfo;
    if (!x) {
        Text_t typename = generic_as_text(NULL, false, ptr_info.pointed);
        Text_t text;
        if (colorize)
            text = Text$concat(Text("\x1b[34;1m"), Text$from_str(ptr_info.sigil), typename, Text("\x1b[m"));
        else
            text = Text$concat(Text$from_str(ptr_info.sigil), typename);

        if (ptr_info.is_optional)
            text = Text$concat(text, Text("?"));

        return text;
    }
    const void *ptr = *(const void**)x;
    if (!ptr) {
        Text_t typename = generic_as_text(NULL, false, ptr_info.pointed);
        if (colorize)
            return Text$concat(Text("\x1b[34;1m!"), typename, Text("\x1b[m"));
        else
            return Text$concat(Text("!"), typename);
    }

    // Check for recursive references, so if `x.foo = x`, then it prints as
    // `@Foo{foo=@..1}` instead of overflowing the stack:
    static recursion_t *recursion = NULL;
    int32_t depth = 0;
    for (recursion_t *r = recursion; r; r = r->next) {
        ++depth;
        if (r->ptr == ptr) {
            Text_t text = Text$concat(
                colorize ? Text("\x1b[34;1m") : Text(""),
                Text$from_str(ptr_info.sigil),
                Text(".."),
                Int32$as_text(&depth, false, &Int32$info),
                colorize ? Text("\x1b[m") : Text(""));
            if (ptr_info.is_optional)
                text = Text$concat(text, colorize ? Text("\x1b[34;1m?\x1b[m") : Text("?"));
            return text;
        }
    }

    Text_t pointed;
    { // Stringify with this pointer flagged as a recursive one:
        recursion_t my_recursion = {.ptr=ptr, .next=recursion};
        recursion = &my_recursion;
        pointed = generic_as_text(ptr, colorize, ptr_info.pointed);
        recursion = recursion->next;
    }
    Text_t text;
    if (colorize)
        text = Text$concat(Text("\x1b[34;1m"), Text$from_str(ptr_info.sigil), Text("\x1b[m"), pointed);
    else
        text = Text$concat(Text$from_str(ptr_info.sigil), pointed);

    if (ptr_info.is_optional)
        text = Text$concat(text, Text("?"));
    return text;
}

PUREFUNC public int32_t Pointer$compare(const void *x, const void *y, const TypeInfo *type) {
    (void)type;
    const void *xp = *(const void**)x, *yp = *(const void**)y;
    return (xp > yp) - (xp < yp);
}

PUREFUNC public bool Pointer$equal(const void *x, const void *y, const TypeInfo *type) {
    (void)type;
    const void *xp = *(const void**)x, *yp = *(const void**)y;
    return xp == yp;
}

// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0