From: Brendan Hansen Date: Tue, 11 Aug 2020 15:12:39 +0000 (-0500) Subject: changed syntax for including files and folders X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=f215f19faf100a02503ce5e9947c0c94b59cfd46;p=onyx.git changed syntax for including files and folders --- diff --git a/core/alloc.onyx b/core/alloc.onyx index 7fe32477..40b7a475 100644 --- a/core/alloc.onyx +++ b/core/alloc.onyx @@ -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; diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index 41ffdc94..4168bc07 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -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; diff --git a/include/onyxparser.h b/include/onyxparser.h index 02e669af..f4e69dc5 100644 --- a/include/onyxparser.h +++ b/include/onyxparser.h @@ -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 7970dd77..59c6e8c9 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/alloc_test.onyx b/progs/alloc_test.onyx index c3298c3b..6126ea9b 100644 --- a/progs/alloc_test.onyx +++ b/progs/alloc_test.onyx @@ -1,5 +1,5 @@ -use "progs/alloc" -use "progs/print_funcs" +#include_file "alloc" +#include_file "printing" use package memory use package printing diff --git a/progs/stack_based.onyx b/progs/stack_based.onyx index ce416371..e9d52522 100644 --- a/progs/stack_based.onyx +++ b/progs/stack_based.onyx @@ -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 diff --git a/src/onyx.c b/src/onyx.c index 6e663a7c..9c969e11 100644 --- a/src/onyx.c +++ b/src/onyx.c @@ -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; diff --git a/src/onyxparser.c b/src/onyxparser.c index 6cab7515..ff1e915a 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -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, diff --git a/src/onyxutils.c b/src/onyxutils.c index 9bb20b38..2cc65668 100644 --- a/src/onyxutils.c +++ b/src/onyxutils.c @@ -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",