Skip to content

Commit

Permalink
calculate array sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
omdxp committed Aug 5, 2024
1 parent d63b090 commit 34255ad
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
24 changes: 22 additions & 2 deletions array.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,28 @@ struct vector *array_brackets_node_vector(struct array_brackets *brackets) {
size_t array_brackets_calculate_size_from_index(struct datatype *dtype,
struct array_brackets *brackets,
int index) {
#warning "TODO: Implement array_brackets_calculate_size_from_index"
return 0;
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,
Expand Down
2 changes: 1 addition & 1 deletion test.c
Original file line number Diff line number Diff line change
@@ -1 +1 @@
int x[50][20];
int x[4][3];

0 comments on commit 34255ad

Please sign in to comment.