-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.c
56 lines (49 loc) · 1.04 KB
/
array.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
#include "lexer.c"
#include <stdio.h>
typedef struct
{
token_T **tokens;
int capacity;
int size;
} TokenArray;
TokenArray *createTokenArray()
{
TokenArray *array = (TokenArray *)malloc(sizeof(TokenArray));
array->tokens = NULL;
array->capacity = 0;
array->size = 0;
return array;
}
void destroyTokenArray(TokenArray *array)
{
if (array == NULL)
return;
if (array->tokens != NULL)
{
for (int i = 0; i < array->size; i++)
{
free(array->tokens[i]->value);
free(array->tokens[i]);
}
free(array->tokens);
}
free(array);
}
void addToTokenArray(TokenArray *array, token_T *token)
{
if (array->size == array->capacity)
{
int newCapacity = array->capacity * 2 + 1;
token_T **newTokens =
(token_T **)realloc(array->tokens, newCapacity * sizeof(token_T *));
if (newTokens == NULL)
{
fprintf(stderr, "Memory reallocation failed\n");
exit(1);
}
array->tokens = newTokens;
array->capacity = newCapacity;
}
array->tokens[array->size] = token;
array->size++;
}