diff --git a/cyoa.leg b/cyoa.leg index b6831a0..ce09e46 100644 --- a/cyoa.leg +++ b/cyoa.leg @@ -5,27 +5,34 @@ #include #include -typedef struct page_t page_t; -typedef struct footer_t footer_t; 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 -#define FOOTER_CHOICES 3 -struct footer_t { - int type; +enum class FooterType { + End, + Goto, + Choices +}; + +struct Footer { + FooterType type; union { std::vector choices; char *link; }; + + Footer(); + ~Footer(); }; -struct page_t { +struct Page { char *id; char *body; - footer_t footer; + Footer footer; + + Page(); + ~Page(); }; #define STATCHECK_GT 1 @@ -55,18 +62,18 @@ struct choice_t { statchange_t statchange; }; -std::vector pages; +std::vector pages; choice_t *make_choice(); -page_t *emit_ending(); -page_t *emit_goto(char*); -page_t *emit_choices(); +Page *emit_ending(); +Page *emit_goto(char*); +Page *emit_choices(); -void append_body(page_t *page, char *str); +void append_body(Page *page, char *str); union value { - page_t *page; + Page *page; char *string; choice_t *choice; std::vector *choices; @@ -135,11 +142,24 @@ Newline = '\r\n' | '\r' | '\n'; %% -static inline page_t *make_page() { - page_t *mypage = (page_t*) malloc(sizeof(page_t)); - mypage->id = NULL; - mypage->body = NULL; - return mypage; +Footer::Footer() { +} + +Footer::~Footer() { +} + +Page::Page() { + id = NULL; + body = NULL; +} + +Page::~Page() { + if (id) { + free(id); + } + if (body) { + free(body); + } } inline choice_t *make_choice() { @@ -149,27 +169,27 @@ inline choice_t *make_choice() { return mychoice; } -page_t *emit_goto(char *id) { - page_t *mypage = make_page(); - mypage->footer.type = FOOTER_GOTO; +Page *emit_goto(char *id) { + Page *mypage = new Page; + mypage->footer.type = FooterType::Goto; mypage->footer.link = id; return mypage; } -page_t *emit_ending() { - page_t *mypage = make_page(); - mypage->footer.type = FOOTER_END; +Page *emit_ending() { + Page *mypage = new Page; + mypage->footer.type = FooterType::End; return mypage; } -page_t *emit_choices() { - page_t *mypage = make_page(); - mypage->footer.type = FOOTER_CHOICES; +Page *emit_choices() { + Page *mypage = new Page; + mypage->footer.type = FooterType::Choices; printf("page 0x%x\n", mypage); return mypage; } -void append_body(page_t *page, char *str) { +void append_body(Page *page, char *str) { if (page->body) { int size = strlen(page->body) + strlen(str) + 1; char *newstring = (char*) calloc(size, sizeof(char)); @@ -182,16 +202,16 @@ void append_body(page_t *page, char *str) { } } -void print_page(page_t *page) { +void print_page(Page *page) { printf("HEADER: %s\nBODY: %s\n", page->id, page->body); switch (page->footer.type) { - case FOOTER_END: + case FooterType::End: printf("THE END\n"); break; - case FOOTER_GOTO: + case FooterType::Goto: printf("GOTO %s\n", page->footer.link); break; - case FOOTER_CHOICES: + case FooterType::Choices: printf("CHOICES:\n"); break; } @@ -202,7 +222,7 @@ int main() { printf("Parsing Error!\n"); return 1; } else { - for (page_t* page: pages) { + for (Page* page : pages) { print_page(page); } }