🚀 change indentation of `nasal_ast::print` & add new function

`nasal_ast::tree`.
This commit is contained in:
ValKmjolnir 2022-08-22 22:35:53 +08:00
parent da8aa4744e
commit 987d3ce9e2
2 changed files with 18 additions and 12 deletions

View File

@ -137,7 +137,8 @@ public:
nasal_ast(const u32 l=0,const u32 t=ast_null):_line(l),_type(t),_num(0){} nasal_ast(const u32 l=0,const u32 t=ast_null):_line(l),_type(t),_num(0){}
nasal_ast(const nasal_ast&); nasal_ast(const nasal_ast&);
nasal_ast(nasal_ast&&); nasal_ast(nasal_ast&&);
void print(u32,bool); void tree();
void print(u32,bool,std::vector<string>&);
void clear(); void clear();
nasal_ast& operator=(const nasal_ast&); nasal_ast& operator=(const nasal_ast&);
@ -207,10 +208,15 @@ void nasal_ast::clear()
_child.clear(); _child.clear();
} }
void nasal_ast::print(u32 depth,bool last=false) void nasal_ast::tree()
{ {
static std::vector<string> intentation={}; std::vector<string> tmp;
for(auto& i:intentation) print(0,false,tmp);
}
void nasal_ast::print(u32 depth,bool last,std::vector<string>& indent)
{
for(auto& i:indent)
std::cout<<i; std::cout<<i;
std::cout<<ast_name[_type]; std::cout<<ast_name[_type];
if(_type==ast_str || _type==ast_id || if(_type==ast_str || _type==ast_id ||
@ -221,22 +227,22 @@ void nasal_ast::print(u32 depth,bool last=false)
std::cout<<":"<<_num; std::cout<<":"<<_num;
std::cout<<'\n'; std::cout<<'\n';
if(last && depth) if(last && depth)
intentation.back()=" "; indent.back()=" ";
else if(!last && depth) else if(!last && depth)
#ifdef _WIN32 #ifdef _WIN32
intentation.back()="| "; indent.back()="| ";
#else #else
intentation.back()=""; indent.back()="";
#endif #endif
for(u32 i=0;i<_child.size();++i) for(u32 i=0;i<_child.size();++i)
{ {
#ifdef _WIN32 #ifdef _WIN32
intentation.push_back(i==_child.size()-1?"`-":"|-"); indent.push_back(i==_child.size()-1?"+-":"|-");
#else #else
intentation.push_back(i==_child.size()-1?"└─":"├─"); indent.push_back(i==_child.size()-1?"└─":"├─");
#endif #endif
_child[i].print(depth+1,i==_child.size()-1); _child[i].print(depth+1,i==_child.size()-1,indent);
intentation.pop_back(); indent.pop_back();
} }
} }

View File

@ -99,7 +99,7 @@ private:
nasal_ast ret_expr(); nasal_ast ret_expr();
public: public:
nasal_parse(nasal_err& e):ptr(0),in_func(0),in_loop(0),tokens(nullptr),nerr(e){} nasal_parse(nasal_err& e):ptr(0),in_func(0),in_loop(0),tokens(nullptr),nerr(e){}
void print(){root.print(0);} void print(){root.tree();}
void compile(const nasal_lexer&); void compile(const nasal_lexer&);
nasal_ast& ast(){return root;} nasal_ast& ast(){return root;}
const nasal_ast& ast() const {return root;} const nasal_ast& ast() const {return root;}