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
28 Star 110 Fork 86

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 (2)
Tags (3)
master
geektime
chapter4_final
chapter3_cmake
chapter3_parser
master
Branches (2)
Tags (3)
master
geektime
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 (2)
Tags (3)
master
geektime
chapter4_final
chapter3_cmake
chapter3_parser
pythonvm
/
src
/
runtime
/
functionObject.cpp
pythonvm
/
src
/
runtime
/
functionObject.cpp
functionObject.cpp 5.76 KB
Copy Edit Raw Blame History
hinus authored 2018年10月21日 15:58 +08:00 . add iter function
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
#include "object/hiInteger.hpp"
#include "object/hiList.hpp"
#include "object/hiString.hpp"
#include "object/hiDict.hpp"
#include "runtime/universe.hpp"
#include "runtime/functionObject.hpp"
#include "memory/heap.hpp"
#include "memory/oopClosure.hpp"
FunctionKlass* FunctionKlass::instance = NULL;
FunctionKlass* FunctionKlass::get_instance() {
if (instance == NULL)
instance = new FunctionKlass();
return instance;
}
FunctionKlass::FunctionKlass() {
add_super(ObjectKlass::get_instance());
set_name(new HiString("function"));
HiTypeObject* tp_obj = new HiTypeObject();
tp_obj->set_own_klass(this);
}
void FunctionKlass::print(HiObject* obj) {
printf("<function : ");
FunctionObject* fo = static_cast<FunctionObject*>(obj);
assert(fo && fo->klass() == (Klass*) this);
fo->func_name()->print();
printf(">");
}
size_t FunctionKlass::size() {
return sizeof(FunctionObject);
}
void FunctionKlass::oops_do(OopClosure* f, HiObject* obj) {
FunctionObject* fo = (FunctionObject*)obj;
assert(fo->klass() == (Klass*)this);
f->do_oop((HiObject**)&fo->_func_code);
f->do_oop((HiObject**)&fo->_func_name);
f->do_oop((HiObject**)&fo->_globals);
f->do_oop((HiObject**)&fo->_closure);
f->do_array_list(&fo->_defaults);
}
FunctionObject::FunctionObject(HiObject* code_object) {
CodeObject* co = (CodeObject*) code_object;
_func_code = co;
_func_name = co->_co_name;
_flags = co->_flag;
_globals = NULL;
_closure = NULL;
set_klass(FunctionKlass::get_instance());
}
FunctionObject::FunctionObject(NativeFuncPointer nfp) {
_func_code = NULL;
_func_name = NULL;
_flags = 0;
_globals = NULL;
_closure = NULL;
_native_func = nfp;
set_klass(NativeFunctionKlass::get_instance());
}
void FunctionObject::set_default(ArrayList<HiObject*>* defaults) {
if (defaults == NULL) {
_defaults = NULL;
return;
}
_defaults = new ArrayList<HiObject*>(defaults->length());
for (int i = 0; i < defaults->length(); i++) {
_defaults->set(i, defaults->get(i));
}
}
/*
* Operations for native calls.
*/
NativeFunctionKlass* NativeFunctionKlass::instance = NULL;
NativeFunctionKlass* NativeFunctionKlass::get_instance() {
if (instance == NULL)
instance = new NativeFunctionKlass();
return instance;
}
NativeFunctionKlass::NativeFunctionKlass() {
add_super(FunctionKlass::get_instance());
set_name(new HiString("native function"));
HiTypeObject* tp_obj = new HiTypeObject();
tp_obj->set_own_klass(this);
}
HiObject* FunctionObject::call(ObjList args) {
return (*_native_func)(args);
}
/*
* Operations for methods
* Method is a wrapper for function.
*/
MethodKlass* MethodKlass::instance = NULL;
MethodKlass* MethodKlass::get_instance() {
if (instance == NULL)
instance = new MethodKlass();
return instance;
}
MethodKlass::MethodKlass() {
add_super(FunctionKlass::get_instance());
set_name(new HiString("method"));
HiTypeObject* tp_obj = new HiTypeObject();
tp_obj->set_own_klass(this);
}
size_t MethodKlass::size() {
return sizeof(MethodObject);
}
void MethodKlass::oops_do(OopClosure* f, HiObject* obj) {
MethodObject* mo = (MethodObject*)obj;
assert(mo->klass() == (Klass*)this);
f->do_oop((HiObject**)&mo->_owner);
f->do_oop((HiObject**)&mo->_func);
}
HiObject* len(ObjList args) {
return args->get(0)->len();
}
HiObject* string_upper(ObjList args) {
HiObject* arg0 = args->get(0);
assert(arg0->klass() == StringKlass::get_instance());
HiString* str_obj = (HiString*)arg0;
int length = str_obj->length();
if (length <= 0)
return Universe::HiNone;
char* v = new char[length];
char c;
for (int i = 0; i < length; i++) {
c = str_obj->value()[i];
if (c >= 'a' && c <= 'z')
v[i] = c - 0x20;
else
v[i] = c;
}
return new HiString(v, length);
}
HiObject* type_of(ObjList args) {
HiObject* arg0 = args->get(0);
return arg0->klass()->type_object();
}
HiObject* isinstance(ObjList args) {
HiObject* x = args->get(0);
HiObject* y = args->get(1);
assert(y && y->klass() == TypeKlass::get_instance());
Klass* k = x->klass();
if (k->type_object() == y)
return Universe::HiTrue;
for (int i = 0; i < k->mro()->size(); i++) {
if (k->mro()->get(i) == y) {
return Universe::HiTrue;
}
}
return Universe::HiFalse;
}
HiObject* builtin_super(ObjList args) {
return NULL;
}
HiObject* sysgc(ObjList args) {
Universe::heap->gc();
return Universe::HiNone;
}
bool MethodObject::is_function(HiObject *x) {
Klass* k = x->klass();
if (k == (Klass*) FunctionKlass::get_instance())
return true;
if (k->mro() == NULL)
return false;
for (int i = 0; i < k->mro()->size(); i++) {
if (k->mro()->get(i) ==
FunctionKlass::get_instance()->type_object()) {
return true;
}
}
return false;
}
bool MethodObject::is_yield_function(HiObject *x) {
Klass* k = x->klass();
if (k != (Klass*) FunctionKlass::get_instance())
return false;
FunctionObject* fo = (FunctionObject*)x;
return ((fo->flags() & FunctionObject::CO_GENERATOR) != 0);
}
size_t NativeFunctionKlass::size() {
return sizeof(FunctionObject);
}
void NativeFunctionKlass::oops_do(OopClosure* f, HiObject* obj) {
FunctionObject* fo = (FunctionObject*)obj;
assert(fo->klass() == (Klass*)this);
f->do_oop((HiObject**)&fo->_func_code);
f->do_oop((HiObject**)&fo->_func_name);
f->do_oop((HiObject**)&fo->_globals);
f->do_oop((HiObject**)&fo->_closure);
f->do_array_list(&fo->_defaults);
}
HiObject* iter(ObjList args) {
return args->get(0)->iter();
}
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虚拟机
No labels
LGPL-3.0
Use LGPL-3.0
Cancel

Releases

No release

Contributors

All

Language(Optional)

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/hinus/pythonvm.git
git@gitee.com:hinus/pythonvm.git
hinus
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 によって変換されたページ (->オリジナル) /