vm.h 28 KB

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