Skip to content

Commit

Permalink
Exceeded the maximum number of rules?
Browse files Browse the repository at this point in the history
  • Loading branch information
cgbsu@github.com committed Sep 1, 2022
1 parent 05d55cc commit a5d6b2d
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 16 deletions.
1 change: 1 addition & 0 deletions Include/Warp/Common.hpp
Expand Up @@ -16,6 +16,7 @@
#include <memory>
#include <functional>
#include <utility>
#include <unordered_map>
#include <cmath>
#include <climits>

Expand Down
101 changes: 89 additions & 12 deletions Include/Warp/Parsing/FunctionDeclarations.hpp
Expand Up @@ -9,6 +9,17 @@ namespace Warp::Parsing
Constant
};

template<typename ValueParameterType, typename TypeParameterType>
struct Constant
{
using ValueType = ValueParameterType;
using TypeType = TypeParameterType;
std::string name;
TypeType type;
ValueType value;
};


using FunctionDeclaritionTermsType = MathematicalExpressionTermsType
::AddOnePriority<
TreeTerm<
Expand All @@ -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<auto> 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;
Expand All @@ -54,45 +80,59 @@ namespace Warp::Parsing
NonTerminalTypeTagParameterConstant
>::Type;

using ConstantType = Constant<SyntaxNode, TypeType>;

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<auto TermTagParameterConstant>
constexpr static const auto term = BaseTermsType::template term<TermTagParameterConstant>;
constexpr static const auto term = TermsType::template term<TermTagParameterConstant>;

constexpr static const auto let_keyword = term<Keyword::Let>;
constexpr static const auto equal = term<MultiPurposeOperator::Equal>;
constexpr static const auto identifier = term<Identifier::Identifier>;
constexpr static const auto open_parenthesis = term<Brackets::OpenParenthesis>;
constexpr static const auto close_parenthesis = term<Brackets::CloseParenthesis>;
constexpr static const auto semi_colon = term<Declaration::SemiColon>;
constexpr static const auto constant_declaration = term<Declaration::Constant>;
constexpr static const auto constant = term<Construct::Constant>;

constexpr static const auto reduce_to = term<reduce_to_tag>;

Expand All @@ -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<typename MathematicalExpressionGeneratorParameterType>
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<TagType::Expression>;
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<WholeMathematicalParserType>()
);
}
};
}

Expand Down
13 changes: 11 additions & 2 deletions Include/Warp/Parsing/GeneralTermTags.hpp
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions Include/Warp/Runtime/Compiler/Frame.hpp
@@ -0,0 +1,12 @@
#include <Warp/Common.hpp>
#include <Warp/Utilities.hpp>

#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

30 changes: 30 additions & 0 deletions Include/Warp/Testing/TestParser.hpp
Expand Up @@ -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<ParserParameterType, ReduceToTagParameterConstant>.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,
Expand All @@ -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<ParserParameterType, ReduceToParameterType>.parse(
context,
((debug == true) ? ctpg::parse_options{}.set_verbose() : ctpg::parse_options{}),
ctpg::buffers::string_buffer(StringParameterConstant.string),
std::cerr
);
}

}

Expand Down
36 changes: 34 additions & 2 deletions Test/FunctionDeclarations.cpp
@@ -1,19 +1,51 @@
#include <Warp/Parsing/FunctionDeclarations.hpp>
#include <Warp/Runtime/Compiler/SimpleExecutor.hpp>
#include <CppUTest/TestHarness.h>
#define WARP__TESTING__HEADER__TESTING__PARSE__TESTING__UTILITIES__HPP__CHECK__MACRO__REQUIRED CHECK
#include <Warp/Testing/ParseTestingUtilities.hpp>

using namespace Warp::Utilities;
using namespace Warp::Testing;
using namespace Warp::Parsing;
using namespace Warp::Runtime::Compiler::SimpleExecutor;

using WholeType = NumericTypeResolver<NumericTypeTag::Whole>::Type;
using IntegerType = NumericTypeResolver<NumericTypeTag::Integer>::Type;
using FixedType = NumericTypeResolver<NumericTypeTag::FixedPoint>::Type;
using CharType = NumericTypeResolver<NumericTypeTag::Character>::Type;
using BoolType = NumericTypeResolver<NumericTypeTag::Bool>::Type;

using NumericConstantType = Constant<SyntaxNode, NumericTypeTag>;
using NumericContexType = std::unordered_map<std::string, NumericConstantType>;

// 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<WholeType>(constant.value).number << "\n";

};

0 comments on commit a5d6b2d

Please sign in to comment.