Hash :
fd65c24d
Author :
Thomas de Grivel
Date :
2024-03-29T20:41:39
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
/* c3
* Copyright 2022-2024 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include "assert.h"
#include <string.h>
#include "buf.h"
#include "character.h"
#include "compare.h"
#include "env.h"
#include "facts.h"
#include "facts_with.h"
#include "facts_with_cursor.h"
#include "file.h"
#include "module.h"
#include "tag.h"
#include "sym.h"
bool module_ensure_loaded (const s_sym *module, s_facts *facts)
{
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_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(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");
return false;
}
return true;
}
module_maybe_reload(module, facts);
return true;
}
bool module_has_symbol (const s_sym *module, const s_ident *ident,
s_facts *facts)
{
s_tag tag_ident;
s_tag tag_module_name;
s_tag tag_operator;
s_tag tag_symbol;
tag_init_ident(&tag_ident, ident);
tag_init_sym( &tag_module_name, module);
tag_init_sym( &tag_operator, &g_sym_operator);
tag_init_sym( &tag_symbol, &g_sym_symbol);
return facts_find_fact_by_tags(facts, &tag_module_name,
&tag_symbol, &tag_ident) ||
facts_find_fact_by_tags(facts, &tag_module_name,
&tag_operator, &tag_ident);
}
bool module_is_loading (const s_sym *module)
{
return env_module_is_loading(&g_c3_env, module);
}
bool module_load (const s_sym *module, s_facts *facts)
{
return env_module_load(&g_c3_env, module, facts);
}
s_tag * module_load_time (const s_sym *module, s_facts *facts,
s_tag *dest)
{
s_facts_with_cursor cursor;
s_tag tag_module_name;
s_tag tag_load_time;
s_tag tag_time_var;
tag_init_sym(&tag_module_name, module);
tag_init_sym(&tag_load_time, &g_sym_load_time);
tag_init_var(&tag_time_var);
facts_with(facts, &cursor, (t_facts_spec) {
&tag_module_name, &tag_load_time, &tag_time_var, NULL, NULL });
if (! facts_with_cursor_next(&cursor)) {
facts_with_cursor_clean(&cursor);
return NULL;
}
facts_with_cursor_clean(&cursor);
*dest = tag_time_var;
return dest;
}
bool module_maybe_reload (const s_sym *module, s_facts *facts)
{
return env_module_maybe_reload(&g_c3_env, module, facts);
}
s_str * module_path (const s_sym *module, const s_str *prefix,
s_str *dest)
{
character b = 0;
character c;
s_buf in;
s_buf out;
sw out_size;
sw r;
assert(dest);
assert(module);
buf_init_str(&in, false, (s_str *) &module->str);
out_size = module_path_size(module, prefix);
buf_init_alloc(&out, out_size);
if ((r = buf_write_str(&out, prefix)) < 0)
goto error;
while ((r = buf_read_character_utf8(&in, &c)) > 0) {
if (c == '.')
c = '/';
else if (character_is_uppercase(c)) {
if (character_is_lowercase(b)) {
if ((r = buf_write_1(&out, "_")) < 0)
goto error;
}
b = c;
c = character_to_lower(c);
}
else
b = c;
if ((r = buf_write_character_utf8(&out, c)) < 0)
goto error;
}
if ((r = buf_write_1(&out, FACTS_EXT)) < 0)
goto error;
return buf_to_str(&out, dest);
error:
buf_clean(&out);
err_puts("module_path: error");
assert(! "module_path: error");
return NULL;
}
sw module_path_size (const s_sym *module, const s_str *prefix)
{
character b = 0;
character c;
s_buf in;
sw r;
sw result;
assert(prefix);
assert(module);
buf_init_str(&in, false, (s_str *) &module->str);
result = prefix->size;
while ((r = buf_read_character_utf8(&in, &c)) > 0) {
if (c == '.')
c = '/';
else if (character_is_uppercase(c)) {
if (character_is_lowercase(b))
result += strlen("_");
b = c;
c = character_to_lower(c);
}
else
b = c;
result += character_utf8_size(c);
}
result += strlen(FACTS_EXT);
return result;
}