obj.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #pragma once
  2. #include "__stl__.h"
  3. #include "str.h"
  4. typedef int64_t _Int;
  5. typedef double _Float;
  6. const _Int _Int_MAX_POS = 9223372036854775807LL;
  7. const _Int _Int_MAX_NEG = -9223372036854775807LL;
  8. const _Float _FLOAT_INF_POS = INFINITY;
  9. const _Float _FLOAT_INF_NEG = -INFINITY;
  10. class PyObject;
  11. class CodeObject;
  12. class BasePointer;
  13. class VM;
  14. typedef std::shared_ptr<PyObject> PyVar;
  15. typedef PyVar PyVarOrNull;
  16. class PyVarList: public std::vector<PyVar> {
  17. PyVar& at(size_t) = delete;
  18. inline void __checkIndex(size_t i) const {
  19. if (i >= size()){
  20. auto msg = "std::vector index out of range, " + std::to_string(i) + " not in [0, " + std::to_string(size()) + ")";
  21. throw std::out_of_range(msg);
  22. }
  23. }
  24. public:
  25. PyVar& operator[](size_t i) {
  26. __checkIndex(i);
  27. return std::vector<PyVar>::operator[](i);
  28. }
  29. const PyVar& operator[](size_t i) const {
  30. __checkIndex(i);
  31. return std::vector<PyVar>::operator[](i);
  32. }
  33. // define constructors the same as std::vector
  34. using std::vector<PyVar>::vector;
  35. };
  36. typedef std::unordered_map<_Str, PyVar> PyVarDict;
  37. typedef std::shared_ptr<const BasePointer> _Pointer;
  38. typedef PyVar (*_CppFunc)(VM*, PyVarList);
  39. typedef std::shared_ptr<CodeObject> _Code;
  40. struct _Func {
  41. _Str name;
  42. _Code code;
  43. std::vector<_Str> args;
  44. _Str starredArg; // empty if no *arg
  45. PyVarDict kwArgs; // empty if no k=v
  46. bool hasName(const _Str& val) const {
  47. bool _0 = std::find(args.begin(), args.end(), val) != args.end();
  48. bool _1 = starredArg == val;
  49. bool _2 = kwArgs.find(val) != kwArgs.end();
  50. return _0 || _1 || _2;
  51. }
  52. };
  53. struct BoundedMethod {
  54. PyVar obj;
  55. PyVar method;
  56. };
  57. struct _Range {
  58. _Int start = 0;
  59. _Int stop = -1;
  60. _Int step = 1;
  61. };
  62. struct _Slice {
  63. int start = 0;
  64. int stop = 2147483647; // contain types always use int32 as index, no support for int64
  65. void normalize(int len){
  66. if(start < 0) start += len;
  67. if(stop < 0) stop += len;
  68. if(start < 0) start = 0;
  69. if(stop > len) stop = len;
  70. }
  71. };
  72. class _Iterator {
  73. protected:
  74. PyVar _ref; // keep a reference to the object so it will not be deleted while iterating
  75. VM* vm;
  76. public:
  77. virtual PyVar next() = 0;
  78. virtual bool hasNext() = 0;
  79. _Pointer var;
  80. _Iterator(VM* vm, PyVar _ref) : vm(vm), _ref(_ref) {}
  81. };
  82. typedef std::variant<_Int,_Float,bool,_Str,PyVarList,_CppFunc,_Func,std::shared_ptr<_Iterator>,BoundedMethod,_Range,_Slice,_Pointer> _Value;
  83. #define UNREACHABLE() throw std::runtime_error("unreachable code! (this should be a bug, please report it)");
  84. struct PyObject {
  85. PyVarDict attribs;
  86. _Value _native;
  87. inline bool isType(const PyVar& type){
  88. return attribs[__class__] == type;
  89. }
  90. // currently __name__ is only used for 'type'
  91. _Str getName(){
  92. _Value val = attribs["__name__"]->_native;
  93. return std::get<_Str>(val);
  94. }
  95. _Str getTypeName(){
  96. return attribs[__class__]->getName();
  97. }
  98. PyObject(_Value val): _native(val) {}
  99. };