obj.h 3.2 KB

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