diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 8be6998..097546c 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -16,10 +16,16 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/../bin/std") # add native module libraries file(GLOB_RECURSE STDLIB_IO_SOURCE_FILES src/io/*.c) -add_library(io ${STDLIB_IO_SOURCE_FILES}) +add_library(gscio ${STDLIB_IO_SOURCE_FILES}) file(GLOB_RECURSE STDLIB_MEM_SOURCE_FILES src/mem/*.c) -add_library(mem ${STDLIB_MEM_SOURCE_FILES}) +add_library(gscmem ${STDLIB_MEM_SOURCE_FILES}) file(GLOB_RECURSE STDLIB_OS_SOURCE_FILES src/os/*.c) -add_library(os ${STDLIB_OS_SOURCE_FILES}) +add_library(gscos ${STDLIB_OS_SOURCE_FILES}) + +# Complete standard library +add_library(gscstd + ${STDLIB_IO_SOURCE_FILES} + ${STDLIB_MEM_SOURCE_FILES} + ${STDLIB_OS_SOURCE_FILES}) diff --git a/lib/src/entry/entrypoint.c b/lib/src/entry/entrypoint.c index c41e482..75cf927 100644 --- a/lib/src/entry/entrypoint.c +++ b/lib/src/entry/entrypoint.c @@ -2,9 +2,6 @@ // Created by servostar on 6/10/24. // -extern void entry(void); - int main(int argc, char* argv[]) { - entry(); return 0; } diff --git a/lib/src/io.gsc b/lib/src/io.gsc index db8de9b..c729644 100644 --- a/lib/src/io.gsc +++ b/lib/src/io.gsc @@ -6,7 +6,7 @@ # | Generic Input/Output | # `----------------------------------------` -import "def.gsc" +include "def.gsc" # platform specific handle to an I/O device # can be a file, buffer, window or something else diff --git a/lib/src/mem.gsc b/lib/src/mem.gsc index 0307666..b9df686 100644 --- a/lib/src/mem.gsc +++ b/lib/src/mem.gsc @@ -6,7 +6,7 @@ # | Memory Management | # `----------------------------------------` -import "def.gsc" +include "def.gsc" # Allocate `len` bytes of heap memory # Returns a pointer to the memory as `ptr` diff --git a/lib/src/os.gsc b/lib/src/os.gsc index bfd6958..5f7a190 100644 --- a/lib/src/os.gsc +++ b/lib/src/os.gsc @@ -6,7 +6,7 @@ # | Operating System | # `----------------------------------------` -import "def.gsc" +include "def.gsc" # Return a hard coded C string identifying the underlying operating system # Will return one of the following: diff --git a/lib/src/std.gsc b/lib/src/std.gsc index cf690e3..36d6a75 100644 --- a/lib/src/std.gsc +++ b/lib/src/std.gsc @@ -7,10 +7,10 @@ # `----------------------------------------` # standard type definitions -import "def.gsc" +include "def.gsc" # I/O operations -import "io.gsc" +include "io.gsc" # memory management -import "mem.gsc" +include "mem.gsc" diff --git a/src/ast/ast.c b/src/ast/ast.c index 7da6bf5..9f00f56 100644 --- a/src/ast/ast.c +++ b/src/ast/ast.c @@ -101,6 +101,7 @@ const char* AST_node_to_string(const struct AST_Node_t* node) { case AST_Ident: case AST_Macro: case AST_Import: + case AST_Include: case AST_Storage: case AST_Typekind: case AST_Sign: diff --git a/src/ast/ast.h b/src/ast/ast.h index 5c71d2c..a7be0be 100644 --- a/src/ast/ast.h +++ b/src/ast/ast.h @@ -79,6 +79,7 @@ enum AST_SyntaxElement_t { AST_AddressOf, AST_Dereference, AST_Reference, + AST_Include, AST_ELEMENT_COUNT }; diff --git a/src/cfg/opt.c b/src/cfg/opt.c index 57ed113..22f0e98 100644 --- a/src/cfg/opt.c +++ b/src/cfg/opt.c @@ -212,6 +212,36 @@ TargetConfig* default_target_config_from_args() { char* default_import_path = mem_strdup(MemoryNamespaceOpt, "."); g_array_append_val(config->import_paths, default_import_path); + if (is_option_set("import-paths")) { + const Option* opt = get_option("import-paths"); + + if (opt->value != NULL) { + + const char* start = opt->value; + const char* end = NULL; + while((end = strchr(start, ',')) != NULL) { + + const int len = end - start; + char* import_path = malloc(len + 1); + memcpy(import_path, start, len); + import_path[len] = 0; + + g_array_append_val(config->import_paths, import_path); + + start = end; + } + + const int len = strlen(start); + if (len > 0) { + char* import_path = malloc(len + 1); + memcpy(import_path, start, len); + import_path[len] = 0; + + g_array_append_val(config->import_paths, import_path); + } + } + } + return config; } diff --git a/src/compiler.c b/src/compiler.c index 289daf7..7e85543 100644 --- a/src/compiler.c +++ b/src/compiler.c @@ -170,6 +170,10 @@ static void run_backend_codegen(const Module* module, const TargetConfig* target const char* get_absolute_import_path(const TargetConfig* config, const char* import_target_name) { INFO("resolving absolute path for import target: %s", import_target_name); + if (!g_str_has_suffix(import_target_name, ".gsc")) { + import_target_name = g_strjoin("", import_target_name, ".gsc", NULL); + } + for (guint i = 0; i < config->import_paths->len; i++) { const char* import_directory_path = g_array_index(config->import_paths, char*, i); @@ -204,7 +208,7 @@ static int compile_module_with_dependencies(ModuleFileStack *unit, ModuleFile* f for (size_t i = 0; i < AST_get_child_count(root_module); i++) { AST_NODE_PTR child = AST_get_node(root_module, i); - if (child->kind == AST_Import) { + if (child->kind == AST_Import || child->kind == AST_Include) { const char* path = get_absolute_import_path(target, child->value); if (path == NULL) { diff --git a/src/lex/lexer.l b/src/lex/lexer.l index bfde8ff..e955b0e 100644 --- a/src/lex/lexer.l +++ b/src/lex/lexer.l @@ -81,6 +81,7 @@ "!" {DEBUG("\"%s\" tokenized with \'OpBitnot\'", yytext); return(OpBitnot);}; "^" {DEBUG("\"%s\" tokenized with \'OpBitxor\'", yytext); return(OpBitxor);}; "import" {DEBUG("\"%s\" tokenized with \'KeyImport\'", yytext); return(KeyImport);}; +"include" {DEBUG("\"%s\" tokenized with \'KeyInclude\'", yytext); return(KeyInclude);}; "silent" {DEBUG("\"%s\" tokenized with \'KeySilent\'", yytext); return(KeySilent);}; "box" {DEBUG("\"%s\" tokenized with \'KeyBox\'", yytext); return(KeyBox);}; "typeof" {DEBUG("\"%s\" tokenized with \'FunTypeof\'", yytext); return(FunTypeof);}; diff --git a/src/llvm/link/lld.c b/src/llvm/link/lld.c index e1a2175..687dd3a 100644 --- a/src/llvm/link/lld.c +++ b/src/llvm/link/lld.c @@ -65,20 +65,16 @@ TargetLinkConfig* lld_create_link_config(__attribute__((unused)) const Target* t g_array_append_val(config->object_file_names, target_object); INFO("resolved path of target object: %s", target_object); - // if it is an app, add entrypoint library - if (target_config->mode == Application) { - char* entrypoint = g_strdup("libentrypoint.a"); - g_array_append_val(module->imports, entrypoint); - } - // resolve absolute paths to dependent library object files DEBUG("resolving target dependencies..."); for (guint i = 0; i < module->imports->len; i++) { const char* dependency = g_array_index(module->imports, const char*, i); - const char* dependency_object = get_absolute_link_path(target_config, dependency); + const char* library = g_strjoin("", "libgsc", dependency, ".a", NULL); + + const char* dependency_object = get_absolute_link_path(target_config, library); if (dependency_object == NULL) { - ERROR("failed to resolve path to dependency object: %s", dependency); + ERROR("failed to resolve path to dependency object: %s", library); lld_delete_link_config(config); return NULL; } diff --git a/src/set/set.c b/src/set/set.c index 1ad9652..e10a4c2 100644 --- a/src/set/set.c +++ b/src/set/set.c @@ -2379,12 +2379,14 @@ Module *create_set(AST_NODE_PTR currentNode) { GHashTable *functions = g_hash_table_new(g_str_hash, g_str_equal); GHashTable *variables = g_hash_table_new(g_str_hash, g_str_equal); GArray *imports = g_array_new(FALSE, FALSE, sizeof(const char *)); + GArray *includes = g_array_new(FALSE, FALSE, sizeof(const char *)); rootModule->boxes = boxes; rootModule->types = types; rootModule->functions = functions; rootModule->variables = variables; rootModule->imports = imports; + rootModule->includes = includes; DEBUG("created Module struct"); @@ -2456,6 +2458,10 @@ Module *create_set(AST_NODE_PTR currentNode) { DEBUG("create Import"); g_array_append_val(imports, AST_get_node(currentNode, i)->value); break; + case AST_Include: + DEBUG("create Include"); + g_array_append_val(includes, AST_get_node(currentNode, i)->value); + break; default: INFO("Provided source file could not be parsed because of semantic error."); break; diff --git a/src/set/types.h b/src/set/types.h index 2eb6e44..98e9f97 100644 --- a/src/set/types.h +++ b/src/set/types.h @@ -580,6 +580,7 @@ typedef struct Module_t { GHashTable* variables; // to be resolved after the module has been parsed completely GArray* imports; + GArray* includes; } Module; diff --git a/src/yacc/parser.y b/src/yacc/parser.y index 70f3069..29dd70c 100644 --- a/src/yacc/parser.y +++ b/src/yacc/parser.y @@ -53,6 +53,7 @@ %type opbool %type opbit %type moduleimport +%type moduleinclude %type programbody %type fundef %type fundecl @@ -109,6 +110,7 @@ %token OpBitnot %token OpBitxor %token KeyImport +%token KeyInclude %token KeySilent %token KeyBox %token FunTypeof @@ -142,6 +144,7 @@ program: program programbody {AST_push_node(root, $2); | programbody {AST_push_node(root, $1);}; programbody: moduleimport {$$ = $1;} + | moduleinclude {$$ = $1;} | fundef{$$ = $1;} | fundecl{$$ = $1;} | box{$$ = $1;} @@ -328,6 +331,9 @@ funcall: Ident argumentlist {AST_NODE_PTR funcall = AST_new_node(new_loc(), AST_ moduleimport: KeyImport ValStr {$$ = AST_new_node(new_loc(), AST_Import, $2); DEBUG("Module-Import"); }; +moduleinclude: KeyInclude ValStr {$$ = AST_new_node(new_loc(), AST_Include, $2); + DEBUG("Module-Include"); }; + statementlist: statementlist statement {AST_push_node($1, $2); $$ = $1;} | statement {AST_NODE_PTR list = AST_new_node(new_loc(), AST_StmtList, NULL);