From: Brendan Hansen Date: Tue, 9 Feb 2021 03:05:56 +0000 (-0600) Subject: added ability to cause static errors with '#error' X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=08b6f9c34274625a8c65c37b7fa45d7773c5daf8;p=onyx.git added ability to cause static errors with '#error' --- diff --git a/bin/onyx b/bin/onyx index d9c520d4..44334388 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/docs/compile_time_vars b/docs/compile_time_vars index a81e31fb..1427edfd 100644 --- a/docs/compile_time_vars +++ b/docs/compile_time_vars @@ -1,6 +1,6 @@ With the recent addition of compile time if statements, there should be a set of variables that give information about the current compilation. These would include things such as: - - Chosen backend (JS, WASI, others in the future) + - Chosen runtime (JS, WASI, others in the future) - 32-Bit pointers are enabled The standard library will be rewritten to use these variables to conditionally include file diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index 33b47dd4..0df44681 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -29,6 +29,7 @@ typedef struct AstCompound AstCompound; typedef struct AstDirectiveSolidify AstDirectiveSolidify; typedef struct AstStaticIf AstStaticIf; +typedef struct AstDirectiveError AstDirectiveError; typedef struct AstReturn AstReturn; typedef struct AstJump AstJump; @@ -167,6 +168,7 @@ typedef enum AstKind { Ast_Kind_Directive_Solidify, Ast_Kind_Static_If, + Ast_Kind_Directive_Error, Ast_Kind_Count } AstKind; @@ -869,7 +871,11 @@ struct AstStaticIf { bh_arr(struct Entity *) false_entities; }; +struct AstDirectiveError { + AstNode_base; + OnyxToken* error_msg; +}; extern AstNode empty_node; @@ -897,6 +903,7 @@ extern const char* entity_state_strings[Entity_State_Count]; typedef enum EntityType { Entity_Type_Unknown, + Entity_Type_Error, Entity_Type_Load_Path, Entity_Type_Load_File, Entity_Type_Binding, @@ -936,6 +943,7 @@ typedef struct Entity { Scope *scope; union { + AstDirectiveError *error; AstInclude *include; AstUsePackage *use_package; AstBinding *binding; diff --git a/onyx.exe b/onyx.exe index aa1006f0..febaea9b 100644 Binary files a/onyx.exe and b/onyx.exe differ diff --git a/src/onyx.c b/src/onyx.c index 1e2f58d9..4fc4d438 100644 --- a/src/onyx.c +++ b/src/onyx.c @@ -298,6 +298,14 @@ static b32 process_entity(Entity* ent) { EntityState before_state = ent->state; switch (before_state) { + case Entity_State_Error: + if (ent->type != Entity_Type_Error) { + onyx_report_error(ent->expr->token->pos, "Error entity unexpected. This is definitely a compiler bug"); + } else { + onyx_report_error(ent->error->token->pos, "Static error occured: '%b'", ent->error->error_msg->text, ent->error->error_msg->length); + } + break; + case Entity_State_Parse_Builtin: process_load_entity(ent); ent->state = Entity_State_Finalized; diff --git a/src/onyxastnodes.c b/src/onyxastnodes.c index d7a700c1..b2f2596b 100644 --- a/src/onyxastnodes.c +++ b/src/onyxastnodes.c @@ -83,6 +83,7 @@ static const char* ast_node_names[] = { "SOLIDIFY", "STATIC IF", + "STATIC ERROR", "AST_NODE_KIND_COUNT", }; diff --git a/src/onyxentities.c b/src/onyxentities.c index ed849d5b..b965d57e 100644 --- a/src/onyxentities.c +++ b/src/onyxentities.c @@ -291,6 +291,14 @@ void add_entities_for_node(bh_arr(Entity *) *target_arr, AstNode* node, Scope* s ENTITY_INSERT(ent); break; } + + case Ast_Kind_Directive_Error: { + ent.state = Entity_State_Error; + ent.type = Entity_Type_Error; + ent.error = (AstDirectiveError *) node; + ENTITY_INSERT(ent); + break; + } default: { ent.type = Entity_Type_Expression; diff --git a/src/onyxparser.c b/src/onyxparser.c index 7f41b140..da698741 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -2113,7 +2113,6 @@ static AstStaticIf* parse_static_if_stmt(OnyxParser* parser) { static_if_node->cond = parse_expression(parser, 0); - // TODO: Add else statements to static ifs bh_arr_new(global_heap_allocator, static_if_node->true_entities, 2); bh_arr_push(parser->alternate_entity_placement_stack, &static_if_node->true_entities); @@ -2307,6 +2306,14 @@ static void parse_top_level_statement(OnyxParser* parser) { ENTITY_SUBMIT(include); return; } + else if (parse_possible_directive(parser, "error")) { + AstDirectiveError *error = make_node(AstDirectiveError, Ast_Kind_Directive_Error); + error->token = dir_token; + error->error_msg = expect_token(parser, Token_Type_Literal_String); + + ENTITY_SUBMIT(error); + return; + } else { OnyxToken* directive_token = expect_token(parser, '#'); OnyxToken* symbol_token = expect_token(parser, Token_Type_Symbol);