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

#include <stdbool.h>
#include "debug.h"
#include "parser/proc.h"
#include "parser/type.h"
#include "parser.h"

#define HASHMAP_ROWS 8

static bool
parse_params(struct parser *ctx, struct procedure *proc)
{
    struct parameter *param;
    struct variable *var;
    struct type *typ;
    int n_ptrs;

    debug("Parsing procedure parameters...\n");

    while (ctx->tok.kind != TK_RPAREN) {
        if (ctx->tok.kind != TK_IDENTIFIER) {
            tok_error(&ctx->tok, "expected parameter type name or \")\"\n");
            return false;
        }

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

        if (ctx->tok.kind != TK_IDENTIFIER) {
            tok_error(&ctx->tok, "expected parameter name\n");
            return false;
        }

        param = malloc(sizeof(struct parameter));
        var = &param->var;
        var->hashmap_entry.hash = ctx->tok.hash;
        var->name = ctx->tok.pos;
        var->name_len = ctx->tok.len;
        var->typ = typ;
        var->n_ptrs = n_ptrs;
        hashmap_add(&proc->vars, &var->hashmap_entry);
        list_append(&proc->params, &param->list_entry);

        if (next_token(ctx)->kind == TK_COMMA && next_token(ctx)->kind == TK_RPAREN) {
            tok_warn(&ctx->tok, "extra \",\" at end of parameter list\n");
        }
    }
    next_token(ctx);

    return true;
}

void
parse_proc(struct parser *ctx)
{
    struct procedure *proc;

    debug("Parsing procedure declaration...\n");

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

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

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

    if (next_token(ctx)->kind != TK_LPAREN) {
        tok_error(&ctx->tok, "expected \"(\" after procedure name\n");
        free(proc);
        return;
    }

    /* Set up variables hashmap */
    proc->vars.rows = malloc(HASHMAP_ROWS * sizeof(struct list));
    proc->vars.n_rows = HASHMAP_ROWS;
    hashmap_init(&proc->vars);

    /* Set up parameter list */
    list_init(&proc->params);

    /* Parse parameters */
    if (next_token(ctx)->kind != TK_RPAREN) {
        if (!parse_params(ctx, proc)) {
            hashmap_free_entries(&proc->vars);
            free(proc->vars.rows);
            free(proc);
            return;
        }
    } else {
        next_token(ctx);
    }

    /* Parse return type if specified */
    if (ctx->tok.kind == TK_ARROW) {
        next_token(ctx);
        if (!parse_type_ref(ctx, &proc->ret_typ, &proc->ret_n_ptrs)) {
            hashmap_free_entries(&proc->vars);
            free(proc->vars.rows);
            free(proc);
            return;
        }
    }

    /* Add procedure to parser's registry */
    hashmap_add(ctx->procs, &proc->hashmap_entry);

    /* We are finished now if this is just a declaration */
    if (ctx->tok.kind == TK_SEMICOLON) {
        next_token(ctx);
        return;
    }

    if (ctx->tok.kind != TK_LBRACE) {
        tok_error(&ctx->tok, "Expected \";\" or \"{\"\n");
        return;
    }

    /* TODO: Parse body/code */

    if (next_token(ctx)->kind != TK_RBRACE) {
        tok_error(&ctx->tok, "Expected \"}\" after procedure body\n");
        return;
    }

    next_token(ctx);
}