diff options
| author | hachem <im@hachem.wtf> | 2026-09-19 11:01:51 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2026-09-19 11:01:51 +0200 |
| commit | 08b8cbbccd8c8d7dab75746ee9d134f67455de91 (patch) | |
| tree | 83de1ed169e14b2595cd9da9e5c16f1d84c8c808 /ast.c | |
| parent | 05bf718e37efdc5201ad281e85da7850af31bd59 (diff) | |
feat: start work on ast
Diffstat (limited to 'ast.c')
| -rw-r--r-- | ast.c | 68 |
1 files changed, 68 insertions, 0 deletions
@@ -0,0 +1,68 @@ +#include "ast.h" +#include "lexer.h" + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +static struct Node* node_new(enum NodeKind kind) +{ + struct Node* node = calloc(1, sizeof *node); + if (node == NULL) + { + (void)fprintf(stderr, "out of memory\n"); + exit(1); + } + node->kind = kind; + node->line = yylineno; + return node; +} + +struct Node* node_leaf(enum NodeKind kind) +{ + return node_new(kind); +} + +struct Node* node_char(char character) +{ + struct Node* node = node_new(NODE_CHAR); + node->character = character; + return node; +} + +struct Node* node_unary(enum NodeKind kind, struct Node* first) +{ + struct Node* node = node_new(kind); + node->first = first; + return node; +} + +struct Node* node_binary(enum NodeKind kind, struct Node* first, struct Node* second) +{ + struct Node* node = node_new(kind); + node->first = first; + node->second = second; + return node; +} + +struct Node* node_append(struct Node* list, struct Node* node) +{ + struct Node* last = list; + + if (list == NULL) + return node; + while (last->next != NULL) + last = last->next; + last->next = node; + return list; +} + +void node_free(struct Node* node) +{ + if (node == NULL) + return; + node_free(node->first); + node_free(node->second); + node_free(node->next); + free(node); +} |
