1
1
#include " Parser.h"
2
2
3
- int Parser::opPriority (const std::string& symbol )
3
+ int Parser::opPriority (const std::string& symbols )
4
4
{
5
- return this -> operations ->getPriority (symbol );
5
+ return operations->getPriority (symbols );
6
6
}
7
7
8
- bool Parser::opAssociativity (const std::string& symbol )
8
+ bool Parser::opAssociativity (const std::string& symbols )
9
9
{
10
- return this -> operations ->getAssociativity (symbol );
10
+ return operations->getAssociativity (symbols );
11
11
}
12
12
13
- int Parser::opBinary (const std::string& symbol )
13
+ int Parser::opBinary (const std::string& symbols )
14
14
{
15
- return this -> operations ->getBinary (symbol );
15
+ return operations->getBinary (symbols );
16
16
}
17
17
18
- bool Parser::isOperator (const std::string& symbol )
18
+ bool Parser::isOperator (const std::string& symbols )
19
19
{
20
- if (symbol == " (" || symbol == " )" )
20
+ if (symbols == " (" || symbols == " )" )
21
21
return false ;
22
- bool acc = this -> opAssociativity (symbol );
23
- int bin = this -> opBinary (symbol );
24
- if ((acc == true && bin == 2 ) || (acc == false && bin == 1 ) )
22
+ bool associativity = opAssociativity (symbols );
23
+ int binary = opBinary (symbols );
24
+ if ((associativity == true && binary == 2 ) || (associativity == false && binary == 1 ) )
25
25
return true ;
26
26
return false ;
27
27
}
28
28
29
- bool Parser::isFunction (const std::string& symbol )
29
+ bool Parser::isFunction (const std::string& symbols )
30
30
{
31
- if (symbol == " (" || symbol == " )" )
31
+ if (symbols == " (" || symbols == " )" )
32
32
return false ;
33
- if (this -> opAssociativity (symbol ) == true && this -> opBinary (symbol ) == 1 )
33
+ if (opAssociativity (symbols ) == true && opBinary (symbols ) == 1 )
34
34
return true ;
35
35
return false ;
36
36
}
37
37
38
- bool Parser::isIdent (const std::string& symbol )
38
+ bool Parser::isIdent (const std::string& symbols )
39
39
{
40
- return (symbol >= " 0" && symbol <= " 9" );
40
+ return (symbols >= " 0" && symbols <= " 9" );
41
41
}
42
42
43
43
bool Parser::isIdent (char symbol)
@@ -50,17 +50,17 @@ bool Parser::isLetter(char symbol)
50
50
return (symbol >= ' a' && symbol <= ' z' );
51
51
}
52
52
53
- bool Parser::isLetter (const std::string& symbol )
53
+ bool Parser::isLetter (const std::string& symbols )
54
54
{
55
- return (symbol >= " a" && symbol <= " z" );
55
+ return (symbols >= " a" && symbols <= " z" );
56
56
}
57
57
58
58
double Parser::calculation (const std::string& symbol, double a, double b)
59
59
{
60
- return this -> operations ->calculation (symbol, a, b);
60
+ return operations->calculation (symbol, a, b);
61
61
}
62
62
63
- bool Parser::parenthesesBalance (std::stack<std::string>& operations_, std::string& output)
63
+ void Parser::parenthesesBalance (std::stack<std::string>& operations_, std::string& output)
64
64
{
65
65
std::string symbols;
66
66
while (!operations_.empty ())
@@ -73,10 +73,7 @@ bool Parser::parenthesesBalance(std::stack<std::string>& operations_, std::strin
73
73
output += symbols + " |" ;
74
74
}
75
75
if (operations_.empty () && symbols != " (" )
76
- {
77
- std::cerr << " Error: parentheses mismatched" << std::endl;
78
- return false ;
79
- }
76
+ throw std::runtime_error (" Error: parentheses mismatched" );
80
77
if (!operations_.empty ())
81
78
{
82
79
symbols = operations_.top ();
@@ -86,7 +83,6 @@ bool Parser::parenthesesBalance(std::stack<std::string>& operations_, std::strin
86
83
operations_.pop ();
87
84
}
88
85
}
89
- return true ;
90
86
}
91
87
92
88
void Parser::operationPriorityArrangment (std::stack<std::string>& operations_, std::string& current_operator, std::string& output)
@@ -106,7 +102,7 @@ void Parser::operationPriorityArrangment(std::stack<std::string>& operations_, s
106
102
operations_.push (current_operator);
107
103
}
108
104
109
- bool Parser::parse (const std::string& input, std::string& output)
105
+ void Parser::parse (const std::string& input, std::string& output)
110
106
{
111
107
std::stack<std::string> operations_;
112
108
int length = input.length ();
@@ -119,10 +115,7 @@ bool Parser::parse(const std::string& input, std::string& output)
119
115
if (current_substring == " (" )
120
116
operations_.push (current_substring);
121
117
else if (current_substring == " )" )
122
- {
123
- if (!parenthesesBalance (operations_, output))
124
- return false ;
125
- }
118
+ parenthesesBalance (operations_, output);
126
119
else if (isIdent (current_substring))
127
120
{
128
121
int j = 0 ;
@@ -162,27 +155,20 @@ bool Parser::parse(const std::string& input, std::string& output)
162
155
else if (isOperator (current_substring))
163
156
operationPriorityArrangment (operations_, current_substring, output);
164
157
else
165
- {
166
- std::cerr << " Unknown token in" << current_substring << std::endl;
167
- return false ;
168
- }
158
+ throw std::runtime_error (" Unknown operation in " + current_substring);
169
159
}
170
160
}
171
161
while (!operations_.empty ())
172
162
{
173
163
std::string current_substring = operations_.top ();
174
164
operations_.pop ();
175
165
if (current_substring == " (" || current_substring == " )" )
176
- {
177
- std::cerr << " Error: parentheses mismatched" << std::endl;
178
- return false ;
179
- }
166
+ throw std::runtime_error (" Error: parentheses mismatched" );
180
167
output += current_substring + " |" ;
181
168
}
182
- return true ;
183
169
}
184
170
185
- bool Parser::evaluate (const std::string& input)
171
+ void Parser::evaluate (const std::string& input)
186
172
{
187
173
int length = input.length ();
188
174
std::vector<std::string> operations_ (length);
@@ -229,10 +215,7 @@ bool Parser::evaluate(const std::string& input)
229
215
std::string res = " (" + std::to_string (iteration_of_calculation++) + " )" ;
230
216
std::cout << res << " = " ;
231
217
if (last_index < arguments)
232
- {
233
- std::cerr << " Error: insufficient arguments in expression" << std::endl;
234
- return false ;
235
- }
218
+ throw std::runtime_error (" Error: insufficient arguments in expression" );
236
219
std::string prev_substring = operations_[last_index - 1 ];
237
220
double prev_value = values[last_index - 1 ];
238
221
if (arguments == 1 )
@@ -248,7 +231,7 @@ bool Parser::evaluate(const std::string& input)
248
231
{
249
232
std::string prev_substring1 = operations_[last_index - 2 ];
250
233
double prev_value1 = values[last_index - 2 ];
251
- std::cout << prev_substring1 << " " << input[i] << " " ;
234
+ std::cout << prev_substring1 << " " << current_substring << " " ;
252
235
value = calculation (current_substring, prev_value1, prev_value);
253
236
std::cout << prev_substring << " = " << value << std::endl;
254
237
last_index -= 2 ;
@@ -264,11 +247,10 @@ bool Parser::evaluate(const std::string& input)
264
247
std::string final_operation = operations_[last_index - 1 ];
265
248
double final_value = values[last_index - 1 ];
266
249
last_index--;
267
- std::cerr << " Result : " << final_operation << " = " << final_value << std::endl;
268
- return true ;
250
+ std::cout << " Result : " << final_operation << " = " << final_value << std::endl;
269
251
}
270
- std::cerr << " Error: too many values entered by the user " << std::endl;
271
- return false ;
252
+ else
253
+ throw std::runtime_error ( " Error: too many values entered by the user " ) ;
272
254
}
273
255
274
256
0 commit comments