From ae1266ccc971552d16763c146ec708036d1ae1f6 Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Sun, 2 Jan 2022 17:50:40 -0600 Subject: [PATCH] Finish up the cli driver --- spec.txt | 17 +++++++++++++++++ src/base/cyoa.leg | 16 ---------------- src/base/storybook.cpp | 13 ++++++++----- src/base/storybook.h | 6 ++++++ 4 files changed, 31 insertions(+), 21 deletions(-) diff --git a/spec.txt b/spec.txt index 2171e5d..72df87b 100644 --- a/spec.txt +++ b/spec.txt @@ -96,3 +96,20 @@ I'm getting pretty frustrated with what I perceive to be bugs in the peg/leg par I don't really feel like maintaining it. And GNU Bison is like right there. If I can write a cfg for my little language here I'll switch. I also can't shake the feeling that this is over-engineered, and I really don't need a parser for all this, but oh well. I could probably do it all with just lex actually. Maybe I need to spend some time rethinking this. + +1/2/22 + +Okay I decided to just power through, and I finished the CLI version, which I'll be committing with this update. I did write a CFG +however I realized that since this isn't a programming language an LR parser isn't exactly right. I made the right choice with the +PEG, I just need better tooling. Anyway the immediate problem that I was having was that I assumed that semantic actions following +an optional term in leg would be run with a null pointer or not be run at all, but it turns out you need to wrap the semantic +action in the optional as well. Oh well lesson learned. Maybe some day I'll write my own. + +Next up is definitely the Godot Native module. I've already read a bit about how to do it, but I know there will be a few hitches +here and there. Also I want to convert the base module to use entirely c++ semantics, with the exception of printf since I'm not +sure if there is a c++ parallel, which is a bit odd. C++ is weird, no wonder I've avoided using it until now. It seems like c++ +developers spend a lot of time coming up with new ways to do things, then declaring them unidomatic and banning them. I guess +that's kind of a trend with higher level languages as they get older cough Java cough. Regardless, I need to learn it and this +is as good of a project as any. + +Also I still haven't mentioned to Pam I was doing this. Hope that's not a problem... diff --git a/src/base/cyoa.leg b/src/base/cyoa.leg index f5d77be..6187b41 100644 --- a/src/base/cyoa.leg +++ b/src/base/cyoa.leg @@ -153,22 +153,6 @@ void append_body(Page *page, char *str) { } } -void print_page(Page *page) { - printf("HEADER: %s\nBODY: %s\n", page->id, page->body); - switch (page->footer.type) { - case FooterType::End: - printf("THE END\n"); - break; - case FooterType::Goto: - printf("GOTO %s\n", page->footer.link); - break; - case FooterType::Choices: - printf("CHOICES:\n"); - // TODO - break; - } -} - std::vector CyoaParse(FILE *file) { yyin = file; yyparse(); diff --git a/src/base/storybook.cpp b/src/base/storybook.cpp index c7d017d..24e13d4 100644 --- a/src/base/storybook.cpp +++ b/src/base/storybook.cpp @@ -56,10 +56,9 @@ void Storybook::Advance() { } } -void Storybook::Advance(int choice_option) { - // TODO throw an exception if the ending type is not choices. +void Storybook::Advance(int choice_option) { if (!IsChoiceAvailable(choice_option)) { - // TODO throw exception? + throw IllegalChoiceException(); } Choice* choice = (*current->footer.choices)[choice_option]; @@ -122,9 +121,13 @@ void Storybook::Play() { printf("Make a selection: "); int choice_option; std::cin >> choice_option; - Advance(choice_option); + try { + Advance(choice_option); + } catch (IllegalChoiceException e) { + printf("You can't do that!\n\n"); + } break; default: - IsEnded = true; // TODO + IsEnded = true; } } diff --git a/src/base/storybook.h b/src/base/storybook.h index fc94d9e..b80dd4d 100644 --- a/src/base/storybook.h +++ b/src/base/storybook.h @@ -31,6 +31,12 @@ public: // This will probably be moved to the cli driver void Play(); + + class IllegalChoiceException : std::exception { + virtual const char *what() const noexcept { + return "Illegal Choice."; + } + }; }; #endif /* STORYBOOK_H */