Use vectors

This commit is contained in:
Dane Johnson 2021-12-17 16:02:24 -06:00
parent d7acfd8c8e
commit a797d269c5

View File

@ -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<choice_t*> 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<page_t*> 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<choice_t*> *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<choice_t*>(); $$.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;