aboutsummaryrefslogtreecommitdiff
path: root/src/parser.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/parser.c')
-rw-r--r--src/parser.c36
1 files changed, 24 insertions, 12 deletions
diff --git a/src/parser.c b/src/parser.c
index 22c8b77..f531705 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -478,24 +478,27 @@ int parser_parse_file(Parser *self, BufferView filepath) {
int result;
char *file_data;
usize file_size;
- char *filepath_tmp;
amal_log_debug("Parsing %.*s", (int)filepath.size, filepath.data);
assert(!self->started && "Parser can't be reused. Create a new parser.");
self->started = bool_true;
- /* TODO: Somehow free this... */
- /*return_if_error(scoped_allocator_alloc(self->allocator, filepath.size + 1, (void**)&filepath_tmp));*/
- filepath_tmp = malloc(filepath.size + 1);
- am_memcpy(filepath_tmp, filepath.data, filepath.size);
- filepath_tmp[filepath.size] = '\0';
- result = read_whole_file(filepath_tmp, &file_data, &file_size);
+ assert(filepath.size > 0 && filepath.data[filepath.size] == '\0');
+ result = read_whole_file(filepath.data, &file_data, &file_size);
if(result != 0) return result;
- result = parser_parse_buffer(self, create_buffer_view(file_data, file_size), create_buffer_view(filepath_tmp, filepath.size));
- /* TODO: Somehow free this.. causes issue where filepath becomes corrupt */
- /*am_free(file_data);*/
+ result = parser_parse_buffer(self, create_buffer_view(file_data, file_size), filepath);
return result;
}
+static CHECK_RESULT int file_path_join(BufferView directory, BufferView file, ScopedAllocator *allocator, Buffer *result) {
+ return_if_error(buffer_init(result, allocator));
+ return_if_error(buffer_append(result, NULL, directory.size + 1 + file.size + 1));
+ am_memcpy(result->data, directory.data, directory.size);
+ result->data[directory.size] = '/';
+ am_memcpy(result->data + directory.size + 1, file.data, file.size);
+ result->data[directory.size + 1 + file.size] = '\0';
+ return 0;
+}
+
/*
Path can be path to included library path (or system library path) in which case
the path separator is a dot, otherwise the path separator is forward slash '/'
@@ -503,7 +506,16 @@ the path separator is a dot, otherwise the path separator is forward slash '/'
int parser_queue_file(Parser *self, BufferView path) {
/* TODO: Do not load same path twice or the compile will fail (and it can have recursive import) also for performance reasons */
/* TODO: Parse special path (to include library path with dots) */
- /* TODO: Path should be relative to the file that uses @import */
- throw_if_error(amal_compiler_load_file(self->compiler, path));
+ BufferView file_directory;
+ BufferView filename;
+ Buffer file_to_parse;
+ file_directory = file_get_parent_directory(self->tokenizer.code_name);
+ filename = file_get_name(path);
+ return_if_error(file_path_join(file_directory, filename, self->allocator, &file_to_parse));
+ /*
+ We want buffer to be null terminated but null terminated character
+ should not be included for the length.
+ */
+ throw_if_error(amal_compiler_load_file(self->compiler, create_buffer_view(file_to_parse.data, file_to_parse.size - 1)));
return PARSER_OK;
}