Commit 38f690996a0cdf5597fa928496c2d6db131a7f75

Thomas de Grivel 2023-08-27T18:49:57

test array

diff --git a/libc3/buf_parse.c b/libc3/buf_parse.c
index 007d5b4..fcd783f 100644
--- a/libc3/buf_parse.c
+++ b/libc3/buf_parse.c
@@ -423,7 +423,7 @@ sw buf_parse_brackets (s_buf *buf, s_call *dest)
   ident_init(&tmp.ident, sym_1("Array"), sym_1("data"));
   tmp.arguments = list_new(NULL, list_new(NULL, NULL));
   arg_dimensions = &(list_next(tmp.arguments)->tag);
-  if ((r = buf_parse_tag(buf, &tmp.arguments->tag)) < 0)
+  if ((r = buf_parse_tag_primary(buf, &tmp.arguments->tag)) <= 0)
     goto restore;
   result += r;
   while (1) {
@@ -452,12 +452,17 @@ sw buf_parse_brackets (s_buf *buf, s_call *dest)
     dimensions_last = &(*dimensions_last)->next.data.list;
     dimension++;
   }
+  if (! dimension) {
+    goto restore;
+  }
   arg_dimensions->type = TAG_ARRAY;
-  list_to_array(dimensions, TAG_UW, &arg_dimensions->data.array);
+  if (! list_to_array(dimensions, TAG_UW, &arg_dimensions->data.array))
+    goto restore;
   *dest = tmp;
   r = result;
   goto clean;
  restore:
+  r = 0;
   buf_save_restore_rpos(buf, &save);
  clean:
   buf_save_clean(buf, &save);
@@ -2050,6 +2055,7 @@ sw buf_parse_tag (s_buf *buf, s_tag *dest)
     result += r;
   }
   if ((r = buf_parse_tag_call_op(buf, dest)) != 0 ||
+      (r = buf_parse_tag_brackets(buf, dest)) != 0 ||
       (r = buf_parse_tag_primary(buf, dest)) != 0)
     goto end;
  end:
@@ -2089,6 +2095,16 @@ sw buf_parse_tag_bool (s_buf *buf, s_tag *dest)
   return r;
 }
 
+sw buf_parse_tag_brackets (s_buf *buf, s_tag *dest)
+{
+  sw r;
+  assert(buf);
+  assert(dest);
+  if ((r = buf_parse_brackets(buf, &dest->data.call)) > 0)
+    dest->type = TAG_CALL;
+  return r;
+}
+
 sw buf_parse_tag_call (s_buf *buf, s_tag *dest)
 {
   sw r;
diff --git a/libc3/buf_parse.h b/libc3/buf_parse.h
index 9102496..81291e6 100644
--- a/libc3/buf_parse.h
+++ b/libc3/buf_parse.h
@@ -39,12 +39,15 @@
  * dest is untouched.
  */
 
+#define BUF_PARSE_TAG_NO_BRACKETS 1
+
 sw buf_parse_array (s_buf *buf, s_array *dest);
 sw buf_parse_array_data (s_buf *buf, s_array *dest);
 sw buf_parse_array_dimension_count (s_buf *buf, s_array *dest);
 sw buf_parse_array_dimensions (s_buf *buf, s_array *dest);
 sw buf_parse_array_type (s_buf *buf, s_array *dest);
 sw buf_parse_bool (s_buf *buf, bool *dest);
+sw buf_parse_brackets (s_buf *buf, s_call *dest);
 sw buf_parse_call (s_buf *buf, s_call *dest);
 sw buf_parse_call_args_paren (s_buf *buf, s_call *dest);
 sw buf_parse_call_op (s_buf *buf, s_call *dest);
@@ -87,6 +90,7 @@ sw buf_parse_sym (s_buf *buf, const s_sym **dest);
 sw buf_parse_tag (s_buf *buf, s_tag *dest);
 sw buf_parse_tag_array (s_buf *buf, s_tag *dest);
 sw buf_parse_tag_bool (s_buf *buf, s_tag *dest);
+sw buf_parse_tag_brackets (s_buf *buf, s_tag *dest);
 sw buf_parse_tag_call (s_buf *buf, s_tag *dest);
 sw buf_parse_tag_call_op (s_buf *buf, s_tag *dest);
 sw buf_parse_tag_call_paren (s_buf *buf, s_tag *dest);
diff --git a/libc3/list.c b/libc3/list.c
index 0e27469..cbb9edd 100644
--- a/libc3/list.c
+++ b/libc3/list.c
@@ -144,7 +144,7 @@ s_array * list_to_array (s_list *list, e_tag_type type,
                          s_array *dest)
 {
   s8 *data;
-  s8 *data_list;
+  void *data_list;
   s_list *l;
   uw len;
   uw size;
@@ -153,13 +153,14 @@ s_array * list_to_array (s_list *list, e_tag_type type,
   len = list_length(list);
   size = tag_type_size(type);
   dest->dimension = 1;
+  dest->type = type;
   if (! (dest->dimensions = calloc(1, sizeof(s_array_dimension))))
     errx(1, "list_to_array: out of memory: 1");
   dest->dimensions[0].count = len;
   dest->dimensions[0].item_size = size;
+  dest->size = len * size;
   if (! (data = dest->data = calloc(len, size)))
     errx(1, "list_to_array: out of memory: 2");
-  data = dest->data;
   l = list;
   while (l) {
     data_list = tag_to_pointer(&l->tag, type);
diff --git a/test/ic3/array.in b/test/ic3/array.in
index bd82e6b..2f9e4ef 100644
--- a/test/ic3/array.in
+++ b/test/ic3/array.in
@@ -1,6 +1,8 @@
 a = (u8) {0, 1}
 Array.data(a, (uw) {0})
 Array.data(a, (uw) {1})
+quote a[0]
+quote a[1]
 a[0]
 a[1]
 b = (u8) {{0, 1},
diff --git a/test/ic3/array.out.expected b/test/ic3/array.out.expected
index 9ab6798..2ce1f86 100644
--- a/test/ic3/array.out.expected
+++ b/test/ic3/array.out.expected
@@ -1,6 +1,8 @@
 (u8) {0, 1}
 0
 1
+Array.data(a, (uw) {0})
+Array.data(a, (uw) {1})
 0
 1
 (u8) {{0, 1}, {2, 3}}