Commit 9d080f5624153f9fc56a8d77567ed4af51d4a341

Thomas de Grivel 2024-03-29T20:41:39

defmodule + def

diff --git a/ic3/.ic3_history b/ic3/.ic3_history
index f2c9602..16965a7 100644
--- a/ic3/.ic3_history
+++ b/ic3/.ic3_history
@@ -1,7 +1,3 @@
-end
-dt(200)
-dt(a)
-a = [1, 2, 3]
 dt(a)
 def dt = macro (x) do
   quote do
@@ -97,3 +93,7 @@ Plop.a
 defmodule Plop do def a = 1 end
 Plop.a
 Plop
+defmodule Plop do def a = 1 end
+Plop.a
+defmodule Plop do def a = 1 end
+Plop.a
diff --git a/libc3/env.c b/libc3/env.c
index 9be7826..92f9883 100644
--- a/libc3/env.c
+++ b/libc3/env.c
@@ -768,6 +768,7 @@ bool env_eval_ident (s_env *env, const s_ident *ident, s_tag *dest)
     err_write_1("env_eval_ident: unbound ident: ");
     err_inspect_ident(ident);
     err_write_1("\n");
+    assert(! "env_eval_ident: unbound ident");
     return false;
   }
   tag_init_copy(dest, tag);
diff --git a/libc3/ident.c b/libc3/ident.c
index b4c3a1a..1ed03cd 100644
--- a/libc3/ident.c
+++ b/libc3/ident.c
@@ -15,6 +15,7 @@
 #include "buf_inspect.h"
 #include "character.h"
 #include "env.h"
+#include "facts.h"
 #include "facts_with.h"
 #include "facts_with_cursor.h"
 #include "module.h"
@@ -80,15 +81,9 @@ s_tag * ident_get (const s_ident *ident, s_facts *facts, s_tag *dest)
   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,
-      &tag_symbol, &tag_ident,    /* module exports symbol */
-      NULL, NULL });
-  if (! facts_with_cursor_next(&cursor)) {
-    facts_with_cursor_clean(&cursor);
+  if (! facts_find_fact_by_tags(facts, &tag_module, &tag_symbol,
+                                &tag_ident))
     return NULL;
-  }
-  facts_with_cursor_clean(&cursor);
   facts_with(facts, &cursor, (t_facts_spec) {
       &tag_ident, &tag_symbol_value, &tag_var,
       NULL, NULL });
diff --git a/libc3/module.c b/libc3/module.c
index 8075919..065edc3 100644
--- a/libc3/module.c
+++ b/libc3/module.c
@@ -26,30 +26,26 @@
 
 bool module_ensure_loaded (const s_sym *module, s_facts *facts)
 {
-  s_facts_with_cursor cursor;
   s_tag tag_module_name;
   s_tag tag_is_a;
   s_tag tag_module;
   if (module_is_loading(module))
     return true;
-  tag_init_sym(&tag_module_name, module);
   tag_init_sym(&tag_is_a, &g_sym_is_a);
   tag_init_sym(&tag_module, &g_sym_module);
-  facts_with(facts, &cursor, (t_facts_spec) {
-      &tag_module_name,
-      &tag_is_a, &tag_module,     /* module exists */
-      NULL, NULL });
-  if (! facts_with_cursor_next(&cursor)) {
+  tag_init_sym(&tag_module_name, module);
+  if (! facts_find_fact_by_tags(facts, &tag_module_name, &tag_is_a,
+                                &tag_module)) {
     if (! module_load(module, facts)) {
       err_write_1("module_ensure_loaded: module not found: ");
       err_puts(module->str.ptr.pchar);
       assert(! "module_ensure_loaded: module not found");
-      facts_with_cursor_clean(&cursor);
       return false;
     }
+    return true;
   }
-  facts_with_cursor_clean(&cursor);
-  return module_maybe_reload(module, facts);
+  module_maybe_reload(module, facts);
+  return true;
 }
 
 bool module_has_symbol (const s_sym *module, const s_ident *ident,
diff --git a/test/ic3/defmodule.in b/test/ic3/defmodule.in
index 26ea9a4..d321b83 100644
--- a/test/ic3/defmodule.in
+++ b/test/ic3/defmodule.in
@@ -6,3 +6,5 @@ end
 defmodule Double do
   def double = fn (x) { x * 2 }
 end
+quote Double.double(21)
+Double.double(21)
diff --git a/test/ic3/defmodule.out.expected b/test/ic3/defmodule.out.expected
index 27ea00b..53e356a 100644
--- a/test/ic3/defmodule.out.expected
+++ b/test/ic3/defmodule.out.expected
@@ -4,3 +4,5 @@ defmodule Double do
   def double = fn (x) { x * 2 }
 end
 Double
+Double.double(21)
+42