Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Sort array initializer in parser.

  • Loading branch information...
commit 06322f43f4cc24bccb22858cb5ee80eaf831bbfb 1 parent a7ee8f8
@rui314 authored
Showing with 47 additions and 2 deletions.
  1. +6 −0 list.c
  2. +1 −0  list.h
  3. +35 −2 parse.c
  4. +5 −0 test/initializer.c
View
6 list.c
@@ -109,6 +109,12 @@ List *list_reverse(List *list) {
return r;
}
+void list_clear(List *list) {
+ list->len = 0;
+ list->head = NULL;
+ list->tail = NULL;
+}
+
int list_len(List *list) {
return list->len;
}
View
1  list.h
@@ -37,6 +37,7 @@ extern void *list_get(List *list, int index);
extern void *list_head(List *list);
extern void *list_tail(List *list);
extern List *list_reverse(List *list);
+extern void list_clear(List *list);
extern int list_len(List *list);
extern Iter *list_iter(List *list);
extern void *iter_next(Iter *iter);
View
37 parse.c
@@ -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();
Iter *iter = list_iter(dict_keys(ctype->fields));
for (;;) {
@@ -1569,7 +1592,12 @@ static void read_struct_initializer(List *inits, Ctype *ctype, int off, bool des
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 flexible = (ctype->len <= 0);
int elemsize = ctype->ptr->size;
@@ -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) {
Token *tok = read_token();
if (is_string(ctype)) {
View
5 test/initializer.c
@@ -87,6 +87,11 @@ void test_array_designator(void) {
expect(2, x2[1].b);
expect(3, x2[2].a);
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) {
Please sign in to comment.
Something went wrong with that request. Please try again.