From 27e22daa10040999f6e5bfe47aa2b8b504b40071 Mon Sep 17 00:00:00 2001 From: lemon Date: Sun, 7 Aug 2022 06:41:48 +0200 Subject: union init --- bootstrap/test.cff | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'bootstrap/test.cff') diff --git a/bootstrap/test.cff b/bootstrap/test.cff index e466bba..a000224 100644 --- a/bootstrap/test.cff +++ b/bootstrap/test.cff @@ -1,11 +1,12 @@ import "libc.hff"; import "libc.hff"; + union Val { - x i64, - lo i32, - y f64, + u u32, + f f32, } + enum Color { Red, Green, Blue } @@ -50,9 +51,16 @@ struct Vec2f { fn spanz(cstr *const u8) [#]const u8 { extern fn strlen(s *const u8) usize; - return cstr[0::strlen(cstr)]; + return cstr[0::strlen(cstr) + 1]; } +defmacro transmute(Type, x) [ + (do + union T { from typeof(x), to Type }; + T{x}.to; + ) +] + extern fn main (argc int, argv **u8) int { let colors [3]Color = { :Red, :Green, :Blue } ; @@ -77,6 +85,7 @@ extern fn main (argc int, argv **u8) int { printf("sizeof(is) = %zu\n", sizeof(is)); printf("sizeof *void = %zu\n", sizeof *void); + printf("1.2 -> %#.8x\n", transmute(u32, 1.2f)); return 0; -- cgit v1.2.3