Commit 62b5c03fd6406f5cae1039c650e09a3a086c2ee0

Thomas de Grivel 2023-01-31T13:11:19

c3_init(s_env *env)

diff --git a/c3s/c3s.c b/c3s/c3s.c
index 0198494..fd23121 100644
--- a/c3s/c3s.c
+++ b/c3s/c3s.c
@@ -61,7 +61,6 @@ sw buf_xfer_spaces (s_buf *out, s_buf *in)
 
 int main (int argc, char **argv)
 {
-  s_env env;
   s8 i[BUF_SIZE];
   s_buf in;
   s_tag input;
@@ -69,17 +68,16 @@ int main (int argc, char **argv)
   s_buf out;
   sw r;
   s_tag result;
-  c3_init();
+  c3_init(NULL);
   if (argc < 1)
     return usage(argv[0]);
   buf_init(&in, false, sizeof(i), i);
   buf_file_open_r(&in, stdin);
   buf_init(&out, false, sizeof(o), o);
   buf_file_open_w(&out, stdout);
-  env_init(&env);
   while ((r = buf_xfer_spaces(&out, &in)) >= 0) {
     if ((r = buf_parse_tag(&in, &input)) > 0) {
-      if (! env_eval_tag(&env, &result, &input)) {
+      if (! eval_tag(&result, &input)) {
         tag_clean(&input);
         continue;
       }
@@ -101,10 +99,9 @@ int main (int argc, char **argv)
     if ((r = buf_refill_compact(&in)) < 0)
       break;
   }
-  env_clean(&env);
   buf_readline_close(&in);
   buf_file_close(&out);
-  c3_clean();
+  c3_clean(NULL);
   return 0;
 }
 
diff --git a/ic3/ic3.c b/ic3/ic3.c
index 5555b90..2ff681b 100644
--- a/ic3/ic3.c
+++ b/ic3/ic3.c
@@ -61,7 +61,6 @@ sw buf_xfer_spaces (s_buf *out, s_buf *in)
 
 int main (int argc, char **argv)
 {
-  s_env env;
   s8 i[BUF_SIZE];
   s_buf in;
   s_tag input;
@@ -69,7 +68,7 @@ int main (int argc, char **argv)
   s_buf out;
   sw r;
   s_tag result;
-  c3_init();
+  c3_init(NULL);
   if (argc < 1)
     return usage(argv[0]);
   buf_init(&in, false, sizeof(i), i);
@@ -77,10 +76,9 @@ int main (int argc, char **argv)
   in.line = 0;
   buf_init(&out, false, sizeof(o), o);
   buf_file_open_w(&out, stdout);
-  env_init(&env);
   while ((r = buf_xfer_spaces(&out, &in)) >= 0) {
     if ((r = buf_parse_tag(&in, &input)) > 0) {
-      if (! env_eval_tag(&env, &input, &result)) {
+      if (! eval_tag(&input, &result)) {
         tag_clean(&input);
         continue;
       }
@@ -100,10 +98,9 @@ int main (int argc, char **argv)
          (r = buf_ignore_character(&in)) <= 0))
       break;
   }
-  env_clean(&env);
   buf_linenoise_close(&in);
   buf_file_close(&out);
-  c3_clean();
+  c3_clean(NULL);
   return 0;
 }
 
diff --git a/libc3/c3.c b/libc3/c3.c
index 5e29d93..bfba734 100644
--- a/libc3/c3.c
+++ b/libc3/c3.c
@@ -18,21 +18,25 @@
 
 s_env g_c3_env = {0};
 
-void c3_init ()
+void c3_init (s_env *env)
 {
-  env_init(&g_c3_env);
+  if (! env)
+    env = &g_c3_env;
+  env_init(env);
 #ifdef DEBUG
   buf_init_alloc(&g_debug_buf, 1024);
   buf_file_open_w(&g_debug_buf, stderr);
 #endif
 }
 
-void c3_clean ()
+void c3_clean (s_env *env)
 {
+  if (! env)
+    env = &g_c3_env;
 #ifdef DEBUG
   buf_file_close(&g_debug_buf);
   buf_clean(&g_debug_buf);
 #endif
   sym_delete_all();
-  env_clean(&g_c3_env);
+  env_clean(env);
 }
diff --git a/libc3/c3.h b/libc3/c3.h
index c174f33..c46d063 100644
--- a/libc3/c3.h
+++ b/libc3/c3.h
@@ -42,7 +42,7 @@
 
 extern s_env g_c3_env;
 
-void c3_init ();
-void c3_clean ();
+void c3_init (s_env *env);
+void c3_clean (s_env *env);
 
 #endif /* C3_H */
diff --git a/libc3/env.c b/libc3/env.c
index eb45305..3049d34 100644
--- a/libc3/env.c
+++ b/libc3/env.c
@@ -69,7 +69,7 @@ void env_error_tag (s_env *env, const s_tag *tag)
   }
 }
 
-s_tag * env_eval_call_fn (s_env *env, s_call *call, s_tag *dest)
+s_tag * env_eval_call_fn (s_env *env, const s_call *call, s_tag *dest)
 {
   s_arg *args;
   s_list *call_args;
@@ -109,7 +109,7 @@ s_tag * env_eval_call_fn (s_env *env, s_call *call, s_tag *dest)
   return dest;
 }
 
-s_tag * env_eval_call_macro (s_env *env, s_call *call, s_tag *dest)
+s_tag * env_eval_call_macro (s_env *env, const s_call *call, s_tag *dest)
 {
   s_tag *expanded;
   assert(env);
@@ -121,7 +121,7 @@ s_tag * env_eval_call_macro (s_env *env, s_call *call, s_tag *dest)
   return dest;
 }
 
-const s_tag * env_eval_ident (s_env *env, s_ident *ident)
+const s_tag * env_eval_ident (s_env *env, const s_ident *ident)
 {
   const s_tag *tag;
   assert(env);
@@ -133,7 +133,7 @@ const s_tag * env_eval_ident (s_env *env, s_ident *ident)
   return tag;
 }
 
-s_tag * env_eval_progn (s_env *env, s_list *program, s_tag *dest)
+s_tag * env_eval_progn (s_env *env, const s_list *program, s_tag *dest)
 {
   s_tag tmp;
   assert(env);
@@ -147,7 +147,7 @@ s_tag * env_eval_progn (s_env *env, s_list *program, s_tag *dest)
   return dest;
 }
 
-s_tag * env_eval_tag (s_env *env, s_tag *tag, s_tag *dest)
+s_tag * env_eval_tag (s_env *env, const s_tag *tag, s_tag *dest)
 {
   switch (tag->type.type) {
   case TAG_VOID: return tag_init_void(dest);
@@ -197,6 +197,7 @@ s_env * env_init (s_env *env)
   facts_init(&env->facts);
   env->module.name = sym_1("C3");
   env->module.facts = &env->facts;
+  /* TODO: load module ? */
   return env;
 }
 
diff --git a/libc3/env.h b/libc3/env.h
index 309ed72..7930517 100644
--- a/libc3/env.h
+++ b/libc3/env.h
@@ -21,13 +21,13 @@ void    env_clean (s_env *env);
 s_env * env_init (s_env *env);
 
 /* modifiers */
-s_tag *       env_eval_call_fn (s_env *env, s_call *call, s_tag *dest);
-s_tag *       env_eval_call_macro (s_env *env, s_call *call,
+s_tag *       env_eval_call_fn (s_env *env, const s_call *call, s_tag *dest);
+s_tag *       env_eval_call_macro (s_env *env, const s_call *call,
                                    s_tag *dest);
-s_tag *       env_eval_fn (s_env *env, s_fn *fn, s_tag *dest);
-const s_tag * env_eval_ident (s_env *env, s_ident *ident);
-s_tag *       env_eval_progn (s_env *env, s_list *program, s_tag *dest);
-s_tag *       env_eval_tag (s_env *env, s_tag *tag, s_tag *dest);
+s_tag *       env_eval_fn (s_env *env, const s_fn *fn, s_tag *dest);
+const s_tag * env_eval_ident (s_env *env, const s_ident *ident);
+s_tag *       env_eval_progn (s_env *env, const s_list *program, s_tag *dest);
+s_tag *       env_eval_tag (s_env *env, const s_tag *tag, s_tag *dest);
 
 /* control structures */
 void env_error_f (s_env *env, const char *fmt, ...);
diff --git a/libc3/eval.c b/libc3/eval.c
index 5a3696a..9f9d0f1 100644
--- a/libc3/eval.c
+++ b/libc3/eval.c
@@ -14,10 +14,9 @@
 #include <assert.h>
 #include <err.h>
 #include <stdlib.h>
-#include "binding.h"
-#include "env.h"
-#include "eval.h"
-#include "frame.h"
-#include "list.h"
-#include "tag.h"
+#include "c3.h"
 
+s_tag * eval_tag (const s_tag *tag, s_tag *dest)
+{
+  return env_eval_tag(&g_c3_env, tag, dest);
+}
diff --git a/libc3/eval.h b/libc3/eval.h
index 27267b0..5676e93 100644
--- a/libc3/eval.h
+++ b/libc3/eval.h
@@ -16,12 +16,12 @@
 
 #include "types.h"
 
-s_tag *       eval_call_function (s_env *env, s_call *call,
+s_tag *       eval_call_function (const s_call *call,
                                   s_tag *dest);
-s_tag *       eval_call_macro (s_env *env, s_call *call, s_tag *dest);
-s_tag *       eval_fn (s_env *env, s_fn *fn, s_tag *dest);
-const s_tag * eval_ident (s_env *env, s_ident *ident);
-s_tag *       eval_progn (s_env *env, s_list *program, s_tag *dest);
-s_tag *       eval_tag (s_env *env, s_tag *tag, s_tag *dest);
+s_tag *       eval_call_macro (const s_call *call, s_tag *dest);
+s_tag *       eval_fn (const s_fn *fn, s_tag *dest);
+const s_tag * eval_ident (const s_ident *ident);
+s_tag *       eval_progn (const s_list *program, s_tag *dest);
+s_tag *       eval_tag (const s_tag *tag, s_tag *dest);
 
 #endif /* EVAL_H */
diff --git a/test/libc3_test.c b/test/libc3_test.c
index ed15289..05d1b59 100644
--- a/test/libc3_test.c
+++ b/test/libc3_test.c
@@ -40,7 +40,7 @@ void types_test ();
 int main (int argc, char **argv)
 {
   test_init(argc, argv);
-  c3_init();
+  c3_init(NULL);
   if (test_target("types")) {
     printf("\ntypes\n");
     types_test();
@@ -131,6 +131,6 @@ int main (int argc, char **argv)
   }
   test_summary();
   test_clean();
-  c3_clean();
+  c3_clean(NULL);
   return 0;
 }