Commit 8a0a06dd47bfaa8e952ea5bda05e3592a1b719b2

Thomas de Grivel 2024-07-31T18:55:51

wip str_init_cast

diff --git a/libkc3/buf_inspect.c b/libkc3/buf_inspect.c
index d1dadfc..dc020e0 100644
--- a/libkc3/buf_inspect.c
+++ b/libkc3/buf_inspect.c
@@ -2347,8 +2347,11 @@ sw buf_inspect_struct (s_buf *buf, const s_struct *s)
   if ((r = buf_write_1(buf, "%")) < 0)
     return r;
   result += r;
-  if (! sym_is_module(s->type->module))
+  if (! sym_is_module(s->type->module)) {
+    err_puts("buf_inspect_struct: sym_is_module(s->type->module)");
+    assert(! "buf_inspect_struct: sym_is_module(s->type->module)");
     return -1;
+  }
   if ((r = buf_write_str(buf, &s->type->module->str)) < 0)
     return r;
   result += r;
diff --git a/libkc3/buf_inspect_s.c.in b/libkc3/buf_inspect_s.c.in
index 5429fb2..e24904b 100644
--- a/libkc3/buf_inspect_s.c.in
+++ b/libkc3/buf_inspect_s.c.in
@@ -55,11 +55,12 @@ sw buf_inspect_s_bits$_size (const s_bits$ *s)
     result += r;
     result += strlen(" ");
   }
-  u = *s;
   if (*s < 0) {
     result += strlen("-");
     u = -*s;
   }
+  else
+    u = *s;
   result += buf_inspect_u_bits$_decimal_size(&u);
   return result;
 }
diff --git a/libkc3/buf_inspect_s16.c b/libkc3/buf_inspect_s16.c
index ab1450a..c208a96 100644
--- a/libkc3/buf_inspect_s16.c
+++ b/libkc3/buf_inspect_s16.c
@@ -55,11 +55,12 @@ sw buf_inspect_s16_size (const s16 *s)
     result += r;
     result += strlen(" ");
   }
-  u = *s;
   if (*s < 0) {
     result += strlen("-");
     u = -*s;
   }
+  else
+    u = *s;
   result += buf_inspect_u16_decimal_size(&u);
   return result;
 }
diff --git a/libkc3/buf_inspect_s32.c b/libkc3/buf_inspect_s32.c
index 353460c..1b1010c 100644
--- a/libkc3/buf_inspect_s32.c
+++ b/libkc3/buf_inspect_s32.c
@@ -55,11 +55,12 @@ sw buf_inspect_s32_size (const s32 *s)
     result += r;
     result += strlen(" ");
   }
-  u = *s;
   if (*s < 0) {
     result += strlen("-");
     u = -*s;
   }
+  else
+    u = *s;
   result += buf_inspect_u32_decimal_size(&u);
   return result;
 }
diff --git a/libkc3/buf_inspect_s64.c b/libkc3/buf_inspect_s64.c
index 38c7968..b46d560 100644
--- a/libkc3/buf_inspect_s64.c
+++ b/libkc3/buf_inspect_s64.c
@@ -55,11 +55,12 @@ sw buf_inspect_s64_size (const s64 *s)
     result += r;
     result += strlen(" ");
   }
-  u = *s;
   if (*s < 0) {
     result += strlen("-");
     u = -*s;
   }
+  else
+    u = *s;
   result += buf_inspect_u64_decimal_size(&u);
   return result;
 }
diff --git a/libkc3/buf_inspect_s8.c b/libkc3/buf_inspect_s8.c
index 21917ab..952166f 100644
--- a/libkc3/buf_inspect_s8.c
+++ b/libkc3/buf_inspect_s8.c
@@ -55,11 +55,12 @@ sw buf_inspect_s8_size (const s8 *s)
     result += r;
     result += strlen(" ");
   }
-  u = *s;
   if (*s < 0) {
     result += strlen("-");
     u = -*s;
   }
+  else
+    u = *s;
   result += buf_inspect_u8_decimal_size(&u);
   return result;
 }
diff --git a/libkc3/buf_inspect_sw.c b/libkc3/buf_inspect_sw.c
index 06d65ca..e88225f 100644
--- a/libkc3/buf_inspect_sw.c
+++ b/libkc3/buf_inspect_sw.c
@@ -55,11 +55,12 @@ sw buf_inspect_sw_size (const sw *s)
     result += r;
     result += strlen(" ");
   }
-  u = *s;
   if (*s < 0) {
     result += strlen("-");
     u = -*s;
   }
+  else
+    u = *s;
   result += buf_inspect_uw_decimal_size(&u);
   return result;
 }
diff --git a/libkc3/buf_inspect_u.c.in b/libkc3/buf_inspect_u.c.in
index 91f0652..1249a45 100644
--- a/libkc3/buf_inspect_u.c.in
+++ b/libkc3/buf_inspect_u.c.in
@@ -11,6 +11,7 @@
  * THIS SOFTWARE.
  */
 /* Gen from buf_inspect_u.c.in BITS=_BITS$ bits=_bits$ */
+#include <string.h>
 #include "alloc.h"
 #include "buf.h"
 #include "buf_inspect.h"
@@ -133,7 +134,7 @@ sw buf_inspect_u_bits$_size (const u_bits$ *u)
     if ((r = buf_inspect_paren_sym_size(&g_sym_U_bits$)) < 0)
       return r;
     result += r;
-    result++;
+    result += strlen(" ");
   }
   if ((r = buf_inspect_u_bits$_base_size(&g_kc3_base_decimal, u)) < 0)
     return r;
diff --git a/libkc3/buf_inspect_u16.c b/libkc3/buf_inspect_u16.c
index cbe4e77..5057c48 100644
--- a/libkc3/buf_inspect_u16.c
+++ b/libkc3/buf_inspect_u16.c
@@ -11,6 +11,7 @@
  * THIS SOFTWARE.
  */
 /* Gen from buf_inspect_u.c.in BITS=16 bits=16 */
+#include <string.h>
 #include "alloc.h"
 #include "buf.h"
 #include "buf_inspect.h"
@@ -133,7 +134,7 @@ sw buf_inspect_u16_size (const u16 *u)
     if ((r = buf_inspect_paren_sym_size(&g_sym_U16)) < 0)
       return r;
     result += r;
-    result++;
+    result += strlen(" ");
   }
   if ((r = buf_inspect_u16_base_size(&g_kc3_base_decimal, u)) < 0)
     return r;
diff --git a/libkc3/buf_inspect_u32.c b/libkc3/buf_inspect_u32.c
index 84b41b3..de62184 100644
--- a/libkc3/buf_inspect_u32.c
+++ b/libkc3/buf_inspect_u32.c
@@ -11,6 +11,7 @@
  * THIS SOFTWARE.
  */
 /* Gen from buf_inspect_u.c.in BITS=32 bits=32 */
+#include <string.h>
 #include "alloc.h"
 #include "buf.h"
 #include "buf_inspect.h"
@@ -133,7 +134,7 @@ sw buf_inspect_u32_size (const u32 *u)
     if ((r = buf_inspect_paren_sym_size(&g_sym_U32)) < 0)
       return r;
     result += r;
-    result++;
+    result += strlen(" ");
   }
   if ((r = buf_inspect_u32_base_size(&g_kc3_base_decimal, u)) < 0)
     return r;
diff --git a/libkc3/buf_inspect_u64.c b/libkc3/buf_inspect_u64.c
index 4349dc4..51c5021 100644
--- a/libkc3/buf_inspect_u64.c
+++ b/libkc3/buf_inspect_u64.c
@@ -11,6 +11,7 @@
  * THIS SOFTWARE.
  */
 /* Gen from buf_inspect_u.c.in BITS=64 bits=64 */
+#include <string.h>
 #include "alloc.h"
 #include "buf.h"
 #include "buf_inspect.h"
@@ -133,7 +134,7 @@ sw buf_inspect_u64_size (const u64 *u)
     if ((r = buf_inspect_paren_sym_size(&g_sym_U64)) < 0)
       return r;
     result += r;
-    result++;
+    result += strlen(" ");
   }
   if ((r = buf_inspect_u64_base_size(&g_kc3_base_decimal, u)) < 0)
     return r;
diff --git a/libkc3/buf_inspect_u8.c b/libkc3/buf_inspect_u8.c
index 11ad801..a01c12d 100644
--- a/libkc3/buf_inspect_u8.c
+++ b/libkc3/buf_inspect_u8.c
@@ -11,6 +11,7 @@
  * THIS SOFTWARE.
  */
 /* Gen from buf_inspect_u.c.in BITS=8 bits=8 */
+#include <string.h>
 #include "alloc.h"
 #include "buf.h"
 #include "buf_inspect.h"
@@ -133,7 +134,7 @@ sw buf_inspect_u8_size (const u8 *u)
     if ((r = buf_inspect_paren_sym_size(&g_sym_U8)) < 0)
       return r;
     result += r;
-    result++;
+    result += strlen(" ");
   }
   if ((r = buf_inspect_u8_base_size(&g_kc3_base_decimal, u)) < 0)
     return r;
diff --git a/libkc3/buf_inspect_uw.c b/libkc3/buf_inspect_uw.c
index 34bb0ea..724f3ac 100644
--- a/libkc3/buf_inspect_uw.c
+++ b/libkc3/buf_inspect_uw.c
@@ -11,6 +11,7 @@
  * THIS SOFTWARE.
  */
 /* Gen from buf_inspect_u.c.in BITS=W bits=w */
+#include <string.h>
 #include "alloc.h"
 #include "buf.h"
 #include "buf_inspect.h"
@@ -133,7 +134,7 @@ sw buf_inspect_uw_size (const uw *u)
     if ((r = buf_inspect_paren_sym_size(&g_sym_Uw)) < 0)
       return r;
     result += r;
-    result++;
+    result += strlen(" ");
   }
   if ((r = buf_inspect_uw_base_size(&g_kc3_base_decimal, u)) < 0)
     return r;
diff --git a/libkc3/str.c b/libkc3/str.c
index fd1c7af..0f7783e 100644
--- a/libkc3/str.c
+++ b/libkc3/str.c
@@ -211,6 +211,8 @@ s_str * str_init_alloc (s_str *str, uw size, const char *p)
   return str;
 }
 
+DEF_STR_INIT(bool, bool)
+
 s_str * str_init_cast (s_str *str, const s_sym * const *type,
                        const s_tag *tag)
 {
@@ -218,10 +220,16 @@ s_str * str_init_cast (s_str *str, const s_sym * const *type,
   assert(type);
   assert(tag);
   switch (tag->type) {
+  case TAG_BOOL:
+    return str_init_bool(str, tag->data.bool);
   case TAG_CHARACTER:
     return str_init_character(str, tag->data.character);
   case TAG_MAP:
     return str_init_map(str, &tag->data.map);
+  case TAG_PTR:
+    return str_init_ptr(str, tag->data.ptr);
+  case TAG_PTR_FREE:
+    return str_init_ptr_free(str, tag->data.ptr_free);
   case TAG_S8:
     return str_init_s8(str, tag->data.s8);
   case TAG_S16:
@@ -352,6 +360,8 @@ s_str * str_init_f (s_str *str, const char *fmt, ...)
 }
 
 DEF_STR_INIT_STRUCT(map)
+DEF_STR_INIT(ptr, u_ptr_w)
+DEF_STR_INIT(ptr_free, u_ptr_w)
 DEF_STR_INIT_INT(s8)
 DEF_STR_INIT_INT(s16)
 DEF_STR_INIT_INT(s32)
diff --git a/libkc3/str.h b/libkc3/str.h
index 371318c..31218e7 100644
--- a/libkc3/str.h
+++ b/libkc3/str.h
@@ -49,6 +49,8 @@ s_str * str_init_copy_1 (s_str *str, const char *p);
 s_str * str_init_empty (s_str *str);
 s_str * str_init_f (s_str *str, const char *fmt, ...);
 PROTOTYPE_STR_INIT_STRUCT(map);
+PROTOTYPE_STR_INIT(ptr, u_ptr_w);
+PROTOTYPE_STR_INIT(ptr_free, u_ptr_w);
 PROTOTYPE_STR_INIT_INT(s8);
 PROTOTYPE_STR_INIT_INT(s16);
 PROTOTYPE_STR_INIT_INT(s32);
diff --git a/test/ikc3/str.kc3 b/test/ikc3/str.kc3
index 7c313ee..92803cf 100644
--- a/test/ikc3/str.kc3
+++ b/test/ikc3/str.kc3
@@ -54,3 +54,15 @@ quote "#{%KC3.Operator{}}"
 "#{%KC3.Operator{}}"
 quote "#{%KC3.Operator{sym: :-}}"
 "#{%KC3.Operator{sym: :-}}"
+quote "#{false}"
+"#{(Sw) 0}"
+quote "#{(Ptr) 0}"
+"#{(Ptr) 0}"
+quote "#{(Sw) 0}"
+"#{(Sw) 0}"
+quote "#{(Uw) 0}"
+"#{(Uw) 0}"
+quote "#{%Buf{}}"
+"#{%Buf{}}"
+quote "#{%BufRW{}}"
+"#{%BufRW{}}"