-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyList.cpp
More file actions
142 lines (105 loc) · 3.55 KB
/
PyList.cpp
File metadata and controls
142 lines (105 loc) · 3.55 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
/*
* File: PyList.cpp
* Author: Kent D. Lee
* (c) 2013
* Created on February 27, 2013, 9:12 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 "PyList.h"
#include "PyListIterator.h"
#include "PyException.h"
#include "PyNone.h"
#include "PyInt.h"
#include <sstream>
using namespace std;
PyList::PyList(vector<PyObject*>* lst) : PyObject() {
data = *lst;
dict["__getitem__"] = (PyObject* (PyObject::*)(vector<PyObject*>*)) (&PyList::__getitem__);
dict["__setitem__"] = (PyObject* (PyObject::*)(vector<PyObject*>*)) (&PyList::__setitem__);
dict["__len__"] = (PyObject* (PyObject::*)(vector<PyObject*>*)) (&PyList::__len__);
dict["__iter__"] = (PyObject* (PyObject::*)(vector<PyObject*>*)) (&PyList::__iter__);
dict["append"] = (PyObject* (PyObject::*)(vector<PyObject*>*)) (&PyList::append);
}
PyList::~PyList() {
}
PyType* PyList::getType() {
return PyTypes[PyListType];
}
void PyList::setVal(int index, PyObject* obj) {
data[index] = obj;
}
string PyList::toString() {
ostringstream s;
s << "[";
for (int i=0;i<data.size();i++) {
s << data[i]->toString();
if (i < data.size()-1)
s << ", ";
}
s << "]";
return s.str();
}
PyObject* PyList::getVal(int index) {
if (index >= data.size()) {
throw new PyException(PYSTOPITERATIONEXCEPTION,"Stop Iteration");
}
return data[index];
}
PyObject* PyList::__getitem__(vector<PyObject*>* args) {
ostringstream msg;
if (args->size() != 1) {
msg << "TypeError: expected 1 arguments, got " << args->size();
throw new PyException(PYWRONGARGCOUNTEXCEPTION,msg.str());
}
PyInt* intObj = (PyInt*) (*args)[0];
int index = intObj->getVal();
if (index >= data.size()) {
throw new PyException(PYILLEGALOPERATIONEXCEPTION, "Index out of range.");
}
return data[index];
}
PyObject* PyList::__setitem__(vector<PyObject*>* args) {
ostringstream msg;
if (args->size() != 2) {
msg << "TypeError: expected 2 arguments, got " << args->size();
throw new PyException(PYWRONGARGCOUNTEXCEPTION,msg.str());
}
PyInt* intObj = (PyInt*) (*args)[0];
int index = intObj->getVal();
if (index >= data.size()) {
throw new PyException(PYILLEGALOPERATIONEXCEPTION, "Index out of range.");
}
data[index] = (*args)[1];
return new PyNone();
}
PyObject* PyList::__len__(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 PyInt(data.size());
}
PyObject* PyList::__iter__(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 PyListIterator(this);
}
PyObject* PyList::append(vector<PyObject*>* args) {
PyObject* obj = (*args)[0];
data.push_back(obj);
return new PyNone();
}