-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyInt.cpp
More file actions
310 lines (242 loc) · 9.24 KB
/
PyInt.cpp
File metadata and controls
310 lines (242 loc) · 9.24 KB
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
/*
* File: PyInt.cpp
* Author: Kent D. Lee
* (c) 2013
* Created on February 12, 2013, 10:13 PM
*
* License:
* Please read the LICENSE file in this distribution for details regarding
* the licensing of this code. This code is freely available for educational
* use. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
*
* Description:
* See the associated header file for a description of the purpose of this
* class. Implementation details are provided here. Read below for
* any specific details.
*
*/
#include "PyInt.h"
#include "PyType.h"
#include "PyStr.h"
#include "PyBool.h"
#include "PyFloat.h"
#include "PyException.h"
#include <iostream>
#include <sstream>
#include <math.h>
using namespace std;
PyInt::PyInt(int val): PyObject() {
this->val = val;
dict["__add__"] = (PyObject* (PyObject::*)(vector<PyObject*>*)) (&PyInt::__add__);
dict["__sub__"] = (PyObject* (PyObject::*)(vector<PyObject*>*)) (&PyInt::__sub__);
dict["__mul__"] = (PyObject* (PyObject::*)(vector<PyObject*>*)) (&PyInt::__mul__);
dict["__floordiv__"] = (PyObject* (PyObject::*)(vector<PyObject*>*)) (&PyInt::__floordiv__);
dict["__truediv__"] = (PyObject* (PyObject::*)(vector<PyObject*>*)) (&PyInt::__truediv__);
dict["__eq__"] = (PyObject* (PyObject::*)(vector<PyObject*>*)) (&PyInt::__eq__);
dict["__gt__"] = (PyObject* (PyObject::*)(vector<PyObject*>*)) (&PyInt::__gt__);
dict["__lt__"] = (PyObject* (PyObject::*)(vector<PyObject*>*)) (&PyInt::__lt__);
dict["__ge__"] = (PyObject* (PyObject::*)(vector<PyObject*>*)) (&PyInt::__ge__);
dict["__le__"] = (PyObject* (PyObject::*)(vector<PyObject*>*)) (&PyInt::__le__);
dict["__float__"] = (PyObject* (PyObject::*)(vector<PyObject*>*)) (&PyInt::__float__);
dict["__int__"] = (PyObject* (PyObject::*)(vector<PyObject*>*)) (&PyInt::__int__);
dict["__bool__"] = (PyObject* (PyObject::*)(vector<PyObject*>*)) (&PyInt::__bool__);
}
PyInt::PyInt(const PyInt& orig) : PyInt(orig.val) {
}
PyInt::~PyInt() {
}
string PyInt::toString() {
stringstream ss;
ss << val;
return ss.str();
}
int PyInt::getVal() {
return val;
}
PyObject* PyInt::__add__(vector<PyObject*>* args) {
PyInt* x;
PyFloat* y;
ostringstream msg;
if (args->size() != 1) {
msg << "TypeError: expected 1 arguments, got " << args->size();
throw new PyException(PYWRONGARGCOUNTEXCEPTION,msg.str());
}
PyObject* arg = (*args)[0];
switch (arg->getType()->typeId()) {
case PyIntType:
x = (PyInt*) arg;
return new PyInt(this->val + x->val);
case PyFloatType:
y = (PyFloat*) arg;
return new PyFloat(this->val + y->getVal());
default:
throw new PyException(PYILLEGALOPERATIONEXCEPTION, "Invalid types for +: int and " + arg->getType()->toString());
}
}
PyObject* PyInt::__sub__(vector<PyObject*>* args) {
PyInt* x;
PyFloat* y;
ostringstream msg;
if (args->size() != 1) {
msg << "TypeError: expected 1 arguments, got " << args->size();
throw new PyException(PYWRONGARGCOUNTEXCEPTION,msg.str());
}
PyObject* arg = (*args)[0];
switch (arg->getType()->typeId()) {
case PyIntType:
x = (PyInt*) arg;
return new PyInt(this->val - x->val);
case PyFloatType:
y = (PyFloat*) arg;
return new PyFloat(this->val - y->getVal());
default:
throw new PyException(PYILLEGALOPERATIONEXCEPTION, "Invalid types for -: int and " + arg->getType()->toString());
}
}
PyObject* PyInt::__mul__(vector<PyObject*>* args) {
PyInt* x;
PyFloat* y;
ostringstream msg;
if (args->size() != 1) {
msg << "TypeError: expected 1 arguments, got " << args->size();
throw new PyException(PYWRONGARGCOUNTEXCEPTION,msg.str());
}
PyObject* arg = (*args)[0];
switch (arg->getType()->typeId()) {
case PyIntType:
x = (PyInt*) arg;
return new PyInt(this->val * x->val);
case PyFloatType:
y = (PyFloat*) arg;
return new PyFloat(this->val * y->getVal());
default:
throw new PyException(PYILLEGALOPERATIONEXCEPTION, "Invalid types for *: int and " + arg->getType()->toString());
}
}
PyObject* PyInt::__truediv__(vector<PyObject*>* args) {
PyInt* x;
PyFloat* y;
ostringstream msg;
if (args->size() != 1) {
msg << "TypeError: expected 1 arguments, got " << args->size();
throw new PyException(PYWRONGARGCOUNTEXCEPTION,msg.str());
}
PyObject* arg = (*args)[0];
switch (arg->getType()->typeId()) {
case PyIntType:
x = (PyInt*) arg;
if (x->val == 0)
throw new PyException(PYILLEGALOPERATIONEXCEPTION, "ZeroDivisionError: integer division or modulo by zero");
return new PyFloat(this->val / x->val);
case PyFloatType:
y = (PyFloat*) arg;
if (y->getVal() == 0)
throw new PyException(PYILLEGALOPERATIONEXCEPTION, "ZeroDivisionError: integer division or modulo by zero");
return new PyFloat(this->val / y->getVal());
default:
throw new PyException(PYILLEGALOPERATIONEXCEPTION, "Invalid types for *: int and " + arg->getType()->toString());
}
}
PyObject* PyInt::__floordiv__(vector<PyObject*>* args) {
PyInt* x;
PyFloat* y;
ostringstream msg;
if (args->size() != 1) {
msg << "TypeError: expected 1 arguments, got " << args->size();
throw new PyException(PYWRONGARGCOUNTEXCEPTION,msg.str());
}
PyObject* arg = (*args)[0];
switch (arg->getType()->typeId()) {
case PyIntType:
x = (PyInt*) arg;
if (x->val == 0)
throw new PyException(PYILLEGALOPERATIONEXCEPTION, "ZeroDivisionError: integer division or modulo by zero");
return new PyInt(this->val / x->val);
case PyFloatType:
y = (PyFloat*) arg;
if (y->getVal() == 0)
throw new PyException(PYILLEGALOPERATIONEXCEPTION, "ZeroDivisionError: integer division or modulo by zero");
return new PyFloat(floor(this->val / y->getVal()));
default:
throw new PyException(PYILLEGALOPERATIONEXCEPTION, "Invalid types for *: int and " + arg->getType()->toString());
}
}
PyObject* PyInt::__eq__(vector<PyObject*>* args) {
ostringstream msg;
if (args->size() != 1) {
msg << "TypeError: expected 1 arguments, got " << args->size();
throw new PyException(PYWRONGARGCOUNTEXCEPTION,msg.str());
}
//We should check the type of args[0] before casting it.
PyInt* other = (PyInt*) (*args)[0];
return new PyBool(val == other->val);
}
PyObject* PyInt::__lt__(vector<PyObject*>* args) {
ostringstream msg;
if (args->size() != 1) {
msg << "TypeError: expected 1 arguments, got " << args->size();
throw new PyException(PYWRONGARGCOUNTEXCEPTION,msg.str());
}
//We should check the type of args[0] before casting it.
PyInt* other = (PyInt*) (*args)[0];
return new PyBool(val < other->val);
}
PyObject* PyInt::__gt__(vector<PyObject*>* args) {
ostringstream msg;
if (args->size() != 1) {
msg << "TypeError: expected 1 arguments, got " << args->size();
throw new PyException(PYWRONGARGCOUNTEXCEPTION,msg.str());
}
//We should check the type of args[0] before casting it.
PyInt* other = (PyInt*) (*args)[0];
return new PyBool(val > other->val);
}
PyObject* PyInt::__le__(vector<PyObject*>* args) {
ostringstream msg;
if (args->size() != 1) {
msg << "TypeError: expected 1 arguments, got " << args->size();
throw new PyException(PYWRONGARGCOUNTEXCEPTION,msg.str());
}
//We should check the type of args[0] before casting it.
PyInt* other = (PyInt*) (*args)[0];
return new PyBool(val <= other->val);
}
PyObject* PyInt::__ge__(vector<PyObject*>* args) {
ostringstream msg;
if (args->size() != 1) {
msg << "TypeError: expected 1 arguments, got " << args->size();
throw new PyException(PYWRONGARGCOUNTEXCEPTION,msg.str());
}
//We should check the type of args[0] before casting it.
PyInt* other = (PyInt*) (*args)[0];
return new PyBool(val >= other->val);
}
PyType* PyInt::getType() {
return PyTypes[PyIntType];
}
PyObject* PyInt::__float__(vector<PyObject*>* args) {
ostringstream msg;
if (args->size() != 0) {
msg << "TypeError: expected 0 arguments, got " << args->size();
throw new PyException(PYWRONGARGCOUNTEXCEPTION,msg.str());
}
return new PyFloat(this->getVal());
}
PyObject* PyInt::__int__(vector<PyObject*>* args) {
ostringstream msg;
if (args->size() != 0) {
msg << "TypeError: expected 0 arguments, got " << args->size();
throw new PyException(PYWRONGARGCOUNTEXCEPTION,msg.str());
}
return this;
}
PyObject* PyInt::__bool__(vector<PyObject*>* args) {
ostringstream msg;
if (args->size() != 0) {
msg << "TypeError: expected 0 arguments, got " << args->size();
throw new PyException(PYWRONGARGCOUNTEXCEPTION,msg.str());
}
if (this->getVal() == 0)
return new PyBool(false);
return new PyBool(true);
}