summaryrefslogtreecommitdiff
path: root/include/parser/ast.h
blob: 94635118433fc42a8914aff4a9cbb524e8af08b3 (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
/*
 * AST (Abstract Syntax Tree) definitions.
 * Copyright (c) 2023-2024, Quinn Stephens and the OSMORA team.
 * Provided under the BSD 3-Clause license.
 */

#ifndef _PARSER_AST_H
#define _PARSER_AST_H

#include <stdlib.h>
#include "list.h"

enum ast_node_kind {
    NK_UNKNOWN,

    NK_PROCEDURE,
    NK_RETURN
};

struct ast_node {
    struct list_entry list_entry;

    enum ast_node_kind kind;

    struct ast_node *parent;
    struct list children;
};

static inline void
ast_append_child(struct ast_node *parent, struct ast_node *child)
{
    child->parent = parent;
    list_append(&parent->children, &child->list_entry);
}

static inline void
ast_remove_child(struct ast_node *child)
{
    list_remove(&child->list_entry);
    child->parent->children.length--;
}

static inline struct ast_node *
ast_new_node(enum ast_node_kind kind)
{
    struct ast_node *node;

    node = malloc(sizeof(struct ast_node));
    node->kind = kind;
    node->parent = NULL;
    list_init(&node->children);

    return node;
}

#endif /* !_PARSER_AST_H */