Commit 8d8c89988a15b84caa6829581081b98b91457711

Thomas de Grivel 2023-12-21T03:27:57

fix tests

diff --git a/ic3/.ic3_history b/ic3/.ic3_history
index f7b7c47..1582c71 100644
--- a/ic3/.ic3_history
+++ b/ic3/.ic3_history
@@ -13,3 +13,6 @@ a = (U8) { 1, 2 }
 a[0]
 a = (U8) { 1, 2 }
 a[0]
+quote (GL.Vertex) {}
+(GL.Vertex) {}
+
diff --git a/libc3/array.c b/libc3/array.c
index b70d9c9..4c789ac 100644
--- a/libc3/array.c
+++ b/libc3/array.c
@@ -294,69 +294,57 @@ s_array * array_init_copy (s_array *a, const s_array *src)
   s_array tmp = {0};
   assert(a);
   assert(src);
-  assert(src->dimension);
-  assert(src->dimensions);
-  (void) i;
-  if (! src->dimension) {
-    err_puts("array_init_copy: zero dimension");
-    assert(! "array_init_copy: zero dimension");
-    return NULL;
-  }
-#ifdef DEBUG
-  while (i < src->dimension) {
-    assert(src->dimensions[i].count);
-    i++;
-  }
-#endif
+  tmp.count     = src->count;
   tmp.dimension = src->dimension;
-  if (! (tmp.dimensions = calloc(tmp.dimension,
-                                 sizeof(s_array_dimension)))) {
-    assert(! "array_init_copy: out of memory: dimensions");
-    warnx("array_init_copy: out of memory: dimensions");
-    return NULL;
-  }
-  memcpy(tmp.dimensions, src->dimensions,
-         src->dimension * sizeof(s_array_dimension));
-  tmp.count = src->count;
-  tmp.size = src->size;
-  tmp.type = src->type;
-  if (src->data) {
-    tmp.data = tmp.data_free = calloc(1, src->size);
-    if (! tmp.data) {
-      warnx("array_init_copy: failed to allocate memory");
-      assert(! "array_init_copy: failed to allocate memory");
-      free(tmp.dimensions);
-      return NULL;
-    }
-    if (! sym_to_init_copy(src->type, &init_copy)) {
-      free(tmp.dimensions);
+  tmp.size      = src->size;
+  tmp.type      = src->type;
+  if (tmp.dimension) {
+    if (! (tmp.dimensions = calloc(tmp.dimension,
+                                   sizeof(s_array_dimension)))) {
+      assert(! "array_init_copy: out of memory: dimensions");
+      warnx("array_init_copy: out of memory: dimensions");
       return NULL;
     }
-    data_tmp = tmp.data;
-    data_src = src->data;
-    i = 0;
-    item_size = src->dimensions[src->dimension - 1].item_size;
-    while (i < src->count) {
-      if (! init_copy(data_tmp, data_src))
-        goto ko_data;
-      data_tmp += item_size;
-      data_src += item_size;
-      i++;
-    }
-  }
-  else if (src->tags) {
-    tmp.tags = calloc(src->count, sizeof(s_tag));
-    if (! tmp.tags) {
-      warn("array_init_copy: failed to allocate memory");
-      assert(! "array_init_copy: failed to allocate memory");
-      free(tmp.dimensions);
-      return NULL;
+    memcpy(tmp.dimensions, src->dimensions,
+           src->dimension * sizeof(s_array_dimension));
+    if (src->data) {
+      tmp.data = tmp.data_free = calloc(1, src->size);
+      if (! tmp.data) {
+        warnx("array_init_copy: failed to allocate memory");
+        assert(! "array_init_copy: failed to allocate memory");
+        free(tmp.dimensions);
+        return NULL;
+      }
+      if (! sym_to_init_copy(src->type, &init_copy)) {
+        free(tmp.dimensions);
+        return NULL;
+      }
+      data_tmp = tmp.data;
+      data_src = src->data;
+      i = 0;
+      item_size = src->dimensions[src->dimension - 1].item_size;
+      while (i < src->count) {
+        if (! init_copy(data_tmp, data_src))
+          goto ko_data;
+        data_tmp += item_size;
+        data_src += item_size;
+        i++;
+      }
     }
-    i = 0;
-    while (i < src->count) {
-      if (! tag_init_copy(tmp.tags + i, src->tags + i))
-        goto ko_tags;
-      i++;
+    else if (src->tags) {
+      tmp.tags = calloc(src->count, sizeof(s_tag));
+      if (! tmp.tags) {
+        warn("array_init_copy: failed to allocate memory");
+        assert(! "array_init_copy: failed to allocate memory");
+        free(tmp.dimensions);
+        return NULL;
+      }
+      i = 0;
+      while (i < src->count) {
+        if (! tag_init_copy(tmp.tags + i, src->tags + i))
+          goto ko_tags;
+        i++;
+      }
     }
   }
   *a = tmp;
diff --git a/libc3/buf_inspect.c b/libc3/buf_inspect.c
index 77a6cbe..e97f180 100644
--- a/libc3/buf_inspect.c
+++ b/libc3/buf_inspect.c
@@ -52,6 +52,13 @@ sw buf_inspect_array (s_buf *buf, const s_array *array)
   if ((r = buf_write_1(buf, " ")) < 0)
     goto clean;
   result += r;
+  if (! array->dimension) {
+    if ((r = buf_write_1(buf, "{}")) < 0)
+      goto clean;
+    result += r;
+    r = result;
+    goto clean;
+  }
   if ((r = buf_inspect_array_data(buf, array)) < 0) {
     err_puts("buf_inspect_array: buf_inspect_array_data");
     goto clean;
diff --git a/libc3/buf_parse.c b/libc3/buf_parse.c
index ec5047a..f7e6ffc 100644
--- a/libc3/buf_parse.c
+++ b/libc3/buf_parse.c
@@ -58,15 +58,11 @@ sw buf_parse_array (s_buf *buf, s_array *dest)
   if ((r = buf_ignore_spaces(buf)) < 0)
     goto restore;
   result += r;
-  if ((r = buf_peek_array_dimension_count(buf, &tmp)) <= 0) {
+  if ((r = buf_peek_array_dimension_count(buf, &tmp)) <= 0 ||
+      (tmp.dimension &&
+       (r = buf_peek_array_dimensions(buf, &tmp)) <= 0) ||
+      (r = buf_parse_array_data(buf, &tmp)) <= 0)
     goto restore;
-  }
-  if ((r = buf_peek_array_dimensions(buf, &tmp)) <= 0) {
-    goto restore;
-  }
-  if ((r = buf_parse_array_data(buf, &tmp)) < 0) {
-    goto restore;
-  }
   result += r;
   *dest = tmp;
   r = result;
@@ -231,16 +227,28 @@ sw buf_parse_array_dimension_count (s_buf *buf, s_array *dest)
   s_array tmp;
   assert(buf);
   assert(dest);
-  tmp = *dest;
   buf_save_init(buf, &save);
   if ((r = buf_read_1(buf, "{")) <= 0)
     goto clean;
   result += r;
-  tmp.dimension = 1;
   if ((r = buf_ignore_spaces(buf)) < 0)
     goto restore;
   result += r;
-  while ((r = buf_read_1(buf, "{")) > 0) {
+  if ((r = buf_read_1(buf, "}")) < 0)
+    goto restore;
+  if (r > 0) {
+    result += r;
+    dest->dimension = 0;
+    r = result;
+    goto clean;
+  }
+  tmp = *dest;
+  tmp.dimension = 1;
+  while (1) {
+    if ((r = buf_read_1(buf, "{")) < 0)
+      goto restore;
+    if (! r)
+      break;
     result += r;
     tmp.dimension++;
     if ((r = buf_ignore_spaces(buf)) < 0)
diff --git a/libc3/env.c b/libc3/env.c
index 5f4db79..baa7edf 100644
--- a/libc3/env.c
+++ b/libc3/env.c
@@ -116,35 +116,37 @@ bool env_eval_array (s_env *env, const s_array *array, s_array *dest)
   assert(array);
   assert(dest);
   array_init_copy(&tmp, array);
-  item_size = tmp.dimensions[tmp.dimension - 1].item_size;
-  if (! tmp.data && array->tags) {
-    if (! sym_to_init_cast(tmp.type, &init_cast))
-      return false;
-    tmp.data = tmp.data_free = calloc(tmp.dimensions[0].count,
-                                      tmp.dimensions[0].item_size);
-    if (! tmp.data) {
-      warn("env_eval_array: failed to allocate memory");
-      assert(! "env_eval_array: failed to allocate memory");
-      return false;
-    }
-    data = tmp.data;
-    tag = tmp.tags;
-    i = 0;
-    while (i < tmp.count) {
-      if (! env_eval_tag(env, tag, &tag_eval))
-        goto ko;
-      if (! init_cast(data, &tag_eval)) {
-        err_write_1("env_eval_array: cannot cast ");
-        err_inspect_tag(&tag_eval);
-        err_write_1(" to ");
-        err_inspect_sym(&tmp.type);
-        err_puts(".");
-        goto ko;
+  if (tmp.dimension) {
+    item_size = tmp.dimensions[tmp.dimension - 1].item_size;
+    if (! tmp.data && array->tags) {
+      if (! sym_to_init_cast(tmp.type, &init_cast))
+        return false;
+      tmp.data = tmp.data_free = calloc(tmp.dimensions[0].count,
+                                        tmp.dimensions[0].item_size);
+      if (! tmp.data) {
+        warn("env_eval_array: failed to allocate memory");
+        assert(! "env_eval_array: failed to allocate memory");
+        return false;
+      }
+      data = tmp.data;
+      tag = tmp.tags;
+      i = 0;
+      while (i < tmp.count) {
+        if (! env_eval_tag(env, tag, &tag_eval))
+          goto ko;
+        if (! init_cast(data, &tag_eval)) {
+          err_write_1("env_eval_array: cannot cast ");
+          err_inspect_tag(&tag_eval);
+          err_write_1(" to ");
+          err_inspect_sym(&tmp.type);
+          err_puts(".");
+          goto ko;
+        }
+        tag_clean(&tag_eval);
+        data += item_size;
+        tag++;
+        i++;
       }
-      tag_clean(&tag_eval);
-      data += item_size;
-      tag++;
-      i++;
     }
   }
   *dest = tmp;