From 6add5bd2298c7ce7e27144b7db28aecec60fd1dd Mon Sep 17 00:00:00 2001 From: Spartan322 Date: Thu, 24 Aug 2023 17:29:52 -0400 Subject: Cleanup and streamline --- .../v2script/AbstractSyntaxTree.hpp | 16 ++++++++--- .../v2script/AiBehaviorGrammar.hpp | 2 +- .../v2script/DecisionGrammar.hpp | 2 +- src/openvic-dataloader/v2script/EffectGrammar.hpp | 2 +- src/openvic-dataloader/v2script/EventGrammar.hpp | 31 +++++++++++++++------- .../v2script/ModifierGrammar.hpp | 5 ++-- src/openvic-dataloader/v2script/TriggerGrammar.hpp | 2 +- 7 files changed, 41 insertions(+), 19 deletions(-) diff --git a/include/openvic-dataloader/v2script/AbstractSyntaxTree.hpp b/include/openvic-dataloader/v2script/AbstractSyntaxTree.hpp index 0d0b019..1dcd7c7 100644 --- a/include/openvic-dataloader/v2script/AbstractSyntaxTree.hpp +++ b/include/openvic-dataloader/v2script/AbstractSyntaxTree.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -50,6 +51,12 @@ namespace ovdl::v2script::ast { constexpr bool is_type() const { return get_type().compare(detail::type_name()) == 0; } + + template + constexpr std::optional cast_to() { + if (is_type()) return static_cast(*this); + return std::nullopt; + } }; using NodePtr = Node*; @@ -77,7 +84,7 @@ namespace ovdl::v2script::ast { std::vector result; result.reserve(ptrs.size()); for (auto&& p : ptrs) { - if (p == nullptr) continue; + if (!p || p == nullptr) continue; result.push_back(NodeUPtr(p)); } return result; @@ -103,13 +110,15 @@ namespace ovdl::v2script::ast { }) \ } + // Value Expression Nodes OVDL_AST_STRING_NODE(IdentifierNode); + OVDL_AST_STRING_NODE(StringNode); + + // Assignment Nodes OVDL_AST_STRING_NODE(FactorNode); OVDL_AST_STRING_NODE(MonthNode); OVDL_AST_STRING_NODE(NameNode); - OVDL_AST_STRING_NODE(StringNode); OVDL_AST_STRING_NODE(FireOnlyNode); - OVDL_AST_STRING_NODE(IdNode); OVDL_AST_STRING_NODE(TitleNode); OVDL_AST_STRING_NODE(DescNode); @@ -172,6 +181,7 @@ namespace ovdl::v2script::ast { } OVDL_AST_LIST_NODE(ListNode); + OVDL_AST_LIST_NODE(ModifierNode); OVDL_AST_LIST_NODE(MtthNode); OVDL_AST_LIST_NODE(EventOptionNode); diff --git a/src/openvic-dataloader/v2script/AiBehaviorGrammar.hpp b/src/openvic-dataloader/v2script/AiBehaviorGrammar.hpp index a772a7c..bbdc67d 100644 --- a/src/openvic-dataloader/v2script/AiBehaviorGrammar.hpp +++ b/src/openvic-dataloader/v2script/AiBehaviorGrammar.hpp @@ -26,7 +26,7 @@ namespace ovdl::v2script::grammar { return LEXY_MOV(list); }, [](lexy::nullopt = {}) { - return nullptr; + return lexy::nullopt {}; }); }; } \ No newline at end of file diff --git a/src/openvic-dataloader/v2script/DecisionGrammar.hpp b/src/openvic-dataloader/v2script/DecisionGrammar.hpp index 3e77616..e6e3b57 100644 --- a/src/openvic-dataloader/v2script/DecisionGrammar.hpp +++ b/src/openvic-dataloader/v2script/DecisionGrammar.hpp @@ -103,7 +103,7 @@ namespace ovdl::v2script::grammar { return ast::make_node_ptr(LEXY_MOV(list)); }, [](lexy::nullopt = {}) { - return nullptr; + return lexy::nullopt {}; }); }; diff --git a/src/openvic-dataloader/v2script/EffectGrammar.hpp b/src/openvic-dataloader/v2script/EffectGrammar.hpp index 71697c4..db35f02 100644 --- a/src/openvic-dataloader/v2script/EffectGrammar.hpp +++ b/src/openvic-dataloader/v2script/EffectGrammar.hpp @@ -34,7 +34,7 @@ namespace ovdl::v2script::grammar { return LEXY_MOV(list); }, [](lexy::nullopt = {}) { - return nullptr; + return lexy::nullopt {}; }); }; } \ No newline at end of file diff --git a/src/openvic-dataloader/v2script/EventGrammar.hpp b/src/openvic-dataloader/v2script/EventGrammar.hpp index 216d645..4a64ce4 100644 --- a/src/openvic-dataloader/v2script/EventGrammar.hpp +++ b/src/openvic-dataloader/v2script/EventGrammar.hpp @@ -49,8 +49,13 @@ namespace ovdl::v2script::grammar { struct EventMtthStatement { OVDL_GRAMMAR_KEYWORD_DEFINE(months); + struct MonthValue { + static constexpr auto rule = lexy::dsl::inline_; + static constexpr auto value = lexy::as_string | lexy::new_; + }; + static constexpr auto rule = lexy::dsl::list( - (months_p >> lexy::dsl::p) | + (months_p >> lexy::dsl::p) | lexy::dsl::p); static constexpr auto value = @@ -61,6 +66,20 @@ namespace ovdl::v2script::grammar { }); }; + template + struct _StringStatement { + static constexpr auto rule = Production >> (lexy::dsl::p | lexy::dsl::p); + static constexpr auto value = + lexy::callback( + [](auto&& value) { + auto result = ast::make_node_ptr(std::move(static_cast(value)->_name)); + delete value; + return result; + }); + }; + template + static constexpr auto StringStatement = lexy::dsl::p<_StringStatement>; + struct EventOptionList { OVDL_GRAMMAR_KEYWORD_FLAG_DEFINE(name); OVDL_GRAMMAR_KEYWORD_FLAG_DEFINE(ai_chance); @@ -68,7 +87,7 @@ namespace ovdl::v2script::grammar { static constexpr auto rule = [] { constexpr auto create_flags = name_rule::flag.create() + ai_chance_rule::flag.create(); - constexpr auto name_statement = name_p >> (lexy::dsl::p | lexy::dsl::p); + constexpr auto name_statement = StringStatement; constexpr auto ai_chance_statement = ai_chance_p >> lexy::dsl::curly_bracketed(lexy::dsl::p); return create_flags + lexy::dsl::list(name_statement | ai_chance_statement | lexy::dsl::p); @@ -83,14 +102,6 @@ namespace ovdl::v2script::grammar { }; struct EventStatement { - template - struct _StringStatement { - static constexpr auto rule = Production >> (lexy::dsl::p | lexy::dsl::p); - static constexpr auto value = lexy::forward; - }; - template - static constexpr auto StringStatement = lexy::dsl::p<_StringStatement>; - OVDL_GRAMMAR_KEYWORD_FLAG_DEFINE(id); OVDL_GRAMMAR_KEYWORD_FLAG_DEFINE(title); OVDL_GRAMMAR_KEYWORD_FLAG_DEFINE(desc); diff --git a/src/openvic-dataloader/v2script/ModifierGrammar.hpp b/src/openvic-dataloader/v2script/ModifierGrammar.hpp index 2b28712..74deef7 100644 --- a/src/openvic-dataloader/v2script/ModifierGrammar.hpp +++ b/src/openvic-dataloader/v2script/ModifierGrammar.hpp @@ -3,14 +3,15 @@ #include "SimpleGrammar.hpp" #include "TriggerGrammar.hpp" #include +#include namespace ovdl::v2script::grammar { constexpr auto modifier_keyword = LEXY_KEYWORD("modifier", lexy::dsl::inline_); constexpr auto factor_keyword = LEXY_KEYWORD("factor", lexy::dsl::inline_); struct FactorStatement { - static constexpr auto rule = factor_keyword >> lexy::dsl::equal_sign + lexy::dsl::p; - static constexpr auto value = lexy::forward; + static constexpr auto rule = factor_keyword >> lexy::dsl::equal_sign + lexy::dsl::inline_; + static constexpr auto value = lexy::as_string | lexy::new_; }; struct ModifierStatement { diff --git a/src/openvic-dataloader/v2script/TriggerGrammar.hpp b/src/openvic-dataloader/v2script/TriggerGrammar.hpp index 514da49..ffed1d3 100644 --- a/src/openvic-dataloader/v2script/TriggerGrammar.hpp +++ b/src/openvic-dataloader/v2script/TriggerGrammar.hpp @@ -34,7 +34,7 @@ namespace ovdl::v2script::grammar { return LEXY_MOV(list); }, [](lexy::nullopt = {}) { - return nullptr; + return lexy::nullopt {}; }); }; } \ No newline at end of file -- cgit v1.2.3-56-ga3b1