code.hpp 2.5 KB

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