base.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #pragma once
  2. #include "stdint.h"
  3. #include "stdbool.h"
  4. #include "stdlib.h"
  5. #include "assert.h"
  6. #include "string.h"
  7. #include "pocketpy/common/utils.h"
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. typedef int16_t pkpy_Type;
  12. typedef struct PyObject PyObject;
  13. typedef struct pkpy_VM pkpy_VM;
  14. struct pkpy_G {
  15. pkpy_VM* vm;
  16. } extern pkpy_g;
  17. typedef struct PyVar{
  18. pkpy_Type type;
  19. bool is_ptr;
  20. int extra;
  21. union {
  22. int64_t _i64;
  23. double _f64;
  24. PyObject* _obj;
  25. void* _ptr;
  26. // Vec2
  27. };
  28. } PyVar;
  29. #define PyVar__as(T, self) _Generic((T), \
  30. int64_t: self->_i64, \
  31. double: self->_f64, \
  32. PyObject*: self->_obj, \
  33. void*: self->_ptr, \
  34. )
  35. static_assert(sizeof(PyVar) == 16, "sizeof(PyVar) != 16");
  36. /* predefined vars */
  37. static const pkpy_Type tp_object = 1, tp_type = 2;
  38. static const pkpy_Type tp_int = 3, tp_float = 4, tp_bool = 5, tp_str = 6;
  39. static const pkpy_Type tp_list = 7, tp_tuple = 8;
  40. static const pkpy_Type tp_slice = 9, tp_range = 10, tp_module = 11;
  41. static const pkpy_Type tp_function = 12, tp_native_func = 13, tp_bound_method = 14;
  42. static const pkpy_Type tp_super = 15, tp_exception = 16, tp_bytes = 17, tp_mappingproxy = 18;
  43. static const pkpy_Type tp_dict = 19, tp_property = 20, tp_star_wrapper = 21;
  44. static const pkpy_Type tp_staticmethod = 22, tp_classmethod = 23;
  45. static const pkpy_Type tp_none_type = 24, tp_not_implemented_type = 25;
  46. static const pkpy_Type tp_ellipsis = 26;
  47. static const pkpy_Type tp_op_call = 27, tp_op_yield = 28;
  48. PK_INLINE bool PyVar__is_null(const PyVar* self) { return self->type == 0; }
  49. PK_INLINE int64_t PyVar__hash(const PyVar* self) { return self->extra + self->_i64; }
  50. PK_INLINE void PyVar__ctor(PyVar* self, pkpy_Type type, PyObject* obj){
  51. self->type = type;
  52. self->is_ptr = true;
  53. self->_obj = obj;
  54. }
  55. void PyVar__ctor3(PyVar* self, PyObject* existing);
  56. PK_INLINE bool PyVar__IS_OP(const PyVar* a, const PyVar* b){
  57. return a->is_ptr && b->is_ptr && a->_obj == b->_obj;
  58. }
  59. #define pkpy_Var__is_null(self) ((self)->type == 0)
  60. #define pkpy_Var__set_null(self) do { (self)->type = 0; } while(0)
  61. bool pkpy_Var__eq__(void *vm, PyVar a, PyVar b);
  62. int64_t pkpy_Var__hash__(void *vm, PyVar a);
  63. extern PyVar pkpy_NULL, pkpy_OP_CALL, pkpy_OP_YIELD;
  64. #ifdef __cplusplus
  65. }
  66. #endif
  67. /*
  68. SSO types:
  69. 1. int64_t
  70. 2. double
  71. 3. bool (dummy)
  72. 4. tuple (extra + void*)
  73. 5. string (extra + void* or buf)
  74. */