-
Notifications
You must be signed in to change notification settings - Fork 0
/
FunctionDeclarations.hpp
202 lines (175 loc) · 5.79 KB
/
FunctionDeclarations.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <Warp/Parsing/MathematicalExpressions.hpp>
#ifndef WARP__PARSING__HEADER__PARSING__FUNCTION__DECLARATIONS__HPP
#define WARP__PARSING__HEADER__PARSING__FUNCTION__DECLARATIONS__HPP
namespace Warp::Parsing
{
enum class FunctionTags {
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<
MultiPurposeOperator::Equal,
CharTerm,
'=',
ctpg::associativity::no_assoc
>
>::AddOnePriority<
TreeTerm<
Identifier::Identifier,
RegexTerm,
FixedString{"[a-zA-Z_][a-zA-Z0-9_]+"},
FixedString{"Identifier"},
ctpg::associativity::no_assoc
>
>::AddOnePriority<
TreeTerm<
Keyword::Let,
StringTerm,
FixedString{"let"},
ctpg::associativity::no_assoc
>
>::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,
typename TypeTagParameterType,
typename ContextParamterType
>
struct FunctionDeclarationParser
{
using TypeType = TypeTagParameterType;
using ContextType = ContextParamterType;
using BaseTermsType = TermsParameterType;
constexpr static const auto reduce_to_tag = ReductionTagParameterConstant;
template<auto NonTerminalTypeTagParameterConstant>
using TypeResolverTemplate = TypeResolverParameterTemplate<
NonTerminalTypeTagParameterConstant
>::Type;
using ConstantType = Constant<SyntaxNode, TypeType>;
using TermsType = BaseTermsType::template AddOnePriority<
TypeTreeTerm<
Construct::Constant,
NonTerminalTerm,
ConstantType,
FixedString{"Constant"}
>
>;
using WholeMathematicalParserType = HomogenousMathematicalExpressionParser<
NumericTypeTag::Whole,
TermsType,
TypeResolverParameterTemplate
>;
using IntegerMathematicalParserType = HomogenousMathematicalExpressionParser<
NumericTypeTag::Integer,
TermsType,
TypeResolverParameterTemplate
>;
using FixedPointMathematicalParserType = HomogenousMathematicalExpressionParser<
NumericTypeTag::FixedPoint,
TermsType,
TypeResolverParameterTemplate
>;
using CharacterMathematicalParserType = HomogenousMathematicalExpressionParser<
NumericTypeTag::Character,
TermsType,
TypeResolverParameterTemplate
>;
using BoolMathematicalParserType = HomogenousMathematicalExpressionParser<
NumericTypeTag::Bool,
TermsType,
TypeResolverParameterTemplate
>;
template<auto 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>;
constexpr static const auto terms = concatinate_tuples(
WholeMathematicalParserType::terms,
IntegerMathematicalParserType::terms,
FixedPointMathematicalParserType::terms,
CharacterMathematicalParserType::terms,
BoolMathematicalParserType::terms,
ctpg::terms(
let_keyword,
equal,
open_parenthesis,
close_parenthesis
)
);
constexpr static const auto non_terminal_terms = concatinate_tuples(
WholeMathematicalParserType::non_terminal_terms,
IntegerMathematicalParserType::non_terminal_terms,
FixedPointMathematicalParserType::non_terminal_terms,
CharacterMathematicalParserType::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>()
);
}
};
}
#endif // WARP__PARSING__HEADER__PARSING__FUNCTION__DECLARATIONS__HPP