added ability to cause static errors with '#error'
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 9 Feb 2021 03:05:56 +0000 (21:05 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 9 Feb 2021 03:05:56 +0000 (21:05 -0600)
bin/onyx
docs/compile_time_vars
include/onyxastnodes.h
onyx.exe
src/onyx.c
src/onyxastnodes.c
src/onyxentities.c
src/onyxparser.c

index d9c520d451ace33fe6c14d93a1a30ef192d313a9..4433438845d5a123128d0b2596910b9dfdfdc8be 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index a81e31fb1458d37d0942f70aae8b8d2f8b6e50f8..1427edfdaabbcc83fe4a0615f687c4aae03034a7 100644 (file)
@@ -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
index 33b47dd4fd634fe41349fd5343441629ab144523..0df44681bb58405f1ef891da9252374fd099710f 100644 (file)
@@ -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;
index aa1006f0ef2f547be4e747de0a2e0ce98709a0f9..febaea9b56054d6ed071be86666d79d4a89bf194 100644 (file)
Binary files a/onyx.exe and b/onyx.exe differ
index 1e2f58d9b6d04a9e8d45f0718505a726a0bd0003..4fc4d438250223a7f6295f2b87187f8f706a4713 100644 (file)
@@ -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;
index d7a700c142ba167608f39447de666138a68afde8..b2f2596b9ed05eab5600d6c133374eba19b991ac 100644 (file)
@@ -83,6 +83,7 @@ static const char* ast_node_names[] = {
 
     "SOLIDIFY",
     "STATIC IF",
+    "STATIC ERROR",
 
     "AST_NODE_KIND_COUNT",
 };
index ed849d5b509283e72d1718e2297e8cbc4dd21588..b965d57ec66352cd025436635d75ba565a590984 100644 (file)
@@ -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;
index 7f41b1400f9ebd94bc38d9cec791f4dcf11ed912..da6987415406011681abba3810706bf65ef4416f 100644 (file)
@@ -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);