Commit 788e0b72e866b277597dad2e08b6569c9ec402fe

Thomas de Grivel 2024-10-25T13:29:58

Struct.put()

diff --git a/.ikc3_history b/.ikc3_history
index 77e9d02..b271fb3 100644
--- a/.ikc3_history
+++ b/.ikc3_history
@@ -1,6 +1,3 @@
-1 + 1
-1 + 10000000000000000000000000000000000000000000000000
-2 * 10000000000000000000000000000000000000000000000000
 type(10000000000000000000000000000000000000000000000000)
 type(1)
 type(255)
@@ -97,3 +94,6 @@ exit
 exit(Ã)
 exit()
 Map.put(%{}, :a, 1)
+Struct.put(%KC3.Operator{}, :sym, :plop)
+type(%KC3.Operator{})
+Struct.put(%KC3.Operator{}, :sym, :plop)
diff --git a/ikc3/.ikc3_history b/ikc3/.ikc3_history
index e1efa27..b271fb3 100644
--- a/ikc3/.ikc3_history
+++ b/ikc3/.ikc3_history
@@ -1,64 +1,3 @@
-HTTP.mime_type(File.ext("Plop.html"))
-(Str) HTTP.mime_type(File.ext("Plop.html"))
-Str.split((Str) HTTP.mime_type(File.ext("Plop.html")), "/")
-List.to_tuple(Str.split((Str) HTTP.mime_type(File.ext("Plop.html")), "/"))
-(Tuple) (Str.split((Str) HTTP.mime_type(File.ext("Plop.html")), "/"))
-(Str.split((Str) HTTP.mime_type(File.ext("Plop.html")), "/"))
-hd(Str.split((Str) HTTP.mime_type(File.ext("Plop.html")), "/"))
-List.first(Str.split((Str) HTTP.mime_type(File.ext("Plop.html")), "/"))
-first(Str.split((Str) HTTP.mime_type(File.ext("Plop.html")), "/"))
-(Sym) first(Str.split((Str) HTTP.mime_type(File.ext("Plop.html")), "/"))
-SH.run("file -b ${SH.escape(path)}")
-size = 10
-if (size > 1024 * 1024 * 1024 * 1024) do                                    
-if (size > 1024 * 1024 * 1024 * 1024) do
- s = size * 10 / 1024 / 1024 / 1024 / 1024
-"#{s / 10}.#{s % 10}Tb"
-size = 100
-if (size > 1024) do
-s = size * 10 / 1024
-"#{s / 10}.#{s % 10}Kb"
-quote %
-10 % 3
-human_size(1024)
-human_size(1025)
-human_size(1024)
-human_size(2024024)
-human_size(100)
-Str.size("abc")
-human_size(0)
-human_size((Uw) 114)
-type(1024 * 1024 * 1024 * 1024)
-human_size((Uw) 114)
-system(["ls", "-l"])
-puts(system(["ls", "-l"]))
-puts(system(["ls", "-l"])
-)
-1 +
-2
-1 + 2
-1 + 100000000000000000000000000000000000000000000
-1 + 100000000000000000000000000000000000000000000.
-1 + 10000000000000000000000000000000000000000000.
-1 + 10000000000000000000000000000000000000000000
-1 + 10000000000000.0
-1 + 10000000000000/2
-1 + 10000000000000/3
-1 + 2/3
-3 **** 4
-def op = %KC3.Operator{sym: :****, symbol_value: fn (a, b) { a * b * 4 }}
-3 **** 4
-3 * 4 * 4
-Sh.escape("abc")
-Sh.escape("abc;")
-puts(Sh.escape("abc;"))
-puts(Sh.escape("abc"))
-puts(Sh.escape("ab c"))
-puts(Sh.escape("ab(c"))
-puts(Sh.escape("abc"))
-1 + 1
-1 + 10000000000000000000000000000000000000000000000000
-2 * 10000000000000000000000000000000000000000000000000
 type(10000000000000000000000000000000000000000000000000)
 type(1)
 type(255)
@@ -94,6 +33,67 @@ sym
 Facts.with(db, [[KC3, :operator, op = ?], [op, :sym, sym = ?]], fn (fact) { puts("#{op} #{sym}") })
 Facts.with(Facts.env_db(), [[KC3, :operator, op = ?], [op, :sym, sym = ?]], fn (fact) { puts("#{op} #{sym}") })
 Facts.with(Facts.env_db(), [[KC3, :operator, op = ?], [op, :sym, sym = ?]], fn (fact) { puts("#{inspect(op)} #{inspect(sym)}") })
-(a = ?) <- 1; a
+[[KC3, :operator, op = ?], [op, :sym, sym = ?]]
+%HTTP.Request{}
+quote %HTTP.Request{}
+quote %HTTP.Request{url: "/plop"}
+quote %HTTP.Request{method: GET, url: "/plop"}
+quote %HTTP.Request{method: GET, url: "/plop", body: void}
+quote [a: 1, b: 2]
+[a: 1, b: 2]
+AList.to_map([a: 1, b: 2])
+1 + 1
+1 + 100000000000000000000000000000000000000000000000000000000
+type(1 + 100000000000000000000000000000000000000000000000000000000)
+type(1 + 1)
+type(255)
+type(256)
+type(1)
+type(1 + 256)
+1 + 256
+(U8) 256
+(U8) 257
+(U16) 1
+(Str) 1
+(Str) 100000000000000000000000000000000000000000000000000000000
+(Str) 100000
+(Str) %{a: 1, b: 2}
+puts(inspect(%{a: 1, b: 2}))
+puts(inspect(quote if true do 1 else %{a: 1, b: 2} end))
+puts(quote if true do 1 else %{a: 1, b: 2} end)
+quote 1 + 1
+type(quote 1 + 1)
+type(quote plop(1 + 1))
+type(quote quote plop(1 + 1))
+def eval = macro (args) { args }
+eval(quote 1 + 1)
+eval(1 + 1)
+def eval = macro (args) { unquote(args) }
+eval(1 + 1)
+def eval = macro (args) { args }
+eval(1 + 1)
+eval(quote 1 + 1)
+a = %KC3.Operator{sym: :****, symbol_value: fn (a, b) { (a + b) * 4 }}
+def op_muul = %KC3.Operator{sym: :****, symbol_value: fn (a, b) { (a + b) * 4 }}
+3 **** 4
+3 //// 4
+def op_muul = %KC3.Operator{sym: :////, symbol_value: fn (a, b) { (a - b) * 4 }}
+3 //// 4
+{a, b, c} = {1, 2, 3}
+%{a: a, b: b, c: c} = %{a: 1, b: 2, c: 3}
 a
-args()
+b
+c
+Compare.str_case_insensitive("Range", "range")
+!Compare.str_case_insensitive("Range", "range")
+! Compare.str_case_insensitive("Range", "range")
+! 0
+quit
+q
+exit
+exit(Ã)
+exit()
+Map.put(%{}, :a, 1)
+Struct.put(%KC3.Operator{}, :sym, :plop)
+type(%KC3.Operator{})
+Struct.put(%KC3.Operator{}, :sym, :plop)
diff --git a/lib/kc3/0.1/struct.kc3 b/lib/kc3/0.1/struct.kc3
index 0836b72..0709195 100644
--- a/lib/kc3/0.1/struct.kc3
+++ b/lib/kc3/0.1/struct.kc3
@@ -2,4 +2,6 @@ defmodule Struct do
 
   def offset = cfn Uw "struct_offset" (Struct, Sym, Result)
 
+  def put = cfn Tag "kc3_struct_put" (Tag, Sym, Tag, Result)
+
 end
diff --git a/libkc3/kc3.c b/libkc3/kc3.c
index a1c08a0..bf508e2 100644
--- a/libkc3/kc3.c
+++ b/libkc3/kc3.c
@@ -393,6 +393,11 @@ void kc3_license (void)
 
 bool kc3_load (const s_str *path)
 {
+  if (true) {
+    err_write_1("kc3_load: ");
+    err_inspect_str(path);
+    err_write_1("\n");
+  }
   return env_load(&g_kc3_env, path);
 }
 
@@ -489,6 +494,26 @@ s_str * kc3_strerror (sw err_no, s_str *dest)
   return str_init_1_alloc(dest, s);
 }
 
+s_tag * kc3_struct_put (const s_tag *s, const s_sym * const *key,
+                        const s_tag *value, s_tag *dest)
+{
+  s_struct tmp;
+  assert(s);
+  assert(key);
+  assert(value);
+  assert(dest);
+  if (s->type != TAG_STRUCT) {
+    err_puts("kc3_struct_put: not a struct");
+    assert(! "kc3_struct_put: not a struct");
+    return NULL;
+  }
+  if (! struct_put(&s->data.struct_, *key, value, &tmp))
+    return NULL;
+  dest->type = TAG_STRUCT;
+  dest->data.struct_ = tmp;
+  return dest;
+}
+
 s_str * kc3_system (const s_list * const *list, s_str *dest)
 {
   char **a = NULL;
diff --git a/libkc3/kc3_main.h b/libkc3/kc3_main.h
index 7a474bc..3effab5 100644
--- a/libkc3/kc3_main.h
+++ b/libkc3/kc3_main.h
@@ -48,6 +48,9 @@ uw *           kc3_offsetof (const s_sym * const *module,
                              const s_sym * const *field, uw *dest);
 sw             kc3_puts (const s_tag *tag);
 s_str *        kc3_str (const s_tag *tag, s_str *dest);
+s_tag *        kc3_struct_put (const s_tag *s,
+                               const s_sym * const *key,
+                               const s_tag *value, s_tag *dest);
 
 /* Operators. */
 s_tag *      kc3_access (const s_tag *tag, const s_list * const *addr,
diff --git a/libkc3/struct.c b/libkc3/struct.c
index 920a93e..bd3ab11 100644
--- a/libkc3/struct.c
+++ b/libkc3/struct.c
@@ -449,6 +449,23 @@ uw * struct_offset (const s_struct *s, const s_sym * const *key,
   return dest;
 }
 
+s_struct * struct_put (const s_struct *s, const s_sym *key,
+                       const s_tag *value, s_struct *dest)
+{
+  s_struct tmp;
+  if (! struct_init_copy(&tmp, s)) {
+    err_puts("struct_put: struct_init_copy");
+    return NULL;
+  }
+  if (! struct_set(&tmp, key, value)) {
+    err_puts("struct_put: struct_set");
+    struct_clean(&tmp);
+    return NULL;
+  }
+  *dest = tmp;
+  return dest;
+}
+
 s_struct * struct_set (s_struct *s, const s_sym *key,
                        const s_tag *value)
 {
@@ -464,19 +481,32 @@ s_struct * struct_set (s_struct *s, const s_sym *key,
   while (i < s->type->map.count) {
     if (s->type->map.key[i].type == TAG_SYM &&
         s->type->map.key[i].data.sym == key) {
-      if (! tag_type(s->type->map.value + i, &type_sym))
+      if (! tag_type(s->type->map.value + i, &type_sym)) {
+        err_puts("struct_set: tag_type");
+        assert(! "struct_set: tag_type");
         return NULL;
+      }
       if (type_sym == &g_sym_Var)
         type_sym = s->type->map.value[i].data.var.type;
       data = (s8 *) s->data + s->type->offset[i];
-      if (! tag_to_const_pointer(value, type_sym, &data_src))
+      if (! tag_to_const_pointer(value, type_sym, &data_src)) {
+        err_puts("struct_set: tag_to_const_pointer");
+        assert(! "struct_set: tag_to_const_pointer");
         return NULL;
+      }
       data_clean(type_sym, data);
-      if (! data_init_copy(type_sym, data, data_src))
+      if (! data_init_copy(type_sym, data, data_src)) {
+        err_puts("struct_set: data_init_copy");
+        assert(! "struct_set: data_init_copy");
         return NULL;
+      }
       return s;
     }
     i++;
   }
+  err_write_1("struct_set: key not found: ");
+  err_inspect_sym(&key);
+  err_write_1("\n");
+  assert(! "struct_set: key not found");
   return NULL;
 }
diff --git a/libkc3/struct.h b/libkc3/struct.h
index 7f3a4d0..fb5b7b7 100644
--- a/libkc3/struct.h
+++ b/libkc3/struct.h
@@ -64,5 +64,7 @@ const s_sym ** struct_get_var_type (const s_struct *s, const s_sym *key,
 uw *           struct_offset (const s_struct *s,
                               const s_sym * const *key,
                               uw *dest);
+s_struct *     struct_put (const s_struct *s, const s_sym *key,
+                           const s_tag *value, s_struct *dest);
 
 #endif /* LIBKC3_STRUCT_H */