From 4ffdea07bbd624882c2cf63846b94cac3a6e8725 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 8 Aug 2023 22:43:46 -0400 Subject: [PATCH] wip --- assets/parser.c.erb | 61 +++++++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 16 deletions(-) diff --git a/assets/parser.c.erb b/assets/parser.c.erb index c9d4572..21d44af 100644 --- a/assets/parser.c.erb +++ b/assets/parser.c.erb @@ -1,5 +1,7 @@ #include ".h" #include +#include +#include /************************************************************************** * User code blocks @@ -640,26 +642,53 @@ static immutable parser_state_t[] parser_state_table = [ <% end %> ]; -/* state_values linked list functionality */ - -typedef struct state_values_list_entry_s -{ - /** State value object. */ - state_value_t state_value; - - /** Linked list previous entry. */ - struct state_values_list_entry_s * prev; - - /** Linked list next entry. */ - struct state_values_list_entry_s * next; -} state_values_list_entry_t; +/* state_values stack functionality */ +/** state_values stack type. */ typedef struct { size_t length; - state_values_list_entry_t * first; - state_values_list_entry_t * last; -} state_values_list_t; + size_t capacity; + state_value_t * entries; +} state_values_stack_t; + +/** + * Initialize state_values stack structure. + * + * @param stack + * state_values stack structure. + */ +void state_values_stack_init(state_values_stack_t * stack) +{ + const size_t initial_capacity = 10u; + stack->length = 0u; + stack->capacity = initial_capacity; + stack->entries = (state_value_t *)malloc(initial_capacity * sizeof(state_value_t)); +} + +/** + * Push a new state_value to the state_values stack. + * + * @param stack + * state_values stack structure. + */ +void state_values_stack_push(state_values_stack_t * stack) +{ + size_t const current_capacity = stack->capacity; + size_t const current_length = stack->length; + if (current_length >= current_capacity) + { + size_t const new_capacity = current_capacity * 2u; + state_value_t * new_entries = malloc(new_capacity * sizeof(state_value_t)); + memcpy(new_entries, stack->entries, current_length * sizeof(state_value_t); + free(stack->entries); + stack->capacity = new_capacity; + stack->entries = new_entries; + } + memset(&stack->entries[current_length], 0, sizeof(state_value_t)); + stack->length = current_length + 1u; +} + /** * Execute user code associated with a parser rule. *