summaryrefslogtreecommitdiff
path: root/ast.h
blob: c41713d41a88dc38dcb02838c36acf0c864c7cbb (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
#ifndef AST_H
#define AST_H

#include "scope.h"

struct stmt_node;
struct expr_node;

struct type_ref_node {
    struct type_ref type;
};

struct int_lit_node {
    integral_t val;
};

struct float_lit_node {
    floating_t val;
};

struct char_lit_node {
    char val;
};

struct str_lit_node {
    char* val;
};

struct var_ref_node {
    struct var_def* def_ref;
};

struct var_decl_node {
    struct type_ref_node type;
    struct var_def* def_ref;
    struct expr_node* initial_value;
};

struct assign_node {
    struct expr_node* lval;
    struct expr_node* rval;
};

struct expr_list_node {
    struct expr_node* expr;
    struct expr_list_node* next;
};

struct call_node {
    /* TODO: function pointers */
    struct fn_decl_node* called_fn_ref; /* borrowed */
    struct expr_list_node* args;
};

struct unary_node {
    enum {
        UNARY_NEG,
    } op;
    struct expr_node* expr;
};

struct binary_node {
    enum {
        BINARY_ADD,
        BINARY_SUB,
        BINARY_MUL,
        BINARY_DIV,
    } op;
    struct expr_node* lhs;
    struct expr_node* rhs;
};

struct expr_node {
    enum {
        EXPR_INT_LIT,
        EXPR_FLOAT_LIT,
        EXPR_CHAR_LIT,
        EXPR_STR_LIT,
        EXPR_VAR_REF,
        EXPR_ASSIGN,
        EXPR_CALL,
        EXPR_UNARY,
        EXPR_BINARY,
    } type;
    union {
        struct int_lit_node int_lit;
        struct float_lit_node float_lit;
        struct char_lit_node char_lit;
        struct str_lit_node str_lit;
        struct var_ref_node var_ref;
        struct assign_node assign;
        struct call_node call;
        struct unary_node unary;
        struct binary_node binary;
    } inner;

    const struct type_def* resolved_type;
};

struct group_node {
    struct stmt_node* head;
    struct scope* scope;
};

struct decl_list_node {
    struct var_decl_node* decl;
    struct decl_list_node* next;
};

struct fn_decl_node {
    struct type_ref_node return_type;
    char* name;
    struct decl_list_node* args;
    struct group_node body;
    struct scope* scope;

    const struct type_def* resolved_return_type;
};

struct return_node {
    struct expr_node* ret_val; /* null to return void */
};

struct if_node {
    struct expr_node* cond;
    struct stmt_node* true_branch;
    struct stmt_node* false_branch;
    struct scope* scope;
};

struct loop_init_node {
    enum {
        INIT_EXPR_LIST,
        INIT_DECL,
    } type;
    union {
        struct expr_list_node* expr_list;
        struct var_decl_node* decl;
    };
};

struct loop_node {
    struct loop_init_node* init; /* null if absent */
    struct expr_node* cond; /* null if absent */
    struct expr_node* incr; /* null if absent */
    struct stmt_node* body;
    struct scope* scope;
};

struct stmt_node {
    enum {
        STMT_EMPTY,
        STMT_EXPR,
        STMT_VAR_DECL,
        STMT_RETURN,
        STMT_GROUP,
        STMT_IF,
        STMT_LOOP,
    } type;
    union {
        struct expr_node expr;
        struct var_decl_node var_decl;
        struct return_node return_;
        struct group_node group;
        struct if_node if_;
        struct loop_node loop;
    } inner;

    struct stmt_node* next;
};

struct root_node {
    enum {
        ROOT_FN_DECL,
    } type;
    union {
        struct fn_decl_node fn_decl;
    } inner;

    struct root_node* next;
};

struct ast {
    struct root_node* root_node;
    struct scope* root_scope;

    /* nice shortcuts to have */
    const struct type_def* void_type;
    const struct type_def* char_type;
    const struct type_def* integral_type;
    const struct type_def* decimal_type;
    const struct type_def* pointer_type;
};

void ast_destroy(struct ast* ast);

#endif