From f63b52f9108e3acc555822348e631e3d72040d39 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Tue, 21 Oct 2025 22:10:23 -0400 Subject: [PATCH] curate fixed stack --- code2/grime/fixed_stack.odin | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 code2/grime/fixed_stack.odin diff --git a/code2/grime/fixed_stack.odin b/code2/grime/fixed_stack.odin new file mode 100644 index 0000000..7111f8d --- /dev/null +++ b/code2/grime/fixed_stack.odin @@ -0,0 +1,29 @@ +package grime + +FStack :: struct ($Type: typeid, $Size: u32) { + items: [Size]Type, + idx: u32, +} +stack_clear :: #force_inline proc "contextless" (stack: ^FStack($Type, $Size)) { stack.idx = 0 } +stack_push :: #force_inline proc "contextless" (stack: ^FStack($Type, $Size ), value: Type) { + assert_contextless(stack.idx < u32(len( stack.items )), "Attempted to push on a full stack") + stack.items[stack.idx] = value + stack.idx += 1 +} +stack_pop :: #force_inline proc "contextless" (stack: ^FStack($Type, $Size)) { + assert(stack.idx > 0, "Attempted to pop an empty stack") + stack.idx -= 1 + if stack.idx == 0 { + stack.items[stack.idx] = {} + } +} +stack_peek_ref :: #force_inline proc "contextless" (s: ^FStack($Type, $Size)) -> (^Type) { + return & s.items[/*last_idx*/ max( 0, s.idx - 1 )] +} +stack_peek :: #force_inline proc "contextless" (s: ^FStack($Type, $Size)) -> Type { + return s.items[/*last_idx*/ max( 0, s.idx - 1 )] +} +stack_push_contextless :: #force_inline proc "contextless" (s: ^FStack($Type, $Size), value: Type) { + s.items[s.idx] = value + s.idx += 1 +}