aboutsummaryrefslogtreecommitdiff
path: root/src/environment.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/environment.c')
-rw-r--r--src/environment.c44
1 files changed, 42 insertions, 2 deletions
diff --git a/src/environment.c b/src/environment.c
index a34e0907..4fdfbd4e 100644
--- a/src/environment.c
+++ b/src/environment.c
@@ -7,8 +7,12 @@
#include "cordhelpers.h"
#include "environment.h"
#include "parse.h"
+#include "stdlib/c_strings.h"
#include "stdlib/datatypes.h"
+#include "stdlib/memory.h"
#include "stdlib/paths.h"
+#include "stdlib/pointers.h"
+#include "stdlib/simpleparse.h"
#include "stdlib/tables.h"
#include "stdlib/text.h"
#include "stdlib/util.h"
@@ -547,12 +551,12 @@ CORD get_id_suffix(const char *filename)
{
assert(filename);
Path_t path = Path$from_str(filename);
- Path_t build_dir = Path$with_component(Path$parent(path), Text(".build"));
+ Path_t build_dir = Path$sibling(path, Text(".build"));
if (mkdir(Path$as_c_string(build_dir), 0755) != 0) {
if (!Path$is_directory(build_dir, true))
err(1, "Could not make .build directory");
}
- Path_t id_file = Path$with_component(build_dir, Texts(Path$base_name(path), Text$from_str(".id")));
+ Path_t id_file = Path$child(build_dir, Texts(Path$base_name(path), Text$from_str(".id")));
OptionalText_t id = Path$read(id_file);
if (id.length < 0) err(1, "Could not read ID file: ", id_file);
return Text$as_c_string(Texts(Text("$"), id));
@@ -789,4 +793,40 @@ void set_binding(env_t *env, const char *name, type_t *type, CORD code)
Table$str_set(env->locals, name, new(binding_t, .type=type, .code=code));
}
+const char *module_alias(ast_t *use)
+{
+ static Table_t cache = {};
+ const char **cached = Table$get(cache, &use, Table$info(Pointer$info("@", &Memory$info), &CString$info));
+ if (cached) return *cached;
+ const char *name = Match(use, Use)->path;
+ const char *alias = name;
+ if (streq(name, "commands")) alias = "commands_v1.0";
+ else if (streq(name, "random")) alias = "random_v1.0";
+ else if (streq(name, "base64")) alias = "base64_v1.0";
+ else if (streq(name, "core")) alias = "core_v1.0";
+ else if (streq(name, "patterns")) alias = "patterns_v1.0";
+ else if (streq(name, "pthreads")) alias = "pthreads_v1.0";
+ else if (streq(name, "shell")) alias = "shell_v1.0";
+ else if (streq(name, "time")) alias = "time_v1.0";
+ else if (streq(name, "uuid")) alias = "uuid_v1.0";
+ else {
+ Path_t alias_file = Path$sibling(Path$from_str(use->file->filename), Text("modules.ini"));
+ OptionalClosure_t by_line = Path$by_line(alias_file);
+ if (by_line.fn) {
+ OptionalText_t (*next_line)(void*) = by_line.fn;
+ for (Text_t line; (line=next_line(by_line.userdata)).length >= 0; ) {
+ char *line_str = Text$as_c_string(line);
+ const char *line_alias = NULL, *full_version = NULL;
+ if (!strparse(line_str, &line_alias, "=", &full_version)
+ && streq(line_alias, name)) {
+ alias = full_version;
+ break;
+ }
+ }
+ }
+ }
+ Table$set(&cache, &use, &alias, Table$info(Pointer$info("@", &Memory$info), &CString$info));
+ return alias;
+}
+
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0