From a8155e885f5f8021d7f089ad5377a5edf280f4ee Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Fri, 17 Dec 2021 15:42:47 -0600 Subject: [PATCH] Change the way choices work again --- cyoa.leg | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/cyoa.leg b/cyoa.leg index e01f793..0b8c4ab 100644 --- a/cyoa.leg +++ b/cyoa.leg @@ -73,7 +73,7 @@ page_node_t *pages; choice_t *make_choice(); void add_page(page_t*); -void add_choice(page_t*, choice_t*); +void add_choice(choicelist*, choice_t*); page_t *emit_ending(); page_t *emit_goto(char*); @@ -85,6 +85,8 @@ union value { page_t *page; char *string; choice_t *choice; + choicelist *choices; + int num; }; #define YYSTYPE union value @@ -108,7 +110,7 @@ TextLine = < (!Newline .)* Newline > { $$.string = strndup(yytext, yyleng); Footer = Ending { $$.page = emit_ending(); } | g:Goto { $$.page = emit_goto(g.string); } - | Choice+ { $$.page = emit_ending(); } + | c:ChoiceList { $$.page = emit_choices(c.choices); } ; Goto = 'GOTO' Spacing < i:Identifier > Newline @@ -117,7 +119,19 @@ Goto = 'GOTO' Spacing < i:Identifier > Newline Ending = 'THE END' Newline; -Choice = { $$.choice = make_choice(); } [0-9]+ ')' Spacing < (!Redirect .)+ > Redirect; +ChoiceList = c:Choice l:ChoiceList { $$ = l; add_choice($$.choices, c.choice); } + | c:Choice { $$.choices = NULL; add_choice($$.choices, c.choice); } + +Choice = b:Bullet f:Freetext r:Redirect + { + $$ = r; + $$.choice->option = b.num; + $$.choice->flavor = f.string; + }; + +Bullet = < [0-9]+ > ')' Spacing { $$.num = atoi(yytext); }; + +Freetext = < (!Redirect .)+ > { $$.string = strndup(yytext, yyleng); }; Redirect = StatCheck? Spacing '[' Identifier ']' Spacing StatChange? Newline; @@ -153,20 +167,19 @@ void add_page(page_t *page) { } } -void add_choice(page_t *page, choice_t* choice) { - assert(page != choice); +void add_choice(choicelist *choices, choice_t* choice) { choicelist *mychoices = (choicelist *) malloc(sizeof(choicelist)); mychoices->choice = choice; mychoices->next = NULL; - choicelist *curr = page->footer.choices; + choicelist *curr = choices; choicelist *prev = NULL; if (curr) { for(; curr; prev = curr, curr = curr->next); prev->next = mychoices; } else { - page->footer.choices = mychoices; + choices = mychoices; } }