Piethon  1.0
A Python-like interpreter using flex and bison.
number.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <cstdlib>
3 #include <cmath>
4 #include "exception.h"
5 #include "number.h"
6 
7 using std::ostream;
8 using std::fixed;
9 
10 ostream& operator<<(ostream& out, const Number& num) {
11  if (num.type == INT) {
12  return out << num.value.i;
13  } else if (num.type == FLOAT) {
14  return out << fixed << num.value.f;
15  } else {
16  throw UnknownType();
17  }
18 }
19 
20 bool operator==(const Number& lhs, const Number& rhs) {
21  if (lhs.type == INT && rhs.type == INT) {
22  return lhs.value.i == rhs.value.i;
23  } else if (lhs.type == FLOAT && rhs.type == FLOAT) {
24  return lhs.value.f == rhs.value.f;
25  } else if (lhs.type == INT && rhs.type == FLOAT) {
26  return lhs.value.i == rhs.value.f;
27  } else if (lhs.type == FLOAT && rhs.type == INT) {
28  return lhs.value.f == rhs.value.i;
29  } else {
30  throw UnknownType();
31  }
32 }
33 
34 bool operator!=(const Number& lhs, const Number& rhs) {
35  return !(lhs == rhs);
36 }
37 
38 bool operator<(const Number& lhs, const Number& rhs) {
39  if (lhs.type == INT && rhs.type == INT) {
40  return lhs.value.i < rhs.value.i;
41  } else if (lhs.type == FLOAT && rhs.type == FLOAT) {
42  return lhs.value.f < rhs.value.f;
43  } else if (lhs.type == INT && rhs.type == FLOAT) {
44  return lhs.value.i < rhs.value.f;
45  } else if (lhs.type == FLOAT && rhs.type == INT) {
46  return lhs.value.f < rhs.value.i;
47  } else {
48  throw UnknownType();
49  }
50 }
51 
52 bool operator>(const Number& lhs, const Number& rhs) {
53  if (lhs.type == INT && rhs.type == INT) {
54  return lhs.value.i > rhs.value.i;
55  } else if (lhs.type == FLOAT && rhs.type == FLOAT) {
56  return lhs.value.f > rhs.value.f;
57  } else if (lhs.type == INT && rhs.type == FLOAT) {
58  return lhs.value.i > rhs.value.f;
59  } else if (lhs.type == FLOAT && rhs.type == INT) {
60  return lhs.value.f > rhs.value.i;
61  } else {
62  throw UnknownType();
63  }
64 }
65 
66 bool operator<=(const Number& lhs, const Number& rhs) {
67  return !(lhs > rhs);
68 }
69 
70 bool operator>=(const Number& lhs, const Number& rhs) {
71  return !(lhs < rhs);
72 }
73 
75  type = rhs.type;
76  value = rhs.value;
77  return *this;
78 }
79 
81  *this = *this + rhs;
82  return *this;
83 }
84 
86  *this = *this - rhs;
87  return *this;
88 }
89 
91  *this = *this * rhs;
92  return *this;
93 }
94 
96  *this = *this / rhs;
97  return *this;
98 }
99 
101  *this = *this % rhs;
102  return *this;
103 }
104 
106  *this = *this ^ rhs;
107  return *this;
108 }
109 
110 Number Number::operator+(const Number& rhs) const {
111  if (type == INT && rhs.type == INT) {
112  return Number(value.i + rhs.value.i);
113  } else if (type == FLOAT && rhs.type == FLOAT) {
114  return Number(value.f + rhs.value.f);
115  } else if (type == INT && rhs.type == FLOAT) {
116  return Number(value.i + rhs.value.f);
117  } else if (type == FLOAT && rhs.type == INT) {
118  return Number(value.f + rhs.value.i);
119  } else {
120  throw UnknownType();
121  }
122 }
123 
124 Number Number::operator-(const Number& rhs) const {
125  if (type == INT && rhs.type == INT) {
126  return Number(value.i - rhs.value.i);
127  } else if (type == FLOAT && rhs.type == FLOAT) {
128  return Number(value.f - rhs.value.f);
129  } else if (type == INT && rhs.type == FLOAT) {
130  return Number(value.i - rhs.value.f);
131  } else if (type == FLOAT && rhs.type == INT) {
132  return Number(value.f - rhs.value.i);
133  } else {
134  throw UnknownType();
135  }
136 }
137 
138 Number Number::operator*(const Number& rhs) const {
139  if (type == INT && rhs.type == INT) {
140  return Number(value.i * rhs.value.i);
141  } else if (type == FLOAT && rhs.type == FLOAT) {
142  return Number(value.f * rhs.value.f);
143  } else if (type == INT && rhs.type == FLOAT) {
144  return Number(value.i * rhs.value.f);
145  } else if (type == FLOAT && rhs.type == INT) {
146  return Number(value.f * rhs.value.i);
147  } else {
148  throw UnknownType();
149  }
150 }
151 
152 Number Number::operator/(const Number& rhs) const {
153  if (type == INT && rhs.type == INT) {
154  if (rhs.value.i == 0) {
155  throw DivideByZero();
156  }
157  return Number(value.i / rhs.value.i);
158  } else if (type == FLOAT && rhs.type == FLOAT) {
159  return Number(value.f / rhs.value.f);
160  } else if (type == INT && rhs.type == FLOAT) {
161  return Number(value.i / rhs.value.f);
162  } else if (type == FLOAT && rhs.type == INT) {
163  return Number(value.f / rhs.value.i);
164  } else {
165  throw UnknownType();
166  }
167 }
168 
169 Number Number::operator%(const Number& rhs) const {
170  if (type == INT && rhs.type == INT) {
171  if (rhs.value.i == 0) {
172  throw ModByZero();
173  }
174  return Number(value.i % rhs.value.i);
175  } else {
176  throw FloatingPointMod();
177  }
178 }
179 
180 Number Number::operator^(const Number& rhs) const {
181  if (type == INT && rhs.type == INT) {
182  return Number((int) pow(value.i, rhs.value.i));
183  } else if (type == FLOAT && rhs.type == FLOAT) {
184  return Number(pow(value.f, rhs.value.f));
185  } else if (type == INT && rhs.type == FLOAT) {
186  return Number(pow(value.i, rhs.value.f));
187  } else if (type == FLOAT && rhs.type == INT) {
188  return Number(pow(value.f, rhs.value.i));
189  } else {
190  throw UnknownType();
191  }
192 }
193 
195  if (type == INT) {
196  return Number(-value.i);
197  } else if (type == FLOAT) {
198  return Number(-value.f);
199  } else {
200  throw UnknownType();
201  }
202 }
bool operator!=(const Number &lhs, const Number &rhs)
Definition: number.cpp:34
Number operator%(const Number &) const
Definition: number.cpp:169
bool operator<=(const Number &lhs, const Number &rhs)
Definition: number.cpp:66
Number operator+(const Number &) const
Definition: number.cpp:110
bool operator>=(const Number &lhs, const Number &rhs)
Definition: number.cpp:70
bool operator>(const Number &lhs, const Number &rhs)
Definition: number.cpp:52
Number & operator^=(const Number &)
Definition: number.cpp:105
Number & operator-=(const Number &)
Definition: number.cpp:85
Number operator-() const
Definition: number.cpp:194
Number operator/(const Number &) const
Definition: number.cpp:152
Number & operator*=(const Number &)
Definition: number.cpp:90
Number & operator/=(const Number &)
Definition: number.cpp:95
Number & operator+=(const Number &)
Definition: number.cpp:80
ostream & operator<<(ostream &out, const Number &num)
Definition: number.cpp:10
Definition: number.h:10
Number & operator%=(const Number &)
Definition: number.cpp:100
Number operator^(const Number &) const
Definition: number.cpp:180
bool operator<(const Number &lhs, const Number &rhs)
Definition: number.cpp:38
Number & operator=(const Number &)
Definition: number.cpp:74
double f
Definition: number.h:18
Definition: number.h:21
Definition: number.h:9
bool operator==(const Number &lhs, const Number &rhs)
Definition: number.cpp:20
int i
Definition: number.h:17
Number operator*(const Number &) const
Definition: number.cpp:138
Number()
Definition: number.h:23