vm.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. #pragma once
  2. #include "codeobject.h"
  3. #include "iter.h"
  4. #include "error.h"
  5. #define __DEF_PY_AS_C(type, ctype, ptype) \
  6. inline ctype& Py##type##_AS_C(const PyVar& obj) { \
  7. __checkType(obj, ptype); \
  8. return std::get<ctype>(obj->_native); \
  9. }
  10. #define __DEF_PY(type, ctype, ptype) \
  11. inline PyVar Py##type(ctype value) { \
  12. return newObject(ptype, value); \
  13. }
  14. #define DEF_NATIVE(type, ctype, ptype) \
  15. __DEF_PY(type, ctype, ptype) \
  16. __DEF_PY_AS_C(type, ctype, ptype)
  17. typedef void(*PrintFn)(const char*);
  18. #define NUM_POOL_MAX_SIZE 1024
  19. class VM{
  20. private:
  21. std::stack< std::unique_ptr<Frame> > callstack;
  22. std::vector<PyObject*> numPool;
  23. PyVarDict _modules; // 3rd modules
  24. PyVar __py2py_call_signal;
  25. PyVar runFrame(Frame* frame){
  26. while(!frame->isCodeEnd()){
  27. const ByteCode& byte = frame->readCode();
  28. //printf("%s (%d) stack_size: %d\n", OP_NAMES[byte.op], byte.arg, frame->stackSize());
  29. switch (byte.op)
  30. {
  31. case OP_NO_OP: break; // do nothing
  32. case OP_LOAD_CONST: frame->push(frame->code->co_consts[byte.arg]); break;
  33. case OP_LOAD_LAMBDA: {
  34. PyVar obj = frame->code->co_consts[byte.arg];
  35. setAttr(obj, __module__, frame->_module);
  36. frame->push(obj);
  37. } break;
  38. case OP_LOAD_NAME_PTR: {
  39. frame->push(PyPointer(frame->code->co_names[byte.arg]));
  40. } break;
  41. case OP_STORE_NAME_PTR: {
  42. const auto& p = frame->code->co_names[byte.arg];
  43. p->set(this, frame, frame->popValue(this));
  44. } break;
  45. case OP_BUILD_ATTR_PTR: {
  46. const auto& attr = frame->code->co_names[byte.arg];
  47. PyVar obj = frame->popValue(this);
  48. frame->push(PyPointer(std::make_shared<AttrPointer>(obj, attr.get())));
  49. } break;
  50. case OP_BUILD_INDEX_PTR: {
  51. PyVar index = frame->popValue(this);
  52. PyVar obj = frame->popValue(this);
  53. frame->push(PyPointer(std::make_shared<IndexPointer>(obj, index)));
  54. } break;
  55. case OP_STORE_PTR: {
  56. PyVar obj = frame->popValue(this);
  57. _Pointer p = PyPointer_AS_C(frame->__pop());
  58. p->set(this, frame, obj);
  59. } break;
  60. case OP_DELETE_PTR: {
  61. _Pointer p = PyPointer_AS_C(frame->__pop());
  62. p->del(this, frame);
  63. } break;
  64. case OP_BUILD_SMART_TUPLE:
  65. {
  66. PyVarList items = frame->__popNReversed(byte.arg);
  67. bool done = false;
  68. for(auto& item : items){
  69. if(!item->isType(_tp_pointer)) {
  70. done = true;
  71. PyVarList values(items.size());
  72. for(int i=0; i<items.size(); i++){
  73. values[i] = frame->__deref_pointer(this, items[i]);
  74. }
  75. frame->push(PyTuple(values));
  76. break;
  77. }
  78. }
  79. if(done) break;
  80. std::vector<_Pointer> pointers(items.size());
  81. for(int i=0; i<items.size(); i++)
  82. pointers[i] = PyPointer_AS_C(items[i]);
  83. frame->push(PyPointer(std::make_shared<CompoundPointer>(pointers)));
  84. } break;
  85. case OP_BUILD_STRING:
  86. {
  87. PyVarList items = frame->popNValuesReversed(this, byte.arg);
  88. _StrStream ss;
  89. for(const auto& i : items) ss << PyStr_AS_C(asStr(i));
  90. frame->push(PyStr(ss));
  91. } break;
  92. case OP_LOAD_EVAL_FN: {
  93. frame->push(builtins->attribs["eval"]);
  94. } break;
  95. case OP_LIST_APPEND: {
  96. PyVar obj = frame->popValue(this);
  97. PyVar list = frame->topNValue(this, -2);
  98. fastCall(list, "append", {list, obj});
  99. } break;
  100. case OP_STORE_FUNCTION:
  101. {
  102. PyVar obj = frame->popValue(this);
  103. const _Func& fn = PyFunction_AS_C(obj);
  104. setAttr(obj, __module__, frame->_module);
  105. frame->f_globals()[fn->name] = obj;
  106. } break;
  107. case OP_BUILD_CLASS:
  108. {
  109. const _Str& clsName = frame->code->co_names[byte.arg]->name;
  110. PyVar clsBase = frame->popValue(this);
  111. if(clsBase == None) clsBase = _tp_object;
  112. __checkType(clsBase, _tp_type);
  113. PyVar cls = newUserClassType(clsName, clsBase);
  114. while(true){
  115. PyVar fn = frame->popValue(this);
  116. if(fn == None) break;
  117. const _Func& f = PyFunction_AS_C(fn);
  118. setAttr(cls, f->name, fn);
  119. }
  120. frame->f_globals()[clsName] = cls;
  121. } break;
  122. case OP_RETURN_VALUE: return frame->popValue(this);
  123. case OP_PRINT_EXPR:
  124. {
  125. const PyVar& expr = frame->topValue(this);
  126. if(expr == None) break;
  127. _stdout(PyStr_AS_C(asRepr(expr)).c_str());
  128. _stdout("\n");
  129. } break;
  130. case OP_POP_TOP: frame->popValue(this); break;
  131. case OP_BINARY_OP:
  132. {
  133. PyVar rhs = frame->popValue(this);
  134. PyVar lhs = frame->popValue(this);
  135. frame->push(fastCall(lhs, BIN_SPECIAL_METHODS[byte.arg], {lhs,rhs}));
  136. } break;
  137. case OP_COMPARE_OP:
  138. {
  139. PyVar rhs = frame->popValue(this);
  140. PyVar lhs = frame->popValue(this);
  141. // for __ne__ we use the negation of __eq__
  142. int op = byte.arg == 3 ? 2 : byte.arg;
  143. PyVar res = fastCall(lhs, CMP_SPECIAL_METHODS[op], {lhs,rhs});
  144. if(op != byte.arg) res = PyBool(!PyBool_AS_C(res));
  145. frame->push(res);
  146. } break;
  147. case OP_IS_OP:
  148. {
  149. bool ret_c = frame->popValue(this) == frame->popValue(this);
  150. if(byte.arg == 1) ret_c = !ret_c;
  151. frame->push(PyBool(ret_c));
  152. } break;
  153. case OP_CONTAINS_OP:
  154. {
  155. PyVar rhs = frame->popValue(this);
  156. PyVar lhs = frame->popValue(this);
  157. bool ret_c = PyBool_AS_C(call(rhs, __contains__, {lhs}));
  158. if(byte.arg == 1) ret_c = !ret_c;
  159. frame->push(PyBool(ret_c));
  160. } break;
  161. case OP_UNARY_NEGATIVE:
  162. {
  163. PyVar obj = frame->popValue(this);
  164. frame->push(numNegated(obj));
  165. } break;
  166. case OP_UNARY_NOT:
  167. {
  168. PyVar obj = frame->popValue(this);
  169. PyVar obj_bool = asBool(obj);
  170. frame->push(PyBool(!PyBool_AS_C(obj_bool)));
  171. } break;
  172. case OP_POP_JUMP_IF_FALSE:
  173. if(!PyBool_AS_C(asBool(frame->popValue(this)))) frame->jumpTo(byte.arg);
  174. break;
  175. case OP_LOAD_NONE: frame->push(None); break;
  176. case OP_LOAD_TRUE: frame->push(True); break;
  177. case OP_LOAD_FALSE: frame->push(False); break;
  178. case OP_ASSERT:
  179. {
  180. PyVar expr = frame->popValue(this);
  181. _assert(PyBool_AS_C(expr), "assertion failed");
  182. } break;
  183. case OP_RAISE_ERROR:
  184. {
  185. _Str msg = PyStr_AS_C(asRepr(frame->popValue(this)));
  186. _Str type = PyStr_AS_C(frame->popValue(this));
  187. _error(type, msg);
  188. } break;
  189. case OP_BUILD_LIST:
  190. {
  191. PyVarList items = frame->popNValuesReversed(this, byte.arg);
  192. frame->push(PyList(items));
  193. } break;
  194. case OP_BUILD_MAP:
  195. {
  196. PyVarList items = frame->popNValuesReversed(this, byte.arg*2);
  197. PyVar obj = call(builtins->attribs["dict"], {});
  198. for(int i=0; i<items.size(); i+=2){
  199. call(obj, __setitem__, {items[i], items[i+1]});
  200. }
  201. frame->push(obj);
  202. } break;
  203. case OP_DUP_TOP: frame->push(frame->topValue(this)); break;
  204. case OP_CALL:
  205. {
  206. PyVarList args = frame->popNValuesReversed(this, byte.arg);
  207. PyVar callable = frame->popValue(this);
  208. PyVar ret = call(callable, args, true);
  209. if(ret == __py2py_call_signal) return ret;
  210. frame->push(ret);
  211. } break;
  212. case OP_JUMP_ABSOLUTE: frame->jumpTo(byte.arg); break;
  213. case OP_GET_ITER:
  214. {
  215. PyVar obj = frame->popValue(this);
  216. PyVarOrNull iter_fn = getAttr(obj, __iter__, false);
  217. if(iter_fn != nullptr){
  218. PyVar tmp = call(iter_fn, {obj});
  219. PyIter_AS_C(tmp)->var = PyPointer_AS_C(frame->__pop());
  220. frame->push(tmp);
  221. }else{
  222. typeError("'" + obj->getTypeName() + "' object is not iterable");
  223. }
  224. } break;
  225. case OP_FOR_ITER:
  226. {
  227. const PyVar& iter = frame->topValue(this);
  228. auto& it = PyIter_AS_C(iter);
  229. if(it->hasNext()){
  230. it->var->set(this, frame, it->next());
  231. }
  232. else{
  233. frame->popValue(this);
  234. frame->jumpTo(byte.arg);
  235. }
  236. } break;
  237. case OP_JUMP_IF_FALSE_OR_POP:
  238. {
  239. const PyVar& expr = frame->topValue(this);
  240. if(asBool(expr)==False) frame->jumpTo(byte.arg);
  241. else frame->popValue(this);
  242. } break;
  243. case OP_JUMP_IF_TRUE_OR_POP:
  244. {
  245. const PyVar& expr = frame->topValue(this);
  246. if(asBool(expr)==True) frame->jumpTo(byte.arg);
  247. else frame->popValue(this);
  248. } break;
  249. case OP_BUILD_SLICE:
  250. {
  251. PyVar stop = frame->popValue(this);
  252. PyVar start = frame->popValue(this);
  253. _Slice s;
  254. if(start != None) {__checkType(start, _tp_int); s.start = PyInt_AS_C(start);}
  255. if(stop != None) {__checkType(stop, _tp_int); s.stop = PyInt_AS_C(stop);}
  256. frame->push(PySlice(s));
  257. } break;
  258. case OP_IMPORT_NAME:
  259. {
  260. const _Str& name = frame->code->co_names[byte.arg]->name;
  261. auto it = _modules.find(name);
  262. if(it == _modules.end()) _error("ImportError", "module '" + name + "' not found");
  263. else frame->push(it->second);
  264. } break;
  265. default:
  266. systemError(_Str("opcode ") + OP_NAMES[byte.op] + " is not implemented");
  267. break;
  268. }
  269. }
  270. if(frame->code->src->mode == EVAL_MODE) {
  271. if(frame->stackSize() != 1) systemError("stack size is not 1 in EVAL_MODE");
  272. return frame->popValue(this);
  273. }
  274. if(frame->stackSize() != 0) systemError("stack not empty in EXEC_MODE");
  275. return None;
  276. }
  277. public:
  278. PyVarDict _types; // builtin types
  279. PyVar None, True, False;
  280. PrintFn _stdout = [](auto s){};
  281. PrintFn _stderr = [](auto s){};
  282. PyVar builtins; // builtins module
  283. PyVar _main; // __main__ module
  284. int maxRecursionDepth = 1000;
  285. VM(){
  286. initializeBuiltinClasses();
  287. }
  288. PyVar asStr(const PyVar& obj){
  289. PyVarOrNull str_fn = getAttr(obj, __str__, false);
  290. if(str_fn != nullptr) return call(str_fn, {});
  291. return asRepr(obj);
  292. }
  293. Frame* topFrame(){
  294. if(callstack.size() == 0) UNREACHABLE();
  295. return callstack.top().get();
  296. }
  297. PyVar asRepr(const PyVar& obj){
  298. if(obj->isType(_tp_type)) return PyStr("<class '" + obj->getName() + "'>");
  299. return call(obj, __repr__, {});
  300. }
  301. PyVar asBool(const PyVar& obj){
  302. if(obj == None) return False;
  303. PyVar tp = obj->attribs[__class__];
  304. if(tp == _tp_bool) return obj;
  305. if(tp == _tp_int) return PyBool(PyInt_AS_C(obj) != 0);
  306. if(tp == _tp_float) return PyBool(PyFloat_AS_C(obj) != 0.0);
  307. PyVarOrNull len_fn = getAttr(obj, "__len__", false);
  308. if(len_fn != nullptr){
  309. PyVar ret = call(len_fn, {});
  310. return PyBool(PyInt_AS_C(ret) > 0);
  311. }
  312. return True;
  313. }
  314. PyVar fastCall(const PyVar& obj, const _Str& name, PyVarList args){
  315. PyVar cls = obj->attribs[__class__];
  316. while(cls != None) {
  317. auto it = cls->attribs.find(name);
  318. if(it != cls->attribs.end()){
  319. return call(it->second, args);
  320. }
  321. cls = cls->attribs[__base__];
  322. }
  323. attributeError(obj, name);
  324. return nullptr;
  325. }
  326. PyVar call(PyVar callable, PyVarList args, bool opCall=false){
  327. if(callable->isType(_tp_type)){
  328. auto it = callable->attribs.find(__new__);
  329. PyVar obj;
  330. if(it != callable->attribs.end()){
  331. obj = call(it->second, args);
  332. }else{
  333. obj = newObject(callable, (_Int)-1);
  334. }
  335. if(obj->isType(callable)){
  336. PyVarOrNull init_fn = getAttr(obj, __init__, false);
  337. if (init_fn != nullptr) call(init_fn, args);
  338. }
  339. return obj;
  340. }
  341. if(callable->isType(_tp_bounded_method)){
  342. auto& bm = PyBoundedMethod_AS_C(callable);
  343. args.insert(args.begin(), bm.obj);
  344. callable = bm.method;
  345. }
  346. if(callable->isType(_tp_native_function)){
  347. auto f = std::get<_CppFunc>(callable->_native);
  348. return f(this, args);
  349. } else if(callable->isType(_tp_function)){
  350. const _Func& fn = PyFunction_AS_C(callable);
  351. PyVarDict locals;
  352. int i = 0;
  353. for(const auto& name : fn->args){
  354. if(i < args.size()) {
  355. locals[name] = args[i++];
  356. }else{
  357. typeError("missing positional argument '" + name + "'");
  358. }
  359. }
  360. // handle *args
  361. if(!fn->starredArg.empty()){
  362. PyVarList vargs;
  363. while(i < args.size()) vargs.push_back(args[i++]);
  364. locals[fn->starredArg] = PyTuple(vargs);
  365. }
  366. // handle keyword arguments
  367. for(const auto& [name, value] : fn->kwArgs){
  368. if(i < args.size()) {
  369. locals[name] = args[i++];
  370. }else{
  371. locals[name] = value;
  372. }
  373. }
  374. if(i < args.size()) typeError("too many arguments");
  375. auto it_m = callable->attribs.find(__module__);
  376. PyVar _module = it_m != callable->attribs.end() ? it_m->second : topFrame()->_module;
  377. if(opCall){
  378. __pushNewFrame(fn->code, _module, locals);
  379. return __py2py_call_signal;
  380. }
  381. return _exec(fn->code, _module, locals);
  382. }
  383. typeError("'" + callable->getTypeName() + "' object is not callable");
  384. return None;
  385. }
  386. inline PyVar call(const PyVar& obj, const _Str& func, PyVarList args){
  387. return call(getAttr(obj, func), args);
  388. }
  389. PyVar exec(const _Code& code, PyVar _module=nullptr){
  390. if(_module == nullptr) _module = _main;
  391. try {
  392. return _exec(code, _module);
  393. } catch (const std::exception& e) {
  394. if(const _Error* _ = dynamic_cast<const _Error*>(&e)){
  395. _stderr(e.what());
  396. }else{
  397. auto re = RuntimeError("UnexpectedError", e.what(), _cleanErrorAndGetSnapshots());
  398. _stderr(re.what());
  399. }
  400. _stderr("\n");
  401. return None;
  402. }
  403. }
  404. Frame* __pushNewFrame(const _Code& code, PyVar _module, const PyVarDict& locals){
  405. if(code == nullptr) UNREACHABLE();
  406. if(callstack.size() > maxRecursionDepth){
  407. throw RuntimeError("RecursionError", "maximum recursion depth exceeded", _cleanErrorAndGetSnapshots());
  408. }
  409. Frame* frame = new Frame(code.get(), _module, locals);
  410. callstack.push(std::unique_ptr<Frame>(frame));
  411. return frame;
  412. }
  413. PyVar _exec(const _Code& code, PyVar _module, const PyVarDict& locals={}){
  414. Frame* frame = __pushNewFrame(code, _module, locals);
  415. Frame* frameBase = frame;
  416. PyVar ret = nullptr;
  417. while(true){
  418. ret = runFrame(frame);
  419. if(ret != __py2py_call_signal){
  420. if(frame == frameBase){ // [ frameBase<- ]
  421. break;
  422. }else{
  423. callstack.pop();
  424. frame = callstack.top().get();
  425. frame->push(ret);
  426. }
  427. }else{
  428. frame = callstack.top().get(); // [ frameBase, newFrame<- ]
  429. }
  430. }
  431. callstack.pop();
  432. return ret;
  433. }
  434. PyVar newUserClassType(_Str name, PyVar base){
  435. PyVar obj = newClassType(name, base);
  436. setAttr(obj, "__name__", PyStr(name));
  437. _types.erase(name);
  438. return obj;
  439. }
  440. PyVar newClassType(_Str name, PyVar base=nullptr) {
  441. if(base == nullptr) base = _tp_object;
  442. PyVar obj = std::make_shared<PyObject>((_Int)0);
  443. setAttr(obj, __class__, _tp_type);
  444. setAttr(obj, __base__, base);
  445. _types[name] = obj;
  446. return obj;
  447. }
  448. PyVar newObject(PyVar type, _Value _native) {
  449. __checkType(type, _tp_type);
  450. PyVar obj = std::make_shared<PyObject>(_native);
  451. setAttr(obj, __class__, type);
  452. return obj;
  453. }
  454. PyVar newNumber(PyVar type, _Value _native) {
  455. if(type != _tp_int && type != _tp_float) UNREACHABLE();
  456. PyObject* _raw = nullptr;
  457. if(numPool.size() > 0) {
  458. _raw = numPool.back();
  459. _raw->_native = _native;
  460. numPool.pop_back();
  461. }else{
  462. _raw = new PyObject(_native);
  463. }
  464. PyVar obj = PyVar(_raw, [this](PyObject* p){
  465. if(numPool.size() < NUM_POOL_MAX_SIZE) numPool.push_back(p);
  466. });
  467. setAttr(obj, __class__, type);
  468. return obj;
  469. }
  470. PyVar newModule(_Str name, bool saveToPath=true) {
  471. PyVar obj = newObject(_tp_module, (_Int)-2);
  472. setAttr(obj, "__name__", PyStr(name));
  473. if(saveToPath) _modules[name] = obj;
  474. return obj;
  475. }
  476. PyVarOrNull getAttr(const PyVar& obj, const _Str& name, bool throw_err=true) {
  477. auto it = obj->attribs.find(name);
  478. if(it != obj->attribs.end()) return it->second;
  479. PyVar cls = obj->attribs[__class__];
  480. while(cls != None) {
  481. it = cls->attribs.find(name);
  482. if(it != cls->attribs.end()){
  483. PyVar valueFromCls = it->second;
  484. if(valueFromCls->isType(_tp_function) || valueFromCls->isType(_tp_native_function)){
  485. return PyBoundedMethod({obj, valueFromCls});
  486. }else{
  487. return valueFromCls;
  488. }
  489. }
  490. cls = cls->attribs[__base__];
  491. }
  492. if(throw_err) attributeError(obj, name);
  493. return nullptr;
  494. }
  495. inline void setAttr(PyVar& obj, const _Str& name, PyVar value) {
  496. obj->attribs[name] = value;
  497. }
  498. void bindMethod(_Str typeName, _Str funcName, _CppFunc fn) {
  499. PyVar type = _types[typeName];
  500. PyVar func = PyNativeFunction(fn);
  501. setAttr(type, funcName, func);
  502. }
  503. void bindMethodMulti(std::vector<_Str> typeNames, _Str funcName, _CppFunc fn) {
  504. for(auto& typeName : typeNames){
  505. bindMethod(typeName, funcName, fn);
  506. }
  507. }
  508. void bindBuiltinFunc(_Str funcName, _CppFunc fn) {
  509. bindFunc(builtins, funcName, fn);
  510. }
  511. void bindFunc(PyVar module, _Str funcName, _CppFunc fn) {
  512. __checkType(module, _tp_module);
  513. PyVar func = PyNativeFunction(fn);
  514. setAttr(module, funcName, func);
  515. }
  516. bool isInstance(PyVar obj, PyVar type){
  517. __checkType(type, _tp_type);
  518. PyVar t = obj->attribs[__class__];
  519. while (t != None){
  520. if (t == type) return true;
  521. t = t->attribs[__base__];
  522. }
  523. return false;
  524. }
  525. inline bool isIntOrFloat(const PyVar& obj){
  526. return obj->isType(_tp_int) || obj->isType(_tp_float);
  527. }
  528. inline bool isIntOrFloat(const PyVar& obj1, const PyVar& obj2){
  529. return isIntOrFloat(obj1) && isIntOrFloat(obj2);
  530. }
  531. _Float numToFloat(const PyVar& obj){
  532. if (obj->isType(_tp_int)){
  533. return (_Float)PyInt_AS_C(obj);
  534. }else if(obj->isType(_tp_float)){
  535. return PyFloat_AS_C(obj);
  536. }
  537. UNREACHABLE();
  538. }
  539. PyVar numNegated(const PyVar& obj){
  540. if (obj->isType(_tp_int)){
  541. return PyInt(-PyInt_AS_C(obj));
  542. }else if(obj->isType(_tp_float)){
  543. return PyFloat(-PyFloat_AS_C(obj));
  544. }
  545. typeError("unsupported operand type(s) for -");
  546. return nullptr;
  547. }
  548. int normalizedIndex(int index, int size){
  549. if(index < 0) index += size;
  550. if(index < 0 || index >= size){
  551. indexError("index out of range, " + std::to_string(index) + " not in [0, " + std::to_string(size) + ")");
  552. }
  553. return index;
  554. }
  555. // for quick access
  556. PyVar _tp_object, _tp_type, _tp_int, _tp_float, _tp_bool, _tp_str;
  557. PyVar _tp_list, _tp_tuple;
  558. PyVar _tp_function, _tp_native_function, _tp_native_iterator, _tp_bounded_method;
  559. PyVar _tp_slice, _tp_range, _tp_module, _tp_pointer;
  560. __DEF_PY_AS_C(Int, _Int, _tp_int)
  561. __DEF_PY_AS_C(Float, _Float, _tp_float)
  562. DEF_NATIVE(Str, _Str, _tp_str)
  563. DEF_NATIVE(List, PyVarList, _tp_list)
  564. DEF_NATIVE(Tuple, PyVarList, _tp_tuple)
  565. DEF_NATIVE(Function, _Func, _tp_function)
  566. DEF_NATIVE(NativeFunction, _CppFunc, _tp_native_function)
  567. DEF_NATIVE(Iter, std::shared_ptr<_Iterator>, _tp_native_iterator)
  568. DEF_NATIVE(BoundedMethod, BoundedMethod, _tp_bounded_method)
  569. DEF_NATIVE(Range, _Range, _tp_range)
  570. DEF_NATIVE(Slice, _Slice, _tp_slice)
  571. DEF_NATIVE(Pointer, _Pointer, _tp_pointer)
  572. inline PyVar PyInt(_Int i) { return newNumber(_tp_int, i); }
  573. inline PyVar PyFloat(_Float f) { return newNumber(_tp_float, f); }
  574. inline bool PyBool_AS_C(PyVar obj){return obj == True;}
  575. inline PyVar PyBool(bool value){return value ? True : False;}
  576. void initializeBuiltinClasses(){
  577. _tp_object = std::make_shared<PyObject>((_Int)0);
  578. _tp_type = std::make_shared<PyObject>((_Int)0);
  579. _types["object"] = _tp_object;
  580. _types["type"] = _tp_type;
  581. _tp_bool = newClassType("bool");
  582. _tp_int = newClassType("int");
  583. _tp_float = newClassType("float");
  584. _tp_str = newClassType("str");
  585. _tp_list = newClassType("list");
  586. _tp_tuple = newClassType("tuple");
  587. _tp_slice = newClassType("slice");
  588. _tp_range = newClassType("range");
  589. _tp_module = newClassType("module");
  590. _tp_pointer = newClassType("_pointer");
  591. newClassType("NoneType");
  592. _tp_function = newClassType("function");
  593. _tp_native_function = newClassType("_native_function");
  594. _tp_native_iterator = newClassType("_native_iterator");
  595. _tp_bounded_method = newClassType("_bounded_method");
  596. this->None = newObject(_types["NoneType"], (_Int)0);
  597. this->True = newObject(_tp_bool, true);
  598. this->False = newObject(_tp_bool, false);
  599. this->builtins = newModule("builtins");
  600. this->_main = newModule("__main__", false);
  601. setAttr(_tp_type, __base__, _tp_object);
  602. setAttr(_tp_type, __class__, _tp_type);
  603. setAttr(_tp_object, __base__, None);
  604. setAttr(_tp_object, __class__, _tp_type);
  605. for (auto& [name, type] : _types) {
  606. setAttr(type, "__name__", PyStr(name));
  607. }
  608. this->__py2py_call_signal = newObject(_tp_object, (_Int)7);
  609. std::vector<_Str> publicTypes = {"type", "object", "bool", "int", "float", "str", "list", "tuple", "range"};
  610. for (auto& name : publicTypes) {
  611. setAttr(builtins, name, _types[name]);
  612. }
  613. }
  614. _Int hash(const PyVar& obj){
  615. if (obj->isType(_tp_int)) return PyInt_AS_C(obj);
  616. if (obj->isType(_tp_bool)) return PyBool_AS_C(obj) ? 1 : 0;
  617. if (obj->isType(_tp_float)){
  618. _Float val = PyFloat_AS_C(obj);
  619. return (_Int)std::hash<_Float>()(val);
  620. }
  621. if (obj->isType(_tp_str)) return PyStr_AS_C(obj).hash();
  622. if (obj->isType(_tp_type)) return (_Int)obj.get();
  623. typeError("unhashable type: " + obj->getTypeName());
  624. return 0;
  625. }
  626. /***** Error Reporter *****/
  627. private:
  628. void _error(const _Str& name, const _Str& msg){
  629. throw RuntimeError(name, msg, _cleanErrorAndGetSnapshots());
  630. }
  631. std::stack<_Str> _cleanErrorAndGetSnapshots(){
  632. std::stack<_Str> snapshots;
  633. while (!callstack.empty()){
  634. if(snapshots.size() < 8){
  635. snapshots.push(callstack.top()->errorSnapshot());
  636. }
  637. callstack.pop();
  638. }
  639. return snapshots;
  640. }
  641. public:
  642. void typeError(const _Str& msg){
  643. _error("TypeError", msg);
  644. }
  645. void systemError(const _Str& msg){
  646. _error("SystemError", msg);
  647. }
  648. void zeroDivisionError(){
  649. _error("ZeroDivisionError", "division by zero");
  650. }
  651. void indexError(const _Str& msg){
  652. _error("IndexError", msg);
  653. }
  654. void valueError(const _Str& msg){
  655. _error("ValueError", msg);
  656. }
  657. void nameError(const _Str& name){
  658. _error("NameError", "name '" + name + "' is not defined");
  659. }
  660. void attributeError(PyVar obj, const _Str& name){
  661. _error("AttributeError", "type '" + obj->getTypeName() + "' has no attribute '" + name + "'");
  662. }
  663. inline void __checkType(const PyVar& obj, const PyVar& type){
  664. if(!obj->isType(type)) typeError("expected '" + type->getName() + "', but got '" + obj->getTypeName() + "'");
  665. }
  666. inline void __checkArgSize(const PyVarList& args, int size, bool method=false){
  667. if(args.size() == size) return;
  668. if(method) typeError(args.size()>size ? "too many arguments" : "too few arguments");
  669. else typeError("expected " + std::to_string(size) + " arguments, but got " + std::to_string(args.size()));
  670. }
  671. void _assert(bool val, const _Str& msg){
  672. if (!val) _error("AssertionError", msg);
  673. }
  674. };
  675. /***** Pointers' Impl *****/
  676. PyVar NamePointer::get(VM* vm, Frame* frame) const{
  677. auto it = frame->f_locals.find(name);
  678. if(it != frame->f_locals.end()) return it->second;
  679. it = frame->f_globals().find(name);
  680. if(it != frame->f_globals().end()) return it->second;
  681. it = vm->builtins->attribs.find(name);
  682. if(it != vm->builtins->attribs.end()) return it->second;
  683. vm->nameError(name);
  684. return nullptr;
  685. }
  686. void NamePointer::set(VM* vm, Frame* frame, PyVar val) const{
  687. switch(scope) {
  688. case NAME_LOCAL: frame->f_locals[name] = val; break;
  689. case NAME_GLOBAL:
  690. {
  691. if(frame->f_locals.count(name) > 0){
  692. frame->f_locals[name] = val;
  693. }else{
  694. frame->f_globals()[name] = val;
  695. }
  696. } break;
  697. default: UNREACHABLE();
  698. }
  699. }
  700. void NamePointer::del(VM* vm, Frame* frame) const{
  701. switch(scope) {
  702. case NAME_LOCAL: {
  703. if(frame->f_locals.count(name) > 0){
  704. frame->f_locals.erase(name);
  705. }else{
  706. vm->nameError(name);
  707. }
  708. } break;
  709. case NAME_GLOBAL:
  710. {
  711. if(frame->f_locals.count(name) > 0){
  712. frame->f_locals.erase(name);
  713. }else{
  714. if(frame->f_globals().count(name) > 0){
  715. frame->f_globals().erase(name);
  716. }else{
  717. vm->nameError(name);
  718. }
  719. }
  720. } break;
  721. default: UNREACHABLE();
  722. }
  723. }
  724. PyVar AttrPointer::get(VM* vm, Frame* frame) const{
  725. return vm->getAttr(obj, attr->name);
  726. }
  727. void AttrPointer::set(VM* vm, Frame* frame, PyVar val) const{
  728. vm->setAttr(obj, attr->name, val);
  729. }
  730. void AttrPointer::del(VM* vm, Frame* frame) const{
  731. vm->typeError("cannot delete attribute");
  732. }
  733. PyVar IndexPointer::get(VM* vm, Frame* frame) const{
  734. return vm->call(obj, __getitem__, {index});
  735. }
  736. void IndexPointer::set(VM* vm, Frame* frame, PyVar val) const{
  737. vm->call(obj, __setitem__, {index, val});
  738. }
  739. void IndexPointer::del(VM* vm, Frame* frame) const{
  740. vm->call(obj, __delitem__, {index});
  741. }
  742. PyVar CompoundPointer::get(VM* vm, Frame* frame) const{
  743. PyVarList args(pointers.size());
  744. for (int i = 0; i < pointers.size(); i++) {
  745. args[i] = pointers[i]->get(vm, frame);
  746. }
  747. return vm->PyTuple(args);
  748. }
  749. void CompoundPointer::set(VM* vm, Frame* frame, PyVar val) const{
  750. if(!val->isType(vm->_tp_tuple) && !val->isType(vm->_tp_list)){
  751. vm->typeError("only tuple or list can be unpacked");
  752. }
  753. const PyVarList& args = std::get<PyVarList>(val->_native);
  754. if(args.size() > pointers.size()) vm->valueError("too many values to unpack");
  755. if(args.size() < pointers.size()) vm->valueError("not enough values to unpack");
  756. for (int i = 0; i < pointers.size(); i++) {
  757. pointers[i]->set(vm, frame, args[i]);
  758. }
  759. }
  760. void CompoundPointer::del(VM* vm, Frame* frame) const{
  761. for (auto& ptr : pointers) ptr->del(vm, frame);
  762. }
  763. /***** Frame's Impl *****/
  764. inline PyVar Frame::__deref_pointer(VM* vm, PyVar v){
  765. if(v->isType(vm->_tp_pointer)) v = vm->PyPointer_AS_C(v)->get(vm, this);
  766. return v;
  767. }
  768. /***** Iterators' Impl *****/
  769. PyVar RangeIterator::next(){
  770. PyVar val = vm->PyInt(current);
  771. current += r.step;
  772. return val;
  773. }
  774. PyVar StringIterator::next(){
  775. return vm->PyStr(str->u8_getitem(index++));
  776. }