From: Brendan Hansen Date: Thu, 31 Dec 2020 17:41:58 +0000 (-0600) Subject: added compile time procedures test case X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=798667c3f93fe85fad95e72f00fab789ae74f8ab;p=onyx.git added compile time procedures test case --- diff --git a/tests/compile_time_procedures b/tests/compile_time_procedures new file mode 100644 index 00000000..ad386a9c --- /dev/null +++ b/tests/compile_time_procedures @@ -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 index 00000000..4ecdb219 --- /dev/null +++ b/tests/compile_time_procedures.onyx @@ -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