Commit dd68fc428e9427fc46ba3cb1e7b5b5e4d3da2279

Thomas de Grivel 2024-07-09T10:51:21

c3_access: map_access, struct_access

diff --git a/libc3/c3.c b/libc3/c3.c
index 761754b..7f338de 100644
--- a/libc3/c3.c
+++ b/libc3/c3.c
@@ -18,7 +18,9 @@
 #include "buf.h"
 #include "c3_main.h"
 #include "env.h"
+#include "map.h"
 #include "str.h"
+#include "struct.h"
 #include "sym.h"
 #include "tag.h"
 
@@ -36,10 +38,20 @@ s_tag * c3_access (const s_tag *tag, const s_sym * const *sym,
   assert(tag);
   assert(sym);
   assert(dest);
-  (void) tag;
-  (void) sym;
-  tag_init(dest);
-  return dest;
+  switch (tag->type) {
+  case TAG_MAP:
+    return map_access(&tag->data.map, *sym, dest);
+  case TAG_STRUCT:
+    return struct_access(&tag->data.struct_, *sym, dest);
+  default:
+    break;
+  }
+  err_write_1("c3_access: cannot access tag type ");
+  err_write_1(tag_type_to_string(tag->type));
+  err_write_1(" for key ");
+  err_inspect_sym(sym);
+  err_write_1("\n");
+  return NULL;
 }
 
 void c3_break (void)
diff --git a/libc3/struct.c b/libc3/struct.c
index 89486b4..9e52df1 100644
--- a/libc3/struct.c
+++ b/libc3/struct.c
@@ -23,6 +23,17 @@
 #include "tag.h"
 #include "tag_type.h"
 
+s_tag * struct_access (const s_struct *s, const s_sym *key, s_tag *dest)
+{
+  s_tag *tag;
+  tag = struct_get_tag((s_struct *) s, key);
+  if (! tag)
+    return NULL;
+  if (! tag_init_copy(dest, tag))
+    return NULL;
+  return dest;
+}
+
 s_struct * struct_allocate (s_struct *s)
 {
   s_struct tmp;
diff --git a/libc3/struct.h b/libc3/struct.h
index ceb5780..bb32c89 100644
--- a/libc3/struct.h
+++ b/libc3/struct.h
@@ -46,6 +46,8 @@ s_struct * struct_set (s_struct *s, const s_sym *key,
                        const s_tag *value);
 
 /* Observers. */
+s_tag *        struct_access (const s_struct *s, const s_sym *key,
+                              s_tag *dest);
 bool           struct_find_key_index (const s_struct *s, const s_sym *key,
                                       uw *dest);
 void *         struct_get (s_struct *s, const s_sym *key);