code.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #ifndef AFUN_CODE_H
  2. #define AFUN_CODE_H
  3. #include "aFuntool.h"
  4. #include "aFunCoreExport.h"
  5. namespace aFuncore {
  6. class AFUN_CORE_EXPORT Code {
  7. public:
  8. class ByteCode;
  9. AFUN_INLINE explicit Code(aFuntool::FilePath file_);
  10. ~Code();
  11. Code &operator=(const Code &)=delete;
  12. [[nodiscard]] std::string getMD5_v1() const;
  13. bool writeByteCode(const aFuntool::FilePath &file_path, bool debug=false) const; // NOLINT 允许忽略返回值
  14. bool readByteCode(const aFuntool::FilePath &file_path);
  15. #ifdef AFUN_DEBUG
  16. void display() const;
  17. #endif
  18. [[nodiscard]] AFUN_INLINE const aFuntool::FilePath &getFilePath() const;
  19. [[nodiscard]] AFUN_INLINE ByteCode *getByteCode() const;
  20. private:
  21. ByteCode *code;
  22. aFuntool::FilePath file;
  23. bool write_v1(FILE *f, bool debug=false) const;
  24. bool read_v1(FILE *f, bool debug=false);
  25. };
  26. class AFUN_CORE_EXPORT Code::ByteCode {
  27. friend class Code;
  28. public:
  29. typedef enum CodeType {
  30. code_start = 0,
  31. code_element = 1,
  32. code_block = 2,
  33. } CodeType;
  34. typedef enum BlockType {
  35. block_p = '(',
  36. block_b = '[',
  37. block_c = '{',
  38. } BlockType;
  39. explicit ByteCode(Code &belong, aFuntool::FileLine line);
  40. ByteCode(Code &belong, const std::string &element, aFuntool::FileLine line, char prefix=aFuntool::NUL);
  41. ByteCode(Code &belong, BlockType block_type, ByteCode *son, aFuntool::FileLine line, char prefix=aFuntool::NUL);
  42. ByteCode &operator=(const ByteCode &)=delete;
  43. ByteCode *connect(ByteCode *new_code);
  44. bool write_v1(FILE *f, bool debug=false) const;
  45. ByteCode *read_v1(FILE *f, bool debug=false, int8_t read_type=code_element, bool to_son=false);
  46. [[nodiscard]] std::string getMD5_v1() const;
  47. #ifdef AFUN_DEBUG
  48. void display() const;
  49. #endif
  50. [[nodiscard]] AFUN_INLINE CodeType getType() const;
  51. [[nodiscard]] AFUN_INLINE char getPrefix() const;
  52. [[nodiscard]] AFUN_INLINE const char *getElement() const;
  53. [[nodiscard]] AFUN_INLINE BlockType getBlockType() const;
  54. [[nodiscard]] AFUN_INLINE ByteCode *getSon() const;
  55. [[nodiscard]] AFUN_INLINE aFuntool::FileLine getFileLine() const;
  56. [[nodiscard]] AFUN_INLINE const aFuntool::FilePath &getFilePath() const;
  57. [[nodiscard]] AFUN_INLINE ByteCode *toNext() const;
  58. [[nodiscard]] AFUN_INLINE ByteCode *toPrev() const;
  59. [[nodiscard]] AFUN_INLINE ByteCode *toFather() const;
  60. private:
  61. CodeType type;
  62. char prefix=aFuntool::NUL;
  63. union CodeData {
  64. char *element; // union 内不使用 std::string
  65. struct Block { // NOLINT 不需要初始化
  66. BlockType block_type;
  67. ByteCode *son;
  68. } block;
  69. AFUN_INLINE CodeData();
  70. } data;
  71. ByteCode *father = nullptr;
  72. ByteCode *next = nullptr;
  73. ByteCode *prev = nullptr;
  74. Code &belong;
  75. aFuntool::FileLine line;
  76. ~ByteCode(); // 限制 ByteCode 只能建立在堆上
  77. };
  78. }
  79. #include "code.inline.h"
  80. #endif //AFUN_CODE_H