Skip to content

Commit

Permalink
Sort array initializer in parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Apr 30, 2012
1 parent a7ee8f8 commit 06322f4
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
6 changes: 6 additions & 0 deletions list.c
Expand Up @@ -109,6 +109,12 @@ List *list_reverse(List *list) {
return r; return r;
} }


void list_clear(List *list) {
list->len = 0;
list->head = NULL;
list->tail = NULL;
}

int list_len(List *list) { int list_len(List *list) {
return list->len; return list->len;
} }
Expand Down
1 change: 1 addition & 0 deletions list.h
Expand Up @@ -37,6 +37,7 @@ extern void *list_get(List *list, int index);
extern void *list_head(List *list); extern void *list_head(List *list);
extern void *list_tail(List *list); extern void *list_tail(List *list);
extern List *list_reverse(List *list); extern List *list_reverse(List *list);
extern void list_clear(List *list);
extern int list_len(List *list); extern int list_len(List *list);
extern Iter *list_iter(List *list); extern Iter *list_iter(List *list);
extern void *iter_next(Iter *iter); extern void *iter_next(Iter *iter);
Expand Down
37 changes: 35 additions & 2 deletions parse.c
Expand Up @@ -1521,7 +1521,30 @@ static void read_initializer_elem(List *inits, Ctype *ctype, int off, bool desig
} }
} }


static void read_struct_initializer(List *inits, Ctype *ctype, int off, bool designated) { static int comp_init(const void *p, const void *q) {
Node * const *a = p;
Node * const *b = q;
return (*a)->initoff < (*b)->initoff ? -1
: (*a)->initoff == (*b)->initoff ? 0 : 1;
}

static void sort_inits(List *inits) {
int len = list_len(inits);
Node **tmp = malloc(sizeof(Node *) * len);
Iter *iter = list_iter(inits);
int i = 0;
while (!iter_end(iter)) {
Node *init = iter_next(iter);
assert(init->type == AST_INIT);
tmp[i++] = init;
}
qsort(tmp, len, sizeof(Node *), comp_init);
list_clear(inits);
for (int i = 0; i < len; i++)
list_push(inits, tmp[i]);
}

static void read_struct_initializer_sub(List *inits, Ctype *ctype, int off, bool designated) {
bool has_brace = maybe_read_brace(); bool has_brace = maybe_read_brace();
Iter *iter = list_iter(dict_keys(ctype->fields)); Iter *iter = list_iter(dict_keys(ctype->fields));
for (;;) { for (;;) {
Expand Down Expand Up @@ -1569,7 +1592,12 @@ static void read_struct_initializer(List *inits, Ctype *ctype, int off, bool des
skip_to_brace(); skip_to_brace();
} }


static void read_array_initializer(List *inits, Ctype *ctype, int off, bool designated) { static void read_struct_initializer(List *inits, Ctype *ctype, int off, bool designated) {
read_struct_initializer_sub(inits, ctype, off, designated);
sort_inits(inits);
}

static void read_array_initializer_sub(List *inits, Ctype *ctype, int off, bool designated) {
bool has_brace = maybe_read_brace(); bool has_brace = maybe_read_brace();
bool flexible = (ctype->len <= 0); bool flexible = (ctype->len <= 0);
int elemsize = ctype->ptr->size; int elemsize = ctype->ptr->size;
Expand Down Expand Up @@ -1608,6 +1636,11 @@ static void read_array_initializer(List *inits, Ctype *ctype, int off, bool desi
} }
} }


static void read_array_initializer(List *inits, Ctype *ctype, int off, bool designated) {
read_array_initializer_sub(inits, ctype, off, designated);
sort_inits(inits);
}

static void read_initializer_list(List *inits, Ctype *ctype, int off, bool designated) { static void read_initializer_list(List *inits, Ctype *ctype, int off, bool designated) {
Token *tok = read_token(); Token *tok = read_token();
if (is_string(ctype)) { if (is_string(ctype)) {
Expand Down
5 changes: 5 additions & 0 deletions test/initializer.c
Expand Up @@ -87,6 +87,11 @@ void test_array_designator(void) {
expect(2, x2[1].b); expect(2, x2[1].b);
expect(3, x2[2].a); expect(3, x2[2].a);
expect(4, x2[2].b); expect(4, x2[2].b);

// int x3[] = { [2] = 3, [0] = 1, 2 };
// expect(0, x3[0]);
// expect(1, x3[1]);
// expect(2, x3[2]);
} }


void test_struct_designator(void) { void test_struct_designator(void) {
Expand Down

0 comments on commit 06322f4

Please sign in to comment.