1 /*******************************************************************************
2 * Reverse Polish Notation calculator. *
3 * Copyright (c) 2007-2009, Samuel Fredrickson <kinghajj@gmail.com> *
4 * All rights reserved. *
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. *
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 *
25 ******************************************************************************/
27 /*******************************************************************************
28 * Commands.cpp - command Calculator methods. *
29 ******************************************************************************/
32 #include <boost/foreach.hpp>
40 static void printAnything(T t
)
46 static void printAnythingDetailed(T t
)
52 static void printAnyList(list
<T
>& l
, void (*printer
)(T
))
55 BOOST_FOREACH(T
& item
, l
)
63 static void printValue(Value 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
>&)
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)
99 void Calculator::printHelp(vector
<string
>&)
101 printHelpItems(helpItems
);
104 void Calculator::printHistory(vector
<string
>&)
107 BOOST_FOREACH(Stack
& item
, history
)
109 printAnyList(item
, printValue
);
115 void Calculator::printHistoryDetailed(vector
<string
>&)
118 BOOST_FOREACH(Stack
& item
, history
)
120 printAnyList(item
, printValueDetailed
);
126 void Calculator::printStack(vector
<string
>&)
130 printAnyList(CurrentStack(), printValue
);
135 void Calculator::printStackDetailed(vector
<string
>&)
139 printAnyList(CurrentStack(), printValueDetailed
);
144 void Calculator::printVariables(vector
<string
>&)
147 BOOST_FOREACH(Variables::value_type
& v
, variables
)
157 void Calculator::printVariablesDetailed(vector
<string
>&)
160 BOOST_FOREACH(Variables::value_type
& v
, variables
)
164 PrintDetailed(v
.second
);
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
);
177 Port::Print("(GCC %i.%i.%i on %s at %s)", __GNUC__
,
182 Print("\nBy Sam Fredrickson <kinghajj@gmail.com>\n");
185 void Calculator::pushHistory(vector
<string
>&)
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();
198 CurrentStack().push_front(sqrt(top
));
199 #elif RPN_LONG_DOUBLE
200 CurrentStack().push_front(sqrtl(top
));
205 void Calculator::swap(vector
<string
>&)
207 if(HasStack() && StackSize() > 1)
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());
226 Commands
Calculator::defaultCommands()
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
);