Choices are also a c++ struct

This commit is contained in:
Dane Johnson 2021-12-17 16:46:50 -06:00
parent 6aed8ed768
commit 2ad0587242

View File

@ -5,9 +5,35 @@
#include <cstdlib>
#include <cassert>
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<choice_t*> choices;
std::vector<Choice*> 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<Page*> 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<choice_t*> *choices;
Choice *choice;
std::vector<Choice*> *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<choice_t*>(); $$.choices->push_back(c.choice); }
| c:Choice { $$.choices = new std::vector<Choice*>(); $$.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;