added a struct test case
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 5 Feb 2021 23:24:16 +0000 (17:24 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 5 Feb 2021 23:24:16 +0000 (17:24 -0600)
.gitignore
bin/onyx
onyx.exe
tests/new_struct_behaviour [new file with mode: 0644]
tests/new_struct_behaviour.onyx [new file with mode: 0644]

index 84259b1b9fabd85faac466a89ff03d7f7ac2505d..b0c39f3d718f19917ecee7e1543d0af7b6b9c068 100644 (file)
@@ -9,4 +9,5 @@ session.vim
 *.pdb
 *.ilk
 *.obj
-*.rdbg
\ No newline at end of file
+*.rdbg
+tmp/
\ No newline at end of file
index b25ec394d8bd4c6baba0f30d3b693053228e3817..41524eaf3be39766b3a2c5fa7fd9cea1469e5cc1 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index 09b90cbc43282c55b2662569d476589f7a03e7eb..f11e97262c190e805f492c4f6f7457634903ba72 100644 (file)
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 (file)
index 0000000..8606c50
--- /dev/null
@@ -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 (file)
index 0000000..9bb6887
--- /dev/null
@@ -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 := <PolyStruct(i32)>.{ 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");
+        }
+    }
+}