pointer.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #pragma once
  2. #include "obj.h"
  3. class Frame;
  4. struct BasePointer {
  5. virtual PyVar get(VM*, Frame*) const = 0;
  6. virtual void set(VM*, Frame*, PyVar) const = 0;
  7. };
  8. enum NameScope {
  9. NAME_LOCAL = 0,
  10. NAME_GLOBAL = 1,
  11. NAME_ATTR = 2,
  12. };
  13. struct NamePointer : BasePointer {
  14. const _Str name;
  15. const NameScope scope;
  16. NamePointer(const _Str& name, NameScope scope) : name(name), scope(scope) {}
  17. PyVar get(VM* vm, Frame* frame) const;
  18. void set(VM* vm, Frame* frame, PyVar val) const;
  19. bool operator==(const NamePointer& other) const {
  20. return name == other.name && scope == other.scope;
  21. }
  22. };
  23. struct AttrPointer : BasePointer {
  24. const _Pointer root;
  25. const NamePointer* attr;
  26. AttrPointer(const _Pointer& root, const NamePointer* attr) : root(root), attr(attr) {}
  27. PyVar get(VM* vm, Frame* frame) const;
  28. void set(VM* vm, Frame* frame, PyVar val) const;
  29. };
  30. struct IndexPointer : BasePointer {
  31. const _Pointer root;
  32. const PyVar index;
  33. IndexPointer(_Pointer root, PyVar index) : root(root), index(index) {}
  34. PyVar get(VM* vm, Frame* frame) const;
  35. void set(VM* vm, Frame* frame, PyVar val) const;
  36. };