-
Notifications
You must be signed in to change notification settings - Fork 5
/
calcParser.hpp
199 lines (167 loc) · 4.98 KB
/
calcParser.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
#ifndef CALC_PARSER_H
#define CALC_PARSER_H
#include "answerManager.hpp"
#include "calcOptr.hpp"
#include "common.hpp"
#include "str.hpp"
template <typename numT> class calcParse {
constStr input;
constStr currentPos;
numT ans;
char end;
bool running;
bool over;
using prevTokenType = enum {
ClearField,
Number,
BinaryOperator,
UnaryOperator,
OpenBracket,
CloseBracket
};
prevTokenType prevToken;
operatorManager<numT> optr;
void gotOpenBracket();
void gotCloseBracket();
void gotPlusMinus();
void gotChar();
void gotNum();
void gotOptr(const Operator &);
void gotAns();
bool gotVar();
inline bool isOpenBracket() { return *this->currentPos == '('; }
inline bool isAns() {
return *this->currentPos == 'a' && isdigit(this->currentPos[1]);
}
inline bool isCloseBracket() { return *this->currentPos == ')'; }
inline bool isPlus() { return *this->currentPos == '+'; }
inline bool isMinus() { return *this->currentPos == '-'; }
inline bool isChar() { return isalpha(*this->currentPos); }
inline bool isNum() {
return isdigit(*this->currentPos) ||
(*this->currentPos == '.' && isdigit(this->currentPos[1]));
}
public:
bool storeAnswers;
explicit calcParse(constStr inp)
: currentPos(NULL), ans(0), end(0), running(false),
over(false), prevToken(ClearField), storeAnswers(true) {
input = trimSpaces(inp);
}
calcParse(constStr inp, char e)
: currentPos(NULL), ans(0), end(e), running(false),
over(false), prevToken(ClearField), storeAnswers(true) {
input = trimSpaces(inp);
}
calcParse(str inp, str start)
: currentPos(start), ans(0), end(0), running(false),
over(false), prevToken(ClearField), storeAnswers(true) {
input = trimSpaces(inp);
}
calcParse(constStr inp, str start, char e)
: currentPos(start), ans(0), end(e), running(false),
over(false), prevToken(ClearField), storeAnswers(true) {
input = trimSpaces(inp);
}
~calcParse() {
delete input;
}
bool isParsing() { return running; }
bool isOver() { return over; }
void startParsing();
numT Ans() { return ans; }
template <typename T>
friend std::ostream& operator<<(std::ostream&, calcParse<T>&);
};
template <typename numT> void calcParse<numT>::gotOpenBracket() {
if (this->prevToken == Number)
this->optr.insertOptr(Operator::H_multiply);
this->prevToken = OpenBracket;
++this->currentPos;
Operator op(Operator::H_openBracket);
this->optr.operatorStack.push(op);
}
template <typename numT> void calcParse<numT>::gotCloseBracket() {
this->prevToken = CloseBracket;
++this->currentPos;
Operator top;
while (optr.operatorStack.pop(top) && top != Operator::H_openBracket) {
optr.calculate(top);
}
if (top != Operator::H_openBracket)
error(brktError);
}
template <typename numT> void calcParse<numT>::gotNum() {
if (this->prevToken == CloseBracket)
this->optr.insertOptr(Operator::H_multiply);
this->prevToken = Number;
numT x = 0;
if (strToNum(&this->currentPos, x, REAL) == 0)
error(parseError);
optr.insertNum(x);
}
template <typename numT> void calcParse<numT>::gotOptr(const Operator &op) {
this->prevToken = op.isUnary() ? UnaryOperator : BinaryOperator;
this->optr.insertOptr(op);
}
template <typename numT> void calcParse<numT>::gotAns() {
numT number;
answers.parseAns(this->currentPos, number);
if (this->prevToken == CloseBracket)
this->optr.insertOptr(Operator::H_multiply);
this->prevToken = Number;
optr.insertNum(number);
}
template <typename numT> void calcParse<numT>::gotChar() {
Operator op;
if (this->isAns())
this->gotAns();
else if (op.parse(this->currentPos))
this->gotOptr(op);
else
error(parseError);
}
template <typename numT> void calcParse<numT>::gotPlusMinus() {
if (this->prevToken == Number or this->prevToken == CloseBracket) {
this->prevToken = BinaryOperator;
this->optr.insertOptr(this->isPlus() ? Operator::H_plus
: Operator::H_minus);
this->currentPos++;
} else
this->gotNum();
}
template <typename numT> void calcParse<numT>::startParsing() {
this->running = true;
prevToken = ClearField;
#ifdef TESTING
if (this->input)
throw "calcParse<T>::input not set\n";
#endif
if (this->currentPos == NULL)
this->currentPos = this->input;
if (*this->currentPos == '#')
error(noError);
while (*this->currentPos && *this->currentPos != end) {
skipSpace(this->currentPos);
if (*this->currentPos == '#') {
break;
}
if (this->isOpenBracket()) {
this->gotOpenBracket();
} else if (this->isCloseBracket()) {
this->gotCloseBracket();
} else if (this->isNum()) {
this->gotNum();
} else if (this->isPlus() or this->isMinus()) {
this->gotPlusMinus();
} else
this->gotChar();
}
optr.finishCalculation();
if (optr.ans(this->ans) && this->storeAnswers == true) {
answers.push(this->ans);
}
this->running = false;
this->over = true;
}
#endif