expression_builder.h
4.04 KB
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
#ifndef POINCARE_SIMPLIFY_EXPRESSION_BUILDER_H
#define POINCARE_SIMPLIFY_EXPRESSION_BUILDER_H
#include <poincare/expression.h>
#include "expression_match.h"
extern "C" {
#include <stdint.h>
}
namespace Poincare {
class ExpressionBuilder {
public:
static constexpr ExpressionBuilder BuildFromType(Expression::Type type, uint8_t numberOfChildren);
static constexpr ExpressionBuilder BuildFromTypeAndValue(Expression::Type type, int32_t value, uint8_t numberOfChildren);
static constexpr ExpressionBuilder Clone(uint8_t matchIndex, uint8_t numberOfChildren);
static constexpr ExpressionBuilder BringUpWildcard(uint8_t matchIndex, uint8_t numberOfChildren);
typedef Expression * (ExternalGenerator)(Expression ** parameters, int numberOfParameters);
static constexpr ExpressionBuilder CallExternalGenerator(ExternalGenerator * generator, uint8_t numberOfChildren);
public:
Expression * build(ExpressionMatch matches[]);
private:
enum class Action {
BuildFromType,
BuildFromTypeAndValue,
Clone,
BringUpWildcard,
CallExternalGenerator
};
constexpr ExpressionBuilder(Expression::Type type, uint8_t numberOfChildren);
constexpr ExpressionBuilder(Expression::Type type, int32_t integerValue, uint8_t numberOfChildren);
constexpr ExpressionBuilder(Action action, uint8_t matchIndex, uint8_t numberOfChildren);
constexpr ExpressionBuilder(ExternalGenerator * generator, uint8_t numberOfChildren);
ExpressionBuilder * child(int index);
Action m_action;
union {
// m_action == BuildFromType and BuildFromTypeAndValue
struct {
Expression::Type m_expressionType;
union {
// m_expressionType == Integer
int32_t m_integerValue;
// m_expressionType == Symbol
char m_symbolName;
};
};
// m_action == Clone or BringUpWildcard
uint8_t m_matchIndex;
// m_action == CallExternalGenerator
ExternalGenerator * m_generator;
};
uint8_t m_numberOfChildren;
};
/* Since they have to be evaluated at compile time, constexpr functions are
* implicitely defined inline. Therefore we have to provide their implementation
* in this header. */
constexpr ExpressionBuilder ExpressionBuilder::BuildFromType(
Expression::Type type,
uint8_t numberOfChildren) {
return ExpressionBuilder(type, numberOfChildren);
}
constexpr ExpressionBuilder ExpressionBuilder::BuildFromTypeAndValue(
Expression::Type type,
int32_t value,
uint8_t numberOfChildren) {
return ExpressionBuilder(type, value, numberOfChildren);
}
constexpr ExpressionBuilder ExpressionBuilder::Clone(
uint8_t matchIndex,
uint8_t numberOfChildren) {
return ExpressionBuilder(ExpressionBuilder::Action::Clone, matchIndex, numberOfChildren);
}
constexpr ExpressionBuilder ExpressionBuilder::BringUpWildcard(
uint8_t matchIndex,
uint8_t numberOfChildren) {
return ExpressionBuilder(ExpressionBuilder::Action::BringUpWildcard, matchIndex, numberOfChildren);
}
constexpr ExpressionBuilder ExpressionBuilder::CallExternalGenerator(
ExternalGenerator * generator,
uint8_t numberOfChildren) {
return ExpressionBuilder(generator, numberOfChildren);
}
constexpr ExpressionBuilder::ExpressionBuilder(Expression::Type type,
uint8_t numberOfChildren)
:
m_action(ExpressionBuilder::Action::BuildFromType),
m_expressionType(type),
m_integerValue(0),
m_numberOfChildren(numberOfChildren) {
}
constexpr ExpressionBuilder::ExpressionBuilder(Expression::Type type,
int32_t integerValue,
uint8_t numberOfChildren)
:
m_action(ExpressionBuilder::Action::BuildFromTypeAndValue),
m_expressionType(type),
m_integerValue(integerValue),
m_numberOfChildren(numberOfChildren) {
}
constexpr ExpressionBuilder::ExpressionBuilder(Action action,
uint8_t matchIndex,
uint8_t numberOfChildren)
:
m_action(action),
m_matchIndex(matchIndex),
m_numberOfChildren(numberOfChildren) {
}
constexpr ExpressionBuilder::ExpressionBuilder(ExternalGenerator * generator,
uint8_t numberOfChildren)
:
m_action(ExpressionBuilder::Action::CallExternalGenerator),
m_generator(generator),
m_numberOfChildren(numberOfChildren) {
}
}
#endif