This repository has been archived by the owner on Apr 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefix.hpp
78 lines (62 loc) · 2.04 KB
/
prefix.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
#pragma once
#include <array>
#include "cexpr/string.hpp"
#include "cexpr/tokens.hpp"
#include "templ/valtype.hpp"
#include "templ/math.hpp"
#include "templ/pair.hpp"
using namespace templ;
// Macros to improve readability by aliasing the operations of generic template entities
#define NODE(Pair) (Pair.second)
#define POS(Pair) (Pair.first)
#define TYPE_WRAP(Value) (VT<Value>{})
#define TREE_EXPR(Pos, Node) (pair(TYPE_WRAP(Pos), TYPE_WRAP(Node)))
namespace cexpr
{
template <string Str, typename ValT>
class prefix
{
public:
template <typename ...ArgL>
static constexpr ValT eval(ArgL&&... args) noexcept
{
const std::array<ValT, sizeof...(args)> values{ args... };
return expression::eval(values);
}
private:
static constexpr bool isop(char ch) noexcept
{
return ch == '+' || ch == '-' || ch == '*' || ch == '/';
}
template <std::size_t Pos>
static constexpr auto parse() noexcept
{
constexpr auto token{ tokens_[Pos].cbegin() };
if constexpr (isop(*token))
{
// Parse left and right expression tree nodes
constexpr auto left{ parse<Pos + 1>() }, right{ parse<POS(left)>() };
// Next token position and expression tree node to propogate up
constexpr auto token_pos{ POS(right) };
constexpr auto tree_node{ operation(ValT{}, TYPE_WRAP(*token), NODE(left), NODE(right)) };
return TREE_EXPR(token_pos, tree_node);
}
else if constexpr (*token == 'x')
{
// Next token position and expression tree node to propogate up
constexpr auto token_pos{ Pos + 1 };
constexpr auto tree_node{ variable(TYPE_WRAP(convert<ValT>(token + 1))) };
return TREE_EXPR(token_pos, tree_node);
}
else
{
// Next token position and expression tree node to propogate up
constexpr auto token_pos{ Pos + 1 };
constexpr auto tree_node{ constant(TYPE_WRAP(convert<ValT>(token))) };
return TREE_EXPR(token_pos, tree_node);
}
}
static constexpr tokens<char, tlength(Str), tcount(Str)> tokens_{ Str };
using expression = decltype(NODE(parse<0>()));
};
} // namespace cexpr