#pragma once #include #include class FormatVisitor : public BaseVisitor { public: void Visit(const BaseNode* node) override { node->Visit(this); } void Visit(const ClassDeclarationNode* node) override { int64_t pub = node->PublicFields().size(); int64_t prot = node->ProtectedFields().size(); int64_t priv = node->PrivateFields().size(); v.push_back(hot_tubs() + "class " + node->ClassName() + " {"); if (pub) { v.push_back(hot_tubs() + " public:"); visiting(node->PublicFields()); if (prot || priv) v.push_back(""); } if (prot) { v.push_back(hot_tubs() + " protected:"); visiting(node->ProtectedFields()); if (priv) v.push_back(""); } if (priv) { v.push_back(hot_tubs() + " private:"); visiting(node->PrivateFields()); } v.push_back(hot_tubs() + "};"); } void Visit(const VarDeclarationNode* node) override { std::string tmp = node->TypeName() + " " + node->VarName(); if (var_tmp) { v.back() += tmp; if (!last) v.back() += ", "; } else { v.push_back(hot_tubs() + tmp + ";"); } } void Visit(const MethodDeclarationNode* node) override { v.push_back(hot_tubs() + node->ReturnTypeName() + " " + node->MethodName() + "("); var_tmp = 1; visiting(node->Arguments()); v.back() += ");"; var_tmp = 0; } const std::vector& GetFormattedCode() const { return v; } private: std::vector v; uint64_t dh = 0; std::string hot_tubs() { return std::string(dh * 4, ' '); } bool var_tmp = 0; bool last = 0; void visiting(std::vector vec) { dh++; for (uint64_t i = 0; i < vec.size(); i++) { if (i == vec.size() - 1) last = 1; vec[i]->Visit(this); } last = 0; dh--; } };