diff options
| author | Ginger Bill <bill@gingerbill.org> | 2016-08-24 23:25:56 +0100 |
|---|---|---|
| committer | Ginger Bill <bill@gingerbill.org> | 2016-08-24 23:25:56 +0100 |
| commit | d2c64be85ca15117b1745b254b1806ea739aef43 (patch) | |
| tree | 2f5192cfbff77a6eef55ddf1fe2f1269b403561a /examples | |
| parent | 6bd898e552392b1bd11ad16c7476833261c1d4b7 (diff) | |
`using` on struct/union fields
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/demo.odin | 142 | ||||
| -rw-r--r-- | examples/file.odin | 2 | ||||
| -rw-r--r-- | examples/game.odin | 16 | ||||
| -rw-r--r-- | examples/runtime.odin | 12 | ||||
| -rw-r--r-- | examples/win32.odin | 36 |
5 files changed, 150 insertions, 58 deletions
diff --git a/examples/demo.odin b/examples/demo.odin index 550b499fb..491a79900 100644 --- a/examples/demo.odin +++ b/examples/demo.odin @@ -1,26 +1,18 @@ // Demo 001 #load "basic.odin" -#load "math.odin" #load "game.odin" main :: proc() { - // _ = hellope(); - // procedures(); - // variables(); - // constants(); - // types(); - // data_control(); + _ = hellope(); + procedures(); + variables(); + constants(); + types(); + data_control(); + using_fields(); - // run_game(); - Thing :: type union { - i: i32, - f: f32, - } - t: Thing; - t.f = 123; - print_int_base(t.i as int, 16); - print_nl(); + // run_game(); } hellope :: proc() -> int { @@ -43,7 +35,7 @@ hellope :: proc() -> int { apple, banana, carrot: bool; box, carboard: bool = true, false; -hellope_value: int = hellope(); +hellope_value: int = hellope(); // The procedure is ran just before `main` variables :: proc() { i: int; // initialized with zero value @@ -293,16 +285,16 @@ types :: proc() { } BinaryNode :: type struct { - left, right: ^BinaryNode, // same format as procedure argument - data: rawptr, + left, right: ^BinaryNode; // same format as procedure argument + data: rawptr; } AddProc :: type proc(a, b: int) -> int Packed :: type struct #packed { - a: u8, - b: u16, - c: u32, + a: u8; + b: u16; + c: u32; } static_assert(size_of(Packed) == 7); // builtin procedure @@ -383,6 +375,50 @@ types :: proc() { static_assert(Certain.TOMB == 8); } + { // Untagged union + BitHack :: type union { + i: i32; + f: f32; + } + b: BitHack; + b.f = 123; + print_int(b.i as int); print_nl(); + + + + // Manually tagged union + + EntityKind :: type enum { + Invalid, + Constant, + Variable, + TypeName, + Procedure, + Builtin, + Count, + } + + Entity :: type struct { + kind: EntityKind; + guid: u64; + + // Other data + + /*using*/ + data: union { + constant: struct{}; + variable: struct{ + visited, is_field, used, anonymous: bool; + }; + procedure: struct { used: bool }; + buitlin: struct { id: i32 }; + }; + } + + + // NOTE(bill): Tagged unions are not added yet but are planned + } + { // Compound Literals @@ -478,9 +514,9 @@ void main() { { // size, align, offset Thing :: type struct { - a: u8, - b: u16, - c, d, e: u32, + a: u8; + b: u16; + c, d, e: u32; } s := size_of(Thing); @@ -619,3 +655,59 @@ data_control :: proc() { } +using_fields :: proc() { + { // Everyday stuff + Vec3 :: type struct { x, y, z: f32; } + + Entity :: type struct { + name: string; + using pos: Vec3; + vel: Vec3; + } + t: Entity; + t.y = 456; + print_f32(t.y); print_nl(); + print_f32(t.pos.y); print_nl(); + print_f32(t.vel.y); print_nl(); + + + Frog :: type struct { // Subtype (kind of) + using entity: Entity; + colour: u32; + jump_height: f32; + } + + f: Frog; + f.y = 1337; + print_f32(f.y); print_nl(); + print_f32(f.pos.y); print_nl(); + print_f32(f.vel.y); print_nl(); + + + Buffalo :: type struct { + using entity: Entity; + speed: f32; + noise_level: f32; + } + } + + + { // Crazy Shit + Vec2 :: type union { + using _xy: struct {x, y: f32}; + e: [2]f32; + v: {2}f32; + } + + Entity :: type struct { + using pos: ^Vec2; + name: string; + } + t: Entity; + t.pos = alloc(size_of(Vec2)) as ^Vec2; // TODO(bill): make an alloc type? i.e. new(Type)? + t.x = 123; + print_f32(t._xy.x); print_nl(); + print_f32(t.pos.x); print_nl(); + print_f32(t.pos._xy.x); print_nl(); + } +} diff --git a/examples/file.odin b/examples/file.odin index 3e1e76238..f007b380a 100644 --- a/examples/file.odin +++ b/examples/file.odin @@ -3,7 +3,7 @@ FileHandle :: type HANDLE; File :: type struct { - handle: FileHandle, + handle: FileHandle; } file_open :: proc(name: string) -> (File, bool) { diff --git a/examples/game.odin b/examples/game.odin index 31b6eecc0..f6afb92bf 100644 --- a/examples/game.odin +++ b/examples/game.odin @@ -34,12 +34,12 @@ to_c_string :: proc(s: string) -> ^u8 { Window :: type struct { - width, height: int, - wc: WNDCLASSEXA, - dc: HDC, - hwnd: HWND, - opengl_context, rc: HGLRC, - c_title: ^u8, + width, height: int; + wc: WNDCLASSEXA; + dc: HDC; + hwnd: HWND; + opengl_context, rc: HGLRC; + c_title: ^u8; } make_window :: proc(title: string, msg, height: int, window_proc: WNDPROC) -> (Window, bool) { @@ -123,8 +123,8 @@ display_window :: proc(w: ^Window) { Entity :: type struct { - pos: Vec2, - dim: Vec2, + pos: Vec2; + dim: Vec2; } diff --git a/examples/runtime.odin b/examples/runtime.odin index cd5f9ae31..3c2c5daf9 100644 --- a/examples/runtime.odin +++ b/examples/runtime.odin @@ -258,18 +258,18 @@ AllocatorProc :: type proc(allocator_data: rawptr, mode: AllocationMode, old_memory: rawptr, old_size: int, flags: u64) -> rawptr; Allocator :: type struct { - procedure: AllocatorProc, - data: rawptr, + procedure: AllocatorProc; + data: rawptr; } Context :: type struct { - thread_id: i32, + thread_id: i32; - user_index: i32, - user_data: rawptr, + user_index: i32; + user_data: rawptr; - allocator: Allocator, + allocator: Allocator; } #thread_local context: Context; diff --git a/examples/win32.odin b/examples/win32.odin index 5e83f5bb0..ee58367f7 100644 --- a/examples/win32.odin +++ b/examples/win32.odin @@ -40,24 +40,24 @@ INVALID_HANDLE_VALUE :: (-1 as int) as HANDLE; WNDPROC :: type proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT WNDCLASSEXA :: type struct { - size, style: u32, - wnd_proc: WNDPROC, - cls_extra, wnd_extra: i32, - instance: HINSTANCE, - icon: HICON, - cursor: HCURSOR, - background: HBRUSH, - menu_name, class_name: ^u8, - sm: HICON, + size, style: u32; + wnd_proc: WNDPROC; + cls_extra, wnd_extra: i32; + instance: HINSTANCE; + icon: HICON; + cursor: HCURSOR; + background: HBRUSH; + menu_name, class_name: ^u8; + sm: HICON; } MSG :: type struct { - hwnd: HWND, - message: u32, - wparam: WPARAM, - lparam: LPARAM, - time: u32, - pt: POINT, + hwnd: HWND; + message: u32; + wparam: WPARAM; + lparam: LPARAM; + time: u32; + pt: POINT; } @@ -189,7 +189,7 @@ wglCreateContextAttribsARBType :: type proc(hdc: HDC, hshareContext: rawptr, att PIXELFORMATDESCRIPTOR :: type struct { size, version, - flags: u32, + flags: u32; pixel_type, color_bits, @@ -210,11 +210,11 @@ PIXELFORMATDESCRIPTOR :: type struct { stencil_bits, aux_buffers, layer_type, - reserved: byte, + reserved: byte; layer_mask, visible_mask, - damage_mask: u32, + damage_mask: u32; } GetDC :: proc(h: HANDLE) -> HDC #foreign |