ref.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #pragma once
  2. #include "obj.h"
  3. namespace pkpy {
  4. struct BaseRef {
  5. virtual PyVar get(VM*, Frame*) const = 0;
  6. virtual void set(VM*, Frame*, PyVar) const = 0;
  7. virtual void del(VM*, Frame*) const = 0;
  8. virtual ~BaseRef() = default;
  9. };
  10. enum NameScope {
  11. NAME_LOCAL = 0,
  12. NAME_GLOBAL,
  13. NAME_ATTR,
  14. NAME_SPECIAL,
  15. };
  16. struct NameRef : BaseRef {
  17. const std::pair<StrName, NameScope> pair;
  18. inline StrName name() const { return pair.first; }
  19. inline NameScope scope() const { return pair.second; }
  20. NameRef(const std::pair<StrName, NameScope>& pair) : pair(pair) {}
  21. PyVar get(VM* vm, Frame* frame) const;
  22. void set(VM* vm, Frame* frame, PyVar val) const;
  23. void del(VM* vm, Frame* frame) const;
  24. };
  25. struct AttrRef : BaseRef {
  26. mutable PyVar obj;
  27. NameRef attr;
  28. AttrRef(PyVar obj, NameRef attr) : obj(obj), attr(attr) {}
  29. PyVar get(VM* vm, Frame* frame) const;
  30. void set(VM* vm, Frame* frame, PyVar val) const;
  31. void del(VM* vm, Frame* frame) const;
  32. };
  33. struct IndexRef : BaseRef {
  34. mutable PyVar obj;
  35. PyVar index;
  36. IndexRef(PyVar obj, PyVar index) : obj(obj), index(index) {}
  37. PyVar get(VM* vm, Frame* frame) const;
  38. void set(VM* vm, Frame* frame, PyVar val) const;
  39. void del(VM* vm, Frame* frame) const;
  40. };
  41. struct TupleRef : BaseRef {
  42. Tuple objs;
  43. TupleRef(Tuple&& objs) : objs(std::move(objs)) {}
  44. PyVar get(VM* vm, Frame* frame) const;
  45. void set(VM* vm, Frame* frame, PyVar val) const;
  46. void del(VM* vm, Frame* frame) const;
  47. };
  48. } // namespace pkpy