Commit 17512ec6fd2e0febdd98d3b45ef8846e4fda19fd

Thomas de Grivel 2024-12-07T23:09:21

wip deserialize

diff --git a/libkc3/deserialize.c b/libkc3/deserialize.c
index 1434355..019c85c 100644
--- a/libkc3/deserialize.c
+++ b/libkc3/deserialize.c
@@ -13,27 +13,37 @@
 #include "assert.h"
 #include "buf.h"
 #include "deserialize.h"
+#include "map.h"
 
-bool *      deserialize_bool (s_buf *buf, bool *dest);
-character * deserialize_character (s_buf *buf, character *c);
-s_list **   deserialize_list (s_buf *buf, s_list **list);
-s_tag *     deserialize_tag (s_buf *buf,  s_tag *tag);
-s_tuple *   deserialize_tuple (s_buf *buf, s_tuple *tuple);
-s8 *        deserialize_s8 (s_buf *buf, s8 *x);
-s16 *       deserialize_s16 (s_buf *buf, s16 *x);
-s32 *       deserialize_s32 (s_buf *buf, s32 *x);
-s64 *       deserialize_s64 (s_buf *buf, s64 *x);
-s_str *     deserialize_str (s_buf *buf, s_str *str);
-sw *        deserialize_sw (s_buf *buf, sw *x);
+bool *      deserialize_bool (s_deserialize *ds, bool *dest);
+character * deserialize_character (s_deserialize *ds, character *c);
 
-u8 * deserialize_u8 (s_buf *buf, u8 *x)
+s_deserialize * deserialize_init (s_deserialize *ds)
 {
-  if (buf_read_u8(buf, x) <= 0)
+  s_deserialize tmp = {0};
+  map_init(&tmp.map, 0);
+  *ds = tmp;
+  return ds;
+}
+
+s_list **   deserialize_list (s_deserialize *ds, s_list **list);
+s_tag *     deserialize_tag (s_deserialize *ds,  s_tag *tag);
+s_tuple *   deserialize_tuple (s_deserialize *ds, s_tuple *tuple);
+s8 *        deserialize_s8 (s_deserialize *ds, s8 *x);
+s16 *       deserialize_s16 (s_deserialize *ds, s16 *x);
+s32 *       deserialize_s32 (s_deserialize *ds, s32 *x);
+s64 *       deserialize_s64 (s_deserialize *ds, s64 *x);
+s_str *     deserialize_str (s_deserialize *ds, s_str *str);
+sw *        deserialize_sw (s_deserialize *ds, sw *x);
+
+u8 * deserialize_u8 (s_deserialize *ds, u8 *x)
+{
+  if (buf_read_u8(&ds->buf, x) <= 0)
     return NULL;
   return x;
 }
 
-u16 * deserialize_u16 (s_buf *buf, u16 *x);
-u32 * deserialize_u32 (s_buf *buf, u32 *x);
-u64 * deserialize_u64 (s_buf *buf, u64 *x);
-uw * deserialize_uw (s_buf *buf, uw *x);
+u16 * deserialize_u16 (s_deserialize *ds, u16 *x);
+u32 * deserialize_u32 (s_deserialize *ds, u32 *x);
+u64 * deserialize_u64 (s_deserialize *ds, u64 *x);
+uw * deserialize_uw (s_deserialize *ds, uw *x);
diff --git a/libkc3/deserialize.h b/libkc3/deserialize.h
index d2b4e49..1d2cfe5 100644
--- a/libkc3/deserialize.h
+++ b/libkc3/deserialize.h
@@ -21,21 +21,21 @@
 
 #include "types.h"
 
-bool *      deserialize_bool (s_buf *buf, bool *dest);
-character * deserialize_character (s_buf *buf, character *c);
-s_list **   deserialize_list (s_buf *buf, s_list **list);
-s_tag *     deserialize_tag (s_buf *buf,  s_tag *tag);
-s_tuple *   deserialize_tuple (s_buf *buf, s_tuple *tuple);
-s8 *        deserialize_s8 (s_buf *buf, s8 *x);
-s16 *       deserialize_s16 (s_buf *buf, s16 *x);
-s32 *       deserialize_s32 (s_buf *buf, s32 *x);
-s64 *       deserialize_s64 (s_buf *buf, s64 *x);
-s_str *     deserialize_str (s_buf *buf, s_str *str);
-sw *        deserialize_sw (s_buf *buf, sw *x);
-u8 *        deserialize_u8 (s_buf *buf, u8 *x);
-u16 *       deserialize_u16 (s_buf *buf, u16 *x);
-u32 *       deserialize_u32 (s_buf *buf, u32 *x);
-u64 *       deserialize_u64 (s_buf *buf, u64 *x);
-uw *        deserialize_uw (s_buf *buf, uw *x);
+bool *      deserialize_bool (s_deserialize *ds, bool *dest);
+character * deserialize_character (s_deserialize *ds, character *c);
+s_list **   deserialize_list (s_deserialize *ds, s_list **list);
+s_tag *     deserialize_tag (s_deserialize *ds,  s_tag *tag);
+s_tuple *   deserialize_tuple (s_deserialize *ds, s_tuple *tuple);
+s8 *        deserialize_s8 (s_deserialize *ds, s8 *x);
+s16 *       deserialize_s16 (s_deserialize *ds, s16 *x);
+s32 *       deserialize_s32 (s_deserialize *ds, s32 *x);
+s64 *       deserialize_s64 (s_deserialize *ds, s64 *x);
+s_str *     deserialize_str (s_deserialize *ds, s_str *str);
+sw *        deserialize_sw (s_deserialize *ds, sw *x);
+u8 *        deserialize_u8 (s_deserialize *ds, u8 *x);
+u16 *       deserialize_u16 (s_deserialize *ds, u16 *x);
+u32 *       deserialize_u32 (s_deserialize *ds, u32 *x);
+u64 *       deserialize_u64 (s_deserialize *ds, u64 *x);
+uw *        deserialize_uw (s_deserialize *ds, uw *x);
 
 #endif /* LIBKC3_DESERIALIZE_H */
diff --git a/libkc3/types.h b/libkc3/types.h
index b064135..761cec8 100644
--- a/libkc3/types.h
+++ b/libkc3/types.h
@@ -173,6 +173,7 @@ typedef struct callable                s_callable;
 typedef struct cfn                     s_cfn;
 typedef struct complex                 s_complex;
 typedef struct cow                     s_cow;
+typedef struct deserialize             s_deserialize;
 typedef struct env                     s_env;
 typedef struct error_handler           s_error_handler;
 typedef struct fact                    s_fact;
@@ -490,6 +491,12 @@ struct cfn {
   bool ready;
 };
 
+struct deserialize {
+  s_map map;
+  s_tuple tuple;
+  s_buf buf;
+};
+
 struct fn {
   bool macro;
   bool special_operator;