summaryrefslogtreecommitdiff
path: root/src/ast.c
blob: f6f8f08975fe4438423e42a8df4828e7a5de18f1 (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
 * Copyright (C) 2023 Johnny Richard
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#include "ast.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

ast_node_t *
ast_node_new(void)
{
  ast_node_t *node = (ast_node_t *)malloc(sizeof(ast_node_t));
  if (node == NULL) {
    printf("OOM: could no allocate a node");
    exit(EXIT_FAILURE);
  }
  node->kind = AST_UNKOWN_NODE;
  return node;
}

void
ast_node_destroy_vector(vector_t *vector)
{
  for (size_t i = 0; i < vector->size; i++) {
    ast_node_destroy(vector_at(vector, i));
  }
  vector_destroy(vector);
}

void
ast_node_destroy(ast_node_t *node)
{
  switch (node->kind) {
    case AST_NAMESPACE:
      ast_node_destroy_vector(node->data.ns.nodes);
      break;
    case AST_FUNCTION_DECLARATION:
      ast_node_destroy(node->data.function.body);
      ast_node_destroy_vector(node->data.function.prototype.parameters);
      break;
    case AST_IF_STMT:
      ast_node_destroy(node->data.if_stmt.condition);
      ast_node_destroy(node->data.if_stmt.body);
      break;
    case AST_BLOCK:
      ast_node_destroy_vector(node->data.block.body);
      break;
    case AST_BINARY_OPERATION:
      ast_node_destroy(node->data.binary_operation.left);
      ast_node_destroy(node->data.binary_operation.right);
      break;
    case AST_VARIABLE_DECLARATION:
      ast_node_destroy(node->data.variable_declaration.value);
      break;
    case AST_RETURN_STMT:
      ast_node_destroy(node->data.return_stmt.argument);
      break;
    case AST_VARIABLE_ASSIGNMENT:
      ast_node_destroy(node->data.variable_assignment.expression);
    case AST_FUNCTION_PARAMETER:
    case AST_LITERAL:
    case AST_UNKOWN_NODE:
    case AST_VARIABLE:
    case AST_FUNCTION_CALL:
      break;
  }
  free(node);
}

ast_node_t *
ast_node_new_return_stmt(ast_node_t *argument)
{
  ast_node_t *node = ast_node_new();

  *node = (ast_node_t){
    .result_type = TYPE_VOID,
    .kind = AST_RETURN_STMT,
    .data = { .return_stmt = { .argument = argument } },
  };

  return node;
}

ast_node_t *
ast_node_new_function_parameter(string_view_t name, type_t type)
{
  ast_node_t *node = ast_node_new();

  *node = (ast_node_t){
    .kind = AST_FUNCTION_PARAMETER,
    .data = {
      .function_parameter = {
        .identifier = {
          .name = name,
        },
        .type = type,
      },
    },
  };

  return node;
}

ast_node_t *
ast_node_new_function_declaration(string_view_t function_name,
                                  type_t return_type,
                                  vector_t *parameters,
                                  ast_node_t *body)
{
  ast_node_t *node = ast_node_new();

  *node = (ast_node_t){
    .kind = AST_FUNCTION_DECLARATION,
    .data = {
      .function = {
        .prototype = {
          .identifier = { .name = function_name },
          .return_type = return_type,
          .parameters = parameters,
        },
        .body = body,
      }
    },
  };

  return node;
}

ast_node_t *
ast_node_new_namespace(vector_t *nodes)
{
  ast_node_t *node = ast_node_new();

  *node = (ast_node_t){
    .kind = AST_NAMESPACE,
    .result_type = TYPE_VOID,
    .data = {
      .ns = {
        .nodes = nodes,
      }
    },
  };

  return node;
}

ast_node_t *
ast_node_new_block(vector_t *body)
{
  ast_node_t *node = ast_node_new();

  *node = (ast_node_t){
    .kind = AST_BLOCK,
    .data = {
      .block = {
        .body = body,
      }
    },
  };

  return node;
}

ast_node_t *
ast_node_new_literal_integer(uint32_t number)
{
  ast_node_t *node = ast_node_new();

  *node = (ast_node_t){
    .kind = AST_LITERAL,
    .result_type = TYPE_I32,
    .data = {
      .literal = {
        .kind = AST_LITERAL_INTEGER,
        .value = { .integer = number }
      },
    },
  };

  return node;
}

ast_node_t *
ast_node_new_if_stmt(ast_node_t *condition, ast_node_t *body)
{
  ast_node_t *node = ast_node_new();

  *node = (ast_node_t){
    .kind = AST_IF_STMT,
    .result_type = TYPE_VOID,
    .data = {
      .if_stmt = {
        .condition = condition,
        .body = body
      },
    },
  };

  return node;
}

ast_node_t *
ast_node_new_literal_bool(bool boolean)
{
  ast_node_t *node = ast_node_new();

  *node = (ast_node_t){
    .kind = AST_LITERAL,
    .result_type = TYPE_BOOL,
    .data = {
      .literal = {
        .kind = AST_LITERAL_BOOL,
        .value = { .boolean = boolean }
      },
    },
  };

  return node;
}

ast_node_t *
ast_node_new_binary_operation(ast_binary_operation_kind_t kind, ast_node_t *left, ast_node_t *right, type_t result_type)
{
  ast_node_t *node = ast_node_new();

  *node = (ast_node_t){
    .kind = AST_BINARY_OPERATION,
    .result_type = result_type,
    .data = {
      .binary_operation = {
        .kind = kind,
        .left = left,
        .right = right,
      },
    },
  };

  return node;
}

ast_node_t *
ast_node_new_variable_declaration(string_view_t variable_name, type_t type, ast_node_t *value)
{
  ast_node_t *node = ast_node_new();

  *node = (ast_node_t){
    .kind = AST_VARIABLE_DECLARATION,
    .result_type = TYPE_VOID,
    .data = {
      .variable_declaration = {
        .identifier = { .name = variable_name },
        .type = type,
        .value = value,
      },
    },
  };

  return node;
}

ast_node_t *
ast_node_new_variable_assignment(ast_identifier_t *identifier, ast_node_t *expression)
{
  ast_node_t *node = ast_node_new();

  *node = (ast_node_t){
    .kind = AST_VARIABLE_ASSIGNMENT,
    .result_type = TYPE_VOID,
    .data = {
      .variable_assignment = {
        .identifier = identifier,
        .expression = expression,
      },
    },
  };

  return node;
}

ast_node_t *
ast_node_new_variable(ast_identifier_t *identifier, type_t result_type)
{
  ast_node_t *node = ast_node_new();

  *node = (ast_node_t){
    .kind = AST_VARIABLE,
    .result_type = result_type,
    .data = { .variable = { .identifier = identifier } },
  };

  return node;
}

ast_node_t *
ast_node_new_function_call(ast_identifier_t *identifier, type_t result_type, vector_t *arguments)
{
  ast_node_t *node = ast_node_new();

  *node = (ast_node_t){
    .kind = AST_FUNCTION_CALL,
    .result_type = result_type,
    .data = { .function_call = { .identifier = identifier, .arguments = arguments } },
  };

  return node;
}

ast_node_t *
ast_node_ns_get_function_node_by_sv(ast_node_t *ns, string_view_t name)
{
  assert(ns->kind == AST_NAMESPACE);

  for (size_t i = 0; i < ns->data.ns.nodes->size; i++) {
    ast_node_t *node = vector_at(ns->data.ns.nodes, i);

    if (node->kind == AST_FUNCTION_DECLARATION && string_view_eq(node->data.function.prototype.identifier.name, name)) {
      return node;
    }
  }
  return NULL;
}

ast_node_t *
ast_node_ns_get_function_node_by_name(ast_node_t *ns, char *function_name)
{
  return ast_node_ns_get_function_node_by_sv(ns, string_view_from_str(function_name));
}

bool
ast_node_is_variable_declaration(ast_node_t *node)
{
  return node->kind == AST_VARIABLE_DECLARATION;
}

bool
ast_node_is_function_declaration(ast_node_t *node)
{
  return node->kind == AST_FUNCTION_DECLARATION;
}

ast_identifier_t *
ast_node_function_declaration_identifier(ast_node_t *node)
{
  assert(node->kind == AST_FUNCTION_DECLARATION);

  return &node->data.function.prototype.identifier;
}

string_view_t
ast_node_function_declaration_name(ast_node_t *node)
{
  return ast_node_function_declaration_identifier(node)->name;
}

char *
ast_type_to_str(type_t type)
{
  switch (type) {
    case TYPE_I32:
      return "i32";
    case TYPE_BOOL:
      return "bool";
    case TYPE_VOID:
      return "void";
  }
  assert(false);
}