-
Notifications
You must be signed in to change notification settings - Fork 139
/
rpn-calculator.cpp
318 lines (303 loc) · 7.53 KB
/
rpn-calculator.cpp
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/** File: rpn-calculator.cpp
* Author: Caio Rodrigues - caiorss <DOT> rodrigues <AT> gmail <DOT> com
* Brief: Reverse Polish Notation Calculator
* Description: A simple reverse polish notation command line calculator
* implemented in modern C++.
***************************************************************************/
#include <iostream>
#include <cmath>
#include <map>
#include <deque>
#include <sstream>
#include <ostream>
#include <string>
#include <string.h>
#include <memory>
#include <functional>
#include<limits>
enum class ASTtype{
number,
function
};
struct ASTNode{
virtual auto getType() const -> ASTtype = 0;
virtual ~ASTNode() = default;
};
struct ASTNum: public ASTNode{
double num;
ASTNum(double x): num(x){}
auto getType() const -> ASTtype {
return ASTtype::number;
}
};
struct ASTFun: public ASTNode{
std::string name;
ASTFun(std::string name): name(name){}
auto getType() const -> ASTtype {
return ASTtype::function;
}
};
using AST = std::deque<std::unique_ptr<ASTNode>>;
auto parseStreamToAST(std::istream& ss) -> AST {
std::string token;
std::stringstream p;
double x;
AST ast;
while(!ss.eof()){
ss >> token;
// Clean p - stream and reset flag bits (p.clear())
p.str(""); p.clear();
// Try parse number or function
p << token;
p >> x;
if(!p.fail() && p.eof()){
// std::cout << "Number = " << x << "\n";
ast.emplace_back(new ASTNum(x));
continue;
}
if(p.fail()){
// std::cout << "Function = " << token << "\n";
ast.emplace_back(new ASTFun(token));
continue;
}
throw std::runtime_error("Error: bad input = " + token);
}
return ast;
}
void printAST(const AST& ast){
for(const auto& a: ast){
if(a->getType() == ASTtype::number){
std::cout << "push operand num = "
<< static_cast<ASTNum*>(a.get())->num << "\n";
}
if(a->getType() == ASTtype::function){
std::cout << "execute function = "
<< static_cast<ASTFun*>(a.get())->name << "\n";
}
continue;
}
}
template<class T>
class Stack{
private:
std::deque<T> _stack;
public:
struct stack_empty_error: public std::exception{
const char* what() const throw(){
return " ==> Error: stack empty." ;
}
};
void push(const T& t){
_stack.push_back(t);
}
T pop(){
if(_stack.empty())
throw stack_empty_error();
auto x = _stack.back();
_stack.pop_back();
return x;
}
T peek(){
if(_stack.empty())
throw stack_empty_error();
return _stack.back();
}
size_t size(){ return _stack.size(); }
bool empty(){ return _stack.empty(); }
void clear(){ _stack.clear(); }
void print(){
std::cout << " stack: ";
for(const auto& x: _stack)
std::cout << " " << x ;
std::cout << "\n";
}
};
/** Simple Reverse Polish Notation Evaluator */
class RPNEvaluator
{
private:
Stack<double> _stack;
std::map<std::string, std::function<void ()>> _functions;
public:
using UnaryFun = std::function<double (double)>;
using BinaryFun = std::function<double (double, double)>;
using CmdFun = std::function<void ()>;
struct evalutor_error: public std::exception{
const std::string text;
evalutor_error(const std::string& text): text(text){}
const char* what() const throw(){
return text.c_str();
}
};
RPNEvaluator()
{
// Fundamental operators
this->_functions["+"] =
[this](){
double a = this->pop();
double b = this->pop();
this->push(b + a);
};
this->_functions["-"] =
[this](){
double a = this->pop();
double b = this->pop();
this->push(b - a);
};
this->_functions["*"] =
[this](){
double a = this->pop();
double b = this->pop();
this->push(b * a);
};
this->_functions["/"] =
[this](){
double a = this->pop();
double b = this->pop();
this->push(b / a);
};
this->_functions["dup"] =
[this](){
this->push(this->peek());
};
this->_functions["drop"] =
[this](){
this->pop();
};
this->_functions["swap"] =
[this](){
double a = this->pop();
double b = this->pop();
this->push(a);
this->push(b);
};
// Fundamental transcendental functions
this->addFunction("abs", abs);
this->addFunction("sqrt", sqrt);
this->addFunction("cbrt", cbrt);
this->addFunction("exp", exp);
this->addFunction("log", log);
this->addFunction("log10", log10);
this->addFunction("sin", sin);
this->addFunction("cos", cos);
this->addFunction("tan", tan);
this->_functions["inv"] =
[this](){
this->push(1.0 / this->pop());
};
// Turn stack value into percent
this->_functions["pct"] =
[this](){
this->push(this->pop() / 100.0);
};
// Useful binary functions
this->addBinaryFunction("hypot", hypot);
// Math constants
this->addConstant("M_PI", M_PI);
this->addConstant("M_E", M_E);
this->addConstant("M_LN2", M_LN2);
this->addConstant("M_LN10", M_LN10);
// Earth's gravity
this->addConstant("M_G", 9.81);
}
auto addConstant(const std::string& name, double value) -> void{
_functions[name] = [this, value](){
this->push(value);
};
}
auto addGenFunction(const std::string& name, CmdFun fun) -> void
{
_functions[name] = fun;
}
auto addFunction(const std::string& name, UnaryFun fun) -> void
{
_functions[name] = [this, fun](){
this->push(fun(this->pop()));
};
}
auto addBinaryFunction(const std::string& name, BinaryFun fun) -> void
{
_functions[name] =
[this, name, fun](){
//std::cerr << " Running function = " << name << std::endl;
this->push(fun(this->pop(), this->pop())) ;
};
}
// Composition and delegation
auto push(double x) -> void {
_stack.push(x);
}
auto pop() -> double {
if(_stack.empty())
throw RPNEvaluator::evalutor_error("Error: attemp to pop fom empty stack.");
return _stack.pop();
}
auto clear() -> void {
_stack.clear();
}
auto show() -> void {
_stack.print();
}
auto peek() -> double {
if(_stack.empty())
throw RPNEvaluator::evalutor_error("Error: attemp to peek value fom empty stack.");
return _stack.peek();
}
auto evaluate(const AST& ast) -> void
{
for(const auto& it : ast){
if(it->getType() == ASTtype::number){
_stack.push(static_cast<ASTNum*>(it.get())->num);
continue;
}
if(it->getType() == ASTtype::function){
std::string name = static_cast<ASTFun*>(it.get())->name;
auto p = _functions.find(name);
if(p != _functions.end()){
// std::cerr << " [INFO] found function = " << name << std::endl;
//_stack.print();
p->second();
}
else
throw evalutor_error("Error: invalid function <" + name + ">");
}
}
//return this->peek();
}
auto evaluate(const std::string& expr) -> void{
if(expr == "")
return;
auto ss = std::stringstream(expr);
AST ast = parseStreamToAST(ss);
return this->evaluate(ast);
}
};
auto main() -> int {
RPNEvaluator eval{};
std::string line;
eval.addGenFunction("clear",
[&](){ eval.clear();
});
eval.addGenFunction("quit",
[&](){
std::cout << " Exiting REPL OK." << "\n";
exit(0);
});
while(true){
std::cout << " EXPR+> ";
std::getline(std::cin, line);
if(std::cin.fail()){
// Reset error flags
std::cin.clear();
}
try {
eval.evaluate(line);
//std::cout << " => " << eval.evaluate(line) << "\n";
eval.show();
} catch (const RPNEvaluator::evalutor_error& ex){
std::cerr << " " << ex.what() << "\n";
eval.show();
}
}
return 0;
}