summaryrefslogtreecommitdiff
path: root/compiler/parser/type.c
blob: 187a2b0887e3d1c616cf9b1c17614aa9cb334f48 (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
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
/*
 * Type parser.
 * Copyright (c) 2023-2024, Quinn Stephens and the OSMORA team.
 * Provided under the BSD 3-Clause license.
 */

#include <stdbool.h>
#include <stdlib.h>
#include "debug.h"
#include "hashmap.h"
#include "lexer/token.h"
#include "parser/type.h"
#include "parser.h"

#define HASHMAP_ROWS 8

static bool
parse_alias(struct parser *ctx, struct type *typ)
{
    struct type *base;

    debug("Parsing type alias definition...\n");

    typ->kind = TYK_ALIAS;

    if (!parse_type_ref(ctx, &base, &typ->n_ptrs)) {
        return false;
    }

    if (ctx->tok.kind != TK_SEMICOLON) {
        tok_error(&ctx->tok, "expected \";\" after type definition\n");
        return false;
    }
    next_token(ctx);

    if (typ->n_ptrs >= 1) {
        typ->size = sizeof(void*);
    } else {
        typ->size = base->size;
    }

    return true;
}

static bool
parse_enum(struct parser *ctx, struct type *typ)
{
    struct enum_member *mem;
    uint64_t value;
    bool error;

    debug("Parsing enum definition...\n");

    if (next_token(ctx)->kind != TK_LBRACE) {
        tok_error(&ctx->tok, "expected \"{\" after \"enum\"\n");
        return false;
    }

    /* Set up type and members hashmap */
    typ->kind = TYK_ENUM;
    typ->members.rows = malloc(HASHMAP_ROWS * sizeof(struct list));
    typ->members.n_rows = HASHMAP_ROWS;
    hashmap_init(&typ->members);

    value = 0;
    error = false;
    next_token(ctx);
    while (ctx->tok.kind != TK_RBRACE) {
        if (ctx->tok.kind != TK_IDENTIFIER) {
            tok_error(&ctx->tok, "expected member name or \"}\"\n");
            error = true;
            break;
        }

        mem = malloc(sizeof(struct enum_member));
        mem->hashmap_entry.hash = ctx->tok.hash;
        mem->name = ctx->tok.pos;
        mem->name_len = ctx->tok.len;
        mem->value = value++;
        hashmap_add(&typ->members, &mem->hashmap_entry);

        if (next_token(ctx)->kind == TK_COMMA) {
            next_token(ctx);
            continue;
        }

        if (ctx->tok.kind != TK_RBRACE) {
            tok_error(&ctx->tok, "expected \",\" or \"}\" after member name\n");
            error = true;
            break;
        }
    }

    /* Free hashmap data if error occurred */
    if (error) {
        hashmap_free_entries(&typ->members);
        free(typ->members.rows);
        return false;
    }

    next_token(ctx);
    return true;
}

static bool
parse_struct(struct parser *ctx, struct type *typ)
{
    struct struct_member *mem;
    struct type *mem_typ;
    int mem_n_ptrs;
    size_t off;
    bool error;

    debug("Parsing struct definition...\n");

    if (next_token(ctx)->kind != TK_LBRACE) {
        tok_error(&ctx->tok, "expected \"{\" after \"struct\"\n");
        return false;
    }

    /* Set up type and members hashmap */
    typ->kind = TYK_STRUCT;
    typ->members.rows = malloc(HASHMAP_ROWS * sizeof(struct list));
    typ->members.n_rows = HASHMAP_ROWS;
    hashmap_init(&typ->members);

    off = 0;
    error = false;
    next_token(ctx);
    while (ctx->tok.kind != TK_RBRACE) {
        if (ctx->tok.kind != TK_IDENTIFIER) {
            tok_error(&ctx->tok, "expected member type name or \"}\"\n");
            error = true;
            break;
        }

        if (!parse_type_ref(ctx, &mem_typ, &mem_n_ptrs)) {
            error = true;
            break;
        }

        if (ctx->tok.kind != TK_IDENTIFIER) {
            tok_error(&ctx->tok, "expected member name\n");
            error = true;
            break;
        }

        mem = malloc(sizeof(struct struct_member));
        mem->hashmap_entry.hash = ctx->tok.hash;
        mem->name = ctx->tok.pos;
        mem->name_len = ctx->tok.len;
        mem->off = off;
        mem->typ = mem_typ;
        mem->n_ptrs = mem_n_ptrs;

        if (mem->n_ptrs >= 1) {
            mem->size = sizeof(void*);
        } else {
            mem->size = mem->typ->size;
        }

        hashmap_add(&typ->members, &mem->hashmap_entry);

        off += mem->size;

        if (next_token(ctx)->kind != TK_SEMICOLON) {
            tok_error(&ctx->tok, "expected \";\" after member name\n");
            error = true;
            break;
        }
        next_token(ctx);
    }

    /* Free hashmap data if error occurred */
    if (error) {
        hashmap_free_entries(&typ->members);
        free(typ->members.rows);
        return false;
    }

    next_token(ctx);
    typ->size = off;
    return true;
}

void
parse_type(struct parser *ctx)
{
    struct type *typ;
    bool success;

    debug("Parsing type definition...\n");

    /* Type name */
    if (next_token(ctx)->kind != TK_IDENTIFIER) {
        tok_error(&ctx->tok, "expected identifier after \"type\"\n");
        return;
    }

    /* Ensure type does not already exist */
    typ = (struct type*)hashmap_find(ctx->types, ctx->tok.hash);
    if (typ != NULL) {
        tok_error(&ctx->tok, "type \"%.*s\" already defined\n", (int)ctx->tok.len, ctx->tok.pos);
        return;
    }

    /* Create type */
    typ = malloc(sizeof(struct type));
    typ->hashmap_entry.hash = ctx->tok.hash;
    typ->name = ctx->tok.pos;
    typ->name_len = ctx->tok.len;

    if (next_token(ctx)->kind != TK_COLON) {
        tok_error(&ctx->tok, "expected \":\" after type name\n");
        free(typ);
        return;
    }

    switch (next_token(ctx)->kind) {
    case TK_IDENTIFIER:
        success = parse_alias(ctx, typ);
        break;
    case TK_ENUM:
        success = parse_enum(ctx, typ);
        break;
    case TK_STRUCT:
        success = parse_struct(ctx, typ);
        break;
    default:
        tok_error(&ctx->tok, "expected identifier, \"enum\", or \"struct\" after \":\"\n");
        success = false;
        break;
    }

    if (!success) {
        free(typ);
        return;
    }

    /* Add type to parser's registry */
    hashmap_add(ctx->types, &typ->hashmap_entry);
}

bool
parse_type_ref(struct parser *ctx, struct type **typ_out, int *n_ptrs_out)
{
    struct type *typ;
    int n_ptrs;

    /* Find type */
    typ = (struct type*)hashmap_find(ctx->types, ctx->tok.hash);
    if (typ == NULL) {
        tok_error(&ctx->tok, "type \"%.*s\" not found\n", (int)ctx->tok.len, ctx->tok.pos);
        return false;
    }

    /* Find number of pointers */
    n_ptrs = 0;
    while (next_token(ctx)->kind == TK_STAR) {
        n_ptrs++;
    }

    /* Ensure number of pointers is allowed */
    if (typ->size == 0 && n_ptrs < 1) {
        tok_error(&ctx->tok, "type \"%.*s\" can only be used in a pointer\n", (int)typ->name_len, typ->name);
        return false;
    }

    *typ_out = typ;
    *n_ptrs_out = n_ptrs;
    return true;
}