From: Brendan Hansen Date: Wed, 30 Dec 2020 13:47:36 +0000 (-0600) Subject: added more to struct_robustness; parsing bug fix X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=8048c085282117977dd74b514c8c4799da407dbc;p=onyx.git added more to struct_robustness; parsing bug fix --- diff --git a/onyx b/onyx index fcbe01d9..08c9d5ca 100755 Binary files a/onyx and b/onyx differ diff --git a/src/onyxparser.c b/src/onyxparser.c index 1a5d6b1a..d7633124 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -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; diff --git a/tests/struct_robustness.onyx b/tests/struct_robustness.onyx index 503d9b3e..2ba09551 100644 --- a/tests/struct_robustness.onyx +++ b/tests/struct_robustness.onyx @@ -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(%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(%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(%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(%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(%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(%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