From a5d6b2da9025c2f48e65d32a04ad44b25b129acc Mon Sep 17 00:00:00 2001 From: "cgbsu@github.com" Date: Thu, 1 Sep 2022 21:40:32 +0000 Subject: [PATCH] Exceeded the maximum number of rules? --- Include/Warp/Common.hpp | 1 + Include/Warp/Parsing/FunctionDeclarations.hpp | 101 +++++++++++++++--- Include/Warp/Parsing/GeneralTermTags.hpp | 13 ++- Include/Warp/Runtime/Compiler/Frame.hpp | 12 +++ Include/Warp/Testing/TestParser.hpp | 30 ++++++ Test/FunctionDeclarations.cpp | 36 ++++++- 6 files changed, 177 insertions(+), 16 deletions(-) create mode 100644 Include/Warp/Runtime/Compiler/Frame.hpp diff --git a/Include/Warp/Common.hpp b/Include/Warp/Common.hpp index 68bcff1..418d869 100644 --- a/Include/Warp/Common.hpp +++ b/Include/Warp/Common.hpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include diff --git a/Include/Warp/Parsing/FunctionDeclarations.hpp b/Include/Warp/Parsing/FunctionDeclarations.hpp index 0ef22fd..f37a20f 100644 --- a/Include/Warp/Parsing/FunctionDeclarations.hpp +++ b/Include/Warp/Parsing/FunctionDeclarations.hpp @@ -9,6 +9,17 @@ namespace Warp::Parsing Constant }; + template + struct Constant + { + using ValueType = ValueParameterType; + using TypeType = TypeParameterType; + std::string name; + TypeType type; + ValueType value; + }; + + using FunctionDeclaritionTermsType = MathematicalExpressionTermsType ::AddOnePriority< TreeTerm< @@ -32,19 +43,34 @@ namespace Warp::Parsing FixedString{"let"}, ctpg::associativity::no_assoc > - //TypeTreeTerm< - // FunctionTags::Constant, - // NonTerminalTerm, - + >::AddOnePriority< + TreeTerm< + Declaration::SemiColon, + CharTerm, + ';', + ctpg::associativity::no_assoc + > + >::AddOnePriority< + TypeTreeTerm< + Declaration::Constant, + NonTerminalTerm, + std::string, + FixedString{"ConstantDeclaration"} + > >; template< typename TermsParameterType, template typename TypeResolverParameterTemplate, - auto ReductionTagParameterConstant + auto ReductionTagParameterConstant, + typename TypeTagParameterType, + typename ContextParamterType > struct FunctionDeclarationParser { + using TypeType = TypeTagParameterType; + using ContextType = ContextParamterType; + using BaseTermsType = TermsParameterType; constexpr static const auto reduce_to_tag = ReductionTagParameterConstant; @@ -54,45 +80,59 @@ namespace Warp::Parsing NonTerminalTypeTagParameterConstant >::Type; + using ConstantType = Constant; + + using TermsType = BaseTermsType::template AddOnePriority< + TypeTreeTerm< + Construct::Constant, + NonTerminalTerm, + ConstantType, + FixedString{"Constant"} + > + >; + using WholeMathematicalParserType = HomogenousMathematicalExpressionParser< NumericTypeTag::Whole, - BaseTermsType, + TermsType, TypeResolverParameterTemplate >; using IntegerMathematicalParserType = HomogenousMathematicalExpressionParser< NumericTypeTag::Integer, - BaseTermsType, + TermsType, TypeResolverParameterTemplate >; using FixedPointMathematicalParserType = HomogenousMathematicalExpressionParser< NumericTypeTag::FixedPoint, - BaseTermsType, + TermsType, TypeResolverParameterTemplate >; using CharacterMathematicalParserType = HomogenousMathematicalExpressionParser< NumericTypeTag::Character, - BaseTermsType, + TermsType, TypeResolverParameterTemplate >; using BoolMathematicalParserType = HomogenousMathematicalExpressionParser< NumericTypeTag::Bool, - BaseTermsType, + TermsType, TypeResolverParameterTemplate >; template - constexpr static const auto term = BaseTermsType::template term; + constexpr static const auto term = TermsType::template term; constexpr static const auto let_keyword = term; constexpr static const auto equal = term; constexpr static const auto identifier = term; constexpr static const auto open_parenthesis = term; constexpr static const auto close_parenthesis = term; + constexpr static const auto semi_colon = term; + constexpr static const auto constant_declaration = term; + constexpr static const auto constant = term; constexpr static const auto reduce_to = term; @@ -115,9 +155,46 @@ namespace Warp::Parsing IntegerMathematicalParserType::non_terminal_terms, FixedPointMathematicalParserType::non_terminal_terms, CharacterMathematicalParserType::non_terminal_terms, - BoolMathematicalParserType::non_terminal_terms + BoolMathematicalParserType::non_terminal_terms, + ctpg::terms( + constant_declaration, + constant + ) ); + constexpr static const auto constant_declaration_rule + = constant_declaration(let_keyword, identifier, equal) + >= [](auto let, auto name, auto equal) { + return std::string{name}; + }; + + template + constexpr static const auto constant_from_math_term() + { + using TagType = MathematicalExpressionGeneratorParameterType + ::TypeSpecificMathematicalExpressionTermTags; + constexpr const auto reduction_tag + = MathematicalExpressionGeneratorParameterType::reduce_to_term_tag; + constexpr const auto expression_term + = MathematicalExpressionGeneratorParameterType::template term; + return ctpg::rules( + constant(constant_declaration, expression_term, semi_colon) + >>=[](auto& context, auto declaration, auto expression, auto semi_colon) + { + const auto name = std::string{declaration}; + const auto constant = ConstantType{name, reduction_tag, expression}; + context[name] = constant; + } + ); + } + + consteval static const auto rules() + { + return ctpg::rules( + constant_declaration_rule, + constant_from_math_term() + ); + } }; } diff --git a/Include/Warp/Parsing/GeneralTermTags.hpp b/Include/Warp/Parsing/GeneralTermTags.hpp index 3a7d2ef..69423a0 100644 --- a/Include/Warp/Parsing/GeneralTermTags.hpp +++ b/Include/Warp/Parsing/GeneralTermTags.hpp @@ -26,8 +26,17 @@ namespace Warp::Parsing }; enum class Identifier { - Identifier, - Meta + Identifier//, + //Meta + }; + + enum class Declaration { + Constant, + SemiColon + }; + + enum class Construct { + Constant }; } #endif // WARP__PARSING__HEADER__PARSING__GENERAL__TERM__TAGS__HPP diff --git a/Include/Warp/Runtime/Compiler/Frame.hpp b/Include/Warp/Runtime/Compiler/Frame.hpp new file mode 100644 index 0000000..f4c3878 --- /dev/null +++ b/Include/Warp/Runtime/Compiler/Frame.hpp @@ -0,0 +1,12 @@ +#include +#include + +#ifndef WARP__RUNTIME__COMPILER__HEADER__RUNTIME__COMPILER__FRAME__HPP +#define WARP__RUNTIME__COMPILER__HEADER__RUNTIME__COMPILER__FRAME__HPP + +namespace Warp::Runtime::Compiler +{ +} + +#endif // WARP__RUNTIME__COMPILER__HEADER__RUNTIME__COMPILER__FRAME__HPP + diff --git a/Include/Warp/Testing/TestParser.hpp b/Include/Warp/Testing/TestParser.hpp index bfec16d..98e2f8b 100644 --- a/Include/Warp/Testing/TestParser.hpp +++ b/Include/Warp/Testing/TestParser.hpp @@ -75,6 +75,21 @@ namespace Warp::Testing ); } + template< + typename ParserParameterType, + FixedString StringParameterConstant, + auto ReduceToTagParameterConstant + > + constexpr auto runtime_parse(auto& context, bool debug = false) + { + return parser.context_parse( + context, + ((debug == true) ? ctpg::parse_options{}.set_verbose() : ctpg::parse_options{}), + ctpg::buffers::string_buffer(StringParameterConstant.string), + std::cerr + ); + } + template< typename ParserParameterType, FixedString StringParameterConstant, @@ -88,6 +103,21 @@ namespace Warp::Testing std::cerr ); } + + template< + typename ParserParameterType, + FixedString StringParameterConstant, + typename ReduceToParameterType + > + constexpr auto typed_runtime_parse(auto& context, bool debug = false) + { + return typed_parser.parse( + context, + ((debug == true) ? ctpg::parse_options{}.set_verbose() : ctpg::parse_options{}), + ctpg::buffers::string_buffer(StringParameterConstant.string), + std::cerr + ); + } } diff --git a/Test/FunctionDeclarations.cpp b/Test/FunctionDeclarations.cpp index 42648ef..25cd975 100644 --- a/Test/FunctionDeclarations.cpp +++ b/Test/FunctionDeclarations.cpp @@ -1,19 +1,51 @@ #include +#include #include #define WARP__TESTING__HEADER__TESTING__PARSE__TESTING__UTILITIES__HPP__CHECK__MACRO__REQUIRED CHECK #include using namespace Warp::Utilities; +using namespace Warp::Testing; using namespace Warp::Parsing; +using namespace Warp::Runtime::Compiler::SimpleExecutor; + +using WholeType = NumericTypeResolver::Type; +using IntegerType = NumericTypeResolver::Type; +using FixedType = NumericTypeResolver::Type; +using CharType = NumericTypeResolver::Type; +using BoolType = NumericTypeResolver::Type; + +using NumericConstantType = Constant; +using NumericContexType = std::unordered_map; // Test instantiation // template struct FunctionDeclarationParser< FunctionDeclaritionTermsType, NumericTypeResolver, - NumericTypeTag::Whole + Construct::Constant, + NumericTypeTag, + NumericConstantType + >; + +using NumericParserType = FunctionDeclarationParser< + FunctionDeclaritionTermsType, + NumericTypeResolver, + Construct::Constant, + NumericTypeTag, + NumericConstantType >; TEST_GROUP(FunctionDeclarations) {}; -TEST(FunctionDeclarations, Nothing) {}; +TEST(FunctionDeclarations, DeclareConstantFromLiteral) +{ + NumericContexType context; + auto constant = runtime_parse< + NumericParserType, + FixedString{"let TheQuestion = 42;"}, + Construct::Constant + >(context, false); + //std::cout << "Value: " << retrieve_value(constant.value).number << "\n"; + +};