Removed unused parameter names from many functions.
[rpn.git] / src / Commands.cpp
blobe9e30dc700fe63b3a1165ab1a06fba6cbf05043c
1 /*******************************************************************************
2 * Reverse Polish Notation calculator. *
3 * Copyright (c) 2007-2009, Samuel Fredrickson <kinghajj@gmail.com> *
4 * All rights reserved. *
5 * *
6 * Redistribution and use in source and binary forms, with or without *
7 * modification, are permitted provided that the following conditions are met: *
8 * * Redistributions of source code must retain the above copyright *
9 * notice, this list of conditions and the following disclaimer. *
10 * * Redistributions in binary form must reproduce the above copyright *
11 * notice, this list of conditions and the following disclaimer in the *
12 * documentation and/or other materials provided with the distribution. *
13 * *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS *
15 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED *
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE *
17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY *
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR *
20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER *
21 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY *
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
24 * DAMAGE. *
25 ******************************************************************************/
27 /*******************************************************************************
28 * Commands.cpp - command Calculator methods. *
29 ******************************************************************************/
31 #include "rpn.h"
32 #include <boost/foreach.hpp>
33 #include <cmath>
34 using namespace std;
35 using namespace RPN;
37 #ifndef DOXYGEN_SKIP
39 template <class T>
40 static void printAnything(T t)
42 Print(t);
45 template <class T>
46 static void printAnythingDetailed(T t)
48 PrintDetailed(t);
51 template <class T>
52 static void printAnyList(list<T>& l, void (*printer)(T))
54 Print("[ ");
55 BOOST_FOREACH(T& item, l)
57 printer(item);
58 Print(", ");
60 Print(']');
63 static void printValue(Value v)
65 printAnything(v);
68 static void printValueDetailed(Value v)
70 printAnythingDetailed(v);
73 void Calculator::dup(vector<string>&)
75 if(HasStack() && StackSize() > 0)
77 Stack& stack(CurrentStack());
78 stack.push_front(stack.front());
82 void Calculator::exit(vector<string>&)
84 status = Stop;
87 void Calculator::pop(vector<string>&)
89 if(HasStack() && StackSize() > 0)
90 CurrentStack().pop_front();
93 void Calculator::popHistory(vector<string>&)
95 if(history.size() > 1)
96 history.pop_front();
99 void Calculator::printHelp(vector<string>&)
101 printHelpItems(helpItems);
104 void Calculator::printHistory(vector<string>&)
106 Print('[');
107 BOOST_FOREACH(Stack& item, history)
109 printAnyList(item, printValue);
110 Print(", ");
112 Print("]\n");
115 void Calculator::printHistoryDetailed(vector<string>&)
117 Print('[');
118 BOOST_FOREACH(Stack& item, history)
120 printAnyList(item, printValueDetailed);
121 Print(", ");
123 Print("]\n");
126 void Calculator::printStack(vector<string>&)
128 if(HasStack())
130 printAnyList(CurrentStack(), printValue);
131 Print('\n');
135 void Calculator::printStackDetailed(vector<string>&)
137 if(HasStack())
139 printAnyList(CurrentStack(), printValueDetailed);
140 Print('\n');
144 void Calculator::printVariables(vector<string>&)
146 Print("[ ");
147 BOOST_FOREACH(Variables::value_type& v, variables)
149 Print(v.first);
150 Print(" = ");
151 Print(v.second);
152 Print(", ");
154 Print("]\n");
157 void Calculator::printVariablesDetailed(vector<string>&)
159 Print("[ ");
160 BOOST_FOREACH(Variables::value_type& v, variables)
162 Print(v.first);
163 Print(" = ");
164 PrintDetailed(v.second);
165 Print(", ");
167 Print("]\n");
170 void Calculator::printVersion(vector<string>&)
172 Port::Print("RPN %i.%i.%i.%i", VERSION_MAJOR, VERSION_MINOR,
173 VERSION_REVIS, VERSION_BUILD);
174 Port::Print(" (%s) ", GIT_BUILD);
176 #ifdef __GNUC__
177 Port::Print("(GCC %i.%i.%i on %s at %s)", __GNUC__,
178 __GNUC_MINOR__,
179 __GNUC_PATCHLEVEL__,
180 __DATE__, __TIME__);
181 #endif
182 Print("\nBy Sam Fredrickson <kinghajj@gmail.com>\n");
185 void Calculator::pushHistory(vector<string>&)
187 if(HasStack())
188 history.push_front(CurrentStack());
191 void Calculator::sqrtTop(vector<string>&)
193 if(HasStack() && StackSize() > 0)
195 Value top = CurrentStack().front();
196 CurrentStack().pop_front();
197 #ifdef RPN_DOUBLE
198 CurrentStack().push_front(sqrt(top));
199 #elif RPN_LONG_DOUBLE
200 CurrentStack().push_front(sqrtl(top));
201 #endif
205 void Calculator::swap(vector<string>&)
207 if(HasStack() && StackSize() > 1)
209 Value a, b;
210 a = CurrentStack().front();
211 CurrentStack().pop_front();
212 b = CurrentStack().front();
213 CurrentStack().pop_front();
214 CurrentStack().push_front(a);
215 CurrentStack().push_front(b);
219 void Calculator::unset(vector<string>& args)
221 variables.erase(args.front());
224 #endif
226 Commands Calculator::defaultCommands()
228 Commands ret;
230 ret["dup"] = Command(&Calculator::dup);
231 ret["help"] = Command(&Calculator::printHelp);
232 ret["pop"] = Command(&Calculator::pop);
233 ret["poph"] = Command(&Calculator::popHistory);
234 ret["ph"] = Command(&Calculator::printHistory);
235 ret["phd"] = Command(&Calculator::printHistoryDetailed);
236 ret["ps"] = Command(&Calculator::printStack);
237 ret["psd"] = Command(&Calculator::printStackDetailed);
238 ret["pv"] = Command(&Calculator::printVariables);
239 ret["pvd"] = Command(&Calculator::printVariablesDetailed);
240 ret["pushh"] = Command(&Calculator::pushHistory);
241 ret["sqrt"] = Command(&Calculator::sqrtTop);
242 ret["swap"] = Command(&Calculator::swap);
243 ret["unset"] = Command(&Calculator::unset, 1);
244 ret["ver"] = Command(&Calculator::printVersion);
245 ret["x"] = Command(&Calculator::exit);
247 return ret;