-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.c
94 lines (81 loc) · 1.62 KB
/
container.c
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
#include "9cc.h"
Node *new_node(int ty, Node *lhs, Node *rhs)
{
Node *node = malloc(sizeof(Node));
node->ty = ty;
node->lhs = lhs;
node->rhs = rhs;
return node;
}
Node *new_node_num(int val)
{
Node *node = malloc(sizeof(Node));
node->ty = ND_NUM;
node->val = val;
return node;
}
Node *new_node_ident(int offset)
{
Node *node = malloc(sizeof(Node));
node->ty = ND_IDENT;
node->offset = offset;
return node;
}
/*
Token *new_token
Creates a new token
*/
Token *new_token()
{
Token *token = malloc(sizeof(Token));
return token;
}
/*
Vector *new_vector
Creates a new vector and initializes its capacity to 16.
*/
Vector *new_vector()
{
Vector *vec = malloc(sizeof(Vector));
vec->data = malloc(sizeof(void *) * 16);
vec->capacity = 16;
vec->len = 0;
return vec;
}
/*
Vector *new_vector
Pushes a new element to vec.
*/
void vec_push(Vector *vec, void *elem)
{
if (vec->capacity == vec->len)
{
vec->capacity *= 2;
vec->data = realloc(vec->data, sizeof(void *) * vec->capacity);
}
vec->data[vec->len++] = elem;
}
/*
void* vec_get
Gets an element of the vector at the specified position
*/
void *vec_get(Vector *vec, int pos)
{
// Sanity check
if (pos >= vec->len) {
printf("Vector index out of range");
exit(1);
}
return vec->data[pos];
}
LVar *find_lvar(Token *token) {
if (!locals)
return NULL;
for (LVar *var = locals; var; var = var->next) {
if (strncmp(var->name, token->input, var->len) == 0) {
// A match was found!
return var;
}
}
return NULL;
}