added compile time procedures test case
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 31 Dec 2020 17:41:58 +0000 (11:41 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 31 Dec 2020 17:41:58 +0000 (11:41 -0600)
tests/compile_time_procedures [new file with mode: 0644]
tests/compile_time_procedures.onyx [new file with mode: 0644]

diff --git a/tests/compile_time_procedures b/tests/compile_time_procedures
new file mode 100644 (file)
index 0000000..ad386a9
--- /dev/null
@@ -0,0 +1,5 @@
+hello world!
+24
+4
+20
+true
diff --git a/tests/compile_time_procedures.onyx b/tests/compile_time_procedures.onyx
new file mode 100644 (file)
index 0000000..4ecdb21
--- /dev/null
@@ -0,0 +1,56 @@
+#include_file "core/std/js"
+
+use package core
+
+Test_VTable :: struct {
+    first  : proc (i32) -> i32;
+    second : proc (i32) -> i32;
+
+    third  : proc (i32, i32) -> i32 = null_proc;
+}
+
+test_vtable1 := Test_VTable.{
+    first  = proc (a: i32) -> i32 { return a * 2; },
+    second = proc (a: i32) -> i32 { return a + 2; },
+}
+
+test_vtable2 := Test_VTable.{
+    first  = proc (a: i32) -> i32 { return a / 2; },
+    second = proc (a: i32) -> i32 { return a - 2; },
+}
+
+
+Test_Thing :: struct {
+    vt : ^Test_VTable;
+
+    data : i32;
+}
+
+// because of the work i did above, this also works now.
+dummy := proc () { println("hello world!"); }
+
+
+main :: proc (args: [] cstr) {
+    dummy();
+
+    t := Test_Thing.{ ^test_vtable1, 10 };
+    println(do_a_thing(^t));
+
+    t.vt = ^test_vtable2;
+    println(do_a_thing(^t));
+
+    tmp_vt := Test_VTable.{ first = test_vtable1.second, second = test_vtable2.first, third = math.max };
+    t.vt = ^tmp_vt;
+    println(do_a_thing(^t));
+
+    println(t.vt.first == test_vtable1.second);
+
+    do_a_thing :: proc (use t: ^Test_Thing) -> i32 {
+        if vt.second == null_proc || vt.first == null_proc do return data;
+
+        res := data |> vt.second() |> vt.first();
+        if vt.third != null_proc do res = vt.third(res, 20);
+
+        return res;
+    }
+}
\ No newline at end of file