pocketpy.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. #pragma once
  2. #include "vm.h"
  3. #include "compiler.h"
  4. #include "repl.h"
  5. #define BIND_NUM_ARITH_OPT(name, op) \
  6. _vm->bindMethodMulti({"int","float"}, #name, [](VM* vm, const pkpy::ArgList& args){ \
  7. if(!vm->isIntOrFloat(args[0], args[1])) \
  8. vm->typeError("unsupported operand type(s) for " #op ); \
  9. if(args[0]->isType(vm->_tp_int) && args[1]->isType(vm->_tp_int)){ \
  10. return vm->PyInt(vm->PyInt_AS_C(args[0]) op vm->PyInt_AS_C(args[1])); \
  11. }else{ \
  12. return vm->PyFloat(vm->numToFloat(args[0]) op vm->numToFloat(args[1])); \
  13. } \
  14. });
  15. #define BIND_NUM_LOGICAL_OPT(name, op, is_eq) \
  16. _vm->bindMethodMulti({"int","float"}, #name, [](VM* vm, const pkpy::ArgList& args){ \
  17. if(!vm->isIntOrFloat(args[0], args[1])){ \
  18. if constexpr(is_eq) return vm->PyBool(args[0] == args[1]); \
  19. vm->typeError("unsupported operand type(s) for " #op ); \
  20. } \
  21. return vm->PyBool(vm->numToFloat(args[0]) op vm->numToFloat(args[1])); \
  22. });
  23. void __initializeBuiltinFunctions(VM* _vm) {
  24. BIND_NUM_ARITH_OPT(__add__, +)
  25. BIND_NUM_ARITH_OPT(__sub__, -)
  26. BIND_NUM_ARITH_OPT(__mul__, *)
  27. BIND_NUM_LOGICAL_OPT(__lt__, <, false)
  28. BIND_NUM_LOGICAL_OPT(__le__, <=, false)
  29. BIND_NUM_LOGICAL_OPT(__gt__, >, false)
  30. BIND_NUM_LOGICAL_OPT(__ge__, >=, false)
  31. BIND_NUM_LOGICAL_OPT(__eq__, ==, true)
  32. #undef BIND_NUM_ARITH_OPT
  33. #undef BIND_NUM_LOGICAL_OPT
  34. _vm->bindBuiltinFunc("print", [](VM* vm, const pkpy::ArgList& args) {
  35. _StrStream ss;
  36. for(int i=0; i<args.size(); i++){
  37. ss << vm->PyStr_AS_C(vm->asStr(args[i])) << " ";
  38. }
  39. (*vm->_stdout) << ss.str() << '\n';
  40. return vm->None;
  41. });
  42. _vm->bindBuiltinFunc("super", [](VM* vm, const pkpy::ArgList& args) {
  43. vm->__checkArgSize(args, 0);
  44. auto it = vm->topFrame()->f_locals.find("self"_c);
  45. if(it == vm->topFrame()->f_locals.end()) vm->typeError("super() can only be called in a class method");
  46. return vm->newObject(vm->_tp_super, it->second);
  47. });
  48. _vm->bindBuiltinFunc("eval", [](VM* vm, const pkpy::ArgList& args) {
  49. vm->__checkArgSize(args, 1);
  50. const _Str& expr = vm->PyStr_AS_C(args[0]);
  51. _Code code = compile(vm, expr.c_str(), "<eval>", EVAL_MODE, false);
  52. return vm->_exec(code, vm->topFrame()->_module, vm->topFrame()->f_locals);
  53. });
  54. _vm->bindBuiltinFunc("isinstance", [](VM* vm, const pkpy::ArgList& args) {
  55. vm->__checkArgSize(args, 2);
  56. return vm->PyBool(vm->isInstance(args[0], args[1]));
  57. });
  58. _vm->bindBuiltinFunc("repr", [](VM* vm, const pkpy::ArgList& args) {
  59. vm->__checkArgSize(args, 1);
  60. return vm->asRepr(args[0]);
  61. });
  62. _vm->bindBuiltinFunc("hash", [](VM* vm, const pkpy::ArgList& args) {
  63. vm->__checkArgSize(args, 1);
  64. return vm->PyInt(vm->hash(args[0]));
  65. });
  66. _vm->bindBuiltinFunc("chr", [](VM* vm, const pkpy::ArgList& args) {
  67. vm->__checkArgSize(args, 1);
  68. _Int i = vm->PyInt_AS_C(args[0]);
  69. if (i < 0 || i > 128) vm->valueError("chr() arg not in range(128)");
  70. return vm->PyStr(std::string(1, (char)i));
  71. });
  72. _vm->bindBuiltinFunc("ord", [](VM* vm, const pkpy::ArgList& args) {
  73. vm->__checkArgSize(args, 1);
  74. _Str s = vm->PyStr_AS_C(args[0]);
  75. if (s.size() != 1) vm->typeError("ord() expected an ASCII character");
  76. return vm->PyInt((_Int)s[0]);
  77. });
  78. _vm->bindBuiltinFunc("globals", [](VM* vm, const pkpy::ArgList& args) {
  79. vm->__checkArgSize(args, 0);
  80. const auto& d = vm->topFrame()->f_globals();
  81. PyVar obj = vm->call(vm->builtins->attribs["dict"], {});
  82. for (const auto& [k, v] : d) {
  83. vm->call(obj, __setitem__, {vm->PyStr(k), v});
  84. }
  85. return obj;
  86. });
  87. _vm->bindBuiltinFunc("locals", [](VM* vm, const pkpy::ArgList& args) {
  88. vm->__checkArgSize(args, 0);
  89. const auto& d = vm->topFrame()->f_locals;
  90. PyVar obj = vm->call(vm->builtins->attribs["dict"], {});
  91. for (const auto& [k, v] : d) {
  92. vm->call(obj, __setitem__, {vm->PyStr(k), v});
  93. }
  94. return obj;
  95. });
  96. _vm->bindBuiltinFunc("dir", [](VM* vm, const pkpy::ArgList& args) {
  97. vm->__checkArgSize(args, 1);
  98. std::vector<_Str> names;
  99. for (auto& [k, _] : args[0]->attribs) names.push_back(k);
  100. for (auto& [k, _] : args[0]->_type->attribs) {
  101. if (k.str().find("__") == 0) continue;
  102. if (std::find(names.begin(), names.end(), k) == names.end()) names.push_back(k);
  103. }
  104. PyVarList ret;
  105. for (const auto& name : names) ret.push_back(vm->PyStr(name));
  106. return vm->PyList(ret);
  107. });
  108. _vm->bindMethod("object", "__repr__", [](VM* vm, const pkpy::ArgList& args) {
  109. PyVar _self = args[0];
  110. _Str s = "<" + _self->getTypeName() + " object at " + std::to_string((uintptr_t)_self.get()) + ">";
  111. return vm->PyStr(s);
  112. });
  113. _vm->bindMethod("type", "__new__", [](VM* vm, const pkpy::ArgList& args) {
  114. vm->__checkArgSize(args, 1);
  115. return args[0]->_type;
  116. });
  117. _vm->bindMethod("range", "__new__", [](VM* vm, const pkpy::ArgList& args) {
  118. _Range r;
  119. switch (args.size()) {
  120. case 1: r.stop = vm->PyInt_AS_C(args[0]); break;
  121. case 2: r.start = vm->PyInt_AS_C(args[0]); r.stop = vm->PyInt_AS_C(args[1]); break;
  122. case 3: r.start = vm->PyInt_AS_C(args[0]); r.stop = vm->PyInt_AS_C(args[1]); r.step = vm->PyInt_AS_C(args[2]); break;
  123. default: vm->typeError("expected 1-3 arguments, but got " + std::to_string(args.size()));
  124. }
  125. return vm->PyRange(r);
  126. });
  127. _vm->bindMethod("range", "__iter__", [](VM* vm, const pkpy::ArgList& args) {
  128. vm->__checkType(args[0], vm->_tp_range);
  129. _Iterator* iter = new RangeIterator(vm, args[0]);
  130. return vm->PyIter(pkpy::shared_ptr<_Iterator>(iter));
  131. });
  132. _vm->bindMethod("NoneType", "__repr__", [](VM* vm, const pkpy::ArgList& args) {
  133. return vm->PyStr("None");
  134. });
  135. _vm->bindMethod("NoneType", "__json__", [](VM* vm, const pkpy::ArgList& args) {
  136. return vm->PyStr("null");
  137. });
  138. _vm->bindMethod("NoneType", "__eq__", [](VM* vm, const pkpy::ArgList& args) {
  139. return vm->PyBool(args[0] == args[1]);
  140. });
  141. _vm->bindMethodMulti({"int", "float"}, "__truediv__", [](VM* vm, const pkpy::ArgList& args) {
  142. if(!vm->isIntOrFloat(args[0], args[1]))
  143. vm->typeError("unsupported operand type(s) for " "/" );
  144. _Float rhs = vm->numToFloat(args[1]);
  145. if (rhs == 0) vm->zeroDivisionError();
  146. return vm->PyFloat(vm->numToFloat(args[0]) / rhs);
  147. });
  148. _vm->bindMethodMulti({"int", "float"}, "__pow__", [](VM* vm, const pkpy::ArgList& args) {
  149. if(!vm->isIntOrFloat(args[0], args[1]))
  150. vm->typeError("unsupported operand type(s) for " "**" );
  151. if(args[0]->isType(vm->_tp_int) && args[1]->isType(vm->_tp_int)){
  152. return vm->PyInt((_Int)round(pow(vm->PyInt_AS_C(args[0]), vm->PyInt_AS_C(args[1]))));
  153. }else{
  154. return vm->PyFloat((_Float)pow(vm->numToFloat(args[0]), vm->numToFloat(args[1])));
  155. }
  156. });
  157. /************ PyInt ************/
  158. _vm->bindMethod("int", "__new__", [](VM* vm, const pkpy::ArgList& args) {
  159. if(args.size() == 0) return vm->PyInt(0);
  160. vm->__checkArgSize(args, 1);
  161. if (args[0]->isType(vm->_tp_int)) return args[0];
  162. if (args[0]->isType(vm->_tp_float)) return vm->PyInt((_Int)vm->PyFloat_AS_C(args[0]));
  163. if (args[0]->isType(vm->_tp_bool)) return vm->PyInt(vm->PyBool_AS_C(args[0]) ? 1 : 0);
  164. if (args[0]->isType(vm->_tp_str)) {
  165. const _Str& s = vm->PyStr_AS_C(args[0]);
  166. try{
  167. _Int val = std::stoll(s.str());
  168. return vm->PyInt(val);
  169. }catch(std::invalid_argument&){
  170. vm->valueError("invalid literal for int(): '" + s + "'");
  171. }
  172. }
  173. vm->typeError("int() argument must be a int, float, bool or str");
  174. return vm->None;
  175. });
  176. _vm->bindMethod("int", "__floordiv__", [](VM* vm, const pkpy::ArgList& args) {
  177. if(!args[0]->isType(vm->_tp_int) || !args[1]->isType(vm->_tp_int))
  178. vm->typeError("unsupported operand type(s) for " "//" );
  179. _Int rhs = vm->PyInt_AS_C(args[1]);
  180. if(rhs == 0) vm->zeroDivisionError();
  181. return vm->PyInt(vm->PyInt_AS_C(args[0]) / rhs);
  182. });
  183. _vm->bindMethod("int", "__mod__", [](VM* vm, const pkpy::ArgList& args) {
  184. if(!args[0]->isType(vm->_tp_int) || !args[1]->isType(vm->_tp_int))
  185. vm->typeError("unsupported operand type(s) for " "%" );
  186. _Int rhs = vm->PyInt_AS_C(args[1]);
  187. if(rhs == 0) vm->zeroDivisionError();
  188. return vm->PyInt(vm->PyInt_AS_C(args[0]) % rhs);
  189. });
  190. _vm->bindMethod("int", "__repr__", [](VM* vm, const pkpy::ArgList& args) {
  191. return vm->PyStr(std::to_string(vm->PyInt_AS_C(args[0])));
  192. });
  193. _vm->bindMethod("int", "__json__", [](VM* vm, const pkpy::ArgList& args) {
  194. return vm->PyStr(std::to_string((int)vm->PyInt_AS_C(args[0])));
  195. });
  196. #define __INT_BITWISE_OP(name,op) \
  197. _vm->bindMethod("int", #name, [](VM* vm, const pkpy::ArgList& args) { \
  198. if(!args[0]->isType(vm->_tp_int) || !args[1]->isType(vm->_tp_int)) \
  199. vm->typeError("unsupported operand type(s) for " #op ); \
  200. return vm->PyInt(vm->PyInt_AS_C(args[0]) op vm->PyInt_AS_C(args[1])); \
  201. });
  202. __INT_BITWISE_OP(__lshift__, <<)
  203. __INT_BITWISE_OP(__rshift__, >>)
  204. __INT_BITWISE_OP(__and__, &)
  205. __INT_BITWISE_OP(__or__, |)
  206. __INT_BITWISE_OP(__xor__, ^)
  207. #undef __INT_BITWISE_OP
  208. _vm->bindMethod("int", "__xor__", [](VM* vm, const pkpy::ArgList& args) {
  209. if(!args[0]->isType(vm->_tp_int) || !args[1]->isType(vm->_tp_int))
  210. vm->typeError("unsupported operand type(s) for " "^" );
  211. return vm->PyInt(vm->PyInt_AS_C(args[0]) ^ vm->PyInt_AS_C(args[1]));
  212. });
  213. /************ PyFloat ************/
  214. _vm->bindMethod("float", "__new__", [](VM* vm, const pkpy::ArgList& args) {
  215. if(args.size() == 0) return vm->PyFloat(0.0);
  216. vm->__checkArgSize(args, 1);
  217. if (args[0]->isType(vm->_tp_int)) return vm->PyFloat((_Float)vm->PyInt_AS_C(args[0]));
  218. if (args[0]->isType(vm->_tp_float)) return args[0];
  219. if (args[0]->isType(vm->_tp_bool)) return vm->PyFloat(vm->PyBool_AS_C(args[0]) ? 1.0 : 0.0);
  220. if (args[0]->isType(vm->_tp_str)) {
  221. const _Str& s = vm->PyStr_AS_C(args[0]);
  222. if(s == "inf") return vm->PyFloat(INFINITY);
  223. if(s == "-inf") return vm->PyFloat(-INFINITY);
  224. try{
  225. _Float val = std::stod(s.str());
  226. return vm->PyFloat(val);
  227. }catch(std::invalid_argument&){
  228. vm->valueError("invalid literal for float(): '" + s + "'");
  229. }
  230. }
  231. vm->typeError("float() argument must be a int, float, bool or str");
  232. return vm->None;
  233. });
  234. _vm->bindMethod("float", "__repr__", [](VM* vm, const pkpy::ArgList& args) {
  235. _Float val = vm->PyFloat_AS_C(args[0]);
  236. if(std::isinf(val) || std::isnan(val)) return vm->PyStr(std::to_string(val));
  237. _StrStream ss;
  238. ss << std::setprecision(std::numeric_limits<_Float>::max_digits10-1) << val;
  239. std::string s = ss.str();
  240. if(std::all_of(s.begin()+1, s.end(), isdigit)) s += ".0";
  241. return vm->PyStr(s);
  242. });
  243. _vm->bindMethod("float", "__json__", [](VM* vm, const pkpy::ArgList& args) {
  244. return vm->PyStr(std::to_string((float)vm->PyFloat_AS_C(args[0])));
  245. });
  246. /************ PyString ************/
  247. _vm->bindMethod("str", "__new__", [](VM* vm, const pkpy::ArgList& args) {
  248. vm->__checkArgSize(args, 1);
  249. return vm->asStr(args[0]);
  250. });
  251. _vm->bindMethod("str", "__add__", [](VM* vm, const pkpy::ArgList& args) {
  252. if(!args[0]->isType(vm->_tp_str) || !args[1]->isType(vm->_tp_str))
  253. vm->typeError("unsupported operand type(s) for " "+" );
  254. const _Str& lhs = vm->PyStr_AS_C(args[0]);
  255. const _Str& rhs = vm->PyStr_AS_C(args[1]);
  256. return vm->PyStr(lhs + rhs);
  257. });
  258. _vm->bindMethod("str", "__len__", [](VM* vm, const pkpy::ArgList& args) {
  259. const _Str& _self = vm->PyStr_AS_C(args[0]);
  260. return vm->PyInt(_self.u8_length());
  261. });
  262. _vm->bindMethod("str", "__contains__", [](VM* vm, const pkpy::ArgList& args) {
  263. const _Str& _self = vm->PyStr_AS_C(args[0]);
  264. const _Str& _other = vm->PyStr_AS_C(args[1]);
  265. return vm->PyBool(_self.str().find(_other.str()) != _Str::npos);
  266. });
  267. _vm->bindMethod("str", "__str__", [](VM* vm, const pkpy::ArgList& args) {
  268. return args[0]; // str is immutable
  269. });
  270. _vm->bindMethod("str", "__iter__", [](VM* vm, const pkpy::ArgList& args) {
  271. _Iterator* iter = new StringIterator(vm, args[0]);
  272. return vm->PyIter(pkpy::shared_ptr<_Iterator>(iter));
  273. });
  274. _vm->bindMethod("str", "__repr__", [](VM* vm, const pkpy::ArgList& args) {
  275. const _Str& _self = vm->PyStr_AS_C(args[0]);
  276. return vm->PyStr(_self.__escape(true));
  277. });
  278. _vm->bindMethod("str", "__json__", [](VM* vm, const pkpy::ArgList& args) {
  279. const _Str& _self = vm->PyStr_AS_C(args[0]);
  280. return vm->PyStr(_self.__escape(false));
  281. });
  282. _vm->bindMethod("str", "__eq__", [](VM* vm, const pkpy::ArgList& args) {
  283. if(args[0]->isType(vm->_tp_str) && args[1]->isType(vm->_tp_str))
  284. return vm->PyBool(vm->PyStr_AS_C(args[0]) == vm->PyStr_AS_C(args[1]));
  285. return vm->PyBool(args[0] == args[1]); // fallback
  286. });
  287. _vm->bindMethod("str", "__getitem__", [](VM* vm, const pkpy::ArgList& args) {
  288. const _Str& _self (vm->PyStr_AS_C(args[0]));
  289. if(args[1]->isType(vm->_tp_slice)){
  290. _Slice s = vm->PySlice_AS_C(args[1]);
  291. s.normalize(_self.u8_length());
  292. return vm->PyStr(_self.u8_substr(s.start, s.stop));
  293. }
  294. int _index = (int)vm->PyInt_AS_C(args[1]);
  295. _index = vm->normalizedIndex(_index, _self.u8_length());
  296. return vm->PyStr(_self.u8_getitem(_index));
  297. });
  298. _vm->bindMethod("str", "__gt__", [](VM* vm, const pkpy::ArgList& args) {
  299. const _Str& _self (vm->PyStr_AS_C(args[0]));
  300. const _Str& _obj (vm->PyStr_AS_C(args[1]));
  301. return vm->PyBool(_self > _obj);
  302. });
  303. _vm->bindMethod("str", "__lt__", [](VM* vm, const pkpy::ArgList& args) {
  304. const _Str& _self (vm->PyStr_AS_C(args[0]));
  305. const _Str& _obj (vm->PyStr_AS_C(args[1]));
  306. return vm->PyBool(_self < _obj);
  307. });
  308. _vm->bindMethod("str", "upper", [](VM* vm, const pkpy::ArgList& args) {
  309. vm->__checkArgSize(args, 1, true);
  310. const _Str& _self (vm->PyStr_AS_C(args[0]));
  311. _StrStream ss;
  312. for(auto c : _self.str()) ss << (char)toupper(c);
  313. return vm->PyStr(ss.str());
  314. });
  315. _vm->bindMethod("str", "lower", [](VM* vm, const pkpy::ArgList& args) {
  316. vm->__checkArgSize(args, 1, true);
  317. const _Str& _self (vm->PyStr_AS_C(args[0]));
  318. _StrStream ss;
  319. for(auto c : _self.str()) ss << (char)tolower(c);
  320. return vm->PyStr(ss.str());
  321. });
  322. _vm->bindMethod("str", "replace", [](VM* vm, const pkpy::ArgList& args) {
  323. vm->__checkArgSize(args, 3, true);
  324. const _Str& _self = vm->PyStr_AS_C(args[0]);
  325. const _Str& _old = vm->PyStr_AS_C(args[1]);
  326. const _Str& _new = vm->PyStr_AS_C(args[2]);
  327. std::string _copy = _self.str();
  328. // replace all occurences of _old with _new in _copy
  329. size_t pos = 0;
  330. while ((pos = _copy.find(_old.str(), pos)) != std::string::npos) {
  331. _copy.replace(pos, _old.str().length(), _new.str());
  332. pos += _new.str().length();
  333. }
  334. return vm->PyStr(_copy);
  335. });
  336. _vm->bindMethod("str", "startswith", [](VM* vm, const pkpy::ArgList& args) {
  337. vm->__checkArgSize(args, 2, true);
  338. const _Str& _self = vm->PyStr_AS_C(args[0]);
  339. const _Str& _prefix = vm->PyStr_AS_C(args[1]);
  340. return vm->PyBool(_self.str().find(_prefix.str()) == 0);
  341. });
  342. _vm->bindMethod("str", "endswith", [](VM* vm, const pkpy::ArgList& args) {
  343. vm->__checkArgSize(args, 2, true);
  344. const _Str& _self = vm->PyStr_AS_C(args[0]);
  345. const _Str& _suffix = vm->PyStr_AS_C(args[1]);
  346. return vm->PyBool(_self.str().rfind(_suffix.str()) == _self.str().length() - _suffix.str().length());
  347. });
  348. _vm->bindMethod("str", "join", [](VM* vm, const pkpy::ArgList& args) {
  349. vm->__checkArgSize(args, 2, true);
  350. const _Str& _self = vm->PyStr_AS_C(args[0]);
  351. PyVarList* _list;
  352. if(args[1]->isType(vm->_tp_list)){
  353. _list = &vm->PyList_AS_C(args[1]);
  354. }else if(args[1]->isType(vm->_tp_tuple)){
  355. _list = &vm->PyTuple_AS_C(args[1]);
  356. }else{
  357. vm->typeError("can only join a list or tuple");
  358. }
  359. _StrStream ss;
  360. for(int i = 0; i < _list->size(); i++){
  361. if(i > 0) ss << _self;
  362. ss << vm->PyStr_AS_C(vm->asStr(_list->operator[](i)));
  363. }
  364. return vm->PyStr(ss.str());
  365. });
  366. /************ PyList ************/
  367. _vm->bindMethod("list", "__iter__", [](VM* vm, const pkpy::ArgList& args) {
  368. vm->__checkType(args[0], vm->_tp_list);
  369. _Iterator* iter = new VectorIterator(vm, args[0]);
  370. return vm->PyIter(pkpy::shared_ptr<_Iterator>(iter));
  371. });
  372. _vm->bindMethod("list", "append", [](VM* vm, const pkpy::ArgList& args) {
  373. vm->__checkArgSize(args, 2, true);
  374. PyVarList& _self = vm->PyList_AS_C(args[0]);
  375. _self.push_back(args[1]);
  376. return vm->None;
  377. });
  378. _vm->bindMethod("list", "insert", [](VM* vm, const pkpy::ArgList& args) {
  379. vm->__checkArgSize(args, 3, true);
  380. PyVarList& _self = vm->PyList_AS_C(args[0]);
  381. int _index = (int)vm->PyInt_AS_C(args[1]);
  382. if(_index < 0) _index += _self.size();
  383. if(_index < 0) _index = 0;
  384. if(_index > _self.size()) _index = _self.size();
  385. _self.insert(_self.begin() + _index, args[2]);
  386. return vm->None;
  387. });
  388. _vm->bindMethod("list", "clear", [](VM* vm, const pkpy::ArgList& args) {
  389. vm->__checkArgSize(args, 1, true);
  390. vm->PyList_AS_C(args[0]).clear();
  391. return vm->None;
  392. });
  393. _vm->bindMethod("list", "copy", [](VM* vm, const pkpy::ArgList& args) {
  394. vm->__checkArgSize(args, 1, true);
  395. return vm->PyList(vm->PyList_AS_C(args[0]));
  396. });
  397. _vm->bindMethod("list", "pop", [](VM* vm, const pkpy::ArgList& args) {
  398. vm->__checkArgSize(args, 1, true);
  399. PyVarList& _self = vm->PyList_AS_C(args[0]);
  400. if(_self.empty()) vm->indexError("pop from empty list");
  401. PyVar ret = _self.back();
  402. _self.pop_back();
  403. return ret;
  404. });
  405. _vm->bindMethod("list", "__add__", [](VM* vm, const pkpy::ArgList& args) {
  406. const PyVarList& _self = vm->PyList_AS_C(args[0]);
  407. const PyVarList& _obj = vm->PyList_AS_C(args[1]);
  408. PyVarList _new_list = _self;
  409. _new_list.insert(_new_list.end(), _obj.begin(), _obj.end());
  410. return vm->PyList(_new_list);
  411. });
  412. _vm->bindMethod("list", "__len__", [](VM* vm, const pkpy::ArgList& args) {
  413. const PyVarList& _self = vm->PyList_AS_C(args[0]);
  414. return vm->PyInt(_self.size());
  415. });
  416. _vm->bindMethod("list", "__getitem__", [](VM* vm, const pkpy::ArgList& args) {
  417. const PyVarList& _self = vm->PyList_AS_C(args[0]);
  418. if(args[1]->isType(vm->_tp_slice)){
  419. _Slice s = vm->PySlice_AS_C(args[1]);
  420. s.normalize(_self.size());
  421. PyVarList _new_list;
  422. for(size_t i = s.start; i < s.stop; i++)
  423. _new_list.push_back(_self[i]);
  424. return vm->PyList(_new_list);
  425. }
  426. int _index = (int)vm->PyInt_AS_C(args[1]);
  427. _index = vm->normalizedIndex(_index, _self.size());
  428. return _self[_index];
  429. });
  430. _vm->bindMethod("list", "__setitem__", [](VM* vm, const pkpy::ArgList& args) {
  431. PyVarList& _self = vm->PyList_AS_C(args[0]);
  432. int _index = (int)vm->PyInt_AS_C(args[1]);
  433. _index = vm->normalizedIndex(_index, _self.size());
  434. _self[_index] = args[2];
  435. return vm->None;
  436. });
  437. _vm->bindMethod("list", "__delitem__", [](VM* vm, const pkpy::ArgList& args) {
  438. PyVarList& _self = vm->PyList_AS_C(args[0]);
  439. int _index = (int)vm->PyInt_AS_C(args[1]);
  440. _index = vm->normalizedIndex(_index, _self.size());
  441. _self.erase(_self.begin() + _index);
  442. return vm->None;
  443. });
  444. /************ PyTuple ************/
  445. _vm->bindMethod("tuple", "__new__", [](VM* vm, const pkpy::ArgList& args) {
  446. vm->__checkArgSize(args, 1);
  447. PyVarList _list = vm->PyList_AS_C(vm->call(vm->builtins->attribs["list"], args));
  448. return vm->PyTuple(_list);
  449. });
  450. _vm->bindMethod("tuple", "__iter__", [](VM* vm, const pkpy::ArgList& args) {
  451. vm->__checkType(args[0], vm->_tp_tuple);
  452. _Iterator* iter = new VectorIterator(vm, args[0]);
  453. return vm->PyIter(pkpy::shared_ptr<_Iterator>(iter));
  454. });
  455. _vm->bindMethod("tuple", "__len__", [](VM* vm, const pkpy::ArgList& args) {
  456. const PyVarList& _self = vm->PyTuple_AS_C(args[0]);
  457. return vm->PyInt(_self.size());
  458. });
  459. _vm->bindMethod("tuple", "__getitem__", [](VM* vm, const pkpy::ArgList& args) {
  460. const PyVarList& _self = vm->PyTuple_AS_C(args[0]);
  461. int _index = (int)vm->PyInt_AS_C(args[1]);
  462. _index = vm->normalizedIndex(_index, _self.size());
  463. return _self[_index];
  464. });
  465. /************ PyBool ************/
  466. _vm->bindMethod("bool", "__repr__", [](VM* vm, const pkpy::ArgList& args) {
  467. bool val = vm->PyBool_AS_C(args[0]);
  468. return vm->PyStr(val ? "True" : "False");
  469. });
  470. _vm->bindMethod("bool", "__json__", [](VM* vm, const pkpy::ArgList& args) {
  471. bool val = vm->PyBool_AS_C(args[0]);
  472. return vm->PyStr(val ? "true" : "false");
  473. });
  474. _vm->bindMethod("bool", "__eq__", [](VM* vm, const pkpy::ArgList& args) {
  475. return vm->PyBool(args[0] == args[1]);
  476. });
  477. _vm->bindMethod("bool", "__xor__", [](VM* vm, const pkpy::ArgList& args) {
  478. bool _self = vm->PyBool_AS_C(args[0]);
  479. bool _obj = vm->PyBool_AS_C(args[1]);
  480. return vm->PyBool(_self ^ _obj);
  481. });
  482. _vm->bindMethod("ellipsis", "__repr__", [](VM* vm, const pkpy::ArgList& args) {
  483. return vm->PyStr("Ellipsis");
  484. });
  485. _vm->bindMethod("_native_function", "__call__", [](VM* vm, const pkpy::ArgList& args) {
  486. const _CppFunc& _self = vm->PyNativeFunction_AS_C(args[0]);
  487. return _self(vm, args.subList(1));
  488. });
  489. _vm->bindMethod("function", "__call__", [](VM* vm, const pkpy::ArgList& args) {
  490. return vm->call(args[0], args.subList(1));
  491. });
  492. _vm->bindMethod("_bounded_method", "__call__", [](VM* vm, const pkpy::ArgList& args) {
  493. vm->__checkType(args[0], vm->_tp_bounded_method);
  494. const _BoundedMethod& _self = vm->PyBoundedMethod_AS_C(args[0]);
  495. pkpy::ArgList newArgs(args.size());
  496. newArgs[0] = _self.obj;
  497. for(int i = 1; i < args.size(); i++) newArgs[i] = args[i];
  498. return vm->call(_self.method, newArgs);
  499. });
  500. }
  501. #include "builtins.h"
  502. #ifdef _WIN32
  503. #define __EXPORT __declspec(dllexport)
  504. #elif __APPLE__
  505. #define __EXPORT __attribute__((visibility("default"))) __attribute__((used))
  506. #else
  507. #define __EXPORT
  508. #endif
  509. void __addModuleTime(VM* vm){
  510. PyVar mod = vm->newModule("time");
  511. vm->bindFunc(mod, "time", [](VM* vm, const pkpy::ArgList& args) {
  512. auto now = std::chrono::high_resolution_clock::now();
  513. return vm->PyFloat(std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count() / 1000000.0);
  514. });
  515. vm->bindFunc(mod, "sleep", [](VM* vm, const pkpy::ArgList& args) {
  516. vm->__checkArgSize(args, 1);
  517. if(!vm->isIntOrFloat(args[0])){
  518. vm->typeError("time.sleep() argument must be int or float");
  519. }
  520. double sec = vm->numToFloat(args[0]);
  521. vm->sleepForSecs(sec);
  522. return vm->None;
  523. });
  524. }
  525. void __addModuleSys(VM* vm){
  526. PyVar mod = vm->newModule("sys");
  527. vm->bindFunc(mod, "getrefcount", [](VM* vm, const pkpy::ArgList& args) {
  528. vm->__checkArgSize(args, 1);
  529. return vm->PyInt(args[0].use_count());
  530. });
  531. vm->bindFunc(mod, "getrecursionlimit", [](VM* vm, const pkpy::ArgList& args) {
  532. vm->__checkArgSize(args, 0);
  533. return vm->PyInt(vm->maxRecursionDepth);
  534. });
  535. vm->bindFunc(mod, "setrecursionlimit", [](VM* vm, const pkpy::ArgList& args) {
  536. vm->__checkArgSize(args, 1);
  537. vm->maxRecursionDepth = (int)vm->PyInt_AS_C(args[0]);
  538. return vm->None;
  539. });
  540. vm->setAttr(mod, "version", vm->PyStr(PK_VERSION));
  541. }
  542. void __addModuleJson(VM* vm){
  543. PyVar mod = vm->newModule("json");
  544. vm->bindFunc(mod, "loads", [](VM* vm, const pkpy::ArgList& args) {
  545. vm->__checkArgSize(args, 1);
  546. const _Str& expr = vm->PyStr_AS_C(args[0]);
  547. _Code code = compile(vm, expr.c_str(), "<json>", JSON_MODE, false);
  548. return vm->_exec(code, vm->topFrame()->_module, vm->topFrame()->f_locals);
  549. });
  550. vm->bindFunc(mod, "dumps", [](VM* vm, const pkpy::ArgList& args) {
  551. vm->__checkArgSize(args, 1);
  552. return vm->asJson(args[0]);
  553. });
  554. }
  555. void __addModuleMath(VM* vm){
  556. PyVar mod = vm->newModule("math");
  557. vm->setAttr(mod, "pi", vm->PyFloat(3.1415926535897932384));
  558. vm->setAttr(mod, "e" , vm->PyFloat(2.7182818284590452354));
  559. vm->bindFunc(mod, "log", [](VM* vm, const pkpy::ArgList& args) {
  560. vm->__checkArgSize(args, 1);
  561. return vm->PyFloat(log(vm->numToFloat(args[0])));
  562. });
  563. vm->bindFunc(mod, "log10", [](VM* vm, const pkpy::ArgList& args) {
  564. vm->__checkArgSize(args, 1);
  565. return vm->PyFloat(log10(vm->numToFloat(args[0])));
  566. });
  567. vm->bindFunc(mod, "log2", [](VM* vm, const pkpy::ArgList& args) {
  568. vm->__checkArgSize(args, 1);
  569. return vm->PyFloat(log2(vm->numToFloat(args[0])));
  570. });
  571. vm->bindFunc(mod, "sin", [](VM* vm, const pkpy::ArgList& args) {
  572. vm->__checkArgSize(args, 1);
  573. return vm->PyFloat(sin(vm->numToFloat(args[0])));
  574. });
  575. vm->bindFunc(mod, "cos", [](VM* vm, const pkpy::ArgList& args) {
  576. vm->__checkArgSize(args, 1);
  577. return vm->PyFloat(cos(vm->numToFloat(args[0])));
  578. });
  579. vm->bindFunc(mod, "tan", [](VM* vm, const pkpy::ArgList& args) {
  580. vm->__checkArgSize(args, 1);
  581. return vm->PyFloat(tan(vm->numToFloat(args[0])));
  582. });
  583. vm->bindFunc(mod, "isclose", [](VM* vm, const pkpy::ArgList& args) {
  584. vm->__checkArgSize(args, 2);
  585. _Float a = vm->numToFloat(args[0]);
  586. _Float b = vm->numToFloat(args[1]);
  587. return vm->PyBool(fabs(a - b) < 1e-6);
  588. });
  589. }
  590. class _PkExported{
  591. public:
  592. virtual ~_PkExported() = default;
  593. virtual void* get() = 0;
  594. };
  595. static std::vector<_PkExported*> _pkLookupTable;
  596. template<typename T>
  597. class PkExported : public _PkExported{
  598. T* _ptr;
  599. public:
  600. template<typename... Args>
  601. PkExported(Args&&... args) {
  602. _ptr = new T(std::forward<Args>(args)...);
  603. _pkLookupTable.push_back(this);
  604. }
  605. ~PkExported() override { delete _ptr; }
  606. void* get() override { return _ptr; }
  607. operator T*() { return _ptr; }
  608. };
  609. #define pkpy_allocate(T, ...) *(new PkExported<T>(__VA_ARGS__))
  610. extern "C" {
  611. __EXPORT
  612. /// Delete a pointer allocated by `pkpy_xxx_xxx`.
  613. /// It can be `VM*`, `REPL*`, `ThreadedVM*`, `char*`, etc.
  614. ///
  615. /// !!!
  616. /// If the pointer is not allocated by `pkpy_xxx_xxx`, the behavior is undefined.
  617. /// For char*, you can also use trivial `delete` in your language.
  618. /// !!!
  619. void pkpy_delete(void* p){
  620. for(int i = 0; i < _pkLookupTable.size(); i++){
  621. if(_pkLookupTable[i]->get() == p){
  622. delete _pkLookupTable[i];
  623. _pkLookupTable.erase(_pkLookupTable.begin() + i);
  624. return;
  625. }
  626. }
  627. free(p);
  628. }
  629. __EXPORT
  630. /// Run a given source on a virtual machine.
  631. ///
  632. /// Return `true` if there is no compile error.
  633. bool pkpy_vm_exec(VM* vm, const char* source){
  634. _Code code = compile(vm, source, "main.py");
  635. if(code == nullptr) return false;
  636. vm->exec(code);
  637. return true;
  638. }
  639. __EXPORT
  640. /// Get a global variable of a virtual machine.
  641. ///
  642. /// Return a json representing the result.
  643. /// If the variable is not found, return `nullptr`.
  644. char* pkpy_vm_get_global(VM* vm, const char* name){
  645. auto it = vm->_main->attribs.find(name);
  646. if(it == vm->_main->attribs.end()) return nullptr;
  647. try{
  648. _Str _json = vm->PyStr_AS_C(vm->asJson(it->second));
  649. return strdup(_json.c_str());
  650. }catch(...){
  651. return nullptr;
  652. }
  653. }
  654. __EXPORT
  655. /// Evaluate an expression.
  656. ///
  657. /// Return a json representing the result.
  658. /// If there is any error, return `nullptr`.
  659. char* pkpy_vm_eval(VM* vm, const char* source){
  660. _Code code = compile(vm, source, "<eval>", EVAL_MODE);
  661. if(code == nullptr) return nullptr;
  662. PyVarOrNull ret = vm->exec(code);
  663. if(ret == nullptr) return nullptr;
  664. try{
  665. _Str _json = vm->PyStr_AS_C(vm->asJson(ret));
  666. return strdup(_json.c_str());
  667. }catch(...){
  668. return nullptr;
  669. }
  670. }
  671. __EXPORT
  672. /// Create a REPL, using the given virtual machine as the backend.
  673. REPL* pkpy_new_repl(VM* vm){
  674. return pkpy_allocate(REPL, vm);
  675. }
  676. __EXPORT
  677. /// Input a source line to an interactive console.
  678. ///
  679. /// Return `0` if need more lines,
  680. /// `1` if execution happened,
  681. /// `2` if execution skipped (compile error or empty input).
  682. int pkpy_repl_input(REPL* r, const char* line){
  683. return r->input(line);
  684. }
  685. __EXPORT
  686. /// Add a source module into a virtual machine.
  687. ///
  688. /// Return `true` if there is no complie error.
  689. bool pkpy_vm_add_module(VM* vm, const char* name, const char* source){
  690. // compile the module but don't execute it
  691. _Code code = compile(vm, source, name + _Str(".py"));
  692. if(code == nullptr) return false;
  693. vm->addLazyModule(name, code);
  694. return true;
  695. }
  696. void __vm_init(VM* vm){
  697. __initializeBuiltinFunctions(vm);
  698. __addModuleSys(vm);
  699. __addModuleTime(vm);
  700. __addModuleJson(vm);
  701. __addModuleMath(vm);
  702. _Code code = compile(vm, __BUILTINS_CODE, "<builtins>");
  703. if(code == nullptr) exit(1);
  704. vm->_exec(code, vm->builtins, {});
  705. pkpy_vm_add_module(vm, "random", __RANDOM_CODE);
  706. pkpy_vm_add_module(vm, "os", __OS_CODE);
  707. }
  708. __EXPORT
  709. /// Create a virtual machine.
  710. VM* pkpy_new_vm(bool use_stdio){
  711. VM* vm = pkpy_allocate(VM, use_stdio);
  712. __vm_init(vm);
  713. return vm;
  714. }
  715. __EXPORT
  716. /// Create a virtual machine that supports asynchronous execution.
  717. ThreadedVM* pkpy_new_tvm(bool use_stdio){
  718. ThreadedVM* vm = pkpy_allocate(ThreadedVM, use_stdio);
  719. __vm_init(vm);
  720. return vm;
  721. }
  722. __EXPORT
  723. /// Read the standard output and standard error as string of a virtual machine.
  724. /// The `vm->use_stdio` should be `false`.
  725. /// After this operation, both stream will be cleared.
  726. ///
  727. /// Return a json representing the result.
  728. char* pkpy_vm_read_output(VM* vm){
  729. if(vm->use_stdio) return nullptr;
  730. _StrStream* s_out = dynamic_cast<_StrStream*>(vm->_stdout);
  731. _StrStream* s_err = dynamic_cast<_StrStream*>(vm->_stderr);
  732. _Str _stdout = s_out->str();
  733. _Str _stderr = s_err->str();
  734. _StrStream ss;
  735. ss << '{' << "\"stdout\": " << _stdout.__escape(false);
  736. ss << ", ";
  737. ss << "\"stderr\": " << _stderr.__escape(false) << '}';
  738. s_out->str("");
  739. s_err->str("");
  740. return strdup(ss.str().c_str());
  741. }
  742. __EXPORT
  743. /// Get the current state of a threaded virtual machine.
  744. ///
  745. /// Return `0` for `THREAD_READY`,
  746. /// `1` for `THREAD_RUNNING`,
  747. /// `2` for `THREAD_SUSPENDED`,
  748. /// `3` for `THREAD_FINISHED`.
  749. int pkpy_tvm_get_state(ThreadedVM* vm){
  750. return vm->getState();
  751. }
  752. __EXPORT
  753. /// Set the state of a threaded virtual machine to `THREAD_READY`.
  754. /// The current state should be `THREAD_FINISHED`.
  755. void pkpy_tvm_reset_state(ThreadedVM* vm){
  756. vm->resetState();
  757. }
  758. __EXPORT
  759. /// Read the current JSONRPC request from shared string buffer.
  760. char* pkpy_tvm_read_jsonrpc_request(ThreadedVM* vm){
  761. _Str s = vm->readJsonRpcRequest();
  762. return strdup(s.c_str());
  763. }
  764. __EXPORT
  765. /// Write a JSONRPC response to shared string buffer.
  766. void pkpy_tvm_write_jsonrpc_response(ThreadedVM* vm, const char* value){
  767. vm->writeJsonrpcResponse(value);
  768. }
  769. __EXPORT
  770. /// Emit a KeyboardInterrupt signal to stop a running threaded virtual machine.
  771. void pkpy_tvm_terminate(ThreadedVM* vm){
  772. vm->terminate();
  773. }
  774. __EXPORT
  775. /// Run a given source on a threaded virtual machine.
  776. /// The excution will be started in a new thread.
  777. ///
  778. /// Return `true` if there is no compile error.
  779. bool pkpy_tvm_exec_async(VM* vm, const char* source){
  780. // although this is a method of VM, it's only used in ThreadedVM
  781. _Code code = compile(vm, source, "main.py");
  782. if(code == nullptr) return false;
  783. vm->execAsync(code);
  784. return true;
  785. }
  786. }