code.hpp 2.3 KB

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