code.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 perfix=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. public:
  32. aFuntool::FileLine line;
  33. aFuntool::FilePath file;
  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 destruct();
  40. void display();
  41. void displayAll();
  42. [[nodiscard]] CodeType getType() const {return type;}
  43. [[nodiscard]] char getPrefix() const {return perfix;}
  44. [[nodiscard]] const char *getElement() const {if (type != code_element) throw aFuncore::AttributesError("Code.Element"); return element;}
  45. [[nodiscard]] BlockType getBlockType() const {if (type != code_block) throw aFuncore::AttributesError("Code.BlockType"); return block_type;}
  46. [[nodiscard]] Code *getSon() const {if (type != code_block) return nullptr; return son;}
  47. [[nodiscard]] Code *toNext() const {return next;}
  48. [[nodiscard]] Code *toPrev() const {return prev;}
  49. [[nodiscard]] Code *toFather() const {return father;}
  50. };
  51. }
  52. #endif //AFUN_CODE_H