Improve printing of ASTs
This commit is contained in:
		
							parent
							
								
									84a13dc576
								
							
						
					
					
						commit
						ecc2c8f464
					
				
					 3 changed files with 58 additions and 16 deletions
				
			
		|  | @ -16,6 +16,47 @@ std::ostream & operator << (std::ostream & str, Expr & e) | |||
|     return str; | ||||
| } | ||||
| 
 | ||||
| static void showString(std::ostream & str, const string & s) | ||||
| { | ||||
|     str << '"'; | ||||
|     for (auto c : (string) s) | ||||
|         if (c == '"' || c == '\\' || c == '$') str << "\\" << c; | ||||
|         else if (c == '\n') str << "\\n"; | ||||
|         else if (c == '\r') str << "\\r"; | ||||
|         else if (c == '\t') str << "\\t"; | ||||
|         else str << c; | ||||
|     str << '"'; | ||||
| } | ||||
| 
 | ||||
| static void showId(std::ostream & str, const string & s) | ||||
| { | ||||
|     assert(!s.empty()); | ||||
|     if (s == "if") | ||||
|         str << '"' << s << '"'; | ||||
|     else { | ||||
|         char c = s[0]; | ||||
|         if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_')) { | ||||
|             showString(str, s); | ||||
|             return; | ||||
|         } | ||||
|         for (auto c : s) | ||||
|             if (!((c >= 'a' && c <= 'z') || | ||||
|                   (c >= 'A' && c <= 'Z') || | ||||
|                   (c >= '0' && c <= '9') || | ||||
|                   c == '_' || c == '\'' || c == '-')) { | ||||
|                 showString(str, s); | ||||
|                 return; | ||||
|             } | ||||
|         str << s; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| std::ostream & operator << (std::ostream & str, const Symbol & sym) | ||||
| { | ||||
|     showId(str, *sym.s); | ||||
|     return str; | ||||
| } | ||||
| 
 | ||||
| void Expr::show(std::ostream & str) | ||||
| { | ||||
|     abort(); | ||||
|  | @ -28,7 +69,7 @@ void ExprInt::show(std::ostream & str) | |||
| 
 | ||||
| void ExprString::show(std::ostream & str) | ||||
| { | ||||
|     str << "\"" << s << "\""; // !!! escaping
 | ||||
|     showString(str, s); | ||||
| } | ||||
| 
 | ||||
| void ExprPath::show(std::ostream & str) | ||||
|  | @ -44,12 +85,12 @@ void ExprVar::show(std::ostream & str) | |||
| void ExprSelect::show(std::ostream & str) | ||||
| { | ||||
|     str << "(" << *e << ")." << showAttrPath(attrPath); | ||||
|     if (def) str << " or " << *def; | ||||
|     if (def) str << " or (" << *def << ")"; | ||||
| } | ||||
| 
 | ||||
| void ExprOpHasAttr::show(std::ostream & str) | ||||
| { | ||||
|     str << "(" << *e << ") ? " << showAttrPath(attrPath); | ||||
|     str << "((" << *e << ") ? " << showAttrPath(attrPath) << ")"; | ||||
| } | ||||
| 
 | ||||
| void ExprAttrs::show(std::ostream & str) | ||||
|  | @ -85,6 +126,10 @@ void ExprLambda::show(std::ostream & str) | |||
|             str << i->name; | ||||
|             if (i->def) str << " ? " << *i->def; | ||||
|         } | ||||
|         if (formals->ellipsis) { | ||||
|             if (!first) str << ", "; | ||||
|             str << "..."; | ||||
|         } | ||||
|         str << " }"; | ||||
|         if (!arg.empty()) str << " @ "; | ||||
|     } | ||||
|  | @ -94,23 +139,24 @@ void ExprLambda::show(std::ostream & str) | |||
| 
 | ||||
| void ExprLet::show(std::ostream & str) | ||||
| { | ||||
|     str << "let "; | ||||
|     str << "(let "; | ||||
|     foreach (ExprAttrs::AttrDefs::iterator, i, attrs->attrs) | ||||
|         if (i->second.inherited) | ||||
|         if (i->second.inherited) { | ||||
|             str << "inherit " << i->first << "; "; | ||||
|         } | ||||
|         else | ||||
|             str << i->first << " = " << *i->second.e << "; "; | ||||
|     str << "in " << *body; | ||||
|     str << "in " << *body << ")"; | ||||
| } | ||||
| 
 | ||||
| void ExprWith::show(std::ostream & str) | ||||
| { | ||||
|     str << "with " << *attrs << "; " << *body; | ||||
|     str << "(with " << *attrs << "; " << *body << ")"; | ||||
| } | ||||
| 
 | ||||
| void ExprIf::show(std::ostream & str) | ||||
| { | ||||
|     str << "if " << *cond << " then " << *then << " else " << *else_; | ||||
|     str << "(if " << *cond << " then " << *then << " else " << *else_ << ")"; | ||||
| } | ||||
| 
 | ||||
| void ExprAssert::show(std::ostream & str) | ||||
|  | @ -120,16 +166,18 @@ void ExprAssert::show(std::ostream & str) | |||
| 
 | ||||
| void ExprOpNot::show(std::ostream & str) | ||||
| { | ||||
|     str << "! " << *e; | ||||
|     str << "(! " << *e << ")"; | ||||
| } | ||||
| 
 | ||||
| void ExprConcatStrings::show(std::ostream & str) | ||||
| { | ||||
|     bool first = true; | ||||
|     str << "("; | ||||
|     foreach (vector<Expr *>::iterator, i, *es) { | ||||
|         if (first) first = false; else str << " + "; | ||||
|         str << **i; | ||||
|     } | ||||
|     str << ")"; | ||||
| } | ||||
| 
 | ||||
| void ExprPos::show(std::ostream & str) | ||||
|  |  | |||
|  | @ -280,7 +280,7 @@ struct ExprOpNot : Expr | |||
|         Expr##name(const Pos & pos, Expr * e1, Expr * e2) : pos(pos), e1(e1), e2(e2) { }; \ | ||||
|         void show(std::ostream & str) \ | ||||
|         { \ | ||||
|             str << *e1 << " " s " " << *e2; \ | ||||
|             str << "(" << *e1 << " " s " " << *e2 << ")";   \ | ||||
|         } \ | ||||
|         void bindVars(const StaticEnv & env) \ | ||||
|         { \ | ||||
|  |  | |||
|  | @ -58,12 +58,6 @@ public: | |||
|     friend std::ostream & operator << (std::ostream & str, const Symbol & sym); | ||||
| }; | ||||
| 
 | ||||
| inline std::ostream & operator << (std::ostream & str, const Symbol & sym) | ||||
| { | ||||
|     str << *sym.s; | ||||
|     return str; | ||||
| } | ||||
| 
 | ||||
| class SymbolTable | ||||
| { | ||||
| private: | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue