Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
1 Star 0 Fork 86

mydecember/pythonvm

forked from hinus/pythonvm
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
Tags (3)
master
chapter4_final
chapter3_cmake
chapter3_parser
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
The license selected for the repository is subject to the license used by the main branch of the repository.
master
Branches (1)
Tags (3)
master
chapter4_final
chapter3_cmake
chapter3_parser
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
Tags (3)
master
chapter4_final
chapter3_cmake
chapter3_parser
pythonvm
/
src
/
object
/
klass.cpp
pythonvm
/
src
/
object
/
klass.cpp
klass.cpp 7.84 KB
Copy Edit Raw Blame History
hinus authored 2018年10月07日 22:24 +08:00 . Summary: traceback, import so
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
#include "object/klass.hpp"
#include "runtime/universe.hpp"
#include "runtime/stringTable.hpp"
#include "runtime/functionObject.hpp"
#include "runtime/interpreter.hpp"
#include "object/hiObject.hpp"
#include "object/hiInteger.hpp"
#include "object/hiDict.hpp"
#include "object/hiList.hpp"
#include "object/hiString.hpp"
#include "memory/heap.hpp"
#include "memory/oopClosure.hpp"
#define ST(x) StringTable::get_instance()->STR(x)
#define STR(x) x##_str
int Klass::compare_klass(Klass* x, Klass* y) {
if (x == y)
return 0;
if (x == IntegerKlass::get_instance())
return -1;
else if (y == IntegerKlass::get_instance())
return 1;
if (x->name()->less(y->name()) == (HiObject*)Universe::HiTrue)
return -1;
else
return 1;
}
void Klass::print(HiObject* x) {
HiObject* meth_repr = get_klass_attr(x, ST(repr));
if (meth_repr != Universe::HiNone) {
Interpreter::get_instance()->
call_virtual(meth_repr, NULL)->
print();
return;
}
printf("<object of ");
x->klass()->name()->print();
printf(", at %p>", x);
}
HiObject* Klass::create_klass(HiObject* x, HiObject* supers, HiObject* name) {
assert(x->klass() == (Klass*)DictKlass::get_instance());
assert(supers->klass() == (Klass*)ListKlass::get_instance());
assert(name->klass() == (Klass*)StringKlass::get_instance());
Klass* new_klass = new Klass();
HiDict* klass_dict = (HiDict*) x;
HiList* supers_list = (HiList*) supers;
new_klass->set_klass_dict(klass_dict);
new_klass->set_name((HiString*)name);
new_klass->set_super_list(supers_list);
new_klass->order_supers();
HiTypeObject* type_obj = new HiTypeObject();
type_obj->set_own_klass(new_klass);
return type_obj;
}
HiObject* Klass::allocate_instance(HiObject* callable, ArrayList<HiObject*>* args) {
HiObject* inst = NULL;
if (_mro->index(IntegerKlass::get_instance()->type_object()) >= 0)
inst = new HiInteger(0);
else if (_mro->index(StringKlass::get_instance()->type_object()) >= 0)
inst = new HiString("");
else if (_mro->index(ListKlass::get_instance()->type_object()) >= 0)
inst = new HiList();
else if (_mro->index(DictKlass::get_instance()->type_object()) >= 0)
inst = new HiDict();
else
inst = new HiObject();
inst->set_klass(((HiTypeObject*)callable)->own_klass());
HiObject* constructor = inst->get_klass_attr(ST(init));
if (constructor != Universe::HiNone) {
Interpreter::get_instance()->call_virtual(constructor, args);
}
return inst;
}
HiObject* Klass::add(HiObject* lhs, HiObject* rhs) {
ObjList args = new ArrayList<HiObject*>();
args->add(rhs);
return find_and_call(lhs, args, ST(add));
}
HiObject* Klass::find_and_call(HiObject* lhs, ObjList args, HiObject* func_name) {
HiObject* func = lhs->get_klass_attr(func_name);
if (func != Universe::HiNone) {
return Interpreter::get_instance()->call_virtual(func, args);
}
printf("class ");
lhs->klass()->name()->print();
printf(" Error : unsupport operation for class ");
assert(false);
return Universe::HiNone;
}
HiObject* Klass::len(HiObject* x) {
return find_and_call(x, NULL, ST(len));
}
HiObject* Klass::subscr(HiObject* x, HiObject* y) {
ObjList args = new ArrayList<HiObject*>();
args->add(y);
return find_and_call(x, args, ST(getitem));
}
void Klass::store_subscr(HiObject* x, HiObject* y, HiObject* z) {
ObjList args = new ArrayList<HiObject*>();
args->add(y);
args->add(z);
find_and_call(x, args, ST(setitem));
}
HiObject* Klass::get_klass_attr(HiObject* x, HiObject* y) {
HiObject* result = Universe::HiNone;
result = find_in_parents(x, y);
if (MethodObject::is_function(result)) {
result = new MethodObject((FunctionObject*)result, x);
}
return result;
}
// This method should only be called in LOAD_ATTR
HiObject* Klass::getattr(HiObject* x, HiObject* y) {
HiObject* func = find_in_parents(x, ST(getattr));
if (func->klass() == FunctionKlass::get_instance()) {
func = new MethodObject((FunctionObject*)func, x);
ObjList args = new ArrayList<HiObject*>();
args->add(y);
return Interpreter::get_instance()->call_virtual(func, args);
}
HiObject* result = Universe::HiNone;
if (x->obj_dict() != NULL) {
result = x->obj_dict()->get(y);
if (result != Universe::HiNone)
return result;
}
return get_klass_attr(x, y);
}
HiObject* Klass::setattr(HiObject* x, HiObject* y, HiObject* z) {
HiObject* func = x->klass()->klass_dict()->get(ST(setattr));
if (func->klass() == FunctionKlass::get_instance()) {
func = new MethodObject((FunctionObject*)func, x);
ObjList args = new ArrayList<HiObject*>();
args->add(y);
args->add(z);
return Interpreter::get_instance()->call_virtual(func, args);
}
if (x->obj_dict() == NULL) {
x->init_dict();
}
x->obj_dict()->put(y, z);
return Universe::HiNone;
}
void Klass::add_super(Klass* klass) {
if (_super == NULL)
_super = new HiList();
_super->append(klass->type_object());
}
HiTypeObject* Klass::super() {
if (_super == NULL)
return NULL;
if (_super->size() <= 0)
return NULL;
return (HiTypeObject*)_super->get(0);
}
Klass::Klass() {
Universe::klasses->add(this);
_klass_dict = NULL;
_name = NULL;
_super = NULL;
_mro = NULL;
}
void Klass::order_supers() {
if (_super == NULL)
return;
if (_mro == NULL)
_mro = new HiList();
int cur = -1;
for (int i = 0; i < _super->size(); i++) {
HiTypeObject* tp_obj = (HiTypeObject*)(_super->get(i));
Klass* k = tp_obj->own_klass();
_mro->append(tp_obj);
if (k->mro() == NULL)
continue;
for (int j = 0; j < k->mro()->size(); j++) {
HiTypeObject* tp_obj = (HiTypeObject*)(k->mro()->get(j));
int index = _mro->index(tp_obj);
if (index < cur) {
printf("Error: method resolution order conflicts.\n");
assert(false);
}
cur = index;
if (index >= 0) {
_mro->delete_index(index);
}
_mro->append(tp_obj);
}
}
if (_mro == NULL)
return;
printf("%s's mro is ", _name->value());
for (int i = 0; i < _mro->size(); i++) {
HiTypeObject* tp_obj = (HiTypeObject*)(_mro->get(i));
Klass* k = tp_obj->own_klass();
printf("%s, ", k->name()->value());
}
printf("\n");
}
HiObject* Klass::find_in_parents(HiObject* x, HiObject* y) {
HiObject* result = Universe::HiNone;
result = x->klass()->klass_dict()->get(y);
if (result != Universe::HiNone) {
return result;
}
// find attribute in all parents.
if (x->klass()->mro() == NULL)
return result;
for (int i = 0; i < x->klass()->mro()->size(); i++) {
result = ((HiTypeObject*)(x->klass()->mro()->get(i)))
->own_klass()->klass_dict()->get(y);
if (result != Universe::HiNone)
break;
}
return result;
}
void* Klass::operator new(size_t size) {
return Universe::heap->allocate_meta(size);
}
// this function will visit all children
void Klass::oops_do(OopClosure* closure, HiObject* obj) {
printf("warning: klass oops_do for ");
_name->print();
printf("\n");
}
void Klass::oops_do(OopClosure* f) {
f->do_oop((HiObject**)&_super);
f->do_oop((HiObject**)&_mro);
f->do_oop((HiObject**)&_name);
f->do_oop((HiObject**)&_klass_dict);
f->do_oop((HiObject**)&_type_object);
}
size_t Klass::size() {
return sizeof(HiObject);
}
HiObject* Klass::iter(HiObject* x) {
return find_and_call(x, NULL, ST(iter));
}
HiObject* Klass::next(HiObject* x) {
return find_and_call(x, NULL, ST(next));
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

自己动手写Python虚拟机
Cancel

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/libown/pythonvm.git
git@gitee.com:libown/pythonvm.git
libown
pythonvm
pythonvm
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /