Merge pull request #127 from Servostar/126-create-include-statement
126 create include statement
This commit is contained in:
commit
087a283112
|
@ -16,10 +16,16 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/../bin/std")
|
||||||
# add native module libraries
|
# add native module libraries
|
||||||
|
|
||||||
file(GLOB_RECURSE STDLIB_IO_SOURCE_FILES src/io/*.c)
|
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)
|
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)
|
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})
|
||||||
|
|
|
@ -2,9 +2,6 @@
|
||||||
// Created by servostar on 6/10/24.
|
// Created by servostar on 6/10/24.
|
||||||
//
|
//
|
||||||
|
|
||||||
extern void entry(void);
|
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
entry();
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
# | Generic Input/Output |
|
# | Generic Input/Output |
|
||||||
# `----------------------------------------`
|
# `----------------------------------------`
|
||||||
|
|
||||||
import "def.gsc"
|
include "def.gsc"
|
||||||
|
|
||||||
# platform specific handle to an I/O device
|
# platform specific handle to an I/O device
|
||||||
# can be a file, buffer, window or something else
|
# can be a file, buffer, window or something else
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
# | Memory Management |
|
# | Memory Management |
|
||||||
# `----------------------------------------`
|
# `----------------------------------------`
|
||||||
|
|
||||||
import "def.gsc"
|
include "def.gsc"
|
||||||
|
|
||||||
# Allocate `len` bytes of heap memory
|
# Allocate `len` bytes of heap memory
|
||||||
# Returns a pointer to the memory as `ptr`
|
# Returns a pointer to the memory as `ptr`
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
# | Operating System |
|
# | Operating System |
|
||||||
# `----------------------------------------`
|
# `----------------------------------------`
|
||||||
|
|
||||||
import "def.gsc"
|
include "def.gsc"
|
||||||
|
|
||||||
# Return a hard coded C string identifying the underlying operating system
|
# Return a hard coded C string identifying the underlying operating system
|
||||||
# Will return one of the following:
|
# Will return one of the following:
|
||||||
|
|
|
@ -7,10 +7,10 @@
|
||||||
# `----------------------------------------`
|
# `----------------------------------------`
|
||||||
|
|
||||||
# standard type definitions
|
# standard type definitions
|
||||||
import "def.gsc"
|
include "def.gsc"
|
||||||
|
|
||||||
# I/O operations
|
# I/O operations
|
||||||
import "io.gsc"
|
include "io.gsc"
|
||||||
|
|
||||||
# memory management
|
# memory management
|
||||||
import "mem.gsc"
|
include "mem.gsc"
|
||||||
|
|
|
@ -101,6 +101,7 @@ const char* AST_node_to_string(const struct AST_Node_t* node) {
|
||||||
case AST_Ident:
|
case AST_Ident:
|
||||||
case AST_Macro:
|
case AST_Macro:
|
||||||
case AST_Import:
|
case AST_Import:
|
||||||
|
case AST_Include:
|
||||||
case AST_Storage:
|
case AST_Storage:
|
||||||
case AST_Typekind:
|
case AST_Typekind:
|
||||||
case AST_Sign:
|
case AST_Sign:
|
||||||
|
|
|
@ -79,6 +79,7 @@ enum AST_SyntaxElement_t {
|
||||||
AST_AddressOf,
|
AST_AddressOf,
|
||||||
AST_Dereference,
|
AST_Dereference,
|
||||||
AST_Reference,
|
AST_Reference,
|
||||||
|
AST_Include,
|
||||||
AST_ELEMENT_COUNT
|
AST_ELEMENT_COUNT
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -212,6 +212,36 @@ TargetConfig* default_target_config_from_args() {
|
||||||
char* default_import_path = mem_strdup(MemoryNamespaceOpt, ".");
|
char* default_import_path = mem_strdup(MemoryNamespaceOpt, ".");
|
||||||
g_array_append_val(config->import_paths, default_import_path);
|
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;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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) {
|
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);
|
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++) {
|
for (guint i = 0; i < config->import_paths->len; i++) {
|
||||||
const char* import_directory_path = g_array_index(config->import_paths, char*, 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++) {
|
for (size_t i = 0; i < AST_get_child_count(root_module); i++) {
|
||||||
AST_NODE_PTR child = AST_get_node(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);
|
const char* path = get_absolute_import_path(target, child->value);
|
||||||
if (path == NULL) {
|
if (path == NULL) {
|
||||||
|
|
|
@ -81,6 +81,7 @@
|
||||||
"!" {DEBUG("\"%s\" tokenized with \'OpBitnot\'", yytext); return(OpBitnot);};
|
"!" {DEBUG("\"%s\" tokenized with \'OpBitnot\'", yytext); return(OpBitnot);};
|
||||||
"^" {DEBUG("\"%s\" tokenized with \'OpBitxor\'", yytext); return(OpBitxor);};
|
"^" {DEBUG("\"%s\" tokenized with \'OpBitxor\'", yytext); return(OpBitxor);};
|
||||||
"import" {DEBUG("\"%s\" tokenized with \'KeyImport\'", yytext); return(KeyImport);};
|
"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);};
|
"silent" {DEBUG("\"%s\" tokenized with \'KeySilent\'", yytext); return(KeySilent);};
|
||||||
"box" {DEBUG("\"%s\" tokenized with \'KeyBox\'", yytext); return(KeyBox);};
|
"box" {DEBUG("\"%s\" tokenized with \'KeyBox\'", yytext); return(KeyBox);};
|
||||||
"typeof" {DEBUG("\"%s\" tokenized with \'FunTypeof\'", yytext); return(FunTypeof);};
|
"typeof" {DEBUG("\"%s\" tokenized with \'FunTypeof\'", yytext); return(FunTypeof);};
|
||||||
|
|
|
@ -65,20 +65,16 @@ TargetLinkConfig* lld_create_link_config(__attribute__((unused)) const Target* t
|
||||||
g_array_append_val(config->object_file_names, target_object);
|
g_array_append_val(config->object_file_names, target_object);
|
||||||
INFO("resolved path of target object: %s", 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
|
// resolve absolute paths to dependent library object files
|
||||||
DEBUG("resolving target dependencies...");
|
DEBUG("resolving target dependencies...");
|
||||||
for (guint i = 0; i < module->imports->len; i++) {
|
for (guint i = 0; i < module->imports->len; i++) {
|
||||||
const char* dependency = g_array_index(module->imports, const char*, 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) {
|
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);
|
lld_delete_link_config(config);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2379,12 +2379,14 @@ Module *create_set(AST_NODE_PTR currentNode) {
|
||||||
GHashTable *functions = g_hash_table_new(g_str_hash, g_str_equal);
|
GHashTable *functions = g_hash_table_new(g_str_hash, g_str_equal);
|
||||||
GHashTable *variables = 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 *imports = g_array_new(FALSE, FALSE, sizeof(const char *));
|
||||||
|
GArray *includes = g_array_new(FALSE, FALSE, sizeof(const char *));
|
||||||
|
|
||||||
rootModule->boxes = boxes;
|
rootModule->boxes = boxes;
|
||||||
rootModule->types = types;
|
rootModule->types = types;
|
||||||
rootModule->functions = functions;
|
rootModule->functions = functions;
|
||||||
rootModule->variables = variables;
|
rootModule->variables = variables;
|
||||||
rootModule->imports = imports;
|
rootModule->imports = imports;
|
||||||
|
rootModule->includes = includes;
|
||||||
|
|
||||||
DEBUG("created Module struct");
|
DEBUG("created Module struct");
|
||||||
|
|
||||||
|
@ -2456,6 +2458,10 @@ Module *create_set(AST_NODE_PTR currentNode) {
|
||||||
DEBUG("create Import");
|
DEBUG("create Import");
|
||||||
g_array_append_val(imports, AST_get_node(currentNode, i)->value);
|
g_array_append_val(imports, AST_get_node(currentNode, i)->value);
|
||||||
break;
|
break;
|
||||||
|
case AST_Include:
|
||||||
|
DEBUG("create Include");
|
||||||
|
g_array_append_val(includes, AST_get_node(currentNode, i)->value);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
INFO("Provided source file could not be parsed because of semantic error.");
|
INFO("Provided source file could not be parsed because of semantic error.");
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -580,6 +580,7 @@ typedef struct Module_t {
|
||||||
GHashTable* variables;
|
GHashTable* variables;
|
||||||
// to be resolved after the module has been parsed completely
|
// to be resolved after the module has been parsed completely
|
||||||
GArray* imports;
|
GArray* imports;
|
||||||
|
GArray* includes;
|
||||||
} Module;
|
} Module;
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -53,6 +53,7 @@
|
||||||
%type <node_ptr> opbool
|
%type <node_ptr> opbool
|
||||||
%type <node_ptr> opbit
|
%type <node_ptr> opbit
|
||||||
%type <node_ptr> moduleimport
|
%type <node_ptr> moduleimport
|
||||||
|
%type <node_ptr> moduleinclude
|
||||||
%type <node_ptr> programbody
|
%type <node_ptr> programbody
|
||||||
%type <node_ptr> fundef
|
%type <node_ptr> fundef
|
||||||
%type <node_ptr> fundecl
|
%type <node_ptr> fundecl
|
||||||
|
@ -109,6 +110,7 @@
|
||||||
%token OpBitnot
|
%token OpBitnot
|
||||||
%token OpBitxor
|
%token OpBitxor
|
||||||
%token KeyImport
|
%token KeyImport
|
||||||
|
%token KeyInclude
|
||||||
%token KeySilent
|
%token KeySilent
|
||||||
%token KeyBox
|
%token KeyBox
|
||||||
%token FunTypeof
|
%token FunTypeof
|
||||||
|
@ -142,6 +144,7 @@ program: program programbody {AST_push_node(root, $2);
|
||||||
| programbody {AST_push_node(root, $1);};
|
| programbody {AST_push_node(root, $1);};
|
||||||
|
|
||||||
programbody: moduleimport {$$ = $1;}
|
programbody: moduleimport {$$ = $1;}
|
||||||
|
| moduleinclude {$$ = $1;}
|
||||||
| fundef{$$ = $1;}
|
| fundef{$$ = $1;}
|
||||||
| fundecl{$$ = $1;}
|
| fundecl{$$ = $1;}
|
||||||
| box{$$ = $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);
|
moduleimport: KeyImport ValStr {$$ = AST_new_node(new_loc(), AST_Import, $2);
|
||||||
DEBUG("Module-Import"); };
|
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);
|
statementlist: statementlist statement {AST_push_node($1, $2);
|
||||||
$$ = $1;}
|
$$ = $1;}
|
||||||
| statement {AST_NODE_PTR list = AST_new_node(new_loc(), AST_StmtList, NULL);
|
| statement {AST_NODE_PTR list = AST_new_node(new_loc(), AST_StmtList, NULL);
|
||||||
|
|
Loading…
Reference in New Issue