added more to struct_robustness; parsing bug fix
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 30 Dec 2020 13:47:36 +0000 (07:47 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 30 Dec 2020 13:47:36 +0000 (07:47 -0600)
onyx
src/onyxparser.c
tests/struct_robustness.onyx

diff --git a/onyx b/onyx
index fcbe01d9c10bb8b68535d5d7938d19f77ace54be..08c9d5cac8971de518df7f8e7d0006c0fb02ba3c 100755 (executable)
Binary files a/onyx and b/onyx differ
index 1a5d6b1a00882e168c8cc2a9bea53d2afc7170e5..d7633124184322b0b2252951b68520d9ea460e63 100644 (file)
@@ -1022,6 +1022,8 @@ static i32 parse_possible_symbol_declaration(OnyxParser* parser, AstNode** ret)
         assert(current_block->binding_scope != NULL);
 
         AstBinding* binding = parse_top_level_binding(parser, symbol);
+        if (parser->hit_unexpected_token) return 2;
+
         symbol_introduce(current_block->binding_scope, symbol, binding->node);
         return 2;
     }
@@ -2076,7 +2078,8 @@ static AstNode* parse_top_level_statement(OnyxParser* parser) {
 
             if (parser->curr->type == ':') {
                 AstBinding* binding = parse_top_level_binding(parser, symbol);
-                binding->node->flags |= private_kind;
+
+                if (binding != NULL) binding->node->flags |= private_kind;
 
                 return (AstNode *) binding;
 
index 503d9b3e9b003c28284d8851770844c88a7db598..2ba09551aec4278fa3adf04ebd04d6f5384529b1 100644 (file)
@@ -2,134 +2,193 @@ use package core
 
 main :: proc (args: [] cstr) {
 
-       test_simple_struct();
-       test_simple_union();
-       test_default_values();
-       test_simple_use();
-       test_polymorphic();
-       test_polymorphic_with_defaults();
+    test_simple_struct();
+    test_simple_union();
+    test_default_values();
+    test_simple_use();
+    test_polymorphic();
+    test_polymorphic_with_defaults();
+    test_union_with_use();
+    test_polymorphic_union();
+    test_polymorphic_union_with_use();
+
+    test_simple_struct :: proc () {
+        println("Testing a simple structure.");
+        
+        SimpleStruct :: struct {
+            age    : u16;
+            height : u32;
+
+            name : str;
+        }
+
+        ss := SimpleStruct.{ 41, 67, "Steve" };
 
-       test_simple_struct :: proc () {
-               println("Testing a simple structure.");
-               
-               SimpleStruct :: struct {
-                       age    : u16;
-                       height : u32;
+        printf("SimpleStruct<%i, %i>(%i, %i, %s)\n",
+            sizeof SimpleStruct,
+            alignof SimpleStruct,
+            cast(u32) ss.age, ss.height, ss.name);
+    }
 
-                       name : str;
-               }
+    test_simple_union :: proc () {
+        println("\n\nTesting a simple union.");
 
-               ss := SimpleStruct.{ 41, 67, "Steve" };
+        SimpleUnion :: struct #union {
+            int_val   : i32;
+            float_val : f32;
+        }
 
-               printf("SimpleStruct<%i, %i>(%i, %i, %s)\n",
-                       sizeof SimpleStruct,
-                       alignof SimpleStruct,
-                       cast(u32) ss.age, ss.height, ss.name);
-       }
+        u : SimpleUnion;
+        u.float_val = 0.5;
 
-       test_simple_union :: proc () {
-               println("\n\nTesting a simple union.");
+        printf("%p == 0x3F000000\n", u.int_val);
+    }
 
-               SimpleUnion :: struct #union {
-                       int_val   : i32;
-                       float_val : f32;
-               }
+    test_default_values :: proc () {
+        println("\n\nTesting a struct with default values.");
 
-               u : SimpleUnion;
-               u.float_val = 0.5;
+        DefaultedStruct :: struct {
+            i : i32 = 0;
+            f : f32 = 1;
+            l : i64 = 2;
+            d : f64 = 3;
+        }
+
+        ds1 := DefaultedStruct.{};
+        print_defaulted(ds1);
 
-               printf("%p == 0x3F000000\n", u.int_val);
-       }
+        ds2 := DefaultedStruct.{ i = 3, d = 0 };
+        print_defaulted(ds2);
 
-       test_default_values :: proc () {
-               println("\n\nTesting a struct with default values.");
+        print_defaulted :: proc (use ds: DefaultedStruct) {
+            printf("DefaultedStruct(%i, %f, %l, %d)\n", i, f, l, d);
+        }
+    }
 
-               DefaultedStruct :: struct {
-                       i : i32 = 0;
-                       f : f32 = 1;
-                       l : i64 = 2;
-                       d : f64 = 3;
-               }
+    test_simple_use :: proc () {
+        println("\n\nTesting a struct with `use`.");
+
+        StructWithUse :: struct {
+            first_member : i32;
+
+            use used_member : UsedMember;
+
+            last_member : i32;
+        }
+
+        UsedMember :: struct {
+            x : f32;
+            y : f32;    
+        }
+
+        swu := StructWithUse.{ 1234, UsedMember.{ 1, 2 }, 5678 };
+
+        swu2 := StructWithUse.{
+            first_member = 1234,
+            used_member  = UsedMember.{ 1, 2 },
+            last_member  = 5678,
+        };
+
+        // This will not work. `use`d members cannot be set directly.
+        // swu3 := StructWithUse.{ 1234, 1, 2, 5678 };
+
+        print_swu :: proc (use swu: StructWithUse) {
+            printf("StructWithUse(%i, (%f, %f), %i)\n",
+                first_member, x, y, last_member);
+        }
+
+        print_swu(swu);
+        print_swu(swu2);
+    }
+
+    test_polymorphic :: proc () {
+        println("\n\nTesting a polymorphic struct.");
+
+        PolyStruct :: struct ($T, $R) {
+            t_data : T;
+            r_data : R;
+        }
 
-               ds1 := DefaultedStruct.{};
-               print_defaulted(ds1);
+        ps1 : PolyStruct(i32, f32);
+        ps1.t_data = 1234;
+        ps1.r_data = 5678;
 
-               ds2 := DefaultedStruct.{ i = 3, d = 0 };
-               print_defaulted(ds2);
+        printf("PolyStruct<i32, f32>(%i, %f)\n", ps1.t_data, ps1.r_data);
 
-               print_defaulted :: proc (use ds: DefaultedStruct) {
-                       printf("DefaultedStruct(%i, %f, %l, %d)\n", i, f, l, d);
-               }
-       }
+        // Currently, this is how you have to do this.
+        ps2 := (#type PolyStruct(f32, i32)).{ 1234, 5678 };
+        printf("PolyStruct<f32, i32>(%f, %i)\n", ps2.t_data, ps2.r_data);
+    }
 
-       test_simple_use :: proc () {
-               println("\n\nTesting a struct with `use`.");
+    test_polymorphic_with_defaults :: proc () {
+        println("\n\nTesting a polymorphic struct with default values.");
 
-               StructWithUse :: struct {
-                       first_member : i32;
+        PolyStruct :: struct ($T, $R) {
+            t_data : T = 1234;
+            r_data : R = 5678;
+        }
 
-                       use used_member : UsedMember;
+        PolyStructTyped :: #type PolyStruct(i32, f32);
 
-                       last_member : i32;
-               }
+        ps := PolyStructTyped.{};
+        printf("PolyStruct<i32, f32>(%i, %f)\n", ps.t_data, ps.r_data);
+    }
 
-               UsedMember :: struct {
-                       x : f32;
-                       y : f32;        
-               }
+    test_polymorphic_union_with_use :: proc () {
+        println("\n\nTesting a polymorphic union with use.");
 
-               swu := StructWithUse.{ 1234, UsedMember.{ 1, 2 }, 5678 };
+        PolyStruct :: struct ($T, $R) {
+            use container : struct #union {
+                t_data : T;
+                r_data : R;
+            };
+        }
 
-               swu2 := StructWithUse.{
-                       first_member = 1234,
-                       used_member  = UsedMember.{ 1, 2 },
-                       last_member  = 5678,
-               };
+        printf("%i == 8\n", sizeof PolyStruct(i32, [] u32));
 
-               // This will not work. `use`d members cannot be set directly.
-               swu3 := StructWithUse.{ 1234, 1, 2, 5678 };
+        ps : PolyStruct(i32, f64);
+        ps.t_data = 1234;
+        ps.r_data = 5678;
+        printf("%i, %d\n", ps.t_data, ps.r_data);
+    }
 
-               print_swu :: proc (use swu: StructWithUse) {
-                       printf("StructWithUse(%i, (%f, %f), %i)\n",
-                               first_member, x, y, last_member);
-               }
+    test_union_with_use :: proc () {
+        println("\n\nTesting a union with use.");
 
-               print_swu(swu);
-               print_swu(swu2);
-       }
+        Vec2 :: struct {
+            x : i32;
+            y : i32;
+        }
 
-       test_polymorphic :: proc () {
-               println("\n\nTesting a polymorphic struct.");
+        Union :: struct #union {
+            use container : Vec2;
+            name          : str;
+        }
 
-               PolyStruct :: struct ($T, $R) {
-                       t_data : T;
-                       r_data : R;
-               }
+        u : Union;
+        u.x = 10;
+        u.y = 20;
+        printf("Union(%i, %i)\n", u.x, u.y);
 
-               ps1 : PolyStruct(i32, f32);
-               ps1.t_data = 1234;
-               ps1.r_data = 5678;
-
-               printf("PolyStruct<i32, f32>(%i, %f)\n", ps1.t_data, ps1.r_data);
-
-               // Currently, this is how you have to do this.
-               ps2 := (#type PolyStruct(f32, i32)).{ 1234, 5678 };
-               printf("PolyStruct<f32, i32>(%f, %i)\n", ps2.t_data, ps2.r_data);
-       }
-
-       test_polymorphic_with_defaults :: proc () {
-               println("\n\nTesting a polymorphic struct with default values.");
-
-               PolyStruct :: struct ($T, $R) {
-                       t_data : T = 1234;
-                       r_data : R = 5678;
-               }
-
-               PolyStructTyped :: #type PolyStruct(i32, f32);
-
-               ps := PolyStructTyped.{};
-               printf("PolyStruct<i32, f32>(%i, %f)\n", ps.t_data, ps.r_data);
-       }
+        u.name = "Joe";
+        printf("Union(%s)\n", u.name);
+    }
+
+    test_polymorphic_union :: proc () {
+        println("\n\nTesting a polymorphic union.");
+
+        PolyUnion :: struct ($T, $R) #union {
+            t_data : T;
+            r_data : R;
+        }
+
+        pu : PolyUnion(str, i32);
+        pu.t_data = "Hello World.";
+        printf("PolyUnion(%s)\n", pu.t_data);
+
+        pu.r_data = 0x4D2;
+        printf("PolyUnion(%i)\n", pu.r_data);
+    }
 }
 
 #include_file "core/std/js"
\ No newline at end of file