From 2ad0587242e8cf609804f96c52a665a9fb7a2650 Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Fri, 17 Dec 2021 16:46:50 -0600 Subject: [PATCH] Choices are also a c++ struct --- cyoa.leg | 95 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 50 insertions(+), 45 deletions(-) diff --git a/cyoa.leg b/cyoa.leg index ce09e46..e1623fc 100644 --- a/cyoa.leg +++ b/cyoa.leg @@ -5,9 +5,35 @@ #include #include -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 + +struct statcheck_t { + char *stat; + int value; + int rel; +}; + +struct statchange_t { + char *stat; + int addend; +}; + +enum ChoiceFlags : int { + StatCheck = 0x1, + StatChange = 0x2, +}; + +struct Choice { + int flags; + int option; + char *flavor; + statcheck_t statcheck; + char *id; + statchange_t statchange; + + Choice(char*, statcheck_t*, statchange_t*); +}; enum class FooterType { End, @@ -18,7 +44,7 @@ enum class FooterType { struct Footer { FooterType type; union { - std::vector choices; + std::vector choices; char *link; }; @@ -35,37 +61,8 @@ struct Page { ~Page(); }; -#define STATCHECK_GT 1 -#define STATCHECK_LT 2 - -struct statcheck_t { - char *stat; - int value; - int rel; -}; - -struct statchange_t { - char *stat; - int addend; -}; - - -#define CHOICE_STATCHECK 0x1 -#define CHOICE_STATCHANGE 0x2 - -struct choice_t { - int flags; - int option; - char *flavor; - statcheck_t statcheck; - char *redirect; - statchange_t statchange; -}; - std::vector pages; -choice_t *make_choice(); - Page *emit_ending(); Page *emit_goto(char*); Page *emit_choices(); @@ -75,9 +72,11 @@ void append_body(Page *page, char *str); union value { Page *page; char *string; - choice_t *choice; - std::vector *choices; + Choice *choice; + std::vector *choices; int num; + statcheck_t *statcheck; + statchange_t *statchange; }; #define YYSTYPE union value @@ -111,7 +110,7 @@ Goto = 'GOTO' Spacing < i:Identifier > Newline Ending = 'THE END' Newline; ChoiceList = c:Choice l:ChoiceList { $$ = l; $$.choices->push_back(c.choice); } - | c:Choice { $$.choices = new std::vector(); $$.choices->push_back(c.choice); } + | c:Choice { $$.choices = new std::vector(); $$.choices->push_back(c.choice); } Choice = b:Bullet f:Freetext r:Redirect { @@ -124,9 +123,10 @@ Bullet = < [0-9]+ > ')' Spacing { $$.num = atoi(yytext); }; Freetext = < (!Redirect .)+ > { $$.string = strndup(yytext, yyleng); }; -Redirect = StatCheck? Spacing '[' Identifier ']' Spacing StatChange? Newline; +Redirect = ck:StatCheck? '[' i:Identifier ']' Spacing cg:StatChange? Newline + { $$.choice = new Choice(i.string, ck.statcheck, cg.statchange); } -StatCheck = '<' StatName Spacing [0-9]+ ('+' | '-')? '>'; +StatCheck = '<' StatName Spacing [0-9]+ ('+' | '-')? '>' Spacing; StatChange = '(' ('+' | '-') [0-9]+ Spacing StatName ')'; @@ -142,6 +142,18 @@ Newline = '\r\n' | '\r' | '\n'; %% +Choice::Choice(char *id, statcheck_t *ck, statchange_t *cg) { + this->id = id; + if (ck) { + statcheck = *ck; + flags |= StatCheck; + } + if (cg) { + statchange = *cg; + flags |= StatChange; + } +} + Footer::Footer() { } @@ -162,13 +174,6 @@ Page::~Page() { } } -inline choice_t *make_choice() { - choice_t *mychoice = (choice_t*) malloc(sizeof(choice_t)); - mychoice->flags = 0x0; - printf("choice 0x%x\n", mychoice); - return mychoice; -} - Page *emit_goto(char *id) { Page *mypage = new Page; mypage->footer.type = FooterType::Goto;