Commit 8b3435ca7e7797d75489849e5bf856736cefbee6

Thomas de Grivel 2024-02-04T22:07:24

wip struct

diff --git a/.ic3_history b/.ic3_history
index e8eab8f..cd8dfd8 100644
--- a/.ic3_history
+++ b/.ic3_history
@@ -1,5 +1,3 @@
-123.123000000001
-123.1230000000001
 123.123
 0.0f
 0.0
@@ -97,3 +95,5 @@ quote unquote 1
 quote quote unquote 1
 quote unquote 1
 quote quote unquote 1
+dlopen("libc3/window/.libs/libc3_window.so.0.0")
+dlopen("libc3/window/sdl2/.libs/libc3_window_sdl2.so.0.0")
diff --git a/ic3/.ic3_history b/ic3/.ic3_history
index c0703f6..84ed4d0 100644
--- a/ic3/.ic3_history
+++ b/ic3/.ic3_history
@@ -1,47 +1,99 @@
-a = (U8) { 1, 2, 3 }
-1
-a = (U8) { 1, 2, 3 }
-quote %GL.Sphere{}
-quote %GL.Sphere{segments_u: 100}
-%GL.Sphere{segments_u: 100}
-%GL.Sphere{segments_u: 100 + 1}
-%GL.Sphere{segments_u: (Uw) (100 + 1)}
-a = (U8) { 1, 2 }
-quote a[0]
-a[0]
-a = (U8) { 1, 2 }
-a[0]
-a = (U8) { 1, 2 }
-a[0]
-quote (GL.Vertex) {}
-(GL.Vertex) {}
-(void) 0
-type(void)
-type((void) 0)
-quote (void) 0
-quote (Void) 0
-type((Void) 0)
-quote (Void) 0
+1.2312299999999999e+2
+1.23123e+2
+123.123
+1.231229999999999e+2
+1.231229999999998e+2
+1.231229999999997e+2
+1.231229999999996e+2
+1.231229999999995e+2
+123.123
+1234567.0f
+1.234f
 %GL.Point2D{}
-dlopen("/home/dx/c/thodg/c3-lang/c3/libc3/window/.libs/libc3_window.so.0.0")
-dlopen("/home/dx/c/thodg/c3-lang/c3/libc3/window/sdl2/.libs/libc3_window_sdl2.so.0.0")
-dlopen("/home/dx/c/thodg/c3-lang/c3/libc3/window/.libs/libc3_window.so.0.0")
-dlopen("/home/dx/c/thodg/c3-lang/c3/libc3/window/sdl2/.libs/libc3_window_sdl2.so.0.0")
-dlopen("/home/dx/c/thodg/c3-lang/c3/libc3/window/.libs/libc3_window.so.0.0")
-dlopen("/home/dx/c/thodg/c3-lang/c3/libc3/window/sdl2/.libs/libc3_window_sdl2.so.0.0")
-(Ptr) 0x193a838c800
-type((Ptr) 0x193a838c800)
-type(0x193a838c800)
+GL.Point2D
+type(GL.Point2D)
+quote %GL.Point2D{}
+GL.Point2D{
+GL.Point2D{}
+GL.Point2D{}}
+GL.Point2D{}
+GL.Point2D
+{}
+facts_next_id
+facts_next_id()
+4 * 27
+24 * 7
+4.0/1920.0
+4.0 / 1920.0
+4.0f / 1920.0f
+4.0f / 1200.0f
+U8[]
+D
+U8[]
+U8[1]
+(U8[]) { 1, 2, 3 }
+(U8[]) {1, 2, 3}
+(U8[]) {0}
+(U8[]) {1, 2, 3}
+dlopen
+dlopen("libc3/window/sdl2/.libs/libc3_window_sdl2.so.0.0", 0)
+dlopen("libc3/window/sdl2/.libs/libc3_window_sdl2.so.0.0", (U32) 0)
+dlopen("libc3/window/sdl2/.libs/libc3_window_sdl2.so.0.0", (U32) 1)
+dlopen("libc3/window/sdl2/.libs/libc3_window_sdl2.so.0.0", (U32) 2)
+dlopen("libc3/window/sdl2/.libs/libc3_window_sdl2.so.0.0", (U32) 0)
+dlopen("libc3/window/.libs/libc3_window.so.0.0", (U32) 0)
+dlopen("libc3/window/.libs/libc3_window.so.0.0", (U32) 4)
+dlopen("libc3/window/.libs/libc3_window.so.0.0", (U32) 1)
+dlopen("libc3/window/.libs/libc3_window.so.0.0", (U32) 2)
+dlopen("libc3/window/.libs/libc3_window.so.0.0", (U32) 4)
+dlopen("libc3/window/.libs/libc3_window.so.0.0", (U32) 8)
+dlopen("libc3/window/.libs/libc3_window.so.0.0", (U32) 16)
+dlopen("libc3/window/.libs/libc3_window.so.0.0", (U32) 32)
+dlopen("libc3/window/.libs/libc3_window.so.0.0", (U32) 0)
+dlopen("/home/dx/c/thodg/c3-lang/c3/libc3/window/.libs/libc3_window.so.0.0", (U32) 0)
+getenv("_")
+getenv("SHELL")
+getenv("HOME")
+getenv("USER")
+"a" + "b"
+"ab" + "cd"
+"ab" + "cd" + "ef"
+"Bonjour " + name + " !"
+hello = fn (name) { "Hello, " + name + " !" }
+hello("Patrice")
+hello("Thomas")
+"Hello, " + "Tiyon" + " !"
+hello = fn (name) { "Hello, " + name + " !" }
+hello("Tiyon")
+hello("Patrice")
+"Hello, #{name} !"
+hello("Tiyon")
+hello = fn (name) { "Hello, " + name + " !" }
+hello("Tiyon")
+hello("Baptiste")
 "a"
 "abc"
-"abc#{1}"
 hello = fn (name) { "Hello, #{name} !" }
 hello("Thomas")
-quote "abc"
-quote "abc #{unquote name} !"
-quote "abc #{name} !"
-(Integer[]) {1, 2}
-a = (Integer[]) {1, 2}
-quote (Integer[]) {1, 2}
-quote (Integer[]) {unquote 1, unquote 2}
-quote (Integer[]) {unquote 1 + 1, unquote 2 + 2, 3 + 3}
+hello = fn (name) { "Hello, #{name} !" }
+name("Thomas")
+hello("Thomas")
+hello = fn (name) { "Hello, #{name} !" }
+hello("Paul")
+quote hello("Paul")
+hello
+quote unquote 1
+quote quote unquote 1
+quote unquote 1
+quote quote unquote 1
+dlopen("libc3/window/.libs/libc3_window_debug.so.0.0")
+dlopen("libc3/window/sdl2/.libs/libc3_window_sdl2_debug.so.0.0")
+dlopen("libc3/window/.libs/libc3_window_debug.so.0.0")
+dlopen("libc3/window/.libs/libc3_window.so.0.0")
+dlopen("../libc3/window/.libs/libc3_window_debug.so.0.0")
+dlopen("../libc3/window/sdl2/.libs/libc3_window_sdl2_debug.so.0.0")
+dlopen("../libc3/window/.libs/libc3_window_debug.so.0.0")
+dlopen("../libc3/window/sdl2/.libs/libc3_window_sdl2_debug.so.0.0")
+%GL.Object{}
+type(%GL.Object{})
+%GL.Object{}
diff --git a/libc3/array.c b/libc3/array.c
index 70775cc..645455e 100644
--- a/libc3/array.c
+++ b/libc3/array.c
@@ -258,30 +258,6 @@ s_array * array_init_cast (s_array *array, const s_tag *tag)
   return NULL;
 }
 
-s_array * array_init_copy_shallow (s_array *array, const s_array *src)
-{
-  s_array tmp = {0};
-  assert(array);
-  assert(src);
-  tmp.array_type   = src->array_type;
-  tmp.count        = src->count;
-  tmp.dimension    = src->dimension;
-  tmp.element_type = src->element_type;
-  tmp.size         = src->size;
-  if (tmp.dimension) {
-    if (! (tmp.dimensions = calloc(tmp.dimension,
-                                   sizeof(s_array_dimension)))) {
-      assert(! "array_init_copy: out of memory: dimensions");
-      warnx("array_init_copy: out of memory: dimensions");
-      return NULL;
-    }
-    memcpy(tmp.dimensions, src->dimensions,
-           src->dimension * sizeof(s_array_dimension));
-  }
-  *array = tmp;
-  return array;
-}
-
 s_array * array_init_copy (s_array *a, const s_array *src)
 {
   u8 *data_tmp;
@@ -352,6 +328,30 @@ s_array * array_init_copy (s_array *a, const s_array *src)
   return NULL;
 }
 
+s_array * array_init_copy_shallow (s_array *array, const s_array *src)
+{
+  s_array tmp = {0};
+  assert(array);
+  assert(src);
+  tmp.array_type   = src->array_type;
+  tmp.count        = src->count;
+  tmp.dimension    = src->dimension;
+  tmp.element_type = src->element_type;
+  tmp.size         = src->size;
+  if (tmp.dimension) {
+    if (! (tmp.dimensions = calloc(tmp.dimension,
+                                   sizeof(s_array_dimension)))) {
+      assert(! "array_init_copy: out of memory: dimensions");
+      warnx("array_init_copy: out of memory: dimensions");
+      return NULL;
+    }
+    memcpy(tmp.dimensions, src->dimensions,
+           src->dimension * sizeof(s_array_dimension));
+  }
+  *array = tmp;
+  return array;
+}
+
 s_array * array_init_void (s_array *array)
 {
   s_array tmp = {0};
diff --git a/libc3/c3.c b/libc3/c3.c
index f7b18c8..52c2f8f 100644
--- a/libc3/c3.c
+++ b/libc3/c3.c
@@ -48,7 +48,7 @@ void ** c3_dlopen (const s_str *path, void **dest)
 {
   assert(path);
   assert(dest);
-  printf("dlopen %s -> %p\n", path->ptr.pchar, (void *) dest);
+  printf("dlopen %p := %s\n", (void *) dest, path->ptr.pchar);
   *dest = dlopen(path->ptr.pchar, RTLD_GLOBAL);
   return dest;
 }
diff --git a/libc3/env.c b/libc3/env.c
index c51c97d..98b2301 100644
--- a/libc3/env.c
+++ b/libc3/env.c
@@ -1011,7 +1011,9 @@ bool env_eval_struct (s_env *env, const s_struct *s, s_tag *dest)
       const void *value;
       if (! tag_to_const_pointer(t->type->map.value + i, type, &value))
         goto ko;
-      data_init_copy(type, (s8 *) t->data + t->type->offset[i], value);
+      if (! data_init_copy(type, (s8 *) t->data + t->type->offset[i],
+                           value))
+        goto ko;
     }
     i++;
   }
diff --git a/libc3/struct.c b/libc3/struct.c
index 5945313..0260e56 100644
--- a/libc3/struct.c
+++ b/libc3/struct.c
@@ -10,7 +10,7 @@
  * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
  * THIS SOFTWARE.
  */
-#include <assert.h>
+#include "assert.h"
 #include <err.h>
 #include <stdlib.h>
 #include <string.h>
@@ -144,6 +144,11 @@ s_struct * struct_init_copy (s_struct *s, const s_struct *src)
   if (src->data) {
     tmp.free_data = true;
     tmp.data = calloc(1, tmp.type->size);
+    if (! tmp.data) {
+      err_puts("struct_init_copy: failed to allocate memory for data");
+      assert(! "struct_init_copy: failed to allocate memory for data");
+      return NULL;
+    }
     i = 0;
     while (i < tmp.type->map.count) {
       if (! tag_type(tmp.type->map.value + i, &sym) ||
@@ -153,8 +158,15 @@ s_struct * struct_init_copy (s_struct *s, const s_struct *src)
       i++;
     }
   }
-  if (src->tag) {
+  else if (src->tag) {
     tmp.tag = calloc(tmp.type->map.count, sizeof(s_tag));
+    if (! tmp.tag) {
+      err_puts("struct_init_copy:"
+               " failed to allocate memory for tags (1)");
+      assert(! "struct_init_copy:"
+             " failed to allocate memory for tags (1)");
+      return NULL;
+    }
     i = 0;
     while (i < tmp.type->map.count) {
       if (! tag_init_copy(tmp.tag + i, src->tag + i))
@@ -184,7 +196,7 @@ s_struct * struct_init_from_lists (s_struct *s, const s_sym *module,
     return NULL;
   tmp.tag = calloc(tmp.type->map.count, sizeof(s_tag));
   if (! tmp.tag) {
-    warn("struct_init_from_lists: tag");
+    err_puts("struct_init_from_lists: tag");
     assert(! "struct_init_from_lists: failed to allocate memory");
     return NULL;
   }
diff --git a/libc3/window/sdl2/gl_object.c b/libc3/window/sdl2/gl_object.c
index 8c4ec28..7be72c9 100644
--- a/libc3/window/sdl2/gl_object.c
+++ b/libc3/window/sdl2/gl_object.c
@@ -34,11 +34,18 @@ s_gl_object * gl_object_allocate (s_gl_object *object, uw vertex_count,
 void gl_object_clean (s_gl_object *object)
 {
   assert(object);
+  assert(glGetError() == GL_NO_ERROR);
+  if (object->gl_vao)
+    glDeleteVertexArrays(1, &object->gl_vao);
+  assert(glGetError() == GL_NO_ERROR);
+  if (object->gl_vbo)
+    glDeleteBuffers(1, &object->gl_vbo);
+  assert(glGetError() == GL_NO_ERROR);
+  if (object->gl_ebo)
+    glDeleteBuffers(1, &object->gl_ebo);
+  assert(glGetError() == GL_NO_ERROR);
   array_clean(&object->vertex);
   array_clean(&object->triangle);
-  glDeleteVertexArrays(1, &object->gl_vao);
-  glDeleteBuffers(1, &object->gl_vbo);
-  glDeleteBuffers(1, &object->gl_ebo);
 }
 
 s_gl_object * gl_object_init (s_gl_object *object)