Commit 929086a1d7ac5ed9bd2259155a78c791f6532b16

Thomas de Grivel 2024-03-19T18:07:49

refactor call_get and ident_get

diff --git a/libc3/call.c b/libc3/call.c
index a45072d..a7cd4bb 100644
--- a/libc3/call.c
+++ b/libc3/call.c
@@ -38,7 +38,7 @@ void call_clean (s_call *call)
 
 bool call_get (s_call *call, s_facts *facts)
 {
-  s_facts_with_cursor cursor;
+  s_facts_cursor cursor;
   s_tag tag_ident;
   s_tag tag_is_a;
   s_tag tag_macro;
@@ -50,14 +50,14 @@ bool call_get (s_call *call, s_facts *facts)
   s_tag tag_symbol_value;
   s_tag tag_var;
   tag_init_ident(&tag_ident, &call->ident);
-  tag_init_1(    &tag_is_a, ":is_a");
-  tag_init_1(    &tag_macro, ":macro");
+  tag_init_sym(  &tag_is_a, &g_sym_is_a);
+  tag_init_sym(  &tag_macro, &g_sym_macro);
   tag_init_sym(  &tag_module_name, call->ident.module);
-  tag_init_1(    &tag_operator, ":operator");
-  tag_init_1(    &tag_special_operator, ":special_operator");
+  tag_init_sym(  &tag_operator, &g_sym_operator);
+  tag_init_sym(  &tag_special_operator, &g_sym_special_operator);
   tag_init_sym(  &tag_sym, call->ident.sym);
-  tag_init_1(    &tag_symbol, ":symbol");
-  tag_init_1(    &tag_symbol_value, ":symbol_value");
+  tag_init_sym(  &tag_symbol, &g_sym_symbol);
+  tag_init_sym(  &tag_symbol_value, &g_sym_symbol_value);
   tag_init_var(  &tag_var);
   if (! facts_find_fact_by_tags(facts, &tag_module_name,
                                 &tag_symbol, &tag_ident) &&
@@ -70,10 +70,10 @@ bool call_get (s_call *call, s_facts *facts)
     err_write_1("\n");
     return false;
   }
-  if (! facts_with(facts, &cursor, (t_facts_spec) {
-        &tag_ident, &tag_symbol_value, &tag_var, NULL, NULL }))
+  if (! facts_with_tags(facts, &cursor, &tag_ident,
+                        &tag_symbol_value, &tag_var))
     return false;
-  if (facts_with_cursor_next(&cursor)) {
+  if (facts_cursor_next(&cursor)) {
     if (tag_var.type == TAG_FN)
       call->fn = fn_new_copy(&tag_var.data.fn);
     else if (tag_var.type == TAG_CFN)
@@ -82,11 +82,11 @@ bool call_get (s_call *call, s_facts *facts)
       err_write_1("call_get: ");
       err_inspect_ident(&call->ident);
       err_puts(" is not a function");
-      facts_with_cursor_clean(&cursor);
+      facts_cursor_clean(&cursor);
       return false;
     }
   }
-  facts_with_cursor_clean(&cursor);
+  facts_cursor_clean(&cursor);
   if (facts_find_fact_by_tags(facts, &tag_ident, &tag_is_a,
                               &tag_macro)) {
     if (call->fn)
@@ -106,7 +106,7 @@ bool call_get (s_call *call, s_facts *facts)
 
 bool call_op_get (s_call *call, s_facts *facts)
 {
-  s_facts_with_cursor cursor;
+  s_facts_cursor cursor;
   s_tag tag_ident;
   s_tag tag_is_a;
   s_tag tag_macro;
@@ -117,32 +117,27 @@ bool call_op_get (s_call *call, s_facts *facts)
   s_tag tag_symbol_value;
   s_tag tag_var;
   tag_init_ident(&tag_ident, &call->ident);
-  tag_init_1(    &tag_is_a,     ":is_a");
-  tag_init_1(    &tag_macro,    ":macro");
+  tag_init_sym(  &tag_is_a, &g_sym_is_a);
+  tag_init_sym(  &tag_macro, &g_sym_macro);
   tag_init_sym(  &tag_module_name, call->ident.module);
-  tag_init_1(    &tag_special_operator, ":special_operator");
+  tag_init_sym(  &tag_special_operator, &g_sym_special_operator);
   tag_init_sym(  &tag_sym, call->ident.sym);
-  tag_init_1(    &tag_symbol,   ":symbol");
-  tag_init_1(    &tag_symbol,   ":symbol_value");
+  tag_init_sym(  &tag_symbol, &g_sym_symbol);
+  tag_init_sym(  &tag_symbol, &g_sym_symbol_value);
   tag_init_var(  &tag_var);
-  facts_with(facts, &cursor, (t_facts_spec) {
-      &tag_module_name,
-      &tag_symbol, &tag_ident,    /* module exports symbol */
-      NULL, NULL });
-  if (! facts_with_cursor_next(&cursor)) {
-    err_write_1("call_get: symbol ");
+  if (! facts_find_fact_by_tags(facts, &tag_module_name,
+                                &tag_symbol, &tag_ident)) {
+    err_write_1("call_op_get: symbol ");
     err_write_1(call->ident.sym->str.ptr.pchar);
     err_write_1(" not found in module ");
     err_write_1(call->ident.module->str.ptr.pchar);
     err_write_1("\n");
-    facts_with_cursor_clean(&cursor);
     return false;
   }
-  facts_with_cursor_clean(&cursor);
-  facts_with(facts, &cursor, (t_facts_spec) {
-      &tag_ident, &tag_symbol_value, &tag_var,
-      NULL, NULL });
-  if (facts_with_cursor_next(&cursor)) {
+  if (! facts_with_tags(facts, &cursor, &tag_ident, &tag_symbol_value,
+                        &tag_var))
+    return false;
+  if (facts_cursor_next(&cursor)) {
     if (tag_var.type == TAG_CFN)
       call->cfn = cfn_new_copy(&tag_var.data.cfn);
     else if (tag_var.type == TAG_FN)
@@ -151,28 +146,25 @@ bool call_op_get (s_call *call, s_facts *facts)
       err_write_1("call_op_get: ");
       err_inspect_ident(&call->ident);
       err_puts(" is not a function");
+      facts_cursor_clean(&cursor);
       return false;
     }
   }
-  facts_with_cursor_clean(&cursor);
-  facts_with(facts, &cursor, (t_facts_spec) {
-      &tag_ident, &tag_is_a, &tag_macro, NULL, NULL });
-  if (facts_with_cursor_next(&cursor)) {
+  facts_cursor_clean(&cursor);
+  if (facts_find_fact_by_tags(facts, &tag_ident, &tag_is_a,
+                              &tag_macro)) {
     if (call->fn)
       call->fn->macro = true;
     if (call->cfn)
       call->cfn->macro = true;
   }
-  facts_with_cursor_clean(&cursor);
-  facts_with(facts, &cursor, (t_facts_spec) {
-      &tag_ident, &tag_is_a, &tag_special_operator, NULL, NULL});
-  if (facts_with_cursor_next(&cursor)) {
+  if (facts_find_fact_by_tags(facts, &tag_ident, &tag_is_a,
+                              &tag_special_operator)) {
     if (call->fn)
       call->fn->special_operator = true;
     if (call->cfn)
       call->cfn->special_operator = true;
   }
-  facts_with_cursor_clean(&cursor);
   return true;
 }
 
diff --git a/libc3/ident.c b/libc3/ident.c
index e315533..00259c4 100644
--- a/libc3/ident.c
+++ b/libc3/ident.c
@@ -72,13 +72,13 @@ s_tag * ident_get (const s_ident *ident, s_facts *facts, s_tag *dest)
   if (! module_ensure_loaded(module, facts))
     return NULL;
   tag_init_ident(&tag_ident, ident);
-  tag_init_1(    &tag_is_a, ":is_a");
-  tag_init_1(    &tag_macro, ":macro");
+  tag_init_sym(  &tag_is_a, &g_sym_is_a);
+  tag_init_sym(  &tag_macro, &g_sym_macro);
   tag_init_sym(  &tag_module, module);
-  tag_init_1(    &tag_special_operator, ":special_operator");
+  tag_init_sym(  &tag_special_operator, &g_sym_special_operator);
   tag_init_sym(  &tag_sym, ident->sym);
-  tag_init_1(    &tag_symbol, ":symbol");
-  tag_init_1(    &tag_symbol_value, ":symbol_value");
+  tag_init_sym(  &tag_symbol, &g_sym_symbol);
+  tag_init_sym(  &tag_symbol_value, &g_sym_symbol_value);
   tag_init_var(  &tag_var);
   facts_with(facts, &cursor, (t_facts_spec) {
       &tag_module,
diff --git a/libc3/sym.c b/libc3/sym.c
index d33d0cf..73b8724 100644
--- a/libc3/sym.c
+++ b/libc3/sym.c
@@ -76,6 +76,8 @@ const s_sym g_sym_end             = {{{NULL},  3, {"end"}}};
 const s_sym g_sym_if_then_else    = {{{NULL}, 12, {"if_then_else"}}};
 const s_sym g_sym_is_a            = {{{NULL},  4, {"is_a"}}};
 const s_sym g_sym_load_time       = {{{NULL},  9, {"load_time"}}};
+const s_sym g_sym_macro           = {{{NULL},  5, {"macro"}}};
+const s_sym g_sym_operator        = {{{NULL},  8, {"operator"}}};
 const s_sym g_sym_operator_pin    = {{{NULL}, 12, {"operator_pin"}}};
 const s_sym g_sym_r               = {{{NULL},  1, {"r"}}};
 const s_sym g_sym_rw              = {{{NULL},  2, {"rw"}}};
@@ -84,6 +86,8 @@ const s_sym g_sym_rx              = {{{NULL},  2, {"rx"}}};
 const s_sym g_sym_special_operator =
   {{{NULL}, 16, {"special_operator"}}};
 const s_sym g_sym_struct_type     = {{{NULL}, 11, {"struct_type"}}};
+const s_sym g_sym_symbol          = {{{NULL},  6, {"symbol"}}};
+const s_sym g_sym_symbol_value    = {{{NULL}, 12, {"symbol_value"}}};
 const s_sym g_sym_w               = {{{NULL},  1, {"w"}}};
 const s_sym g_sym_wx              = {{{NULL},  2, {"wx"}}};
 const s_sym g_sym_x               = {{{NULL},  1, {"x"}}};
@@ -320,6 +324,8 @@ void sym_init_g_sym (void)
   sym_register(&g_sym_if_then_else, NULL);
   sym_register(&g_sym_is_a, NULL);
   sym_register(&g_sym_load_time, NULL);
+  sym_register(&g_sym_macro, NULL);
+  sym_register(&g_sym_operator, NULL);
   sym_register(&g_sym_operator_pin, NULL);
   sym_register(&g_sym_r, NULL);
   sym_register(&g_sym_rw, NULL);
@@ -327,6 +333,8 @@ void sym_init_g_sym (void)
   sym_register(&g_sym_rx, NULL);
   sym_register(&g_sym_special_operator, NULL);
   sym_register(&g_sym_struct_type, NULL);
+  sym_register(&g_sym_symbol, NULL);
+  sym_register(&g_sym_symbol_value, NULL);
   sym_register(&g_sym_w, NULL);
   sym_register(&g_sym_wx, NULL);
   sym_register(&g_sym_x, NULL);
diff --git a/libc3/sym.h b/libc3/sym.h
index 8d5ad82..dd31892 100644
--- a/libc3/sym.h
+++ b/libc3/sym.h
@@ -81,6 +81,8 @@ extern const s_sym g_sym_end;
 extern const s_sym g_sym_if_then_else;
 extern const s_sym g_sym_is_a;
 extern const s_sym g_sym_load_time;
+extern const s_sym g_sym_macro;
+extern const s_sym g_sym_operator;
 extern const s_sym g_sym_operator_pin;
 extern const s_sym g_sym_r;
 extern const s_sym g_sym_rw;
@@ -88,6 +90,8 @@ extern const s_sym g_sym_rwx;
 extern const s_sym g_sym_rx;
 extern const s_sym g_sym_special_operator;
 extern const s_sym g_sym_struct_type;
+extern const s_sym g_sym_symbol;
+extern const s_sym g_sym_symbol_value;
 extern const s_sym g_sym_w;
 extern const s_sym g_sym_wx;
 extern const s_sym g_sym_x;