File size: 8,533 Bytes
20a991d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
 * par2serial-cc: utils.c - Common utilities implementation
 */
#include "utils.h"

/* ══════════════════════════════════════════════════════════
 *  Memory Arena
 * ══════════════════════════════════════════════════════════ */

static ArenaBlock *arena_new_block(size_t min_size) {
    size_t sz = min_size > ARENA_BLOCK_SIZE ? min_size : ARENA_BLOCK_SIZE;
    ArenaBlock *b = (ArenaBlock *)malloc(sizeof(ArenaBlock) + sz);
    if (!b) { fprintf(stderr, "OOM in arena\n"); exit(1); }
    b->next = NULL;
    b->size = sz;
    b->used = 0;
    return b;
}

Arena *arena_create(void) {
    Arena *a = (Arena *)calloc(1, sizeof(Arena));
    a->head = a->current = arena_new_block(ARENA_BLOCK_SIZE);
    a->total_alloc = 0;
    return a;
}

void *arena_alloc(Arena *a, size_t size) {
    /* align to 8 bytes */
    size = (size + 7) & ~(size_t)7;
    if (a->current->used + size > a->current->size) {
        ArenaBlock *nb = arena_new_block(size);
        a->current->next = nb;
        a->current = nb;
    }
    void *p = a->current->data + a->current->used;
    a->current->used += size;
    a->total_alloc += size;
    return p;
}

char *arena_strdup(Arena *a, const char *s) {
    size_t len = strlen(s);
    char *p = (char *)arena_alloc(a, len + 1);
    memcpy(p, s, len + 1);
    return p;
}

char *arena_strndup(Arena *a, const char *s, size_t n) {
    char *p = (char *)arena_alloc(a, n + 1);
    memcpy(p, s, n);
    p[n] = '\0';
    return p;
}

void arena_destroy(Arena *a) {
    ArenaBlock *b = a->head;
    while (b) {
        ArenaBlock *next = b->next;
        free(b);
        b = next;
    }
    free(a);
}

/* ══════════════════════════════════════════════════════════
 *  String Buffer
 * ══════════════════════════════════════════════════════════ */

void strbuf_init(StrBuf *sb) {
    sb->data = NULL;
    sb->len = 0;
    sb->cap = 0;
}

static void strbuf_grow(StrBuf *sb, size_t need) {
    if (sb->len + need + 1 > sb->cap) {
        sb->cap = sb->cap ? sb->cap * 2 : 256;
        while (sb->cap < sb->len + need + 1) sb->cap *= 2;
        sb->data = (char *)realloc(sb->data, sb->cap);
        if (!sb->data) { fprintf(stderr, "OOM in strbuf\n"); exit(1); }
    }
}

void strbuf_append(StrBuf *sb, const char *fmt, ...) {
    va_list ap;
    va_start(ap, fmt);
    /* probe length */
    va_list ap2;
    va_copy(ap2, ap);
    int n = vsnprintf(NULL, 0, fmt, ap2);
    va_end(ap2);
    if (n < 0) { va_end(ap); return; }
    strbuf_grow(sb, (size_t)n);
    vsnprintf(sb->data + sb->len, (size_t)n + 1, fmt, ap);
    sb->len += (size_t)n;
    va_end(ap);
}

void strbuf_append_char(StrBuf *sb, char c) {
    strbuf_grow(sb, 1);
    sb->data[sb->len++] = c;
    sb->data[sb->len] = '\0';
}

void strbuf_append_indent(StrBuf *sb, int indent) {
    for (int i = 0; i < indent; i++)
        strbuf_append(sb, "    ");
}

char *strbuf_detach(StrBuf *sb) {
    char *s = sb->data;
    sb->data = NULL;
    sb->len = sb->cap = 0;
    return s;
}

void strbuf_free(StrBuf *sb) {
    free(sb->data);
    sb->data = NULL;
    sb->len = sb->cap = 0;
}

/* ══════════════════════════════════════════════════════════
 *  Error Reporting
 * ══════════════════════════════════════════════════════════ */

static int error_count = 0;
static int warn_count  = 0;

void p2s_error(SourceLoc loc, const char *fmt, ...) {
    fprintf(stderr, "\033[1;31merror\033[0m");
    if (loc.filename)
        fprintf(stderr, " [%s:%d:%d]", loc.filename, loc.line, loc.col);
    fprintf(stderr, ": ");
    va_list ap;
    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);
    fprintf(stderr, "\n");
    error_count++;
}

void p2s_warn(SourceLoc loc, const char *fmt, ...) {
    fprintf(stderr, "\033[1;33mwarning\033[0m");
    if (loc.filename)
        fprintf(stderr, " [%s:%d:%d]", loc.filename, loc.line, loc.col);
    fprintf(stderr, ": ");
    va_list ap;
    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);
    fprintf(stderr, "\n");
    warn_count++;
}

void p2s_note(const char *fmt, ...) {
    fprintf(stderr, "\033[1;36mnote\033[0m: ");
    va_list ap;
    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);
    fprintf(stderr, "\n");
}

void p2s_fatal(const char *fmt, ...) {
    fprintf(stderr, "\033[1;31mfatal\033[0m: ");
    va_list ap;
    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);
    fprintf(stderr, "\n");
    exit(1);
}

/* ══════════════════════════════════════════════════════════
 *  Hash Map (separate chaining)
 * ══════════════════════════════════════════════════════════ */

static uint32_t hash_str(const char *s) {
    uint32_t h = 5381;
    while (*s) h = h * 33 + (unsigned char)*s++;
    return h;
}

HashMap *hashmap_create(void) {
    HashMap *m = (HashMap *)calloc(1, sizeof(HashMap));
    m->cap = HASHMAP_INIT_CAP;
    m->buckets = (HashEntry **)calloc(m->cap, sizeof(HashEntry *));
    m->len = 0;
    return m;
}

void hashmap_put(HashMap *m, const char *key, void *val) {
    uint32_t idx = hash_str(key) % m->cap;
    /* check existing */
    for (HashEntry *e = m->buckets[idx]; e; e = e->next) {
        if (strcmp(e->key, key) == 0) {
            e->val = val;
            return;
        }
    }
    /* insert */
    HashEntry *e = (HashEntry *)malloc(sizeof(HashEntry));
    e->key  = strdup(key);
    e->val  = val;
    e->next = m->buckets[idx];
    m->buckets[idx] = e;
    m->len++;
    /* rehash at 75% load */
    if (m->len > m->cap * 3 / 4) {
        size_t newcap = m->cap * 2;
        HashEntry **newb = (HashEntry **)calloc(newcap, sizeof(HashEntry *));
        for (size_t i = 0; i < m->cap; i++) {
            HashEntry *cur = m->buckets[i];
            while (cur) {
                HashEntry *next = cur->next;
                uint32_t ni = hash_str(cur->key) % newcap;
                cur->next = newb[ni];
                newb[ni] = cur;
                cur = next;
            }
        }
        free(m->buckets);
        m->buckets = newb;
        m->cap = newcap;
    }
}

void *hashmap_get(HashMap *m, const char *key) {
    uint32_t idx = hash_str(key) % m->cap;
    for (HashEntry *e = m->buckets[idx]; e; e = e->next)
        if (strcmp(e->key, key) == 0) return e->val;
    return NULL;
}

bool hashmap_has(HashMap *m, const char *key) {
    uint32_t idx = hash_str(key) % m->cap;
    for (HashEntry *e = m->buckets[idx]; e; e = e->next)
        if (strcmp(e->key, key) == 0) return true;
    return false;
}

void hashmap_destroy(HashMap *m) {
    for (size_t i = 0; i < m->cap; i++) {
        HashEntry *e = m->buckets[i];
        while (e) {
            HashEntry *next = e->next;
            free(e->key);
            free(e);
            e = next;
        }
    }
    free(m->buckets);
    free(m);
}

/* ══════════════════════════════════════════════════════════
 *  File I/O
 * ══════════════════════════════════════════════════════════ */

char *read_file(const char *path, size_t *out_len) {
    FILE *f = fopen(path, "rb");
    if (!f) {
        fprintf(stderr, "Cannot open file: %s\n", path);
        return NULL;
    }
    fseek(f, 0, SEEK_END);
    long len = ftell(f);
    fseek(f, 0, SEEK_SET);
    char *buf = (char *)malloc((size_t)len + 1);
    if (!buf) { fclose(f); return NULL; }
    fread(buf, 1, (size_t)len, f);
    buf[len] = '\0';
    fclose(f);
    if (out_len) *out_len = (size_t)len;
    return buf;
}