code.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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::StringFilePath file_);
  10. ~Code();
  11. Code &operator=(const Code &)=delete;
  12. void display() const;
  13. [[nodiscard]] std::string getMD5_v1() const;
  14. bool writeByteCode(aFuntool::ConstFilePath file_path, bool debug=false) const; // NOLINT 允许忽略返回值
  15. bool readByteCode(aFuntool::ConstFilePath file_path);
  16. [[nodiscard]] inline aFuntool::ConstFilePath getFilePath() const;
  17. [[nodiscard]] inline ByteCode *getByteCode() const;
  18. private:
  19. ByteCode *code;
  20. aFuntool::StringFilePath 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();
  41. ByteCode &operator=(const ByteCode &)=delete;
  42. ByteCode *connect(ByteCode *new_code);
  43. void display() const;
  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. [[nodiscard]] CodeType getType() const;
  48. [[nodiscard]] char getPrefix() const;
  49. [[nodiscard]] const char *getElement() const;
  50. [[nodiscard]] BlockType getBlockType() const;
  51. [[nodiscard]] ByteCode *getSon() const;
  52. [[nodiscard]] aFuntool::FileLine getFileLine() const;
  53. [[nodiscard]] aFuntool::ConstFilePath getFilePath() const;
  54. [[nodiscard]] ByteCode *toNext() const;
  55. [[nodiscard]] ByteCode *toPrev() const;
  56. [[nodiscard]] ByteCode *toFather() const;
  57. private:
  58. CodeType type;
  59. char prefix=aFuntool::NUL;
  60. union CodeData {
  61. char *element; // union 内不使用 std::string
  62. struct { // NOLINT 不需要初始化
  63. BlockType block_type;
  64. ByteCode *son;
  65. };
  66. inline CodeData();
  67. } data;
  68. ByteCode *father = nullptr;
  69. ByteCode *next = nullptr;
  70. ByteCode *prev = nullptr;
  71. Code &belong;
  72. aFuntool::FileLine line;
  73. };
  74. }
  75. #include "code.inline.h"
  76. #endif //AFUN_CODE_H