code.hpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #ifndef AFUN_CODE_HPP
  2. #define AFUN_CODE_HPP
  3. #include "tool.hpp"
  4. #include "aFunCoreExport.h"
  5. namespace aFuncore {
  6. typedef enum CodeType {
  7. code_start = 0,
  8. code_element = 1,
  9. code_block = 2,
  10. } CodeType;
  11. typedef enum BlockType {
  12. block_p = '(',
  13. block_b = '[',
  14. block_c = '{',
  15. } BlockType;
  16. typedef class Code Code;
  17. class Code {
  18. CodeType type;
  19. char prefix=NUL;
  20. union {
  21. char *element; // union 内不使用 std::string
  22. struct {
  23. BlockType block_type;
  24. Code *son;
  25. };
  26. };
  27. Code *father = nullptr;;
  28. Code *next = nullptr;;
  29. Code *prev = nullptr;;
  30. aFuntool::FileLine line;
  31. aFuntool::FilePath file;
  32. public:
  33. AFUN_CORE_EXPORT explicit Code(FileLine line, ConstFilePath file="");
  34. AFUN_CORE_EXPORT Code (const std::string &element, aFuntool::FileLine line, aFuntool::ConstFilePath file="", char prefix=NUL);
  35. AFUN_CORE_EXPORT Code (BlockType block_type, Code *son, aFuntool::FileLine line, aFuntool::ConstFilePath file="", char prefix=NUL);
  36. AFUN_CORE_EXPORT ~Code();
  37. AFUN_CORE_EXPORT Code *connect(Code *code);
  38. AFUN_CORE_EXPORT void destructAll();
  39. AFUN_CORE_EXPORT void display() const;
  40. AFUN_CORE_EXPORT void displayAll() const;
  41. AFUN_CORE_EXPORT bool write_v1(FILE *f, bool debug=false) const;
  42. AFUN_CORE_EXPORT bool writeAll_v1(FILE *f, bool debug=false) const;
  43. AFUN_CORE_EXPORT Code *read_v1(FILE *f, bool debug=false, int8_t read_type=code_element, bool to_son=false);
  44. AFUN_CORE_EXPORT bool readAll_v1(FILE *f, bool debug=false);
  45. [[nodiscard]] AFUN_CORE_EXPORT std::string getMD5_v1() const;
  46. [[nodiscard]] AFUN_CORE_EXPORT std::string getMD5All_v1() const;
  47. AFUN_CORE_EXPORT bool writeByteCode(ConstFilePath file_path, bool debug=false) const; // NOLINT 允许忽略返回值
  48. AFUN_CORE_EXPORT bool readByteCode(ConstFilePath file_path);
  49. [[nodiscard]] CodeType getType() const {return type;}
  50. [[nodiscard]] char getPrefix() const {return prefix;}
  51. [[nodiscard]] const char *getElement() const {if (type != code_element) return ""; return element;}
  52. [[nodiscard]] BlockType getBlockType() const {if (type != code_block) return block_p; return block_type;}
  53. [[nodiscard]] Code *getSon() const {if (type != code_block) return nullptr; return son;}
  54. [[nodiscard]] Code *toNext() const {return next;}
  55. [[nodiscard]] Code *toPrev() const {return prev;}
  56. [[nodiscard]] Code *toFather() const {return father;}
  57. [[nodiscard]] aFuntool::FileLine getFileLine() const {return line;}
  58. [[nodiscard]] aFuntool::StringFilePath getFilePath() const {return file;}
  59. };
  60. }
  61. #endif //AFUN_CODE_HPP