Commit ea9be04e306cf4bb682fe77228bc7d877d3423f9

Thomas de Grivel 2024-08-11T03:49:29

wip pretty printer for arrays

diff --git a/libkc3/buf_inspect.c b/libkc3/buf_inspect.c
index d6cc79d..dc9ed6f 100644
--- a/libkc3/buf_inspect.c
+++ b/libkc3/buf_inspect.c
@@ -2224,13 +2224,20 @@ sw buf_inspect_integer_size (s_pretty *pretty, const s_integer *x)
 
 sw buf_inspect_list (s_buf *buf, const s_list * const *x)
 {
+  bool alist;
   const s_list *i;
+  s_pretty_save pretty_save;
   sw r;
   sw result = 0;
   assert(buf);
   if ((r = buf_write_1(buf, "[")) <= 0)
     return r;
-  result++;
+  result += r;
+  alist = list_is_alist(x);
+  if (alist) {
+    pretty_save_init(&pretty_save, &buf->pretty);
+    pretty_indent_from_column(&buf->pretty, 0);
+  }
   i = *x;
   while (i) {
     if ((r = buf_inspect_list_tag(buf, &i->tag)) < 0)
@@ -2239,8 +2246,14 @@ sw buf_inspect_list (s_buf *buf, const s_list * const *x)
     switch (i->next.type) {
     case TAG_LIST:
       if (i->next.data.list) {
-        if ((r = buf_write_1(buf, ", ")) < 0)
-          return r;
+        if (alist) {
+          if ((r = buf_write_1(buf, ",\n")) < 0)
+            return r;
+        }
+        else {
+          if ((r = buf_write_1(buf, ", ")) < 0)
+            return r;
+        }
         result += r;
       }
       i = i->next.data.list;
@@ -2255,6 +2268,8 @@ sw buf_inspect_list (s_buf *buf, const s_list * const *x)
       i = NULL;
     }
   }
+  if (alist)
+    pretty_save_clean(&pretty_save, &buf->pretty);
   if ((r = buf_write_1(buf, "]")) < 0)
     return r;
   result += r;
diff --git a/libkc3/str.c b/libkc3/str.c
index 0b1c275..b5b8a99 100644
--- a/libkc3/str.c
+++ b/libkc3/str.c
@@ -288,6 +288,7 @@ s_str * str_init_alloc_copy (s_str *str, uw size, const char *p)
   return str;
 }
 
+DEF_STR_INIT_STRUCT(array)
 DEF_STR_INIT(bool, bool)
 
 s_str * str_init_cast (s_str *str, const s_sym * const *type,
@@ -297,6 +298,8 @@ s_str * str_init_cast (s_str *str, const s_sym * const *type,
   assert(type);
   assert(tag);
   switch (tag->type) {
+  case TAG_ARRAY:
+    return str_init_array(str, &tag->data.array);
   case TAG_BOOL:
     return str_init_bool(str, tag->data.bool);
   case TAG_CHARACTER:
diff --git a/libkc3/str.h b/libkc3/str.h
index f7b05ce..e7c4097 100644
--- a/libkc3/str.h
+++ b/libkc3/str.h
@@ -42,6 +42,7 @@ s_str * str_init_1 (s_str *str, char *free, const char *p);
 s_str * str_init_1_alloc (s_str *str, const char *p);
 s_str * str_init_alloc (s_str *str, uw size);
 s_str * str_init_alloc_copy (s_str *str, uw size, const char *p);
+PROTOTYPE_STR_INIT_STRUCT(array);
 s_str * str_init_cast (s_str *str, const s_sym * const *type,
                        const s_tag *tag);
 s_str * str_init_concatenate (s_str *str, const s_str *a,
diff --git a/test/buf_inspect_test.c b/test/buf_inspect_test.c
index fdc860d..0270ce1 100644
--- a/test/buf_inspect_test.c
+++ b/test/buf_inspect_test.c
@@ -218,9 +218,13 @@ TEST_CASE(buf_inspect_array)
   BUF_INSPECT_TEST_ARRAY("(U8[]){0}",
                          "(U8[]) {0}");
   BUF_INSPECT_TEST_ARRAY("(U8[]){{0, 1}, {2, 3}}",
-                         "(U8[]) {{0, 1}, {2, 3}}");
+                         "(U8[]) {{0, 1},\n"
+                         "        {2, 3}}");
   BUF_INSPECT_TEST_ARRAY("(U8[]){ { { 0 , 1 } , {2 , 3 } } , { { 4 , 5 } , { 6 , 7 } } }",
-                         "(U8[]) {{{0, 1}, {2, 3}}, {{4, 5}, {6, 7}}}");
+                         "(U8[]) {{{0, 1},\n"
+                         "         {2, 3}},\n"
+                         "        {{4, 5},\n"
+                         "         {6, 7}}}");
 }
 TEST_CASE_END(buf_inspect_array)
 
diff --git a/test/ikc3/str.kc3 b/test/ikc3/str.kc3
index 8019691..f3e8369 100644
--- a/test/ikc3/str.kc3
+++ b/test/ikc3/str.kc3
@@ -74,3 +74,5 @@ quote "#{%Buf{}}"
 "#{%Buf{}}"
 quote "#{%BufRW{}}"
 "#{%BufRW{}}"
+quote "#{(U8[]) {{0, 0}, {0, 0}, {0, 0}}}"
+"#{(U8[]) {{0, 0}, {0, 0}, {0, 0}}}"
diff --git a/test/ikc3/str.out.expected b/test/ikc3/str.out.expected
index 5f3d92f..3761183 100644
--- a/test/ikc3/str.out.expected
+++ b/test/ikc3/str.out.expected
@@ -63,3 +63,6 @@
 "%Buf{column: (Sw) 0,\n     flush: (Ptr) 0x0,\n     free: false,\n     line: (Sw) 0,\n     ptr: (Ptr) 0x0,\n     read_only: false,\n     refill: (Ptr) 0x0,\n     rpos: (Uw) 0,\n     save: (Ptr) 0x0,\n     seek: (Ptr) 0x0,\n     size: (Uw) 0,\n     user_ptr: (Ptr) 0x0,\n     wpos: (Uw) 0}"
 "#{%BufRW{}}"
 "%BufRW{r: (Ptr) 0x0,\n        w: (Ptr) 0x0}"
+"#{(U8[]) {{0, 0},
+           {0, 0},
+           {0, 0}}}"
diff --git a/test/ikc3/struct.out.expected b/test/ikc3/struct.out.expected
index fac2d08..2cbc357 100644
--- a/test/ikc3/struct.out.expected
+++ b/test/ikc3/struct.out.expected
@@ -1,6 +1,17 @@
 0.0
 0.0
 %GL.Vec3{}
-%GL.Vec3{x: 0.0f, y: 0.0f, z: 0.0f}
-[position: %GL.Vec3{}, normal: %GL.Vec3{}, tex_coord: %GL.Vec2{}]
-[position: %GL.Vec3{x: 0.0f, y: 0.0f, z: 0.0f}, normal: %GL.Vec3{x: 0.0f, y: 0.0f, z: 0.0f}, tex_coord: %GL.Vec2{x: 0.0f, y: 0.0f}]
+%GL.Vec3{x: 0.0f,
+         y: 0.0f,
+         z: 0.0f}
+[position: %GL.Vec3{},
+ normal: %GL.Vec3{},
+ tex_coord: %GL.Vec2{}]
+[position: %GL.Vec3{x: 0.0f,
+                    y: 0.0f,
+                    z: 0.0f},
+ normal: %GL.Vec3{x: 0.0f,
+                  y: 0.0f,
+                  z: 0.0f},
+ tex_coord: %GL.Vec2{x: 0.0f,
+                     y: 0.0f}]
diff --git a/test/inspect_test.c b/test/inspect_test.c
index 095995e..8b27b99 100644
--- a/test/inspect_test.c
+++ b/test/inspect_test.c
@@ -223,13 +223,22 @@ TEST_CASE(inspect_array)
   INSPECT_TEST_ARRAY("(U8[]) {0, 0, 0}",
                      "(U8[]) {0, 0, 0}");
   INSPECT_TEST_ARRAY("(U8[]) {{0}, {0}}",
-                     "(U8[]) {{0}, {0}}");
+                     "(U8[]) {{0},\n"
+                     "        {0}}");
   INSPECT_TEST_ARRAY("(U8[]) {{0, 0}, {0, 0}}",
-                     "(U8[]) {{0, 0}, {0, 0}}");
+                     "(U8[]) {{0, 0},\n"
+                     "        {0, 0}}");
   INSPECT_TEST_ARRAY("(U8[]) {{0, 0}, {0, 0}, {0, 0}}",
-                     "(U8[]) {{0, 0}, {0, 0}, {0, 0}}");
+                     "(U8[]) {{0, 0},\n"
+                     "        {0, 0},\n"
+                     "        {0, 0}}");
   INSPECT_TEST_ARRAY("(U8[]) {{{0, 0}, {0, 0}}, {{0, 0}, {0, 0}}, {{0, 0}, {0, 0}}}",
-                     "(U8[]) {{{0, 0}, {0, 0}}, {{0, 0}, {0, 0}}, {{0, 0}, {0, 0}}}");
+                     "(U8[]) {{{0, 0},\n"
+                     "         {0, 0}},\n"
+                     "        {{0, 0},\n"
+                     "         {0, 0}},\n"
+                     "        {{0, 0},\n"
+                     "         {0, 0}}}");
   INSPECT_TEST_ARRAY("(U8[]) {1, 2, 3}",
                      "(U8[]) {1, 2, 3}");
   INSPECT_TEST_ARRAY("(U8[]) {1 + 1, 2 + 2, 3 + 3}",
@@ -417,9 +426,9 @@ TEST_CASE_END(inspect_str)
 
 TEST_CASE(inspect_struct)
 {
-  INSPECT_TEST_STRUCT("%KC3.Operator{}", "%KC3.Operator{sym: :+, symbol_value: ?, operator_precedence: 0, operator_associativity: :left}");
+  INSPECT_TEST_STRUCT("%KC3.Operator{}", "%KC3.Operator{sym: :+,\n              symbol_value: ?,\n              operator_precedence: 0,\n              operator_associativity: :left}");
   INSPECT_TEST_STRUCT("%KC3.Operator{sym: :-}",
-                      "%KC3.Operator{sym: :-, symbol_value: ?, operator_precedence: 0, operator_associativity: :left}");
+                      "%KC3.Operator{sym: :-,\n              symbol_value: ?,\n              operator_precedence: 0,\n              operator_associativity: :left}");
 }
 TEST_CASE_END(inspect_struct)