summaryrefslogtreecommitdiff
path: root/compiler/parser/stmt.c
blob: 6fe3d6d7b5fca11819314e8d4c26fb8348e59c91 (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
/*
 * Statement 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/ast.h"
#include "parser/stmt.h"
#include "parser/proc.h"
#include "parser.h"

static bool
parse_ret(struct parser *ctx, struct ast_node *parent, struct procedure *proc)
{
    struct ast_node *node;

    (void)proc;

    debug("Parsing return statement...\n");

    /* TODO: Parse return value */
    next_token(ctx);

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

    node = ast_new_node(NK_RETURN);
    ast_append_child(parent, node);

    return true;
}

void
parse_stmt_block(struct parser *ctx, struct ast_node *parent, struct procedure *proc)
{
    bool success;

    debug("Parsing statement block...\n");

    next_token(ctx);
    while (ctx->tok.kind != TK_RBRACE) {
        switch (ctx->tok.kind) {
        case TK_RET:
            success = parse_ret(ctx, parent, proc);
            break;
        default:
            tok_error(&ctx->tok, "Expected statement\n");
            success = false;
            break;
        }

        if (!success) {
            return;
        }
    }
    next_token(ctx);
}