Started working on the PSP port.
[rpn.git] / src / Operators.cpp
blob673b580131866386b2b1933258438d2e4b72ccf4
1 /*******************************************************************************
2 * Reverse Polish Notation calculator. *
3 * Copyright (c) 2007-2008, 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 * Operators.cpp - operator functions. *
29 ******************************************************************************/
31 #include "rpn.h"
32 #include <cmath>
33 using namespace std;
34 using namespace RPN;
36 #ifndef DOXYGEN_SKIP
38 static Value addition(Value a, Value b)
40 return a + b;
43 static Value subtraction(Value a, Value b)
45 return a - b;
48 static Value multiplication(Value a, Value b)
50 return a * b;
53 static Value division(Value a, Value b)
55 return a / b;
58 static Value power(Value a, Value b)
60 #ifdef RPN_DOUBLE
61 return pow(a, b);
62 #elif RPN_LONG_DOUBLE
63 return powl(a, b);
64 #endif
67 static Value equals(Value a, Value b)
69 return a == b ? 1 : 0;
72 static Value modulo(Value a, Value b)
74 return (long)a % (long)b;
77 static Value _xor(Value a, Value b)
79 return (long)a ^ (long)b;
82 static Value _and(Value a, Value b)
84 return (long)a & (long)b;
87 static Value _or(Value a, Value b)
89 return (long)a | (long)b;
92 #endif
94 Operators RPN::defaultOperators()
96 Operators ret;
98 ret["+"] = addition;
99 ret["-"] = subtraction;
100 ret["*"] = multiplication;
101 ret["/"] = division;
102 ret["**"] = power;
103 ret["="] = equals;
104 ret["%"] = modulo;
105 ret["^"] = _xor;
106 ret["&"] = _and;
107 ret["|"] = _or;
109 return ret;