From: Brendan Hansen Date: Fri, 5 Feb 2021 23:24:16 +0000 (-0600) Subject: added a struct test case X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=72c5ec0d17722a1f8cf6ceef7b8f3f7369f6bb32;p=onyx.git added a struct test case --- diff --git a/.gitignore b/.gitignore index 84259b1b..b0c39f3d 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ session.vim *.pdb *.ilk *.obj -*.rdbg \ No newline at end of file +*.rdbg +tmp/ \ No newline at end of file diff --git a/bin/onyx b/bin/onyx index b25ec394..41524eaf 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/onyx.exe b/onyx.exe index 09b90cbc..f11e9726 100644 Binary files a/onyx.exe and b/onyx.exe differ diff --git a/tests/new_struct_behaviour b/tests/new_struct_behaviour new file mode 100644 index 00000000..8606c502 --- /dev/null +++ b/tests/new_struct_behaviour @@ -0,0 +1,12 @@ +Got a value! +Got a value! +1237 +1235 +11 +219 +Joe is 30. +Function 3 +Function 2 +Function 1 +Function 4 +V3(4.0000, 9.0000, 16.0000) diff --git a/tests/new_struct_behaviour.onyx b/tests/new_struct_behaviour.onyx new file mode 100644 index 00000000..9bb6887d --- /dev/null +++ b/tests/new_struct_behaviour.onyx @@ -0,0 +1,97 @@ +// This is a needlessly complicated test of some of the newer features with structs. + +#load "core/std/js" + +use package core + + +Vec3 :: struct { + x, y, z: f32; +} + +get_a_value :: ($T: type_expr) -> T { + println("Got a value!"); + return cast(T) 1234; +} + +Foo :: struct { + member1 := get_a_value(f32); + member2 := get_a_value(i32); + + get_member2_plus_three :: (foo: ^Foo) -> i32 { + return foo.member2 + 3; + } + + some_constant :: cast(i32) (10 + 209); +} + + +Foo2 :: struct { + member1: i32; + member2: f32; + + SubStruct :: struct { + name : str; + age : i32; + } + + get_member2_plus_three :: (foo: ^$T) -> f32 { + return foo.member2 + 3; + } + + some_constant :: cast(i32) (10 + 40 * 20); +} + +PolyStruct :: struct (T: type_expr) { + member : T; + works : (T) -> T = member_function; + + // NOTE(Brendan Hansen): Currently, these functions are just scoped to the + // struct body, and they do not have access to the struct parameters. + member_function :: (t: $T) -> T { + return t + 1; + } +} + +main :: (args: [] cstr) { + foo := Foo.{}; + println(foo->get_member2_plus_three()); + + ps := .{ 1234 }; + println(ps.works(ps.member)); + + plus_one : (i32) -> i32 = PolyStruct.member_function; + println(plus_one(10)); + + println(foo.some_constant); + + person := Foo2.SubStruct.{ name = "Joe", age = 30 }; + printf("%s is %i.\n", person.name, person.age); + + { + BasicallyANamespace.function_3(); + + use BasicallyANamespace; + function_2(); + + use SubNamespace; + function_4(); + } + + + v := Vec3.{ 4, 9, 16 }; + printf("V3(%f, %f, %f)\n", v.x, v.y, v.z); +} + +BasicallyANamespace :: struct { + function_1 :: () { println("Function 1"); } + function_2 :: () { println("Function 2"); } + function_3 :: () { println("Function 3"); } + + SubNamespace :: struct { + function_4 :: () { + function_1(); + println("Function 4"); + } + } +}