aboutsummaryrefslogtreecommitdiff
path: root/builtins
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-08-22 14:02:48 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-08-22 14:02:48 -0400
commitad51b208b4924d04f1d6a804178a67994b4f9e59 (patch)
tree9ae29e0af02b7406a0cf07fdd8b0dd7fdac0aedf /builtins
parent2055439be4f5053b9a4d631cefd8bb7c83a8e4e3 (diff)
Overhaul of import syntax. Now everything uses `use`: `use foo`, `use
./foo.tm`, `use <foo.h>`, `use libfoo.so`
Diffstat (limited to 'builtins')
-rw-r--r--builtins/files.c4
-rw-r--r--builtins/util.h2
2 files changed, 4 insertions, 2 deletions
diff --git a/builtins/files.c b/builtins/files.c
index 70c76484..f506d596 100644
--- a/builtins/files.c
+++ b/builtins/files.c
@@ -26,10 +26,10 @@ public char *resolve_path(const char *path, const char *relative_to, const char
// Resolve the path to an absolute path, assuming it's relative to the file
// it was found in:
char buf[PATH_MAX] = {0};
- if (streq(path, "~") || strncmp(path, "~/", 2) == 0) {
+ if (streq(path, "~") || starts_with(path, "~/")) {
char *resolved = realpath(heap_strf("%s%s", getenv("HOME"), path+1), buf);
if (resolved) return GC_strdup(resolved);
- } else if (streq(path, ".") || strncmp(path, "./", 2) == 0) {
+ } else if (streq(path, ".") || starts_with(path, "./")) {
char *relative_dir = dirname(GC_strdup(relative_to));
char *resolved = realpath(heap_strf("%s/%s", relative_dir, path), buf);
if (resolved) return GC_strdup(resolved);
diff --git a/builtins/util.h b/builtins/util.h
index 44b780c2..271403ff 100644
--- a/builtins/util.h
+++ b/builtins/util.h
@@ -10,6 +10,8 @@
#include <err.h>
#define streq(a, b) (((a) == NULL && (b) == NULL) || (((a) == NULL) == ((b) == NULL) && strcmp(a, b) == 0))
+#define starts_with(line, prefix) (strncmp(line, prefix, strlen(prefix)) == 0)
+#define ends_with(line, suffix) (strlen(line) >= strlen(suffix) && strcmp(line + strlen(line) - strlen(suffix), suffix) == 0)
#define new(t, ...) ((t*)memcpy(GC_MALLOC(sizeof(t)), &(t){__VA_ARGS__}, sizeof(t)))
#define copy(obj_ptr) ((__typeof(obj_ptr))memcpy(GC_MALLOC(sizeof(*(obj_ptr))), obj_ptr, sizeof(*(obj_ptr))))
#define Match(x, _tag) ((x)->tag == _tag ? &(x)->__data._tag : (errx(1, __FILE__ ":%d This was supposed to be a " # _tag "\n", __LINE__), &(x)->__data._tag))