From a797d269c539e838f5869102d2e5a7f0e62bbc45 Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Fri, 17 Dec 2021 16:02:24 -0600 Subject: [PATCH] Use vectors --- cyoa.leg | 72 +++++++++----------------------------------------------- 1 file changed, 11 insertions(+), 61 deletions(-) diff --git a/cyoa.leg b/cyoa.leg index ed0310e..b6831a0 100644 --- a/cyoa.leg +++ b/cyoa.leg @@ -7,14 +7,9 @@ typedef struct page_t page_t; typedef struct footer_t footer_t; - -typedef struct page_node_t page_node_t; -typedef struct choicelist choicelist; - -struct page_node_t { - page_t *page; - page_node_t *next; -}; +typedef struct statcheck_t statcheck_t; +typedef struct statchange_t statchange_t; +typedef struct choice_t choice_t; #define FOOTER_END 1 #define FOOTER_GOTO 2 @@ -22,7 +17,7 @@ struct page_node_t { struct footer_t { int type; union { - choicelist *choices; + std::vector choices; char *link; }; }; @@ -33,10 +28,6 @@ struct page_t { footer_t footer; }; -typedef struct statcheck_t statcheck_t; -typedef struct statchange_t statchange_t; -typedef struct choice_t choice_t; - #define STATCHECK_GT 1 #define STATCHECK_LT 2 @@ -64,18 +55,10 @@ struct choice_t { statchange_t statchange; }; -struct choicelist { - choice_t *choice; - choicelist *next; -}; - -page_node_t *pages; +std::vector pages; choice_t *make_choice(); -void add_page(page_t*); -void add_choice(choicelist*, choice_t*); - page_t *emit_ending(); page_t *emit_goto(char*); page_t *emit_choices(); @@ -86,7 +69,7 @@ union value { page_t *page; char *string; choice_t *choice; - choicelist *choices; + std::vector *choices; int num; }; @@ -95,7 +78,7 @@ union value { %} -Story = BlankLine* (p:Page { add_page(p.page); })+ EndOfFile; +Story = BlankLine* (p:Page { pages.push_back(p.page); })+ EndOfFile; Page = h:Header b:Body { $$.page = b.page; $$.page->id = h.string} BlankLine*; @@ -120,8 +103,8 @@ Goto = 'GOTO' Spacing < i:Identifier > Newline Ending = 'THE END' Newline; -ChoiceList = c:Choice l:ChoiceList { $$ = l; add_choice($$.choices, c.choice); } - | c:Choice { $$.choices = NULL; add_choice($$.choices, c.choice); } +ChoiceList = c:Choice l:ChoiceList { $$ = l; $$.choices->push_back(c.choice); } + | c:Choice { $$.choices = new std::vector(); $$.choices->push_back(c.choice); } Choice = b:Bullet f:Freetext r:Redirect { @@ -152,38 +135,6 @@ Newline = '\r\n' | '\r' | '\n'; %% -void add_page(page_t *page) { - page_node_t *mynode = (page_node_t*) malloc(sizeof(page_node_t)); - mynode->page = page; - mynode->next = NULL; - - page_node_t *curr = pages; - page_node_t *prev = NULL; - - if (curr) { - for(; curr; prev = curr, curr = curr->next); - prev->next = mynode; - } else { - pages = mynode; - } -} - -void add_choice(choicelist *choices, choice_t* choice) { - choicelist *mychoices = (choicelist *) malloc(sizeof(choicelist)); - mychoices->choice = choice; - mychoices->next = NULL; - - choicelist *curr = choices; - choicelist *prev = NULL; - - if (curr) { - for(; curr; prev = curr, curr = curr->next); - prev->next = mychoices; - } else { - choices = mychoices; - } -} - static inline page_t *make_page() { page_t *mypage = (page_t*) malloc(sizeof(page_t)); mypage->id = NULL; @@ -214,7 +165,6 @@ page_t *emit_ending() { page_t *emit_choices() { page_t *mypage = make_page(); mypage->footer.type = FOOTER_CHOICES; - mypage->footer.choices = NULL; printf("page 0x%x\n", mypage); return mypage; } @@ -252,8 +202,8 @@ int main() { printf("Parsing Error!\n"); return 1; } else { - for (page_node_t *curr = pages; curr; curr = curr->next) { - print_page(curr->page); + for (page_t* page: pages) { + print_page(page); } } return 0;