Skip to content

Commit

Permalink
import: Import paths and ability to add to them
Browse files Browse the repository at this point in the history
  • Loading branch information
obiwac committed Oct 29, 2024
1 parent bf02989 commit 521909d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
23 changes: 23 additions & 0 deletions flamingo/flamingo.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ int flamingo_create(flamingo_t* flamingo, char const* progname, char* src, size_
flamingo->imported_srcs = NULL;
flamingo->imported_flamingos = NULL;

flamingo->import_path_count = 0;
flamingo->import_paths = NULL;

flamingo->cur_fn_body = NULL;
flamingo->cur_fn_rv = NULL;

Expand Down Expand Up @@ -182,6 +185,16 @@ void flamingo_destroy(flamingo_t* flamingo) {
free(flamingo->imported_srcs);
}

// Free the import paths.

for (size_t i = 0; i < flamingo->import_path_count; i++) {
free(flamingo->import_paths[i]);
}

if (flamingo->import_paths != NULL) {
free(flamingo->import_paths);
}

// Finally, free the primitive type members.

primitive_type_member_free(flamingo);
Expand All @@ -203,6 +216,16 @@ void flamingo_register_external_fn_cb(flamingo_t* flamingo, flamingo_external_fn
flamingo->external_fn_cb_data = data;
}

void flamingo_add_import_path(flamingo_t* flamingo, char* path) {
char* const duped = strdup(path);
assert(duped != NULL);

flamingo->import_paths = realloc(flamingo->import_paths, (flamingo->import_path_count + 1) * sizeof *flamingo->import_paths);
assert(flamingo->import_paths != NULL);

flamingo->import_paths[flamingo->import_path_count++] = duped;
}

static int parse(flamingo_t* flamingo, TSNode node) {
size_t const n = ts_node_child_count(node);

Expand Down
7 changes: 7 additions & 0 deletions flamingo/flamingo.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ struct flamingo_t {
flamingo_t* imported_flamingos;
char** imported_srcs;

// Import paths for global imports.
// These shouldn't be inherited by imported instances for now.

size_t import_path_count;
char** import_paths;

// Current function stuff.

flamingo_ts_node_t cur_fn_body;
Expand All @@ -188,6 +194,7 @@ void flamingo_destroy(flamingo_t* flamingo);

char* flamingo_err(flamingo_t* flamingo);
void flamingo_register_external_fn_cb(flamingo_t* flamingo, flamingo_external_fn_cb_t cb, void* data);
void flamingo_add_import_path(flamingo_t* flamingo, char* path);
int flamingo_inherit_env(flamingo_t* flamingo, flamingo_env_t* env);
int flamingo_run(flamingo_t* flamingo);

Expand Down

0 comments on commit 521909d

Please sign in to comment.