2
0

core-activation.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #ifndef AFUN_CORE_ACTIVATION_H
  2. #define AFUN_CORE_ACTIVATION_H
  3. #include "aFuntool.h"
  4. #include "aFunCoreExport.h"
  5. #include "msg.h"
  6. #include "code.h"
  7. #include "object-value.h"
  8. namespace aFuncore {
  9. class Inter;
  10. class AFUN_CORE_EXPORT Activation {
  11. public:
  12. typedef enum ActivationStatus {
  13. as_run = 0,
  14. as_end = 1,
  15. as_end_run = 2,
  16. } ActivationStatus;
  17. class AFUN_CORE_EXPORT VarList {
  18. public:
  19. AFUN_INLINE explicit VarList();
  20. virtual ~VarList();
  21. AFUN_INLINE VarList(VarList &&new_varlist);
  22. AFUN_INLINE VarList &operator=(VarList &&new_varlist) noexcept;
  23. VarList(const VarList &) = delete;
  24. VarList &operator=(const VarList &) = delete;
  25. void clear();
  26. void connect(VarList &new_varlist);
  27. AFUN_INLINE void push(VarSpace *varspace_);
  28. AFUN_INLINE size_t count();
  29. template <typename Callable,typename...T>
  30. void forEach(Callable func, T...arg);
  31. [[nodiscard]] virtual Var *findVar(const std::string &name);
  32. virtual bool defineVar(const std::string &name, Object *data);
  33. virtual bool defineVar(const std::string &name, Var *data);
  34. virtual bool setVar(const std::string &name, Object *data);
  35. virtual bool delVar(const std::string &name);
  36. [[nodiscard]] AFUN_INLINE Object *findObject(const std::string &name);
  37. private:
  38. std::list<VarSpace *> varspace;
  39. };
  40. Inter &inter;
  41. explicit Activation(Inter &inter_);
  42. virtual ~Activation();
  43. Activation &operator=(const Activation &)=delete;
  44. virtual ActivationStatus getCode(const Code::ByteCode *&code) = 0;
  45. virtual void runCode(const Code::ByteCode *code);
  46. virtual void endRun();
  47. [[nodiscard]] AFUN_INLINE VarList &getVarlist();
  48. [[nodiscard]] AFUN_INLINE UpMessage &getUpStream();
  49. [[nodiscard]] AFUN_INLINE DownMessage &getDownStream();
  50. [[nodiscard]] AFUN_INLINE aFuntool::FileLine getFileLine() const;
  51. [[nodiscard]] AFUN_INLINE const aFuntool::FilePath &getFilePath() const;
  52. protected:
  53. VarList varlist;
  54. UpMessage up;
  55. DownMessage down;
  56. aFuntool::FilePath path;
  57. aFuntool::FileLine line;
  58. virtual void runCodeElement(const Code::ByteCode *code);
  59. virtual void runCodeBlockP(const Code::ByteCode *code);
  60. virtual void runCodeBlockC(const Code::ByteCode *code);
  61. virtual void runCodeBlockB(const Code::ByteCode *code);
  62. };
  63. class AFUN_CORE_EXPORT ExeActivation : public Activation {
  64. public:
  65. AFUN_INLINE ExeActivation(const Code &code, Inter &inter_);
  66. AFUN_INLINE ExeActivation(const Code::ByteCode *code, Inter &inter_);
  67. ActivationStatus getCode(const Code::ByteCode *&code) override;
  68. [[nodiscard]] AFUN_INLINE const Code::ByteCode *getStart() const;
  69. private:
  70. const Code::ByteCode *start;
  71. const Code::ByteCode *next;
  72. bool first=true;
  73. };
  74. class AFUN_CORE_EXPORT TopActivation : public ExeActivation {
  75. public:
  76. explicit TopActivation(const Code &code, Inter &inter_);
  77. ~TopActivation() override = default;
  78. [[nodiscard]] AFUN_INLINE const Code &getBase() const;
  79. private:
  80. const Code &base;
  81. };
  82. class AFUN_CORE_EXPORT FuncActivation : public Activation {
  83. public:
  84. AFUN_INLINE explicit FuncActivation(const Code::ByteCode *code, Inter &inter_);
  85. explicit FuncActivation(Function *func, Inter &inter_);
  86. ~FuncActivation() override;
  87. ActivationStatus getCode(const Code::ByteCode *&code) override;
  88. void endRun() override;
  89. private:
  90. enum {
  91. func_first = 0, // 获取函数体前准备
  92. func_get_func = 1, // 获取函数体后,开始获取参数前
  93. func_get_arg = 2, // 获取参数过程
  94. } status = func_first;
  95. bool on_tail = false;
  96. const Code::ByteCode *call;
  97. Function *func = nullptr;
  98. Function::CallFunction *call_func = nullptr;
  99. std::list<Function::CallFunction::ArgCodeList> *acl = nullptr;
  100. std::list<Function::CallFunction::ArgCodeList>::iterator acl_begin;
  101. std::list<Function::CallFunction::ArgCodeList>::iterator acl_end;
  102. };
  103. }
  104. #include "core-activation.inline.h"
  105. #include "core-activation.template.h"
  106. #endif //AFUN_CORE_ACTIVATION_H