-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharray.c
63 lines (52 loc) · 2.01 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
57
58
59
60
61
62
63
#include "compiler.h"
#include "helpers/vector.h"
#include <assert.h>
struct array_brackets *array_brackets_new() {
struct array_brackets *brackets = calloc(1, sizeof(struct array_brackets));
brackets->n_brackets = vector_create(sizeof(struct node *));
return brackets;
}
void array_brackets_free(struct array_brackets *brackets) { free(brackets); }
void array_brackets_add(struct array_brackets *brackets,
struct node *bracket_node) {
assert(bracket_node->type == NODE_TYPE_BRACKET);
vector_push(brackets->n_brackets, &bracket_node);
}
struct vector *array_brackets_node_vector(struct array_brackets *brackets) {
return brackets->n_brackets;
}
size_t array_brackets_calculate_size_from_index(struct datatype *dtype,
struct array_brackets *brackets,
int index) {
struct vector *array_vec = array_brackets_node_vector(brackets);
size_t size = dtype->size;
if (index >= vector_count(array_vec)) {
// char* abc
// return abc[0]; return abc[1];
return size;
}
vector_set_peek_pointer(array_vec, index);
struct node *array_bracket_node = vector_peek_ptr(array_vec);
if (!array_bracket_node) {
return 0;
}
while (array_bracket_node) {
assert(array_bracket_node->bracket.inner->type == NODE_TYPE_NUMBER);
int number = array_bracket_node->bracket.inner->llnum;
size *= number;
array_bracket_node = vector_peek_ptr(array_vec);
}
return size;
}
size_t array_brackets_calculate_size(struct datatype *dtype,
struct array_brackets *brackets) {
return array_brackets_calculate_size_from_index(dtype, brackets, 0);
}
int array_total_indexes(struct datatype *dtype) {
assert(dtype->flags & DATATYPE_FLAG_IS_ARRAY);
struct array_brackets *brackets = dtype->array.brackets;
return vector_count(brackets->n_brackets);
}
size_t array_brackets_count(struct datatype *dtype) {
return vector_count(dtype->array.brackets->n_brackets);
}