blob: 25cecb7f657e01425d1d2c11245d538a9be8a2a1 (
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
|
#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);
}
|