Commit 5abf82abd64e6d6cb818fe795f03c2d81cf37d92

Thomas de Grivel 2024-06-25T12:22:11

fix defstruct with var

diff --git a/.ic3_history b/.ic3_history
index b9d63da..6074ff2 100644
--- a/.ic3_history
+++ b/.ic3_history
@@ -1,4 +1,3 @@
-end
 Plop.sq(4)
 defmodule Plop do defoperator :operator_add :+ cfn Tag "tag_mul" (Tag, Tag, Result) 10 :left end
 defmodule Plop do
@@ -97,3 +96,4 @@ cow 1 + cow 1
 cow(1) + cow(1)
 quote [a: 1, b: 2]
 quote [{:a, 1}, {:b, 2}]
+%C3.Operator{}
diff --git a/ic3/.ic3_history b/ic3/.ic3_history
index c3da4f5..6074ff2 100644
--- a/ic3/.ic3_history
+++ b/ic3/.ic3_history
@@ -1,15 +1,3 @@
-Plop.a
-Plop
-defmodule Plop do def a = 1 end
-Plop.a
-defmodule Plop do def a = 1 end
-Plop.a
-defmodule Plop do defoperator :operator_add :+ cfn Tag "tag_add" (Tag, Tag, Result) 10 :left end
-defmodule Plop do defoperator :operator_add :+ cfn Tag "tag_mul" (Tag, Tag, Result) 10 :left end
-defmodule Plop do
-  defoperator :operator_add :+ cfn Tag "tag_mul" (Tag, Tag, Result) 10 :left
-  def sq = fn (x) { x + x }
-end
 Plop.sq(4)
 defmodule Plop do defoperator :operator_add :+ cfn Tag "tag_mul" (Tag, Tag, Result) 10 :left end
 defmodule Plop do
@@ -79,21 +67,33 @@ if true then true end
 if 42 then true end
 if 0 then true end
 if 0 then true else false end
-def reverse = fn (x) { List.reverse(x) }
-reverse
-C3.reverse
-reverse([1, 2])
-C3.reverse
-reverse
-def reverse = fn (x) { List.reverse(x) }
-reverse
-cow 1
-type(cow 1)
+List.map
+List.map([1, 2, 3], fn (x) { x * 2 })
+List.reverse
+List.reverse([1, 2, 3])
+List.reverse
+List.reverse([1, 2])
+def a = 1
+a
+module()
+defmodule Plop do
+  def m = fn () { module() }
+end
+Plop.m()
+module()
+List.reverse([1, 2, 3])
+List.reverse([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
+(List) {1, 2}
+?
 cow 1
-type(cow 1)
+(Tag) cow 1
 a = cow 1
 a
-type(a)
-def operator_muul = %C3.Operator{sym: :****, symbol_value: cfn Tag "tag_mul" (Tag, Tag, Result), operator_precedence: 11, operator_associativity: :left}
-2 **** 4
-1 + 1
+a + 1
+cow 1
+type(cow 1)
+cow 1 + cow 1
+cow(1) + cow(1)
+quote [a: 1, b: 2]
+quote [{:a, 1}, {:b, 2}]
+%C3.Operator{}
diff --git a/libc3/data.c b/libc3/data.c
index d658eae..665448a 100644
--- a/libc3/data.c
+++ b/libc3/data.c
@@ -628,7 +628,7 @@ void * data_init_copy (const s_sym *type, void *data, const void *src)
   if (type == &g_sym_Uw)
     return uw_init_copy(data, src);
   if (type == &g_sym_Var)
-    return data;
+    return var_init_copy(data, src);
   if (type == &g_sym_Void)
     return data;
   st = struct_type_find(type);
diff --git a/libc3/env.c b/libc3/env.c
index 6a145cc..d6ab103 100644
--- a/libc3/env.c
+++ b/libc3/env.c
@@ -1541,6 +1541,7 @@ bool env_eval_struct (s_env *env, const s_struct *s, s_tag *dest)
   s_tag tag = {0};
   s_tag tmp = {0};
   const s_sym *type;
+  const void *value;
   assert(env);
   assert(s);
   assert(dest);
@@ -1557,12 +1558,13 @@ bool env_eval_struct (s_env *env, const s_struct *s, s_tag *dest)
     return false;
   i = 0;
   while (i < t->type->map.count) {
-    if (t->type->map.value[i].type == TAG_VAR)
-      type = t->type->map.value[i].data.var.type;
-    else
-      if (! tag_type(t->type->map.value + i, &type))
-        goto ko;
     if (s->tag) {
+      if (t->type->map.value[i].type == TAG_VAR)
+        type = t->type->map.value[i].data.var.type;
+      else {
+        if (! tag_type(t->type->map.value + i, &type))
+          goto ko;
+      }
       if (! env_eval_tag(env, s->tag + i, &tag))
         goto ko;
       if (! data_init_cast((s8 *) t->data + t->type->offset[i],
@@ -1579,7 +1581,8 @@ bool env_eval_struct (s_env *env, const s_struct *s, s_tag *dest)
       tag_clean(&tag);
     }
     else {
-      const void *value;
+      if (! tag_type(t->type->map.value + i, &type))
+        goto ko;
       if (! tag_to_const_pointer(t->type->map.value + i, type, &value))
         goto ko;
       if (! data_init_copy(type, (s8 *) t->data + t->type->offset[i],
diff --git a/test/ic3/defoperator.in b/test/ic3/defoperator.in
index 5a859e2..be7fb0c 100644
--- a/test/ic3/defoperator.in
+++ b/test/ic3/defoperator.in
@@ -1,3 +1,5 @@
+quote %C3.Operator{}
+%C3.Operator{}
 quote def operator_muul = %C3.Operator{sym: :****, symbol_value: cfn Tag "tag_mul" (Tag, Tag, Result), operator_precedence: 11, operator_associativity: :left}
 def operator_muul = %C3.Operator{sym: :****, symbol_value: cfn Tag "tag_mul" (Tag, Tag, Result), operator_precedence: 11, operator_associativity: :left}
 quote 4 **** 4