Replace heap_str with GC_strdup

This commit is contained in:
Bruce Hill 2024-07-26 13:30:24 -04:00
parent cfe46ee393
commit b2e752ee32
8 changed files with 17 additions and 24 deletions

2
ast.h
View File

@ -16,7 +16,7 @@
.tag=ast_tag, .__data.ast_tag={__VA_ARGS__}))
#define FakeAST(ast_tag, ...) (new(ast_t, .tag=ast_tag, .__data.ast_tag={__VA_ARGS__}))
#define WrapAST(ast, ast_tag, ...) (new(ast_t, .file=(ast)->file, .start=(ast)->start, .end=(ast)->end, .tag=ast_tag, .__data.ast_tag={__VA_ARGS__}))
#define TextAST(ast, _str) WrapAST(ast, TextLiteral, .str=heap_str(_str))
#define TextAST(ast, _str) WrapAST(ast, TextLiteral, .str=GC_strdup(_str))
struct binding_s;
typedef struct type_ast_s type_ast_t;

View File

@ -28,36 +28,36 @@ public char *resolve_path(const char *path, const char *relative_to, const char
char buf[PATH_MAX] = {0};
if (streq(path, "~") || strncmp(path, "~/", 2) == 0) {
char *resolved = realpath(heap_strf("%s%s", getenv("HOME"), path+1), buf);
if (resolved) return heap_str(resolved);
if (resolved) return GC_strdup(resolved);
} else if (streq(path, ".") || strncmp(path, "./", 2) == 0) {
char *relative_dir = dirname(heap_str(relative_to));
char *relative_dir = dirname(GC_strdup(relative_to));
char *resolved = realpath(heap_strf("%s/%s", relative_dir, path), buf);
if (resolved) return heap_str(resolved);
if (resolved) return GC_strdup(resolved);
} else if (path[0] == '/') {
// Absolute path:
char *resolved = realpath(path, buf);
if (resolved) return heap_str(resolved);
if (resolved) return GC_strdup(resolved);
} else {
// Relative path:
char *relative_dir = dirname(heap_str(relative_to));
char *relative_dir = dirname(GC_strdup(relative_to));
if (!system_path) system_path = ".";
char *copy = heap_str(system_path);
char *copy = GC_strdup(system_path);
for (char *dir, *pos = copy; (dir = strsep(&pos, ":")); ) {
if (dir[0] == '/') {
char *resolved = realpath(heap_strf("%s/%s", dir, path), buf);
if (resolved) return heap_str(resolved);
if (resolved) return GC_strdup(resolved);
} else if (dir[0] == '~' && (dir[1] == '\0' || dir[1] == '/')) {
char *resolved = realpath(heap_strf("%s%s/%s", getenv("HOME"), dir+1, path), buf);
if (resolved) return heap_str(resolved);
if (resolved) return GC_strdup(resolved);
} else if (streq(dir, ".") || strncmp(dir, "./", 2) == 0) {
char *resolved = realpath(heap_strf("%s/%s", relative_dir, path), buf);
if (resolved) return heap_str(resolved);
if (resolved) return GC_strdup(resolved);
} else if (streq(dir, ".") || streq(dir, "..") || strncmp(dir, "./", 2) == 0 || strncmp(dir, "../", 3) == 0) {
char *resolved = realpath(heap_strf("%s/%s/%s", relative_dir, dir, path), buf);
if (resolved) return heap_str(resolved);
if (resolved) return GC_strdup(resolved);
} else {
char *resolved = realpath(heap_strf("%s/%s", dir, path), buf);
if (resolved) return heap_str(resolved);
if (resolved) return GC_strdup(resolved);
}
}
}

View File

@ -11,12 +11,6 @@
public bool USE_COLOR;
public char *heap_str(const char *str)
{
if (!str) return NULL;
return GC_strndup(str, strlen(str));
}
public char *heap_strf(const char *fmt, ...)
{
va_list args;

View File

@ -25,7 +25,6 @@
extern bool USE_COLOR;
char *heap_str(const char *str);
char *heap_strf(const char *fmt, ...);
CORD CORD_asprintf(CORD fmt, ...);
#define CORD_appendf(cord, fmt, ...) CORD_sprintf(cord, "%r" fmt, *(cord) __VA_OPT__(,) __VA_ARGS__)

View File

@ -156,7 +156,7 @@ const char *unescape(const char **out) {
assert(*escape == '\\');
if (unescapes[(int)escape[1]]) {
*endpos = escape + 2;
return heap_str(unescapes[(int)escape[1]]);
return GC_strdup(unescapes[(int)escape[1]]);
} else if (escape[1] == 'U' && escape[2]) {
char *endptr = NULL;
long codepoint = strtol(escape+2, &endptr, 16);

2
repl.c
View File

@ -50,7 +50,7 @@ void repl(void)
|| starts_with(line, "func") || starts_with(line, "struct") || starts_with(line, "lang")) {
printf("\x1b[33;1m..\x1b[m ");
fflush(stdout);
code = heap_str(line);
code = GC_strdup(line);
while ((len=getline(&line, &buf_size, stdin)) >= 0) {
if (len == 1) break;
code = heap_strf("%s%s", code, line);

2
tomo.c
View File

@ -192,7 +192,7 @@ int main(int argc, char *argv[])
// For shared objects, link up all the object files into one .so file:
if (mode == MODE_COMPILE_SHARED_OBJ) {
char *libname_id = heap_str(libname);
char *libname_id = GC_strdup(libname);
for (char *p = libname_id; *p; p++) {
if (!isalnum(*p) && *p != '_' && *p != '$')
*p = '_';

View File

@ -130,7 +130,7 @@ static env_t *load_module(env_t *env, ast_t *module_ast)
env_t *module_env = fresh_scope(env);
Table$str_set(env->imports, libname, module_env);
char *libname_id = heap_str(libname);
char *libname_id = GC_strdup(libname);
for (char *c = libname_id; *c; c++) {
if (!isalnum(*c) && *c != '_')
*c = '_';
@ -147,7 +147,7 @@ static env_t *load_module(env_t *env, ast_t *module_ast)
ast_t *ast = parse_file(tm_path, NULL);
if (!ast) errx(1, "Could not compile file %s", tm_path);
env_t *module_file_env = fresh_scope(module_env);
char *file_prefix = heap_str(file_base_name(line));
char *file_prefix = GC_strdup(file_base_name(line));
for (char *p = file_prefix; *p; p++) {
if (!isalnum(*p) && *p != '_' && *p != '$')
*p = '_';