-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathPyRangeType.cpp
80 lines (71 loc) · 2.82 KB
/
PyRangeType.cpp
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
/*
* File: PyRangeType.cpp
* Author: Kent D. Lee
* (c) 2013
* Created on February 23, 2013, 10:50 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 "PyRangeType.h"
#include "PyType.h"
#include "PyException.h"
#include "PyInt.h"
#include "PyRange.h"
PyRangeType::PyRangeType(string typeString, PyTypeId id) : PyType(typeString, id) {
}
PyRangeType::~PyRangeType() {
}
PyObject* PyRangeType::__call__(vector<PyObject*>* args) {
//cerr << "In range() with " << args->size() << " args to given to range." << endl;
int start;
int stop;
int increment;
switch (args->size()) {
case 1:
if ((*args)[0]->getType()->typeId() != PyIntType) {
throw new PyException(PYILLEGALOPERATIONEXCEPTION,"range arguments must be of int type.");
}
start = 0;
stop = ((PyInt*) ((*args)[0]))->getVal();
increment = 1;
break;
case 2:
if ((*args)[0]->getType()->typeId()!=PyIntType) {
throw new PyException(PYILLEGALOPERATIONEXCEPTION,"range arguments must be of int type.");
}
if ((*args)[1]->getType()->typeId()!=PyIntType) {
throw new PyException(PYILLEGALOPERATIONEXCEPTION,"range arguments must be of int type.");
}
start = ((PyInt*) ((*args)[1]))->getVal();
stop = ((PyInt*) ((*args)[0]))->getVal();
increment = 1;
break;
case 3:
if ((*args)[0]->getType()->typeId()!=PyIntType) {
throw new PyException(PYILLEGALOPERATIONEXCEPTION,"range arguments must be of int type.");
}
if ((*args)[1]->getType()->typeId()!=PyIntType) {
throw new PyException(PYILLEGALOPERATIONEXCEPTION,"range arguments must be of int type.");
}
if ((*args)[2]->getType()->typeId()!=PyIntType) {
throw new PyException(PYILLEGALOPERATIONEXCEPTION,"range arguments must be of int type.");
}
start = ((PyInt*) ((*args)[2]))->getVal();
stop = ((PyInt*) ((*args)[1]))->getVal();
increment = ((PyInt*) ((*args)[0]))->getVal();
break;
default:
throw new PyException(PYILLEGALOPERATIONEXCEPTION,"Incorrect number of arguments for built-in range function.");
break;
}
return new PyRange(start,stop,increment);
}