summaryrefslogtreecommitdiff
path: root/ocaml/test/examine_solid_structure.c
blob: 158083c8b2f54742a2541c499f48c04c4dcaf974 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/// Examine solid pill structure using C Vere

#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include "noun.h"
#include "ur/ur.h"
#include "vere.h"

static void print_noun_structure(u3_noun noun, int depth, int max_depth) {
    if (depth > max_depth) {
        printf("...");
        return;
    }

    if (u3_none == noun) {
        printf("none");
        return;
    }

    if (c3y == u3a_is_atom(noun)) {
        if (u3r_met(3, noun) < 20) {
            printf("atom(%u)", u3r_word(0, noun));
        } else {
            printf("atom(large, %u bytes)", u3r_met(3, noun));
        }
    } else {
        printf("[");
        print_noun_structure(u3h(noun), depth + 1, max_depth);
        printf(" ");
        print_noun_structure(u3t(noun), depth + 1, max_depth);
        printf("]");
    }
}

int main(int argc, char* argv[]) {
    const char* pill_path = argc > 1 ? argv[1] : "../../../ocaml/solid.pill";

    // Read pill
    FILE* f = fopen(pill_path, "rb");
    if (!f) {
        printf("Error: cannot open %s\n", pill_path);
        return 1;
    }

    struct stat st;
    fstat(fileno(f), &st);
    c3_d len_d = st.st_size;
    c3_y* byt_y = malloc(len_d);
    fread(byt_y, 1, len_d, f);
    fclose(f);

    printf("Examining solid pill structure...\n\n");

    // Initialize runtime
    u3C.wag_w |= u3o_hashless;
    u3m_boot_lite(1 << 26);

    // Cue pill
    u3_cue_xeno* sil_u = u3s_cue_xeno_init_with(ur_fib27, ur_fib28);
    u3_weak pil = u3s_cue_xeno_with(sil_u, len_d, byt_y);
    u3s_cue_xeno_done(sil_u);

    if (u3_none == pil) {
        printf("Error: cue failed\n");
        return 1;
    }

    printf("Pill top-level structure:\n");
    print_noun_structure(pil, 0, 3);
    printf("\n\n");

    // Check if it's a cell
    if (c3y == u3a_is_cell(pil)) {
        printf("Pill is a cell [head tail]\n\n");

        u3_noun head = u3h(pil);
        u3_noun tail = u3t(pil);

        printf("Head: ");
        print_noun_structure(head, 0, 2);
        printf("\n\n");

        printf("Tail: ");
        print_noun_structure(tail, 0, 2);
        printf("\n\n");

        // Check if head is an atom (tag)
        if (c3y == u3a_is_atom(head) && u3r_met(3, head) < 20) {
            printf("Head is small atom: %u\n", u3r_word(0, head));
            printf("  (might be a tag)\n\n");
        }

        // If tail is a list, count elements
        if (c3y == u3a_is_cell(tail)) {
            int count = 0;
            u3_noun cur = tail;
            while (c3y == u3a_is_cell(cur) && count < 100) {
                count++;
                cur = u3t(cur);
            }
            printf("Tail appears to be a list with ~%d elements\n", count);
            printf("  (these might be boot events)\n");
        }
    }

    free(byt_y);
    return 0;
}