changed syntax for including files and folders
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 11 Aug 2020 15:12:39 +0000 (10:12 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 11 Aug 2020 15:12:39 +0000 (10:12 -0500)
core/alloc.onyx
include/onyxastnodes.h
include/onyxparser.h
onyx
progs/alloc_test.onyx
progs/stack_based.onyx
src/onyx.c
src/onyxparser.c
src/onyxutils.c

index 7fe324779f640ce70656101f4fbb7f7227e23af0..40b7a4758e504f9fa3305be74e10534e0df74d4f 100644 (file)
@@ -1,11 +1,8 @@
 package memory
 
-use "intrinsics"
+#include_file "intrinsics"
 
-use package printing { print }
-use package intrinsics {
-    memory_size, memory_grow
-}
+use package intrinsics { memory_size, memory_grow }
 
 // Need to define this somewhere
 null :: cast(rawptr) 0;
index 41ffdc94372ccfbbea3fb997ac44f7b61b322193..4168bc07182fbcf9cdd55d327e50ed570e954876 100644 (file)
@@ -47,7 +47,7 @@ typedef struct AstTypeAlias AstTypeAlias;
 
 typedef struct AstBinding AstBinding;
 typedef struct AstMemRes AstMemRes;
-typedef struct AstIncludeFile AstIncludeFile;
+typedef struct AstInclude AstInclude;
 typedef struct AstUsePackage AstUsePackage;
 typedef struct AstAlias AstAlias;
 typedef struct AstGlobal AstGlobal;
@@ -70,6 +70,7 @@ typedef enum AstKind {
     Ast_Kind_Program,
     Ast_Kind_Package,
     Ast_Kind_Include_File,
+    Ast_Kind_Include_Folder,
     Ast_Kind_Use_Package,
     Ast_Kind_Alias,
     Ast_Kind_Memres,
@@ -351,7 +352,7 @@ struct AstTypeAlias { AstType_base; AstType* to; };
 // Top level nodes
 struct AstBinding       { AstTyped_base; AstNode* node; };
 struct AstMemRes        { AstTyped_base; u64 addr; AstTyped *initial_value; }; // HACK: This has to be the same size or bigger as AstDereference
-struct AstIncludeFile   { AstNode_base; OnyxToken *filename; };
+struct AstInclude       { AstNode_base; OnyxToken *name; };
 struct AstUsePackage    {
     AstNode_base;
 
index 02e669af8a09cc1f4fed95af7539f968699f070c..f4e69dc5d633b7af4afc183dab1c71e783a41f7c 100644 (file)
@@ -17,7 +17,7 @@ typedef struct ParseResults {
     // NOTE: The allocator used to make the arrays below
     bh_allocator allocator;
 
-    bh_arr(AstIncludeFile *) files;
+    bh_arr(AstInclude *) includes;
 
     // NOTE: Contains all the nodes that will need some processing (symbol resolution, type checking)
     bh_arr(NodeToProcess) nodes_to_process;
diff --git a/onyx b/onyx
index 7970dd77d1f14611472c14a3e7c52b43baa2f3ad..59c6e8c9f3bea8cdbd2bc64b74687ee0a5412fe5 100755 (executable)
Binary files a/onyx and b/onyx differ
index c3298c3b9c34be2ff9e3f31a5b0b897027f485ef..6126ea9b6f750ae7ee7379f61e85592e62f47ce6 100644 (file)
@@ -1,5 +1,5 @@
-use "progs/alloc"
-use "progs/print_funcs"
+#include_file "alloc"
+#include_file "printing"
 
 use package memory
 use package printing
index ce416371efc8f9f3888a1ae71dcded9fcb91e366..e9d52522e8b06ed506e2813461e04df58c453257 100644 (file)
@@ -1,7 +1,9 @@
 package main
 
-use "printing"
-use "alloc"
+#include_folder "./core"
+
+#include_file "printing"
+#include_file "alloc"
 
 use package printing
 use package memory
index 6e663a7c4d436a431e8cda589ddfa1b03999ad4d..9c969e114d596caf6756be1f99170308c8de70e5 100644 (file)
@@ -90,6 +90,7 @@ static OnyxCompileOptions compile_opts_parse(bh_allocator alloc, int argc, char
 
 static void compile_opts_free(OnyxCompileOptions* opts) {
     bh_arr_free(opts->files);
+    bh_arr_free(opts->included_folders);
 }
 
 
@@ -159,9 +160,10 @@ static void compiler_state_free(CompilerState* cs) {
 
 static char* lookup_included_file(CompilerState* cs, OnyxToken* filename) {
     static char path[256];
-    fori (i, 0, 511) path[i] = 0;
+    fori (i, 0, 255) path[i] = 0;
 
     static char fn[128];
+    fori (i, 0, 127) fn[i] = 0;
     token_toggle_end(filename);
     if (!bh_str_ends_with(filename->text, ".onyx")) {
         bh_snprintf(fn, 128, "%s.onyx", filename->text);
@@ -202,11 +204,16 @@ static i32 sort_entities(const void* e1, const void* e2) {
 }
 
 static void merge_parse_results(CompilerState* compiler_state, ParseResults* results) {
-    bh_arr_each(AstIncludeFile *, include, results->files) {
-        char* filename = lookup_included_file(compiler_state, (*include)->filename);
-        char* formatted_name = bh_strdup(global_heap_allocator, filename);
-
-        bh_arr_push(compiler_state->queued_files, formatted_name);
+    bh_arr_each(AstInclude *, include, results->includes) {
+        if ((*include)->kind == Ast_Kind_Include_File) {
+            char* filename = lookup_included_file(compiler_state, (*include)->name);
+            char* formatted_name = bh_strdup(global_heap_allocator, filename);
+
+            bh_arr_push(compiler_state->queued_files, formatted_name);
+        } else if ((*include)->kind == Ast_Kind_Include_Folder) {
+            const char* folder = bh_aprintf(global_heap_allocator, "%b", (*include)->name->text, (*include)->name->length);
+            bh_arr_push(compiler_state->options->included_folders, folder);
+        }
     }
 
     Entity ent;
index 6cab751545c3565f2cdf3f356e5c41675577ea98..ff1e915ad7d61e689fe3356c7ffa111b37d84106 100644 (file)
@@ -1338,62 +1338,53 @@ static AstNode* parse_top_level_statement(OnyxParser* parser) {
         is_private = 1;
     }
 
-    switch (parser->curr->type) {
+    switch ((u16) parser->curr->type) {
         case Token_Type_Keyword_Use: {
             OnyxToken* use_token = expect_token(parser, Token_Type_Keyword_Use);
 
-            if (parser->curr->type == Token_Type_Keyword_Package) {
-                consume_token(parser);
-
-                AstUsePackage* upack = make_node(AstUsePackage, Ast_Kind_Use_Package);
-                upack->token = use_token;
+            expect_token(parser, Token_Type_Keyword_Package);
 
-                AstNode* pack_symbol = make_node(AstNode, Ast_Kind_Symbol);
-                pack_symbol->token = expect_token(parser, Token_Type_Symbol);
-                upack->package = (AstPackage *) pack_symbol;
+            AstUsePackage* upack = make_node(AstUsePackage, Ast_Kind_Use_Package);
+            upack->token = use_token;
 
-                if (parser->curr->type == Token_Type_Keyword_As) {
-                    consume_token(parser);
-                    upack->alias = expect_token(parser, Token_Type_Symbol);
-                }
+            AstNode* pack_symbol = make_node(AstNode, Ast_Kind_Symbol);
+            pack_symbol->token = expect_token(parser, Token_Type_Symbol);
+            upack->package = (AstPackage *) pack_symbol;
 
-                if (parser->curr->type == '{') {
-                    consume_token(parser);
-
-                    bh_arr_new(global_heap_allocator, upack->only, 4);
+            if (parser->curr->type == Token_Type_Keyword_As) {
+                consume_token(parser);
+                upack->alias = expect_token(parser, Token_Type_Symbol);
+            }
 
-                    while (parser->curr->type != '}') {
-                        if (parser->hit_unexpected_token) return NULL;
+            if (parser->curr->type == '{') {
+                consume_token(parser);
 
-                        AstAlias* alias = make_node(AstAlias, Ast_Kind_Alias);
-                        alias->token = expect_token(parser, Token_Type_Symbol);
+                bh_arr_new(global_heap_allocator, upack->only, 4);
 
-                        if (parser->curr->type == Token_Type_Keyword_As) {
-                            consume_token(parser);
-                            alias->alias = expect_token(parser, Token_Type_Symbol);
-                        } else {
-                            alias->alias = alias->token;
-                        }
+                while (parser->curr->type != '}') {
+                    if (parser->hit_unexpected_token) return NULL;
 
-                        bh_arr_push(upack->only, alias);
+                    AstAlias* alias = make_node(AstAlias, Ast_Kind_Alias);
+                    alias->token = expect_token(parser, Token_Type_Symbol);
 
-                        if (parser->curr->type != '}')
-                            expect_token(parser, ',');
+                    if (parser->curr->type == Token_Type_Keyword_As) {
+                        consume_token(parser);
+                        alias->alias = expect_token(parser, Token_Type_Symbol);
+                    } else {
+                        alias->alias = alias->token;
                     }
 
-                    consume_token(parser);
-                }
+                    bh_arr_push(upack->only, alias);
 
-                add_node_to_process(parser, (AstNode *) upack);
-                return NULL;
-
-            } else {
-                AstIncludeFile* include = make_node(AstIncludeFile, Ast_Kind_Include_File);
-                include->token = use_token;
-                include->filename = expect_token(parser, Token_Type_Literal_String);
+                    if (parser->curr->type != '}')
+                        expect_token(parser, ',');
+                }
 
-                return (AstNode *) include;
+                consume_token(parser);
             }
+
+            add_node_to_process(parser, (AstNode *) upack);
+            return NULL;
         }
 
         case Token_Type_Keyword_Proc:
@@ -1477,6 +1468,32 @@ static AstNode* parse_top_level_statement(OnyxParser* parser) {
             }
         }
 
+        case '#': {
+            while (parser->curr->type == '#') {
+                OnyxToken* dir_token = parser->curr;
+
+                if (parse_possible_directive(parser, "include_file")) {
+                    AstInclude* include = make_node(AstInclude, Ast_Kind_Include_File);
+                    include->token = dir_token;
+                    include->name = expect_token(parser, Token_Type_Literal_String);
+
+                    return (AstNode *) include;
+                }
+                else if (parse_possible_directive(parser, "include_folder")) {
+                    AstInclude* include = make_node(AstInclude, Ast_Kind_Include_Folder);
+                    include->token = dir_token;
+                    include->name = expect_token(parser, Token_Type_Literal_String);
+
+                    return (AstNode *) include;
+                }
+                else {
+                    onyx_message_add(Msg_Type_Unknown_Directive,
+                            dir_token->pos, dir_token->text, dir_token->length);
+                    return NULL;
+                }
+            }
+        }
+
         default: break;
     }
 
@@ -1511,12 +1528,12 @@ OnyxParser onyx_parser_create(bh_allocator alloc, OnyxTokenizer *tokenizer, Prog
     parser.results = (ParseResults) {
         .allocator = global_heap_allocator,
 
-        .files = NULL,
+        .includes = NULL,
         .nodes_to_process = NULL,
 
     };
 
-    bh_arr_new(parser.results.allocator, parser.results.files, 4);
+    bh_arr_new(parser.results.allocator, parser.results.includes, 4);
     bh_arr_new(parser.results.allocator, parser.results.nodes_to_process, 4);
 
     return parser;
@@ -1560,7 +1577,10 @@ ParseResults onyx_parse(OnyxParser *parser) {
             while (curr_stmt != NULL) {
 
                 switch (curr_stmt->kind) {
-                    case Ast_Kind_Include_File: bh_arr_push(parser->results.files, (AstIncludeFile *) curr_stmt); break;
+                    case Ast_Kind_Include_File:
+                    case Ast_Kind_Include_Folder:
+                        bh_arr_push(parser->results.includes, (AstInclude *) curr_stmt);
+                        break;
                     case Ast_Kind_Binding: {
                         if (((AstBinding *) curr_stmt)->node->flags & Ast_Flag_Private_Package) {
                             symbol_introduce(parser->package->private_scope,
index 9bb20b3849d7a87e68a330c4a1bba057d025f281..2cc656686ea24b85dc5a02596755b9e4d9880cff 100644 (file)
@@ -12,10 +12,13 @@ bh_allocator global_heap_allocator;
 
 static const char* ast_node_names[] = {
     "ERROR",
-    "PACKAGE",
     "PROGRAM",
-    "USE",
+    "PACKAGE",
+    "INCLUDE FILE",
+    "INCLUDE FOLDER",
+    "USE PACKAGE",
     "ALIAS",
+    "MEMORY RESERVATION"
 
     "BINDING",
     "FUNCTION",