2
0

object-value.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #ifndef AFUN_OBJECT_VALUE_H
  2. #define AFUN_OBJECT_VALUE_H
  3. #include <list>
  4. #include <mutex>
  5. #include "aFuntool.h"
  6. #include "aFunCoreExport.h"
  7. #include "object.h"
  8. #include "code.h"
  9. #include "inter.h"
  10. namespace aFuncore {
  11. class AFUN_CORE_EXPORT Var : public Object {
  12. public:
  13. Environment &env;
  14. Var(Object *data_, Inter &inter);
  15. Var(Object *data_, Environment &env_);
  16. ~Var() override = default;
  17. [[nodiscard]] virtual Object *getData();
  18. virtual void setData(Object *data_);
  19. void linkObject(std::queue<Object *> &queue) override;
  20. private:
  21. Object *data;
  22. };
  23. class AFUN_CORE_EXPORT VarSpace : public Object {
  24. public:
  25. typedef enum VarOperationFlat {
  26. vof_success = 0, // 成功
  27. vof_not_var = 1, // 变量不存在
  28. vof_redefine_var = 2, // 变量重复定义
  29. vof_fail = 3, // 存在其他错误
  30. } VarOperationFlat;
  31. Environment &env;
  32. explicit VarSpace(Inter &inter);
  33. explicit VarSpace(Environment &env_);
  34. ~VarSpace() override = default;
  35. template <typename Callable,typename...T>
  36. void forEach(Callable func, T...arg);
  37. template <typename Callable,typename...T>
  38. void forEachLock(Callable func, T...arg);
  39. [[nodiscard]] AFUN_INLINE size_t getCount();
  40. [[nodiscard]] virtual Var *findVar(const std::string &name);
  41. virtual VarOperationFlat defineVar(const std::string &name, Object *data);
  42. virtual VarOperationFlat defineVar(const std::string &name, Var *data);
  43. virtual VarOperationFlat setVar(const std::string &name, Object *data);
  44. virtual VarOperationFlat delVar(const std::string &name);
  45. [[nodiscard]] AFUN_INLINE Object *findObject(const std::string &name);
  46. void linkObject(std::queue<Object *> &queue) override;
  47. AFUN_STATIC const size_t VAR_HASH_SIZE = 100; // 环境变量哈希表大小
  48. private:
  49. std::unordered_map<std::string, Var *> var;
  50. };
  51. class AFUN_CORE_EXPORT ProtectVarSpace : public VarSpace {
  52. public:
  53. AFUN_INLINE explicit ProtectVarSpace(Inter &inter);
  54. AFUN_INLINE explicit ProtectVarSpace(Environment &env_);
  55. [[nodiscard]] AFUN_INLINE bool getProtect() const;
  56. AFUN_INLINE bool setProtect(bool protect);
  57. VarOperationFlat defineVar(const std::string &name, Object *data) override;
  58. VarOperationFlat defineVar(const std::string &name, Var *data) override;
  59. VarOperationFlat setVar(const std::string &name, Object *data) override;
  60. VarOperationFlat delVar(const std::string &name) override;
  61. private:
  62. bool is_protect;
  63. };
  64. class AFUN_CORE_EXPORT Function : public virtual Object {
  65. public:
  66. class AFUN_CORE_EXPORT CallFunction;
  67. virtual CallFunction *getCallFunction(const Code::ByteCode *code, Inter &inter) = 0;
  68. virtual bool isInfix();
  69. };
  70. class AFUN_CORE_EXPORT Function::CallFunction {
  71. public:
  72. class ArgCodeList;
  73. CallFunction() = default;
  74. virtual ~CallFunction() = default;
  75. CallFunction(const CallFunction &)=delete;
  76. CallFunction &operator=(const CallFunction &)=delete;
  77. virtual std::list<ArgCodeList> *getArgCodeList(Inter &inter, Activation &activation, const Code::ByteCode *call) = 0;
  78. virtual void runFunction() = 0;
  79. };
  80. class Function::CallFunction::ArgCodeList {
  81. public:
  82. const Code::ByteCode *code = nullptr;
  83. AFUN_INLINE explicit ArgCodeList(const Code::ByteCode *code = nullptr);
  84. AFUN_INLINE ~ArgCodeList();
  85. AFUN_INLINE Object *setObject(Object *res);
  86. AFUN_INLINE Object *getObject();
  87. private:
  88. Object *ret;
  89. };
  90. class AFUN_CORE_EXPORT Literaler : public virtual Object {
  91. public:
  92. virtual void getObject(const std::string &literal, char prefix, Inter &inter, Activation &activation) = 0;
  93. };
  94. class AFUN_CORE_EXPORT CallBackVar : public virtual Object {
  95. public:
  96. virtual bool isCallBack(Inter &inter, Activation &activation);
  97. virtual void callBack(Inter &inter, Activation &activation) = 0;
  98. };
  99. class ImportFunction : public Function {
  100. class CallFunc : public CallFunction {
  101. const Code::ByteCode *call_code;
  102. Inter &inter;
  103. std::list<ArgCodeList> *acl;
  104. std::string import;
  105. public:
  106. CallFunc(const Code::ByteCode *code_, Inter &inter_);
  107. std::list<ArgCodeList> *getArgCodeList(Inter &inter_,
  108. Activation &activation,
  109. const Code::ByteCode *call) override;
  110. void runFunction() override;
  111. ~CallFunc() override;
  112. };
  113. public:
  114. AFUN_INLINE explicit ImportFunction(Inter &inter_);
  115. AFUN_INLINE explicit ImportFunction(Environment &env_);
  116. ~ImportFunction() override = default;
  117. CallFunction *getCallFunction(const Code::ByteCode *code, Inter &inter) override;
  118. };
  119. };
  120. #include "object-value.inline.h"
  121. #include "object-value.template.h"
  122. #endif //AFUN_OBJECT_VALUE_H