Commit 2f3c1d5dbfc2298934b2888bcd8911b008f4975e

Thomas de Grivel 2022-11-25T22:36:38

wip function

diff --git a/libc3/buf_inspect.c b/libc3/buf_inspect.c
index 0769c0f..a76d3e1 100644
--- a/libc3/buf_inspect.c
+++ b/libc3/buf_inspect.c
@@ -794,6 +794,8 @@ sw buf_inspect_tag (s_buf *buf, const s_tag *tag)
     return buf_inspect_character(buf, tag->data.character);
   case TAG_F32:     return buf_inspect_f32(buf, tag->data.f32);
   case TAG_F64:     return buf_inspect_f64(buf, tag->data.f64);
+  case TAG_FUNCTION:
+    return buf_inspect_uw_hex(buf, (uw) &tag->data.function);
   case TAG_IDENT:   return buf_inspect_ident(buf, &tag->data.ident);
   case TAG_INTEGER: return buf_inspect_integer(buf, &tag->data.integer);
   case TAG_LIST:    return buf_inspect_list(buf, tag->data.list);
@@ -820,32 +822,34 @@ sw buf_inspect_tag_size (const s_tag *tag)
 {
   assert(tag);
   switch(tag->type.type) {
-  case TAG_VOID:    return 0;
-  case TAG_BOOL:    return buf_inspect_bool_size(tag->data.bool);
+  case TAG_VOID:     return 0;
+  case TAG_BOOL:     return buf_inspect_bool_size(tag->data.bool);
   case TAG_CALL:
   case TAG_CALL_FUNCTION:
   case TAG_CALL_MACRO:
     return buf_inspect_call_size(&tag->data.call);
   case TAG_CHARACTER:
     return buf_inspect_character_size(tag->data.character);
-  case TAG_F32:     return buf_inspect_f32_size(tag->data.f32);
-  case TAG_F64:     return buf_inspect_f64_size(tag->data.f64);
-  case TAG_IDENT:   return buf_inspect_ident_size(&tag->data.ident);
-  case TAG_INTEGER: return buf_inspect_integer_size(&tag->data.integer);
-  case TAG_LIST:    return buf_inspect_list_size(tag->data.list);
-  case TAG_PTAG:    return buf_inspect_ptag_size(tag->data.ptag);
-  case TAG_QUOTE:   return buf_inspect_quote_size(tag->data.quote);
-  case TAG_S8:      return buf_inspect_s8_size(tag->data.s8);
-  case TAG_S16:     return buf_inspect_s16_size(tag->data.s16);
-  case TAG_S32:     return buf_inspect_s32_size(tag->data.s32);
-  case TAG_S64:     return buf_inspect_s64_size(tag->data.s64);
-  case TAG_STR:     return buf_inspect_str_size(&tag->data.str);
-  case TAG_SYM:     return buf_inspect_sym_size(tag->data.sym);
-  case TAG_TUPLE:   return buf_inspect_tuple_size(&tag->data.tuple);
-  case TAG_U8:      return buf_inspect_u8_size(tag->data.u8);
-  case TAG_U16:     return buf_inspect_u16_size(tag->data.u16);
-  case TAG_U32:     return buf_inspect_u32_size(tag->data.u32);
-  case TAG_U64:     return buf_inspect_u64_size(tag->data.u64);
+  case TAG_F32:      return buf_inspect_f32_size(tag->data.f32);
+  case TAG_F64:      return buf_inspect_f64_size(tag->data.f64);
+  case TAG_FUNCTION: return BUF_INSPECT_UW_HEX_SIZE;
+  case TAG_IDENT:    return buf_inspect_ident_size(&tag->data.ident);
+  case TAG_INTEGER:
+    return buf_inspect_integer_size(&tag->data.integer);
+  case TAG_LIST:     return buf_inspect_list_size(tag->data.list);
+  case TAG_PTAG:     return buf_inspect_ptag_size(tag->data.ptag);
+  case TAG_QUOTE:    return buf_inspect_quote_size(tag->data.quote);
+  case TAG_S8:       return buf_inspect_s8_size(tag->data.s8);
+  case TAG_S16:      return buf_inspect_s16_size(tag->data.s16);
+  case TAG_S32:      return buf_inspect_s32_size(tag->data.s32);
+  case TAG_S64:      return buf_inspect_s64_size(tag->data.s64);
+  case TAG_STR:      return buf_inspect_str_size(&tag->data.str);
+  case TAG_SYM:      return buf_inspect_sym_size(tag->data.sym);
+  case TAG_TUPLE:    return buf_inspect_tuple_size(&tag->data.tuple);
+  case TAG_U8:       return buf_inspect_u8_size(tag->data.u8);
+  case TAG_U16:      return buf_inspect_u16_size(tag->data.u16);
+  case TAG_U32:      return buf_inspect_u32_size(tag->data.u32);
+  case TAG_U64:      return buf_inspect_u64_size(tag->data.u64);
   case TAG_VAR:
     assert(! "variable");
     errx(1, "buf_inspect_tag_size: variable");
@@ -1052,12 +1056,6 @@ sw buf_inspect_u64_hex (s_buf *buf, u64 i)
   return 16;
 }
 
-sw buf_inspect_u64_hex_size (u64 i)
-{
-  (void) i;
-  return 16;
-}
-
 sw buf_inspect_u64_size (u64 i)
 {
   sw size = 0;
@@ -1080,12 +1078,6 @@ sw buf_inspect_uw_hex (s_buf *buf, uw i)
   return -1;
 }
 
-sw buf_inspect_uw_hex_size (uw i)
-{
-  (void) i;
-  return sizeof(uw) / 4;
-}
-
 sw buf_inspect_var (s_buf *buf, const s_tag *var)
 {
   sw r;
diff --git a/libc3/buf_inspect.h b/libc3/buf_inspect.h
index 7f19b8d..ab56126 100644
--- a/libc3/buf_inspect.h
+++ b/libc3/buf_inspect.h
@@ -22,7 +22,8 @@
 
 #include "types.h"
 
-extern const sw buf_inspect_str_byte_size;
+#define BUF_INSPECT_U64_HEX_SIZE 16
+#define BUF_INSPECT_UW_HEX_SIZE (sizeof(uw) / 4)
 
 sw buf_inspect_bool (s_buf *buf, e_bool b);
 sw buf_inspect_bool_size (e_bool b);
@@ -38,6 +39,8 @@ sw buf_inspect_f64_size (f64 x);
 sw buf_inspect_fact (s_buf *buf, const s_fact *fact);
 sw buf_inspect_fact_size (const s_fact *fact);
 sw buf_inspect_fact_spec (s_buf *buf, p_facts_spec spec);
+sw buf_inspect_function (s_buf *buf, const s_function *fun);
+sw buf_inspect_function_size (const s_function *fun);
 sw buf_inspect_ident (s_buf *buf, const s_ident *ident);
 sw buf_inspect_ident_size (const s_ident *ident);
 sw buf_inspect_integer (s_buf *buf, const s_integer *x);
@@ -79,7 +82,6 @@ sw buf_inspect_u64_hex_size (u64 i);
 sw buf_inspect_u64 (s_buf *buf, u64 i);
 sw buf_inspect_u64_size (u64 i);
 sw buf_inspect_uw_hex (s_buf *buf, uw i);
-sw buf_inspect_uw_hex_size (uw i);
 sw buf_inspect_var (s_buf *buf, const s_tag *var);
 
 #endif /* BUF_INSPECT_H */
diff --git a/libc3/eval.c b/libc3/eval.c
index 9e3cd0b..eac1093 100644
--- a/libc3/eval.c
+++ b/libc3/eval.c
@@ -117,6 +117,7 @@ s_tag * eval_tag (s_env *env, s_tag *tag, s_tag *dest)
   case TAG_CHARACTER:
   case TAG_F32:
   case TAG_F64:
+  case TAG_FUNCTION:
   case TAG_INTEGER:
   case TAG_LIST:
   case TAG_PTAG:
diff --git a/libc3/tag.c b/libc3/tag.c
index 0dd83ce..a35df0b 100644
--- a/libc3/tag.c
+++ b/libc3/tag.c
@@ -158,6 +158,7 @@ s8 tag_compare (const s_tag *a, const s_tag *b) {
                                                b->data.character);
   case TAG_F32: return f32_compare(a->data.f32, b->data.f32);
   case TAG_F64: return f64_compare(a->data.f64, b->data.f64);
+  case TAG_FUNCTION: return ptr_compare(a, b);
   case TAG_IDENT: return ident_compare(&a->data.ident, &b->data.ident);
   case TAG_INTEGER: return integer_compare(&a->data.integer,
                                            &b->data.integer);
@@ -249,6 +250,7 @@ t_hash_context * tag_hash_update (t_hash_context *context,
     character_hash_update(context, tag->data.character);          break;
   case TAG_F32: f32_hash_update(context, tag->data.f32);          break;
   case TAG_F64: f64_hash_update(context, tag->data.f64);          break;
+  case TAG_FUNCTION: u64_hash_update(context, (u64) tag);         break;
   case TAG_IDENT: ident_hash_update(context, &tag->data.ident);   break;
   case TAG_INTEGER:
     integer_hash_update(context, &tag->data.integer);             break;
diff --git a/libc3/types.h b/libc3/types.h
index 02e7e7c..3007a38 100644
--- a/libc3/types.h
+++ b/libc3/types.h
@@ -52,6 +52,7 @@ typedef enum {
   TAG_CHARACTER,
   TAG_F32,
   TAG_F64,
+  TAG_FUNCTION,
   TAG_IDENT,
   TAG_INTEGER,
   TAG_S64,
@@ -242,6 +243,7 @@ union tag_data {
   character    character;
   f32          f32;
   f64          f64;
+  s_function   function;
   s_ident      ident;
   s_integer    integer;
   s_list      *list;