diff --git a/.ikc3_history b/.ikc3_history
index 47af30a..e4b5499 100644
--- a/.ikc3_history
+++ b/.ikc3_history
@@ -1,4 +1,3 @@
-type(12345)
type(123456)
type(123456789)
type(123456789012)
@@ -97,3 +96,4 @@ pid
pid()
man getpid
t = Thread.new(fn () { puts("ok") })
+quote t = Thread.new(fn () { puts("ok") })
diff --git a/ekc3/ekc3.c b/ekc3/ekc3.c
index 6998924..918846c 100644
--- a/ekc3/ekc3.c
+++ b/ekc3/ekc3.c
@@ -499,10 +499,10 @@ s_str * ekc3_render_file_to_str (const s_str *path, s_str *dest)
fclose(fp);
return NULL;
}
- file_dir = frame_get_w(&g_kc3_env->global_frame, &g_sym___DIR__);
+ file_dir = frame_get_w(g_kc3_env->global_frame, &g_sym___DIR__);
file_dir_save = *file_dir;
file_dirname(path, &file_dir->data.str);
- file_path = frame_get_w(&g_kc3_env->global_frame, &g_sym___FILE__);
+ file_path = frame_get_w(g_kc3_env->global_frame, &g_sym___FILE__);
file_path_save = *file_path;
file_path->data.str = *path;
result = ekc3_render_buf_to_str(&in, dest);
diff --git a/http/mime_type.c b/http/mime_type.c
index e3113b6..a4c5494 100644
--- a/http/mime_type.c
+++ b/http/mime_type.c
@@ -26,7 +26,7 @@ const s_sym ** http_mime_type (const s_str *ext, const s_sym **dest)
tag_init_sym(&tag_mime_type_sym, sym_1("mime_type"));
tag_init_var(&tag_mime_type_value, &g_sym_Sym);
default_mime_type = sym_1("application/octet-stream");
- if (! facts_with_tags(&g_kc3_env->facts, &cursor, &tag_ext,
+ if (! facts_with_tags(g_kc3_env->facts, &cursor, &tag_ext,
&tag_mime_type_sym, &tag_mime_type_value))
goto default_mime_type;
if (! facts_cursor_next(&cursor, &fact) ||
@@ -130,7 +130,7 @@ bool http_mime_type_def (s_tag *ext, const s_sym * const *mime_type)
assert(mime_type);
tag_init_sym(&tag_mime_type_sym, sym_1("mime_type"));
tag_init_sym(&tag_mime_type_value, *mime_type);
- if (! facts_replace_tags(&g_kc3_env->facts, ext,
+ if (! facts_replace_tags(g_kc3_env->facts, ext,
&tag_mime_type_sym,
&tag_mime_type_value))
return false;
diff --git a/httpd/httpd.c b/httpd/httpd.c
index dfbf5d5..1cc1319 100644
--- a/httpd/httpd.c
+++ b/httpd/httpd.c
@@ -52,7 +52,7 @@ int main (int argc, char **argv)
return 1;
}
str_init_1(&str, NULL, argv[1]);
- if (! facts_open_file(&g_kc3_env->facts, &str)) {
+ if (! facts_open_file(g_kc3_env->facts, &str)) {
err_puts("kc3_httpd: -D: facts_open_file");
assert(! "kc3_httpd: -D: facts_open_file");
kc3_clean(NULL);
@@ -112,13 +112,13 @@ int main (int argc, char **argv)
kc3_clean(NULL);
return 1;
}
- buf_file_close(&g_kc3_env->out);
+ buf_file_close(g_kc3_env->out);
dup2(log_fd, 1);
- buf_fd_open_w(&g_kc3_env->out, 1);
- buf_file_close(&g_kc3_env->err);
+ buf_fd_open_w(g_kc3_env->out, 1);
+ buf_file_close(g_kc3_env->err);
dup2(log_fd, 2);
- buf_fd_open_w(&g_kc3_env->err, 2);
- buf_file_close(&g_kc3_env->in);
+ buf_fd_open_w(g_kc3_env->err, 2);
+ buf_file_close(g_kc3_env->in);
close(0);
}
ident_init(&daemonize_ident, &g_sym_KC3, sym_1("daemonize"));
diff --git a/ikc3/ikc3.c b/ikc3/ikc3.c
index ddc5b48..fb508d6 100644
--- a/ikc3/ikc3.c
+++ b/ikc3/ikc3.c
@@ -48,19 +48,19 @@ sw ikc3_run (void)
sw r;
s_tag result;
while (1) {
- r = buf_ignore_spaces(&g_kc3_env->in);
+ r = buf_ignore_spaces(g_kc3_env->in);
if (r < 0)
return 0;
- r = buf_parse_comments(&g_kc3_env->in);
+ r = buf_parse_comments(g_kc3_env->in);
if (r < 0)
return 0;
- r = buf_parse_tag(&g_kc3_env->in, &input);
+ r = buf_parse_tag(g_kc3_env->in, &input);
if (r > 0) {
if (! eval_tag(&input, &result)) {
tag_clean(&input);
continue;
}
- if (buf_inspect_tag(&g_kc3_env->out, &result) < 0) {
+ if (buf_inspect_tag(g_kc3_env->out, &result) < 0) {
tag_clean(&input);
tag_clean(&result);
return 0;
@@ -70,11 +70,11 @@ sw ikc3_run (void)
}
if (r < 0 ||
(r == 0 &&
- (r = buf_ignore_character(&g_kc3_env->in)) <= 0))
+ (r = buf_ignore_character(g_kc3_env->in)) <= 0))
return 0;
- if ((r = buf_write_1(&g_kc3_env->out, "\n")) < 0)
+ if ((r = buf_write_1(g_kc3_env->out, "\n")) < 0)
return 0;
- if ((r = buf_flush(&g_kc3_env->out)) < 0)
+ if ((r = buf_flush(g_kc3_env->out)) < 0)
return 0;
}
return 0;
@@ -89,16 +89,12 @@ int main (int argc, char **argv)
s_tag *file_path;
s_tag file_path_save;
FILE *fp = 0;
- char in_data[BUF_SIZE];
- s_buf in_original;
sw r;
if (argc < 1)
return usage("ikc3");
if (! kc3_init(NULL, &argc, &argv))
return 1;
env = g_kc3_env;
- in_original = env->in;
- buf_init(&env->in, false, sizeof(in_data), in_data);
while (argc) {
if (! strcmp("--load", *argv) ||
! strcmp("-l", *argv)) {
@@ -123,25 +119,25 @@ int main (int argc, char **argv)
r = 1;
goto clean;
}
- if (! buf_file_open_r(&env->in, fp)) {
+ if (! buf_file_open_r(env->in, fp)) {
r = -1;
goto clean;
}
- file_dir = frame_get_w(&env->global_frame, &g_sym___DIR__);
+ file_dir = frame_get_w(env->global_frame, &g_sym___DIR__);
file_dir_save = *file_dir;
- file_path = frame_get_w(&env->global_frame, &g_sym___FILE__);
+ file_path = frame_get_w(env->global_frame, &g_sym___FILE__);
file_path_save = *file_path;
tag_init_str_1(file_path, NULL, argv[1]);
file_dir->type = TAG_STR;
if (! file_dirname(&file_path->data.str, &file_dir->data.str)) {
- buf_file_close(&env->in);
+ buf_file_close(env->in);
r = -1;
goto clean;
}
r = ikc3_run();
*file_dir = file_dir_save;
*file_path = file_path_save;
- buf_file_close(&env->in);
+ buf_file_close(env->in);
fclose(fp);
if (r)
goto clean;
@@ -156,18 +152,17 @@ int main (int argc, char **argv)
break;
}
#if HAVE_WINEDITLINE
- buf_wineditline_open_r(&env->in, "ikc3> ", ".ikc3_history");
+ buf_wineditline_open_r(env->in, "ikc3> ", ".ikc3_history");
#else
- buf_linenoise_open_r(&env->in, "ikc3> ", ".ikc3_history");
+ buf_linenoise_open_r(env->in, "ikc3> ", ".ikc3_history");
#endif
r = ikc3_run();
#if HAVE_WINEDITLINE
- buf_wineditline_close(&env->in, ".ikc3_history");
+ buf_wineditline_close(env->in, ".ikc3_history");
#else
- buf_linenoise_close(&env->in, ".ikc3_history");
+ buf_linenoise_close(env->in, ".ikc3_history");
#endif
clean:
- env->in = in_original;
kc3_clean(NULL);
return r;
}
diff --git a/kc3s/kc3s.c b/kc3s/kc3s.c
index a427323..e07d7ae 100644
--- a/kc3s/kc3s.c
+++ b/kc3s/kc3s.c
@@ -63,14 +63,14 @@ sw kc3s_run (void)
s_tag input;
sw r;
s_tag result;
- while ((r = buf_ignore_spaces(&g_kc3_env->in)) >= 0) {
- if ((r = buf_parse_tag(&g_kc3_env->in, &input)) > 0) {
+ while ((r = buf_ignore_spaces(g_kc3_env->in)) >= 0) {
+ if ((r = buf_parse_tag(g_kc3_env->in, &input)) > 0) {
if (! eval_tag(&input, &result)) {
tag_clean(&input);
continue;
}
/*
- if (buf_inspect_tag(&g_kc3_env->out, &result) < 0) {
+ if (buf_inspect_tag(g_kc3_env->out, &result) < 0) {
tag_clean(&input);
tag_clean(&result);
break;
@@ -79,14 +79,14 @@ sw kc3s_run (void)
tag_clean(&input);
tag_clean(&result);
/*
- buf_write_u8(&g_kc3_env->out, '\n');
- if ((r = buf_flush(&g_kc3_env->out)) < 0)
+ buf_write_u8(g_kc3_env->out, '\n');
+ if ((r = buf_flush(g_kc3_env->out)) < 0)
break;
*/
}
if (r < 0 ||
(r == 0 &&
- (r = buf_ignore_character(&g_kc3_env->in)) <= 0))
+ (r = buf_ignore_character(g_kc3_env->in)) <= 0))
break;
}
return 0;
@@ -101,15 +101,13 @@ int main (int argc, char **argv)
s_tag file_path_save;
FILE *fp;
char in_data[BUF_SIZE];
- s_buf in_original;
sw r;
if (argc < 1)
return usage(argv[0]);
if (! kc3_init(NULL, &argc, &argv))
return 1;
env = g_kc3_env;
- in_original = env->in;
- buf_init(&env->in, false, sizeof(in_data), in_data);
+ buf_init(env->in, false, sizeof(in_data), in_data);
while (argc) {
if (! strcmp("--load", *argv) ||
! strcmp("-l", *argv)) {
@@ -123,25 +121,25 @@ int main (int argc, char **argv)
goto clean;
}
fp = fopen(argv[1], "rb");
- if (! buf_file_open_r(&env->in, fp)) {
+ if (! buf_file_open_r(env->in, fp)) {
r = -1;
goto clean;
}
- file_dir = frame_get_w(&env->global_frame, &g_sym___DIR__);
+ file_dir = frame_get_w(env->global_frame, &g_sym___DIR__);
file_dir_save = *file_dir;
- file_path = frame_get_w(&env->global_frame, &g_sym___FILE__);
+ file_path = frame_get_w(env->global_frame, &g_sym___FILE__);
file_path_save = *file_path;
tag_init_str_1(file_path, NULL, argv[1]);
file_dir->type = TAG_STR;
if (! file_dirname(&file_path->data.str, &file_dir->data.str)) {
- buf_file_close(&env->in);
+ buf_file_close(env->in);
r = -1;
goto clean;
}
r = kc3s_run();
*file_dir = file_dir_save;
*file_path = file_path_save;
- buf_file_close(&env->in);
+ buf_file_close(env->in);
fclose(fp);
if (r)
goto clean;
@@ -157,7 +155,6 @@ int main (int argc, char **argv)
}
r = kc3s_run();
clean:
- env->in = in_original;
kc3_clean(NULL);
return r;
}
diff --git a/libkc3/env.c b/libkc3/env.c
index f720d05..e4c70d8 100644
--- a/libkc3/env.c
+++ b/libkc3/env.c
@@ -145,14 +145,14 @@ bool env_call_get (s_env *env, s_call *call)
tag_init_sym( &tag_symbol, &g_sym_symbol);
tag_init_sym( &tag_symbol_value, &g_sym_symbol_value);
tag_init_var( &tag_var, &g_sym_Tag);
- if (! facts_find_fact_by_tags(&env->facts, &tag_module_name,
+ if (! facts_find_fact_by_tags(env->facts, &tag_module_name,
&tag_symbol, &tag_ident, &fact)) {
err_puts("env_call_get: facts_find_fact_by_tags 1");
assert(! "env_call_get: facts_find_fact_by_tags 1");
return false;
}
if (! fact) {
- if (! facts_find_fact_by_tags(&env->facts, &tag_module_name,
+ if (! facts_find_fact_by_tags(env->facts, &tag_module_name,
&tag_operator, &tag_ident, &fact)) {
err_puts("env_call_get: facts_find_fact_by_tags 2");
assert(! "env_call_get: facts_find_fact_by_tags 2");
@@ -167,7 +167,7 @@ bool env_call_get (s_env *env, s_call *call)
return false;
}
}
- if (! facts_with_tags(&env->facts, &cursor, &tag_ident,
+ if (! facts_with_tags(env->facts, &cursor, &tag_ident,
&tag_symbol_value, &tag_var)) {
err_puts("env_call_get: facts_with_tags");
assert(! "env_call_get: facts_with_tags");
@@ -198,7 +198,7 @@ bool env_call_get (s_env *env, s_call *call)
call->ident.sym);
}
facts_cursor_clean(&cursor);
- if (! facts_find_fact_by_tags(&env->facts, &tag_ident, &tag_is_a,
+ if (! facts_find_fact_by_tags(env->facts, &tag_ident, &tag_is_a,
&tag_special_operator, &found)) {
err_puts("env_call_get: facts_find_fact_by_tags 4");
assert(! "env_call_get: facts_find_fact_by_tags 4");
@@ -231,26 +231,26 @@ void env_clean (s_env *env)
err_inspect_uw_decimal(&size);
err_write_1("\n");
}
- //facts_save_file(&env->facts, "debug.facts"); // debug
+ //facts_save_file(env->facts, "debug.facts"); // debug
env_clean_globals(env);
env_clean_toplevel(env);
error_handler_delete_all(env->error_handler);
- facts_clean(&env->facts);
- buf_file_close(&env->in);
- buf_clean(&env->in);
- buf_file_close(&env->out);
- buf_clean(&env->out);
- buf_file_close(&env->err);
- buf_clean(&env->err);
- str_clean(&env->argv0_dir);
- str_clean(&env->module_path);
+ facts_delete(env->facts);
+ buf_file_close(env->in);
+ buf_delete(env->in);
+ buf_file_close(env->out);
+ buf_delete(env->out);
+ buf_file_close(env->err);
+ buf_delete(env->err);
+ str_delete(env->argv0_dir);
+ str_delete(env->module_path);
list_delete_all(env->path);
list_delete_all(env->search_modules_default);
}
void env_clean_globals (s_env *env)
{
- frame_clean(&env->global_frame);
+ frame_clean(env->global_frame);
frame_clean(&env->read_time_frame);
}
@@ -279,7 +279,7 @@ bool env_def (s_env *env, const s_ident *ident, s_tag *value)
tag_ident.data.ident.module = env->current_defmodule;
tag_init_sym(&tag_module, tag_ident.data.ident.module);
tag_init_sym(&tag_symbol, &g_sym_symbol);
- if (! facts_add_tags(&env->facts, &tag_module, &tag_symbol,
+ if (! facts_add_tags(env->facts, &tag_module, &tag_symbol,
&tag_ident))
return false;
if (value->type == TAG_STRUCT &&
@@ -301,7 +301,7 @@ bool env_def (s_env *env, const s_ident *ident, s_tag *value)
}
else {
tag_init_sym(&tag_symbol_value, &g_sym_symbol_value);
- if (! facts_replace_tags(&env->facts, &tag_ident, &tag_symbol_value,
+ if (! facts_replace_tags(env->facts, &tag_ident, &tag_symbol_value,
value)) {
return false;
}
@@ -343,7 +343,7 @@ const s_sym * env_def_clean (s_env *env, const s_sym *module,
tag_init_struct_type_update_clean(&tag_st, st,
&clean->data.callable->data.cfn);
tag_init_sym(&tag_struct_type, &g_sym_struct_type);
- if (! facts_replace_tags(&env->facts, &tag_module_name,
+ if (! facts_replace_tags(env->facts, &tag_module_name,
&tag_struct_type, &tag_st)) {
tag_clean(&tag_st);
return NULL;
@@ -377,7 +377,7 @@ s_tag * env_defmodule (s_env *env, const s_sym * const *name,
tag_init_sym(&tag_is_a, &g_sym_is_a);
tag_init_sym(&tag_module, &g_sym_module);
tag_init_sym(&tag_module_name, *name);
- if (! facts_add_tags(&env->facts, &tag_module_name, &tag_is_a,
+ if (! facts_add_tags(env->facts, &tag_module_name, &tag_is_a,
&tag_module))
goto clean;
if (! env_eval_block(env, block, &tmp))
@@ -439,19 +439,19 @@ s_tag * env_defoperator (s_env *env, const s_sym * const *name,
&g_sym_operator_associativity);
tag_init_sym(&tag_op_assoc_value,
*op_assoc);
- facts_add_tags(&env->facts, &tag_module_name, &tag_operator,
+ facts_add_tags(env->facts, &tag_module_name, &tag_operator,
&tag_ident);
- facts_replace_tags(&env->facts, &tag_ident, &tag_is_a, &tag_operator);
- facts_replace_tags(&env->facts, &tag_ident, &tag_sym_sym,
+ facts_replace_tags(env->facts, &tag_ident, &tag_is_a, &tag_operator);
+ facts_replace_tags(env->facts, &tag_ident, &tag_sym_sym,
&tag_sym_value);
- facts_replace_tags(&env->facts, &tag_ident, &tag_arity_sym,
+ facts_replace_tags(env->facts, &tag_ident, &tag_arity_sym,
&tag_arity_u8);
- facts_replace_tags(&env->facts, &tag_ident, &tag_symbol_value,
+ facts_replace_tags(env->facts, &tag_ident, &tag_symbol_value,
symbol_value);
- facts_replace_tags(&env->facts, &tag_ident,
+ facts_replace_tags(env->facts, &tag_ident,
&tag_op_precedence_sym,
&tag_op_precedence_u8);
- facts_replace_tags(&env->facts, &tag_ident,
+ facts_replace_tags(env->facts, &tag_ident,
&tag_op_assoc_rel,
&tag_op_assoc_value);
*dest = tag_ident;
@@ -466,7 +466,7 @@ const s_sym * env_defstruct (s_env *env, s_list *spec)
tag_init_sym(&tag_module_name, env->current_defmodule);
tag_init_struct_type(&tag_st, env->current_defmodule, spec);
tag_init_sym(&tag_struct_type, &g_sym_struct_type);
- if (! facts_replace_tags(&env->facts, &tag_module_name,
+ if (! facts_replace_tags(env->facts, &tag_module_name,
&tag_struct_type, &tag_st)) {
tag_clean(&tag_st);
return NULL;
@@ -501,10 +501,10 @@ void env_error_tag (s_env *env, s_tag *tag)
/* never reached */
return;
}
- if (buf_file_is_open(&env->err)) {
- buf_write_1(&env->err, "error: ");
- buf_inspect_tag(&env->err, tag);
- buf_write_1(&env->err, "\n");
+ if (buf_file_is_open(env->err)) {
+ buf_write_1(env->err, "error: ");
+ buf_inspect_tag(env->err, tag);
+ buf_write_1(env->err, "\n");
return;
}
}
@@ -2517,7 +2517,7 @@ s_tag * env_frames_get (s_env *env, const s_sym *name)
{
s_tag *tag;
if ((tag = frame_get(env->frame, name)) ||
- (tag = frame_get(&env->global_frame, name)))
+ (tag = frame_get(env->global_frame, name)))
return tag;
return NULL;
}
@@ -2559,11 +2559,11 @@ s_tag * env_ident_get (s_env *env, const s_ident *ident, 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, &g_sym_Tag);
- if (! facts_find_fact_by_tags(&env->facts, &tag_module, &tag_symbol,
+ if (! facts_find_fact_by_tags(env->facts, &tag_module, &tag_symbol,
&tag_ident, &fact) ||
! fact)
return NULL;
- if (! facts_with(&env->facts, &cursor, (t_facts_spec) {
+ if (! facts_with(env->facts, &cursor, (t_facts_spec) {
&tag_ident, &tag_symbol_value, &tag_var,
NULL, NULL }))
return NULL;
@@ -2578,7 +2578,7 @@ s_tag * env_ident_get (s_env *env, const s_ident *ident, s_tag *dest)
return NULL;
}
facts_with_cursor_clean(&cursor);
- if (! facts_with(&env->facts, &cursor, (t_facts_spec) {
+ if (! facts_with(env->facts, &cursor, (t_facts_spec) {
&tag_ident, &tag_is_a, &tag_special_operator, NULL, NULL}))
return NULL;
if (! facts_with_cursor_next(&cursor, &fact)) {
@@ -2622,7 +2622,7 @@ bool * env_ident_is_special_operator (s_env *env,
return NULL;
tag_init_sym(&tag_is_a, &g_sym_is_a);
tag_init_sym(&tag_special_operator, &g_sym_special_operator);
- if (! facts_find_fact_by_tags(&env->facts, &tag_ident, &tag_is_a,
+ if (! facts_find_fact_by_tags(env->facts, &tag_ident, &tag_is_a,
&tag_special_operator, &fact))
return NULL;
*dest = fact ? true : false;
@@ -2669,6 +2669,8 @@ s_env * env_init (s_env *env, int *argc, char ***argv)
{
s_str path;
if (! env)
+ env = g_kc3_env;
+ if (! env)
env = g_kc3_env = alloc(sizeof(s_env));
if (! env)
return NULL;
@@ -2681,13 +2683,16 @@ s_env * env_init (s_env *env, int *argc, char ***argv)
return NULL;
if (! env_init_globals(env))
return NULL;
- buf_init_alloc(&env->in, BUF_SIZE);
- buf_file_open_r(&env->in, stdin);
- buf_init_alloc(&env->out, BUF_SIZE);
- buf_file_open_w(&env->out, stdout);
- buf_init_alloc(&env->err, BUF_SIZE);
- buf_file_open_w(&env->err, stderr);
- facts_init(&env->facts);
+ if (! (env->in = buf_new_alloc(BUF_SIZE)))
+ return NULL;
+ buf_file_open_r(env->in, stdin);
+ if (! (env->out = buf_new_alloc(BUF_SIZE)))
+ return NULL;
+ buf_file_open_w(env->out, stdout);
+ if (! (env->err = buf_new_alloc(BUF_SIZE)))
+ return NULL;
+ buf_file_open_w(env->err, stderr);
+ env->facts = facts_new();
env->path = list_new_str_1
(NULL, "./", list_new_str_1
(NULL, "../", list_new_str_1
@@ -2698,7 +2703,9 @@ s_env * env_init (s_env *env, int *argc, char ***argv)
(NULL, "../../../../../", list_new_str_1
(NULL, "../../../../../../", NULL))))))));
str_init_1(&path, NULL, "lib/kc3/0.1/");
- if (! file_search(&path, &g_sym_x, &env->module_path)) {
+ if (! (env->module_path = alloc(sizeof(s_str))))
+ return NULL;
+ if (! file_search(&path, &g_sym_x, env->module_path)) {
err_puts("env_init: lib/kc3/0.1 not found in module path");
assert(! "env_init: lib/kc3/0.1 not found in module path");
return NULL;
@@ -2717,30 +2724,51 @@ s_env * env_init (s_env *env, int *argc, char ***argv)
s_env * env_init_args (s_env *env, int *argc, char ***argv)
{
- char a[PATH_MAX];
s_str argv0;
- s_buf buf;
- s_str dir;
- sw r;
assert(env);
if (argc && argv && *argc && *argv) {
env->argc = (*argc)--;
env->argv = (*argv)++;
str_init_1(&argv0, NULL, env->argv[0]);
- file_dirname(&argv0, &dir);
- buf_init(&buf, false, sizeof(a), a);
- if ((r = buf_write_str(&buf, &dir)) < 0) {
- str_clean(&dir);
- goto ko;
- }
- str_clean(&dir);
- buf_read_to_str(&buf, &env->argv0_dir);
+ if (! (env->argv0_dir = alloc(sizeof(s_str))))
+ return NULL;
+ file_dirname(&argv0, env->argv0_dir);
return env;
}
- ko:
env->argc = 0;
env->argv = NULL;
- str_init_1(&env->argv0_dir, NULL, "./");
+ env->argv0_dir = str_new_1(NULL, "./");
+ return env;
+}
+
+s_env * env_init_copy (s_env *env, s_env *src)
+{
+ s_env tmp = {0};
+ assert(env);
+ assert(src);
+ tmp.argc = src->argc;
+ tmp.argv = src->argv;
+ tmp.argv0_dir = src->argv0_dir;
+ //tmp.stacktrace = NULL;
+ tmp.current_defmodule = &g_sym_KC3;
+ tmp.err = src->err;
+ //tmp.error_handler = NULL;
+ tmp.facts = src->facts;
+ //tmp.frame = NULL;
+ tmp.global_frame = src->global_frame;
+ tmp.in = src->in;
+ tmp.module_path = src->module_path;
+ tmp.out = src->out;
+ tmp.path = src->path;
+ tmp.quote_level = src->quote_level;
+ if (! frame_init(&tmp.read_time_frame, NULL, NULL))
+ return NULL;
+ tmp.search_modules = src->search_modules_default;
+ tmp.search_modules_default = src->search_modules_default;
+ tmp.trace = src->trace;
+ //tmp.unquote_level = 0;
+ //tmp.unwind_protect = NULL;
+ *env = tmp;
return env;
}
@@ -2761,7 +2789,7 @@ s_env * env_init_globals (s_env *env)
return NULL;
if (! tag_init_str_1(file_path, NULL, "stdin"))
return NULL;
- if (! frame_init(&env->global_frame, &env->read_time_frame, NULL))
+ if (! (env->global_frame = frame_new(&env->read_time_frame, NULL)))
return NULL;
return env;
}
@@ -2904,9 +2932,9 @@ bool env_load (s_env *env, const s_str *path)
buf_clean(&buf);
return false;
}
- file_dir = frame_get_w(&env->global_frame, &g_sym___DIR__);
+ file_dir = frame_get_w(env->global_frame, &g_sym___DIR__);
file_dir_save = *file_dir;
- file_path = frame_get_w(&env->global_frame, &g_sym___FILE__);
+ file_path = frame_get_w(env->global_frame, &g_sym___FILE__);
file_path_save = *file_path;
if (! file_dirname(path, &file_dir->data.str))
goto ko;
@@ -2950,7 +2978,7 @@ bool env_load (s_env *env, const s_str *path)
tag.data.str = *path;
tag_init_time_now(&now);
tag_init_sym(&load_time, &g_sym_load_time);
- facts_replace_tags(&env->facts, &tag, &load_time, &now);
+ facts_replace_tags(env->facts, &tag, &load_time, &now);
return true;
ko:
tag_clean(file_dir);
@@ -2974,7 +3002,7 @@ bool env_maybe_reload (s_env *env, const s_str *path)
path_tag.data.str = *path;
tag_init_sym(&load_time_sym, &g_sym_load_time);
tag_init_var(&load_time, &g_sym_Time);
- if (! facts_with_tags(&env->facts, &cursor, &path_tag, &load_time_sym,
+ if (! facts_with_tags(env->facts, &cursor, &path_tag, &load_time_sym,
&load_time))
return false;
if (! facts_cursor_next(&cursor, &fact))
@@ -3037,7 +3065,7 @@ bool env_module_ensure_loaded (s_env *env, const s_sym *module)
tag_init_sym(&tag_is_a, &g_sym_is_a);
tag_init_sym(&tag_module, &g_sym_module);
tag_init_sym(&tag_module_name, module);
- if (! facts_find_fact_by_tags(&env->facts, &tag_module_name,
+ if (! facts_find_fact_by_tags(env->facts, &tag_module_name,
&tag_is_a, &tag_module, &fact)) {
err_puts("env_module_ensure_loaded: facts_find_fact_by_tags");
assert(! "env_module_ensure_loaded: facts_find_fact_by_tags");
@@ -3076,7 +3104,7 @@ bool * env_module_has_ident (s_env *env, const s_sym *module,
tag_init_sym( &tag_operator, &g_sym_operator);
tag_init_sym( &tag_sym_value, ident->sym);
tag_init_sym( &tag_symbol, &g_sym_symbol);
- if (! facts_find_fact_by_tags(&env->facts, &tag_module_name,
+ if (! facts_find_fact_by_tags(env->facts, &tag_module_name,
&tag_symbol, &tag_ident, &fact)) {
err_puts("env_module_has_ident: facts_find_fact_by_tags 1");
assert(! "env_module_has_ident: facts_find_fact_by_tags 1");
@@ -3086,7 +3114,7 @@ bool * env_module_has_ident (s_env *env, const s_sym *module,
*dest = true;
return dest;
}
- if (! facts_find_fact_by_tags(&env->facts, &tag_module_name,
+ if (! facts_find_fact_by_tags(env->facts, &tag_module_name,
&tag_operator, &tag_ident, &fact)) {
err_puts("env_module_has_ident: facts_find_fact_by_tags 2");
assert(! "env_module_has_ident: facts_find_fact_by_tags 2");
@@ -3098,7 +3126,7 @@ bool * env_module_has_ident (s_env *env, const s_sym *module,
}
tag_init_sym(&tag_sym_sym, &g_sym_sym);
tag_init_var(&tag_var, &g_sym_Ident);
- if (! facts_with(&env->facts, &cursor, (t_facts_spec) {
+ if (! facts_with(env->facts, &cursor, (t_facts_spec) {
&tag_module_name, &tag_operator, &tag_var, NULL,
&tag_var, &tag_sym_sym, &tag_sym_value, NULL, NULL})) {
err_puts("env_module_has_ident: facts_with");
@@ -3136,7 +3164,7 @@ bool * env_module_is_loading (s_env *env, const s_sym *module,
tag_init_sym(&tag_module, module);
tag_init_sym(&tag_is_loading, &g_sym_is_loading);
tag_init_bool(&tag_true, true);
- if (! facts_find_fact_by_tags(&env->facts, &tag_module,
+ if (! facts_find_fact_by_tags(env->facts, &tag_module,
&tag_is_loading, &tag_true,
&fact))
return NULL;
@@ -3157,12 +3185,12 @@ bool env_module_is_loading_set (s_env *env, const s_sym *module,
tag_init_sym(&tag_is_loading, &g_sym_is_loading);
tag_init_bool(&tag_true, true);
if (is_loading) {
- if (! facts_replace_tags(&env->facts, &tag_module, &tag_is_loading,
+ if (! facts_replace_tags(env->facts, &tag_module, &tag_is_loading,
&tag_true))
return false;
}
else
- if (! facts_remove_fact_tags(&env->facts, &tag_module,
+ if (! facts_remove_fact_tags(env->facts, &tag_module,
&tag_is_loading, &tag_true, &b) ||
! b)
return false;
@@ -3183,10 +3211,10 @@ bool env_module_load (s_env *env, const s_sym *module)
return false;
if (b)
return true;
- facts_transaction_start(&env->facts, &transaction);
+ facts_transaction_start(env->facts, &transaction);
if (! env_module_is_loading_set(env, module, true))
goto rollback;
- if (module_path(module, &env->module_path, KC3_EXT, &path) &&
+ if (module_path(module, env->module_path, KC3_EXT, &path) &&
file_access(&path, &g_sym_r)) {
tag_init_time_now(&tag_time);
if (! env_load(env, &path)) {
@@ -3199,7 +3227,7 @@ bool env_module_load (s_env *env, const s_sym *module)
}
else {
str_clean(&path);
- if (! module_path(module, &env->module_path, FACTS_EXT, &path)) {
+ if (! module_path(module, env->module_path, FACTS_EXT, &path)) {
err_write_1("env_module_load: ");
err_write_1(module->str.ptr.pchar);
err_puts(": module_path");
@@ -3208,7 +3236,7 @@ bool env_module_load (s_env *env, const s_sym *module)
if (! file_access(&path, &g_sym_r))
goto rollback;
tag_init_time_now(&tag_time);
- if (facts_load_file(&env->facts, &path) < 0) {
+ if (facts_load_file(env->facts, &path) < 0) {
err_write_1("env_module_load: ");
err_write_1(module->str.ptr.pchar);
err_puts(": facts_load_file");
@@ -3220,15 +3248,15 @@ bool env_module_load (s_env *env, const s_sym *module)
str_clean(&path);
tag_init_sym(&tag_module_name, module);
tag_init_sym(&tag_load_time, &g_sym_load_time);
- if (! facts_replace_tags(&env->facts, &tag_module_name,
+ if (! facts_replace_tags(env->facts, &tag_module_name,
&tag_load_time, &tag_time))
goto rollback;
tag_clean(&tag_time);
env_module_is_loading_set(env, module, false);
- facts_transaction_end(&env->facts, &transaction);
+ facts_transaction_end(env->facts, &transaction);
return true;
rollback:
- if (! facts_transaction_rollback(&env->facts, &transaction)) {
+ if (! facts_transaction_rollback(env->facts, &transaction)) {
abort();
return false;
}
@@ -3246,7 +3274,7 @@ const s_time ** env_module_load_time (s_env *env, const s_sym *module,
tag_init_sym(&tag_module_name, module);
tag_init_sym(&tag_load_time, &g_sym_load_time);
tag_init_var(&tag_time_var, &g_sym_Time);
- if (! facts_with(&env->facts, &cursor, (t_facts_spec) {
+ if (! facts_with(env->facts, &cursor, (t_facts_spec) {
&tag_module_name, &tag_load_time, &tag_time_var, NULL, NULL }))
return NULL;
if (! facts_with_cursor_next(&cursor, &fact)) {
@@ -3277,13 +3305,13 @@ bool env_module_maybe_reload (s_env *env, const s_sym *module)
s_str path;
bool r = false;
s_time mtime;
- if (module_path(module, &env->module_path, KC3_EXT, &path)) {
+ if (module_path(module, env->module_path, KC3_EXT, &path)) {
if (file_access(&path, &g_sym_r))
r = true;
else
str_clean(&path);
}
- if (!r && module_path(module, &env->module_path, FACTS_EXT, &path)) {
+ if (!r && module_path(module, env->module_path, FACTS_EXT, &path)) {
if (file_access(&path, &g_sym_r))
r = true;
else
@@ -3334,6 +3362,18 @@ s_list ** env_module_search_modules (s_env *env,
return dest;
}
+s_env * env_new_copy (s_env *src)
+{
+ s_env *env;
+ if (! (env = alloc(sizeof(s_env))))
+ return NULL;
+ if (! env_init_copy(env, src)) {
+ free(env);
+ return NULL;
+ }
+ return env;
+}
+
s8 env_operator_arity (s_env *env, const s_ident *op)
{
s_facts_cursor cursor;
@@ -3347,7 +3387,7 @@ s8 env_operator_arity (s_env *env, const s_ident *op)
tag_init_ident(&tag_op, op);
tag_init_sym( &tag_arity, &g_sym_arity);
tag_init_var( &tag_var, &g_sym_U8);
- facts_with_tags(&env->facts, &cursor, &tag_op, &tag_arity, &tag_var);
+ facts_with_tags(env->facts, &cursor, &tag_op, &tag_arity, &tag_var);
if (! facts_cursor_next(&cursor, &fact))
return -1;
if (fact)
@@ -3374,7 +3414,7 @@ bool * env_operator_find (s_env *env, const s_ident *op, bool *dest)
tag_init_sym( &tag_is_a, &g_sym_is_a);
tag_init_ident(&tag_op, op);
tag_init_sym( &tag_operator, &g_sym_operator);
- if (! facts_find_fact_by_tags(&env->facts, &tag_op, &tag_is_a,
+ if (! facts_find_fact_by_tags(env->facts, &tag_op, &tag_is_a,
&tag_operator, &fact))
return NULL;
*dest = fact ? true : false;
@@ -3413,7 +3453,7 @@ s_tag * env_operator_find_by_sym (s_env *env,
tag_init_sym(&tag_sym_sym, &g_sym_sym);
tag_init_var(&tag_symbol_value, &g_sym_Tag);
tag_init_sym(&tag_symbol_value_sym, &g_sym_symbol_value);
- if (! facts_with(&env->facts, &cursor, (t_facts_spec) {
+ if (! facts_with(env->facts, &cursor, (t_facts_spec) {
&tag_ident, &tag_is_a, &tag_operator,
&tag_sym_sym, &tag_sym,
&tag_operator_associativity_sym, &tag_operator_associativity,
@@ -3477,7 +3517,7 @@ bool * env_operator_is_right_associative (s_env *env, const s_ident *op,
tag_init_sym( &tag_assoc, &g_sym_operator_associativity);
tag_init_ident(&tag_op, op);
tag_init_sym( &tag_right, &g_sym_right);
- if (! facts_find_fact_by_tags(&env->facts, &tag_op, &tag_assoc,
+ if (! facts_find_fact_by_tags(env->facts, &tag_op, &tag_assoc,
&tag_right, &fact))
return NULL;
*dest = fact ? true : false;
@@ -3498,7 +3538,7 @@ sw * env_operator_precedence (s_env *env, const s_ident *op, sw *dest)
tag_init_ident(&tag_op, op);
tag_init_sym( &tag_precedence, &g_sym_operator_precedence);
tag_init_var( &tag_var, &g_sym_Tag);
- if (! facts_with_tags(&env->facts, &cursor, &tag_op, &tag_precedence,
+ if (! facts_with_tags(env->facts, &cursor, &tag_op, &tag_precedence,
&tag_var))
return NULL;
if (! facts_cursor_next(&cursor, &fact))
@@ -3543,7 +3583,7 @@ s_ident * env_operator_resolve (s_env *env, const s_ident *op,
tag_init_var(&tag_var, &g_sym_Ident);
tag_init_sym(&tag_sym_sym, &g_sym_sym);
tag_init_sym(&tag_sym_value, tmp.sym);
- if (! facts_with(&env->facts, &cursor, (t_facts_spec) {
+ if (! facts_with(env->facts, &cursor, (t_facts_spec) {
&tag_module_name, &tag_is_a, &tag_module,
&tag_operator, &tag_var, NULL,
&tag_var, &tag_sym_sym, &tag_sym_value,
@@ -3599,7 +3639,7 @@ const s_sym ** env_operator_symbol (s_env *env, const s_ident *op,
tag_init_ident(&tag_op, op);
tag_init_sym( &tag_sym_sym, &g_sym_sym);
tag_init_var( &tag_var, &g_sym_Sym);
- if (! facts_with_tags(&env->facts, &cursor, &tag_op, &tag_sym_sym,
+ if (! facts_with_tags(env->facts, &cursor, &tag_op, &tag_sym_sym,
&tag_var))
return NULL;
if (! facts_cursor_next(&cursor, &fact))
@@ -3769,7 +3809,7 @@ u8 env_special_operator_arity (s_env *env, const s_ident *ident)
env_ident_resolve_module(env, ident, &tag_ident.data.ident);
tag_init_sym( &tag_arity, &g_sym_arity);
tag_init_var( &tag_var, &g_sym_U8);
- if (! facts_with_tags(&env->facts, &cursor,
+ if (! facts_with_tags(env->facts, &cursor,
&tag_ident, &tag_arity, &tag_var))
return 0;
if (! facts_cursor_next(&cursor, &fact))
@@ -3811,7 +3851,7 @@ bool * env_struct_type_exists (s_env *env, const s_sym *module,
tag_init_var(&tag_var, &g_sym_Tag);
if (! env_module_maybe_reload(env, module))
return NULL;
- if (! facts_with_tags(&env->facts, &cursor, &tag_module,
+ if (! facts_with_tags(env->facts, &cursor, &tag_module,
&tag_struct_type, &tag_var))
return NULL;
if (! facts_cursor_next(&cursor, &fact))
@@ -3843,7 +3883,7 @@ s_struct_type ** env_struct_type_find (s_env *env,
assert(! "env_struct_type_find: env_module_maybe_reload");
return NULL;
}
- if (! facts_with(&env->facts, &cursor, (t_facts_spec) {
+ if (! facts_with(env->facts, &cursor, (t_facts_spec) {
&tag_module, &tag_struct_type, &tag_var,
NULL, NULL })) {
err_write_1("env_struct_type_find: facts_with(");
@@ -3891,7 +3931,7 @@ f_clean env_struct_type_get_clean (s_env *env, const s_sym *module)
tag_init_sym(&tag_module, module);
tag_init_sym(&tag_clean, &g_sym_clean);
tag_init_var(&tag_var, &g_sym_Tag);
- facts_with(&env->facts, &cursor, (t_facts_spec) {
+ facts_with(env->facts, &cursor, (t_facts_spec) {
&tag_module, &tag_clean, &tag_var, NULL, NULL });
if (! facts_with_cursor_next(&cursor, &found))
return NULL;
@@ -3941,7 +3981,7 @@ s_list ** env_struct_type_get_spec (s_env *env,
tag_init_sym(&tag_defstruct, &g_sym_defstruct);
tag_init_sym(&tag_module, module);
tag_init_var(&tag_var, &g_sym_Tag);
- if (! facts_find_fact_by_tags(&env->facts, &tag_module,
+ if (! facts_find_fact_by_tags(env->facts, &tag_module,
&tag_defstruct, &tag_var, &found))
return NULL;
if (! found) {
@@ -3978,7 +4018,7 @@ bool * env_struct_type_has_spec (s_env *env, const s_sym *module,
tag_init_sym(&tag_defstruct, &g_sym_defstruct);
tag_init_sym(&tag_module, module);
tag_init_var(&tag_var, &g_sym_Tag);
- if (! facts_with_tags(&env->facts, &cursor, &tag_module,
+ if (! facts_with_tags(env->facts, &cursor, &tag_module,
&tag_defstruct, &tag_var))
return NULL;
if (! facts_cursor_next(&cursor, &fact))
diff --git a/libkc3/env.h b/libkc3/env.h
index c44441b..45b1036 100644
--- a/libkc3/env.h
+++ b/libkc3/env.h
@@ -20,6 +20,12 @@ extern thread_local s_env *g_kc3_env;
/* Stack allocation compatible functions, call env_clean after use. */
void env_clean (s_env *env);
s_env * env_init (s_env *env, int *argc, char ***argv);
+s_env * env_init_copy (s_env *env, s_env *src);
+
+/* Heap allocation functions, call env_delete after use. */
+void env_delete (s_env *env);
+s_env * env_new (int *argc, char ***argv);
+s_env * env_new_copy (s_env *src);
/* Observers. */
s_list ** env_args (s_env *env, s_list **dest);
diff --git a/libkc3/file.c b/libkc3/file.c
index ee7392b..0abfaba 100644
--- a/libkc3/file.c
+++ b/libkc3/file.c
@@ -433,7 +433,7 @@ s_str * file_search (const s_str *suffix, const s_sym *mode,
const s_str *str;
s_str tmp = {0};
buf_init(&buf, false, sizeof(buf_s), buf_s);
- if ((r = buf_write_str(&buf, &g_kc3_env->argv0_dir)) < 0)
+ if ((r = buf_write_str(&buf, g_kc3_env->argv0_dir)) < 0)
return NULL;
buf_save_init(&buf, &save);
path = g_kc3_env->path;
diff --git a/libkc3/io.c b/libkc3/io.c
index 7116165..f5b859e 100644
--- a/libkc3/io.c
+++ b/libkc3/io.c
@@ -30,10 +30,10 @@
sw err_inspect_ ## name (type x) \
{ \
sw r; \
- r = buf_inspect_ ## name(&g_kc3_env->err, x); \
+ r = buf_inspect_ ## name(g_kc3_env->err, x); \
if (r < 0) \
return r; \
- buf_flush(&g_kc3_env->err); \
+ buf_flush(g_kc3_env->err); \
return r; \
}
@@ -41,10 +41,10 @@
sw io_inspect_ ## name (type x) \
{ \
sw r; \
- r = buf_inspect_ ## name(&g_kc3_env->out, x); \
+ r = buf_inspect_ ## name(g_kc3_env->out, x); \
if (r < 0) \
return r; \
- buf_flush(&g_kc3_env->out); \
+ buf_flush(g_kc3_env->out); \
return r; \
}
@@ -56,7 +56,7 @@
sw err_flush (void)
{
- return buf_flush(&g_kc3_env->err);
+ return buf_flush(g_kc3_env->err);
}
sw err_inspect (const s_tag *x)
@@ -75,10 +75,10 @@ sw err_inspect_buf (const s_buf *buf)
sw err_inspect_list (const s_list *x)
{
sw r;
- r = buf_inspect_list(&g_kc3_env->err, &x);
+ r = buf_inspect_list(g_kc3_env->err, &x);
if (r < 0)
return r;
- buf_flush(&g_kc3_env->err);
+ buf_flush(g_kc3_env->err);
return r;
}
@@ -91,64 +91,64 @@ sw err_puts (const char *x)
{
sw r;
sw result = 0;
- if ((r = buf_write_1(&g_kc3_env->err, x)) < 0)
+ if ((r = buf_write_1(g_kc3_env->err, x)) < 0)
return r;
result += r;
- if ((r = buf_write_u8(&g_kc3_env->err, '\n')) < 0)
+ if ((r = buf_write_u8(g_kc3_env->err, '\n')) < 0)
return r;
result += r;
- buf_flush(&g_kc3_env->err);
+ buf_flush(g_kc3_env->err);
return result;
}
sw err_write (const void *x, uw len)
{
sw r;
- if ((r = buf_write(&g_kc3_env->err, x, len)) > 0)
- buf_flush(&g_kc3_env->err);
+ if ((r = buf_write(g_kc3_env->err, x, len)) > 0)
+ buf_flush(g_kc3_env->err);
return r;
}
sw err_write_1 (const char *x)
{
sw r;
- if ((r = buf_write_1(&g_kc3_env->err, x)) > 0)
- buf_flush(&g_kc3_env->err);
+ if ((r = buf_write_1(g_kc3_env->err, x)) > 0)
+ buf_flush(g_kc3_env->err);
return r;
}
sw err_write_str (const s_str *x)
{
sw r;
- if ((r = buf_write_str(&g_kc3_env->err, x)) > 0)
- buf_flush(&g_kc3_env->err);
+ if ((r = buf_write_str(g_kc3_env->err, x)) > 0)
+ buf_flush(g_kc3_env->err);
return r;
}
sw err_write_u8 (u8 x)
{
sw r;
- if ((r = buf_write_u8(&g_kc3_env->err, x)) > 0)
- buf_flush(&g_kc3_env->err);
+ if ((r = buf_write_u8(g_kc3_env->err, x)) > 0)
+ buf_flush(g_kc3_env->err);
return r;
}
sw io_flush (void)
{
- return buf_flush(&g_kc3_env->out);
+ return buf_flush(g_kc3_env->out);
}
sw io_inspect (const s_tag *x)
{
sw r;
sw result = 0;
- if ((r = buf_inspect_tag(&g_kc3_env->out, x)) < 0)
+ if ((r = buf_inspect_tag(g_kc3_env->out, x)) < 0)
return r;
result += r;
- if ((r = buf_write_u8(&g_kc3_env->out, '\n')) < 0)
+ if ((r = buf_write_u8(g_kc3_env->out, '\n')) < 0)
return r;
result += r;
- buf_flush(&g_kc3_env->out);
+ buf_flush(g_kc3_env->out);
return result;
}
@@ -169,45 +169,45 @@ sw io_puts (const char *x)
{
sw r;
sw result = 0;
- if ((r = buf_write_1(&g_kc3_env->out, x)) < 0)
+ if ((r = buf_write_1(g_kc3_env->out, x)) < 0)
return r;
result += r;
- if ((r = buf_write_u8(&g_kc3_env->out, '\n')) < 0)
+ if ((r = buf_write_u8(g_kc3_env->out, '\n')) < 0)
return r;
result += r;
- buf_flush(&g_kc3_env->out);
+ buf_flush(g_kc3_env->out);
return result;
}
sw io_write (const void *x, uw len)
{
sw r;
- if ((r = buf_write(&g_kc3_env->out, x, len)) > 0)
- buf_flush(&g_kc3_env->out);
+ if ((r = buf_write(g_kc3_env->out, x, len)) > 0)
+ buf_flush(g_kc3_env->out);
return r;
}
sw io_write_1 (const char *x)
{
sw r;
- if ((r = buf_write_1(&g_kc3_env->out, x)) > 0)
- buf_flush(&g_kc3_env->out);
+ if ((r = buf_write_1(g_kc3_env->out, x)) > 0)
+ buf_flush(g_kc3_env->out);
return r;
}
sw io_write_str (const s_str *x)
{
sw r;
- if ((r = buf_write_str(&g_kc3_env->out, x)) > 0)
- buf_flush(&g_kc3_env->out);
+ if ((r = buf_write_str(g_kc3_env->out, x)) > 0)
+ buf_flush(g_kc3_env->out);
return r;
}
sw io_write_u8 (u8 x)
{
sw r;
- if ((r = buf_write_u8(&g_kc3_env->out, x)) > 0)
- buf_flush(&g_kc3_env->out);
+ if ((r = buf_write_u8(g_kc3_env->out, x)) > 0)
+ buf_flush(g_kc3_env->out);
return r;
}
diff --git a/libkc3/kc3.c b/libkc3/kc3.c
index 0df9c66..f5f7fea 100644
--- a/libkc3/kc3.c
+++ b/libkc3/kc3.c
@@ -204,7 +204,7 @@ void ** kc3_dlopen (const s_str *path, void **dest)
s_facts ** kc3_env_db (s_facts **dest)
{
- *dest = &g_kc3_env->facts;
+ *dest = g_kc3_env->facts;
return dest;
}
@@ -313,7 +313,7 @@ s_tag * kc3_facts_first_with_tags (s_facts *facts, s_tag *subject,
uw * kc3_facts_next_id (uw *dest)
{
assert(dest);
- *dest = g_kc3_env->facts.next_id;
+ *dest = g_kc3_env->facts->next_id;
return dest;
}
@@ -464,8 +464,7 @@ s_tag * kc3_let (s_tag *vars, s_tag *tag, s_tag *dest)
void kc3_license (void)
{
- buf_write_1(&g_kc3_env->out, g_kc3_license);
- buf_flush(&g_kc3_env->out);
+ io_write_1(g_kc3_license);
}
s_array * kc3_list_to_array (s_list **list,
@@ -739,16 +738,25 @@ s_tag * kc3_thread_delete (u_ptr_w *thread, s_tag *dest)
u_ptr_w * kc3_thread_new (u_ptr_w *dest, p_callable *start)
{
s_tag *tag;
- if (! (tag = tag_new_tuple(2)))
+ if (! (tag = tag_new_tuple(3)))
return NULL;
- if (! tag_init_callable_copy(tag->data.tuple.tag + 1, start))
+ if (! tag_init_callable_copy(tag->data.tuple.tag + 1, start)) {
+ tag_delete(tag);
return NULL;
+ }
+ tag->data.tuple.tag[2].type = TAG_PTR;
+ if (! (tag->data.tuple.tag[2].data.ptr.p = env_new_copy(g_kc3_env))) {
+ tag_delete(tag);
+ return NULL;
+ }
if (pthread_create((pthread_t *) &dest->p, NULL, kc3_thread_start,
tag)) {
err_puts("kc3_thread_new: pthread_create");
assert(! "kc3_thread_new: pthread_create");
+ tag_delete(tag);
return NULL;
}
+ tag_delete(tag);
return dest;
}
@@ -758,8 +766,9 @@ void * kc3_thread_start (void *arg)
s_callable *start;
tag = arg;
if (tag->type != TAG_TUPLE ||
- tag->data.tuple.count != 2 ||
- tag->data.tuple.tag[1].type != TAG_CALLABLE) {
+ tag->data.tuple.count != 3 ||
+ tag->data.tuple.tag[1].type != TAG_CALLABLE ||
+ tag->data.tuple.tag[2].type != TAG_PTR) {
err_puts("kc3_thread_start: invalid argument");
assert(! "kc3_thread_start: invalid argument");
return NULL;
diff --git a/libkc3/types.h b/libkc3/types.h
index a6e4d69..d07ff51 100644
--- a/libkc3/types.h
+++ b/libkc3/types.h
@@ -739,17 +739,17 @@ struct facts_cursor {
struct env {
sw argc;
char **argv;
- s_str argv0_dir;
+ s_str *argv0_dir;
s_list *stacktrace;
const s_sym *current_defmodule;
- s_buf err;
+ s_buf *err;
s_error_handler *error_handler;
- s_facts facts;
+ s_facts *facts;
s_frame *frame;
- s_frame global_frame;
- s_buf in;
- s_str module_path;
- s_buf out;
+ s_frame *global_frame;
+ s_buf *in;
+ s_str *module_path;
+ s_buf *out;
s_list *path;
uw quote_level;
s_frame read_time_frame;