Commit d2ea2c5b026d3eca4a9f8fe228eed4603558d546

Thomas de Grivel 2023-01-30T16:50:56

wip env

diff --git a/libc3/c3.c b/libc3/c3.c
index b0d9ff1..e18f43a 100644
--- a/libc3/c3.c
+++ b/libc3/c3.c
@@ -16,24 +16,11 @@
 #include "c3.h"
 #include "sym.h"
 
-void c3_clean (s_module *c3)
-{
-  /* TODO */
-  (void) c3;
-}
-
-s_module * c3_init (s_module *c3, s_facts *facts)
-{
-  assert(c3);
-  assert(facts);
-  c3->name = sym_1("C3");
-  c3->facts = facts;
-  /* TODO */
-  return c3;
-}
+s_env g_c3_env = {0};
 
 void libc3_init ()
 {
+  env_init(&g_c3_env);
 #ifdef DEBUG
   buf_init_alloc(&g_debug_buf, 1024);
   buf_file_open_w(&g_debug_buf, stderr);
@@ -43,6 +30,7 @@ void libc3_init ()
 void libc3_shutdown ()
 {
   sym_delete_all();
+  env_clean(&g_c3_env);
 #ifdef DEBUG
   buf_file_close(&g_debug_buf);
   buf_clean(&g_debug_buf);
diff --git a/libc3/c3.h b/libc3/c3.h
index e46cb97..7064c80 100644
--- a/libc3/c3.h
+++ b/libc3/c3.h
@@ -40,12 +40,10 @@
 #include "tuple.h"
 #include "ucd.h"
 
+extern s_env g_c3_env;
+
 /* libc3 */
 void libc3_init ();
 void libc3_shutdown ();
 
-/* c3 */
-s_module * c3_init (s_module *c3, s_facts *facts);
-void       c3_clean (s_module *c3);
-
 #endif /* C3_H */
diff --git a/libc3/env.c b/libc3/env.c
index d42b477..eb45305 100644
--- a/libc3/env.c
+++ b/libc3/env.c
@@ -20,9 +20,11 @@
 #include "buf_inspect.h"
 #include "env.h"
 #include "error_handler.h"
+#include "facts.h"
 #include "frame.h"
 #include "list.h"
 #include "str.h"
+#include "sym.h"
 #include "tag.h"
 
 void env_clean (s_env *env)
@@ -30,6 +32,7 @@ void env_clean (s_env *env)
   assert(env);
   frame_delete_all(env->frame);
   error_handler_delete_all(env->error_handler);
+  facts_clean(&env->facts);
 }
 
 void env_error_f (s_env *env, const char *fmt, ...)
@@ -189,8 +192,11 @@ s_tag * env_eval_tag (s_env *env, s_tag *tag, s_tag *dest)
 s_env * env_init (s_env *env)
 {
   assert(env);
-  env->frame = NULL;
   env->error_handler = NULL;
+  env->frame = NULL;
+  facts_init(&env->facts);
+  env->module.name = sym_1("C3");
+  env->module.facts = &env->facts;
   return env;
 }
 
diff --git a/libc3/types.h b/libc3/types.h
index e2e0c48..0636360 100644
--- a/libc3/types.h
+++ b/libc3/types.h
@@ -265,16 +265,6 @@ struct str {
 };
 
 /* 3 */
-struct env {
-  s_list           *backtrace;
-  s_error_handler  *error_handler;
-  s_frame          *frame;
-  s_buf             err;
-  s_buf             in;
-  s_buf             out;
-  s_unwind_protect *unwind_protect;
-};
-
 struct log {
   s_buf  buf;
   u64    count;
@@ -405,6 +395,18 @@ struct facts_cursor {
 };
 
 /* 6 */
+struct env {
+  s_list           *backtrace;
+  s_buf             err;
+  s_error_handler  *error_handler;
+  s_facts           facts;
+  s_frame          *frame;
+  s_buf             in;
+  s_module          module;
+  s_buf             out;
+  s_unwind_protect *unwind_protect;
+};
+
 struct facts_with_cursor_level {
   s_facts_cursor cursor;
   s_fact *fact;