summaryrefslogtreecommitdiff
path: root/compiler/main.c
blob: ada6243db734512bb86c497dc0e94854a5033ade (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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
 * Compiler command-line interface.
 * Copyright (c) 2023-2024, Quinn Stephens and the OSMORA team.
 * Provided under the BSD 3-Clause license.
 */

#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "debug.h"
#include "hashmap.h"
#include "parser/type.h"
#include "parser.h"

#define HASHMAP_ROWS 16

#define OID_O 0x00

struct option {
    char *name;
    char *value;
};

static char *argv0;
static char *in_fname;
static struct option options[] = {
    [OID_O] = { "o", NULL }
};

__attribute__((noreturn)) static void
cmd_fatal(const char *fmt, ...)
{
    va_list ap;

    fprintf(stderr, "\033[1m%s: \033[31mfatal error:\033[0m ", argv0);
    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);

    fprintf(stderr, "compilation terminated.\n");
    exit(EXIT_FAILURE);
}

static void
cmd_error(const char *fmt, ...)
{
    va_list ap;

    fprintf(stderr, "\033[1m%s: \033[31merror:\033[0m ", argv0);
    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);
}

static char *
load_text_file(char *filename)
{
    FILE *fp;
    size_t size;
    char *buf;

    fp = fopen(filename, "rb");
    if (fp == NULL) {
        cmd_error("%s: %s\n", filename, strerror(errno));
        return NULL;
    }

    /* Get file size */
    fseek(fp, 0, SEEK_END);
    size = (size_t)ftell(fp);
    fseek(fp, 0, SEEK_SET);
    if (size < sizeof(char)) {
        cmd_error("%s: empty file\n", filename);
        fclose(fp);
        return NULL;
    }

    /* Read entire file */
    buf = malloc(size + sizeof(char));
    if (fread(buf, 1, size, fp) != size) {
        cmd_error("%s: %s\n", filename, strerror(errno));
        free(buf);
        fclose(fp);
        return NULL;
    }

    fclose(fp);

    /* Terminate string */
    buf[size] = '\0';

    return buf;
}

static struct option *
find_option(char *name)
{
    for (int o = 0; o < (int)(sizeof(options) / sizeof(struct option)); o++) {
        if (strcmp(options[o].name, name) == 0) {
            return &options[0];
        }
    }

    return NULL;
}

static void
set_out_fname(void)
{
    size_t len;
    char *buf;
    char *dot;

    /* Skip if set already */
    if (options[OID_O].value != NULL) {
        return;
    }

    len = strlen(in_fname);
    buf = malloc(len + 4);
    strcpy(buf, in_fname);

    /* Append extension after last '.' or at end */
    dot = strrchr(buf, '.');
    if (dot == NULL) {
        strcpy(buf + len, ".txt");
    } else {
        strcpy(buf + (dot - buf), ".txt");
    }

    options[OID_O].value = buf;
}

static void
parse_args(int argc, char **argv)
{
    struct option *opt;

    in_fname = NULL;
    for (int a = 1; a < argc; a++) {
        /* Set input filename */
        if (argv[a][0] != '-') {
            if (in_fname != NULL) {
                cmd_error("multiple input files specified\n");
            } else {
                in_fname = argv[a];
            }

            continue;
        }

        /* Identify option */
        opt = find_option(argv[a] + 1);
        if (opt == NULL) {
            cmd_error("unrecognized command-line option '%s'\n", argv[a]);
            continue;
        }

        /* Set option value */
        a++;
        if (a >= argc) {
            cmd_error("missing value after '%s'\n", argv[a - 1]);
        }
        opt->value = argv[a];
    }

    if (in_fname == NULL) {
        cmd_fatal("no input files\n");
    }

    set_out_fname();
}

static void
print_ptrs(int n_ptrs)
{
    for (int n = 0; n < n_ptrs; n++) {
        putchar('*');
    }
}

static void
print_alias(struct type *typ)
{
    printf("alias (%lu bytes, %d pointer(s))", typ->size, typ->n_ptrs);
}

static void
print_enum(struct type *typ)
{
    struct enum_member *mem;

    printf("enum {\n");

    for (size_t r = 0; r < typ->members.n_rows; r++) {
        mem = (struct enum_member*)typ->members.rows[r].head;
        if (mem == (struct enum_member*)&typ->members.rows[r]) {
            continue;
        }

        do {
            printf("    %.*s,\n", (int)mem->name_len, mem->name);

            mem = (struct enum_member*)mem->hashmap_entry.list_entry.next;
        } while (mem != (struct enum_member*)&typ->members.rows[r]);
    }

    printf("}");
}

static void
print_struct(struct type *typ)
{
    struct struct_member *mem;

    printf("struct (%lu bytes) {\n", typ->size);

    for (size_t r = 0; r < typ->members.n_rows; r++) {
        mem = (struct struct_member*)typ->members.rows[r].head;
        if (mem == (struct struct_member*)&typ->members.rows[r]) {
            continue;
        }

        do {
            printf("    %.*s", (int)mem->typ->name_len, mem->typ->name);
            print_ptrs(mem->n_ptrs);
            printf(" %.*s;\n", (int)mem->name_len, mem->name);

            mem = (struct struct_member*)mem->hashmap_entry.list_entry.next;
        } while (mem != (struct struct_member*)&typ->members.rows[r]);
    }

    printf("}");
}

static void
print_type(struct type *typ)
{
    printf("type %.*s: ", (int)typ->name_len, typ->name);

    switch (typ->kind) {
    case TYK_ALIAS:
        print_alias(typ);
        break;
    case TYK_ENUM:
        print_enum(typ);
        break;
    case TYK_STRUCT:
        print_struct(typ);
        break;
    default:
        printf("(unknown)");
        break;
    }

    printf(";\n");

}

int
main(int argc, char **argv)
{
    char *input;
    struct parser parser;
    struct list *types_rows, *procs_rows;
    struct hashmap types, procs;
    struct type *typ;

    argv0 = argv[0];
    if (argc < 2) {
        cmd_fatal("no input files\n");
        exit(EXIT_FAILURE);
    }

    /* Parse command-line arguments */
    parse_args(argc, argv);

    /* Load input file */
    input = load_text_file(in_fname);
    if (input == NULL) {
        exit(EXIT_FAILURE);
    }

    /* Initialize hashmaps */
    types_rows = malloc(HASHMAP_ROWS * sizeof(struct list));
    types.rows = types_rows;
    types.n_rows = HASHMAP_ROWS;
    hashmap_init(&types);
    procs_rows = malloc(HASHMAP_ROWS * sizeof(struct list));
    procs.rows = procs_rows;
    procs.n_rows = HASHMAP_ROWS;
    hashmap_init(&procs);

    /* Parse input */
    parser.tok.fname = in_fname;
    parser.types = &types;
    parser.procs = &procs;
    parser_init(&parser, input);
    parser_parse(&parser);

    /* Print parsed types */
    for (size_t r = 0; r < types.n_rows; r++) {
        typ = (struct type*)types.rows[r].head;
        if (typ == (struct type*)&types.rows[r]) {
            continue;
        }

        do {
            print_type(typ);
            typ = (struct type*)typ->hashmap_entry.list_entry.next;
        } while (typ != (struct type*)&types.rows[r]);
    }

    free(procs_rows);
    free(types_rows);
    free(input);
    return 0;
}