Commit 62e872249af6b8185f27a96c2b50aaa6613b4752

Thomas de Grivel 2024-11-11T15:05:03

List.map = cfn List "list_map" (...)

diff --git a/.ikc3_history b/.ikc3_history
index 2031d04..41f55dc 100644
--- a/.ikc3_history
+++ b/.ikc3_history
@@ -1,4 +1,3 @@
-AList.to_map([a: 1, b: 2])
 1 + 1
 1 + 100000000000000000000000000000000000000000000000000000000
 type(1 + 100000000000000000000000000000000000000000000000000000000)
@@ -97,4 +96,4 @@ Crypt.sha512("Plop", "$6$rounds=123456$abc0123456789$")
 Time.from_str("1970-01-01 00:00:00")
 Time.from_str("2024-10-31 23:00:00")
 Time.to_str(Time.from_str("2024-10-31 23:00:00"))
-
+List.map([1, 2, 3], fn (x) { x * 2 })
diff --git a/lib/kc3/0.1/list.kc3 b/lib/kc3/0.1/list.kc3
index 493bd50..96f27ed 100644
--- a/lib/kc3/0.1/list.kc3
+++ b/lib/kc3/0.1/list.kc3
@@ -44,14 +44,7 @@ defmodule List do
     ([first], sep, acc) { str(reverse([first | acc])) }
   }
 
-  def map = fn {
-    ([], _) do
-      []
-    end
-    ([a | b], f) do
-      [f(a) | map(b, f)]
-    end
-  }
+  def map = cfn List "list_map" (List, Fn, Result)
 
   def reverse = fn {
     (x) { reverse(x, []) }
diff --git a/libkc3/buf_inspect.c b/libkc3/buf_inspect.c
index ed1083b..1535b46 100644
--- a/libkc3/buf_inspect.c
+++ b/libkc3/buf_inspect.c
@@ -2831,6 +2831,38 @@ sw buf_inspect_ratio_size (s_pretty *pretty, const s_ratio *ratio)
   return result;
 }
 
+sw buf_inspect_stacktrace (s_buf *buf, const s_list *stacktrace)
+{
+  s_pretty_save pretty_save;
+  sw r;
+  sw result = 0;
+  const s_list *s;
+  assert(buf);
+  pretty_save_init(&pretty_save, &buf->pretty);
+  pretty_indent_at_column(&buf->pretty, 0);
+  if ((r = buf_write_1(buf, "Stacktrace:\n")) < 0)
+    return r;
+  result += r;
+  s = stacktrace;
+  while (s) {
+    if ((r = buf_write_1(buf, " ")) < 0)
+      return r;
+    result += r;
+    if ((r = buf_inspect_tag(buf, &s->tag)) < 0)
+      return r;
+    result += r;
+    if ((r = buf_write_1(buf, "\n")) < 0)
+      return r;    
+    result += r;
+    s = list_next(s);
+  }
+  pretty_save_clean(&pretty_save, &buf->pretty);
+  return result;
+}
+
+sw buf_inspect_stacktrace_size (s_pretty *pretty,
+                                const s_list *stacktrace);
+
 sw buf_inspect_str (s_buf *buf, const s_str *str)
 {
   bool b = false;
diff --git a/libkc3/buf_inspect.h b/libkc3/buf_inspect.h
index 3427111..2842d89 100644
--- a/libkc3/buf_inspect.h
+++ b/libkc3/buf_inspect.h
@@ -166,7 +166,9 @@ BUF_INSPECT_S_PROTOTYPES(8);
 BUF_INSPECT_S_PROTOTYPES(16);
 BUF_INSPECT_S_PROTOTYPES(32);
 BUF_INSPECT_S_PROTOTYPES(64);
-BUF_INSPECT_S_PROTOTYPES(w);
+sw buf_inspect_stacktrace (s_buf *buf, const s_list *stacktrace);
+sw buf_inspect_stacktrace_size (s_pretty *pretty,
+                                const s_list *stacktrace);
 sw buf_inspect_str (s_buf *buf, const s_str *str);
 sw buf_inspect_str_byte (s_buf *buf, const u8 *byte);
 sw buf_inspect_str_byte_size (s_pretty *pretty, const u8 *byte);
@@ -185,6 +187,7 @@ sw buf_inspect_struct_size (s_pretty *pretty, const s_struct *s);
 sw buf_inspect_struct_type (s_buf *buf, const s_struct_type *st);
 sw buf_inspect_struct_type_size (s_pretty *pretty,
                                  const s_struct_type *st);
+BUF_INSPECT_S_PROTOTYPES(w);
 sw buf_inspect_sym (s_buf *buf, const s_sym * const *sym);
 sw buf_inspect_sym_reserved (s_buf *buf, const s_sym *sym);
 sw buf_inspect_sym_reserved_size (s_pretty *pretty, const s_sym *sym);
diff --git a/libkc3/io.c b/libkc3/io.c
index 4680681..1e928f5 100644
--- a/libkc3/io.c
+++ b/libkc3/io.c
@@ -229,6 +229,7 @@ DEF_ERR_IO_INSPECT(s32_hexadecimal, const s32 *)
 DEF_ERR_IO_INSPECT(s64,             const s64 *)
 DEF_ERR_IO_INSPECT(s64_decimal,     const s64 *)
 DEF_ERR_IO_INSPECT(s64_hexadecimal, const s64 *)
+DEF_ERR_IO_INSPECT(stacktrace,      const s_list *)
 DEF_ERR_IO_INSPECT(str,             const s_str *)
 DEF_ERR_IO_INSPECT(struct,          const s_struct *)
 DEF_ERR_IO_INSPECT(sw,              const sw *)
diff --git a/libkc3/io.h b/libkc3/io.h
index ff6d3e7..cf4bc1d 100644
--- a/libkc3/io.h
+++ b/libkc3/io.h
@@ -76,6 +76,7 @@ PROTOTYPES_ERR_IO_INSPECT(s32_hexadecimal, const s32 *);
 PROTOTYPES_ERR_IO_INSPECT(s64,             const s64 *);
 PROTOTYPES_ERR_IO_INSPECT(s64_decimal,     const s64 *);
 PROTOTYPES_ERR_IO_INSPECT(s64_hexadecimal, const s64 *);
+PROTOTYPES_ERR_IO_INSPECT(stacktrace,      const s_list *);
 PROTOTYPES_ERR_IO_INSPECT(str,             const s_str *);
 PROTOTYPES_ERR_IO_INSPECT(struct,          const s_struct *);
 PROTOTYPES_ERR_IO_INSPECT(sw,              const sw *);
diff --git a/libkc3/list.c b/libkc3/list.c
index 4ec2ab1..2e99fde 100644
--- a/libkc3/list.c
+++ b/libkc3/list.c
@@ -201,6 +201,34 @@ sw list_length (const s_list *list)
   return length;
 }
 
+s_list ** list_map (const s_list * const *list, const s_fn *f,
+                    s_list **dest)
+{
+  s_list *arg;
+  const s_list *l;
+  s_list **tail;
+  s_list *tmp;
+  if (! (arg = list_new(NULL)))
+    return NULL;
+  tmp = NULL;
+  tail = &tmp;
+  l = *list;
+  while (l) {
+    if (! tag_copy(&arg->tag, &l->tag))
+      goto ko;
+    *tail = list_new(NULL);
+    if (! eval_fn_call(f, arg, &(*tail)->tag))
+      goto ko;
+    tail = &(*tail)->next.data.list;
+    l = list_next(l);
+  }
+  *dest = tmp;
+  return dest;
+ ko:
+  list_delete_all(tmp);
+  return NULL;
+}
+
 s_list * list_next (const s_list *list)
 {
   assert(list);
diff --git a/libkc3/list.h b/libkc3/list.h
index c7f9757..8c25808 100644
--- a/libkc3/list.h
+++ b/libkc3/list.h
@@ -55,6 +55,8 @@ bool *    list_has (const s_list * const *list, const s_tag *tag,
 bool      list_is_alist (const s_list * const *list);
 bool      list_is_plist (const s_list *list);
 sw        list_length (const s_list *list);
+s_list ** list_map (const s_list * const *list, const s_fn *f,
+                    s_list **dest);
 s_list  * list_next (const s_list *list);
 s_list ** list_sort (const s_list * const *list, s_list **dest);
 s_list ** list_tail (s_list **list);
diff --git a/libkc3/list_init.h b/libkc3/list_init.h
index 3501666..a30132b 100644
--- a/libkc3/list_init.h
+++ b/libkc3/list_init.h
@@ -181,75 +181,4 @@ s_list * list_new_uw (uw i, s_list *next);
 s_list * list_new_var (const s_sym *type, s_list *next);
 s_list * list_new_void (s_list *next);
 
-/* Setters. */
-s_list * list_array (s_list *list, const s_sym *type, uw dimension,
-                     const uw *dimensions);
-s_list * list_array_copy (s_list *list, const s_array *a);
-s_list * list_bool (s_list *list, bool b);
-s_list * list_call (s_list *list);
-s_list * list_character (s_list *list, character c);
-
-s_list * list_complex (s_list *list, s_complex *c);
-s_list * list_f32 (s_list *list, f32 f);
-s_list * list_f64 (s_list *list, f64 f);
-s_list * list_f128 (s_list *list, f128 f);
-s_list * list_fn_copy (s_list *list, const s_fn *fn);
-s_list * list_ident (s_list *list, const s_ident *ident);
-s_list * list_ident_1 (s_list *list, const char *p);
-s_list * list_integer_1 (s_list *list, const char *p);
-s_list * list_integer_copy (s_list *list, const s_integer *i);
-s_list * list_integer_zero (s_list *list);
-
-s_list * list_map (s_list *list, uw count);
-s_list * list_map_1 (s_list *list, const char *p);
-s_list * list_map_from_lists (s_list *list, const s_list *keys,
-                              const s_list *values);
-s_list * list_ptr (s_list *list, void *p);
-s_list * list_ptr_free (s_list *list, void *p);
-s_list * list_quote_copy (s_list *list, const s_quote *quote);
-s_list * list_ratio_1 (s_list *list, const char *p);
-s_list * list_ratio (s_list *list);
-s_list * list_ratio_copy (s_list *list, const s_ratio *r);
-s_list * list_ratio_zero (s_list *list);
-s_list * list_s8 (s_list *list, s8 i);
-s_list * list_s16 (s_list *list, s16 i);
-s_list * list_s32 (s_list *list, s32 i);
-s_list * list_s64 (s_list *list, s64 i);
-s_list * list_str (s_list *list, char *p_free, uw size, const char *p);
-s_list * list_str_1 (s_list *list, char *p_free, const char *p);
-s_list * list_str_1_alloc (s_list *list, const char *p);
-s_list * list_str_alloc_copy (s_list *list, uw size, const char *p);
-s_list * list_str_cast (s_list *list, const s_sym * const *type,
-                        const s_tag *src);
-s_list * list_str_concatenate (s_list *list, const s_str *a,
-                               const s_str *b);
-s_list * list_str_concatenate_list (s_list *list,
-                                    const s_list * const *src);
-s_list * list_str_copy (s_list *list, const s_str *src);
-s_list * list_str_empty (s_list *list);
-s_list * list_struct (s_list *list, const s_sym *module);
-s_list * list_struct_copy (s_list *list, const s_struct *src);
-s_list * list_struct_with_data (s_list *list, const s_sym *module,
-                                void *data, bool free_data);
-s_list * list_struct_type (s_list *list, const s_sym *module,
-                           const s_list *spec);
-s_list * list_struct_type_update_clean (s_list *list,
-                                        const s_struct_type *st,
-                                        const s_cfn *clean);
-s_list * list_sw (s_list *list, sw i);
-s_list * list_sym (s_list *list, const s_sym *sym);
-s_list * list_tuple (s_list *list, uw count);
-s_list * list_tuple_2 (s_list *list, const s_tag *a, const s_tag *b);
-s_list * list_time (s_list *list);
-s_list * list_time_add (s_list *list, const s_time *a, const s_time *b);
-s_list * list_time_now (s_list *list);
-s_list * list_u8 (s_list *list, u8 i);
-s_list * list_u16 (s_list *list, u16 i);
-s_list * list_u32 (s_list *list, u32 i);
-s_list * list_u64 (s_list *list, u64 i);
-s_list * list_unquote_copy (s_list *list, const s_unquote *unquote);
-s_list * list_uw (s_list *list, uw i);
-s_list * list_var (s_list *list, const s_sym *type);
-s_list * list_void (s_list *list);
-
 #endif /* LIBKC3_LIST_INIT_H */
diff --git a/libkc3/tag.c b/libkc3/tag.c
index 00dea61..dbfc6e7 100644
--- a/libkc3/tag.c
+++ b/libkc3/tag.c
@@ -249,6 +249,12 @@ void tag_clean (s_tag *tag)
   }
 }
 
+s_tag * tag_copy (s_tag *tag, const s_tag *src)
+{
+  tag_clean(tag);
+  return tag_init_copy(tag, src);
+}
+
 void tag_delete (s_tag *tag)
 {
   tag_clean(tag);
diff --git a/libkc3/tag_init.rb b/libkc3/tag_init.rb
index d9e9231..81fe094 100644
--- a/libkc3/tag_init.rb
+++ b/libkc3/tag_init.rb
@@ -81,14 +81,6 @@ end
     ! ["1", "copy", "list"].include?(name)
   end
 
-  def list_proto
-    if with_list?
-      "s_list * list_#{name_suffix} (s_list *list#{comma_args_proto});"
-    else
-      ""
-    end
-  end
-  
   def list_init_proto
     if with_list?
       "s_list * list_init_#{name_suffix} (s_list *list#{comma_args_proto}, s_list *next);"
@@ -451,11 +443,6 @@ class TagInitList
     protos.join "\n"
   end
 
-  def list_proto
-    protos = @items.map &:list_proto
-    protos.join "\n"
-  end
-
   def list_init_proto
     protos = @items.map &:list_init_proto
     protos.join "\n"
@@ -632,9 +619,6 @@ list_init_h.content = """#{h_header("LIBKC3_LIST_INIT_H")}
 /* Heap-allocation functions, call list_delete after use. */
 #{inits.list_new_proto.c_word_wrap}
 
-/* Setters. */
-#{inits.list_proto.c_word_wrap}
-
 #{h_footer("LIBKC3_LIST_INIT_H")}
 """
 list_init_h.commit