code.h 3.0 KB

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