diff options
author | John Hodge <tpg@ucc.asn.au> | 2017-09-29 23:06:58 +0800 |
---|---|---|
committer | John Hodge <tpg@ucc.asn.au> | 2017-09-29 23:06:58 +0800 |
commit | 1563fdae306843e4165cb4d9249245ca0631e497 (patch) | |
tree | 20828f286dd0e0331dc1278d424cfeb27e73a9ad /src | |
parent | 6e1790a5d9d67d95523b5553a1602aeb619a714b (diff) | |
download | mrust-1563fdae306843e4165cb4d9249245ca0631e497.tar.gz |
AST - Support printing AST types in a clean form
Diffstat (limited to 'src')
-rw-r--r-- | src/ast/expr.cpp | 2 | ||||
-rw-r--r-- | src/ast/path.cpp | 15 | ||||
-rw-r--r-- | src/ast/path.hpp | 2 | ||||
-rw-r--r-- | src/ast/types.cpp | 106 | ||||
-rw-r--r-- | src/ast/types.hpp | 2 | ||||
-rw-r--r-- | src/parse/token.cpp | 13 |
6 files changed, 57 insertions, 83 deletions
diff --git a/src/ast/expr.cpp b/src/ast/expr.cpp index 7cf10b27..1142b763 100644 --- a/src/ast/expr.cpp +++ b/src/ast/expr.cpp @@ -94,7 +94,7 @@ NODE(ExprNode_Macro, { { os << " " << m_ident << " "; } - os << "(" << ")"; + os << "(" << " /*TODO*/ " << ")"; },{ return NEWNODE(ExprNode_Macro, m_name, m_ident, m_tokens.clone()); }) diff --git a/src/ast/path.cpp b/src/ast/path.cpp index ae4ff626..690bc9eb 100644 --- a/src/ast/path.cpp +++ b/src/ast/path.cpp @@ -253,7 +253,7 @@ Ordering Path::ord(const Path& x) const return OrdEqual; } -void Path::print_pretty(::std::ostream& os, bool is_type_context) const +void Path::print_pretty(::std::ostream& os, bool is_type_context, bool is_debug) const { TU_MATCH(Path::Class, (m_class), (ent), (Invalid, @@ -264,13 +264,17 @@ void Path::print_pretty(::std::ostream& os, bool is_type_context) const (Local, // Only print comment if there's no binding if( m_binding.is_Unbound() ) - os << "/*var*/"; + { + if( is_debug ) + os << "/*var*/"; + } else assert( m_binding.is_Variable() ); os << ent.name; ), (Relative, - os << ent.hygiene; + if( is_debug ) + os << ent.hygiene; for(const auto& n : ent.nodes) { if( &n != &ent.nodes[0] ) { @@ -325,12 +329,13 @@ void Path::print_pretty(::std::ostream& os, bool is_type_context) const } ) ) - os << "/*" << m_binding << "*/"; + if( is_debug ) + os << "/*" << m_binding << "*/"; } ::std::ostream& operator<<(::std::ostream& os, const Path& path) { - path.print_pretty(os, false); + path.print_pretty(os, false, true); return os; } diff --git a/src/ast/path.hpp b/src/ast/path.hpp index f3a72133..2126e3b2 100644 --- a/src/ast/path.hpp +++ b/src/ast/path.hpp @@ -337,7 +337,7 @@ public: bool operator!=(const Path& x) const { return ord(x) != OrdEqual; } bool operator<(const Path& x) const { return ord(x) != OrdLess; } - void print_pretty(::std::ostream& os, bool is_type_context) const; + void print_pretty(::std::ostream& os, bool is_type_context, bool is_debug=false) const; friend ::std::ostream& operator<<(::std::ostream& os, const Path& path); private: static void resolve_args_nl(::std::vector<PathNode>& nodes, ::std::function<TypeRef(const char*)> fcn); diff --git a/src/ast/types.cpp b/src/ast/types.cpp index f2387068..af99f3d7 100644 --- a/src/ast/types.cpp +++ b/src/ast/types.cpp @@ -202,10 +202,11 @@ Ordering TypeRef::ord(const TypeRef& x) const return os << coretype_name(ct); } -::std::ostream& operator<<(::std::ostream& os, const TypeRef& tr) { +void TypeRef::print(::std::ostream& os, bool is_debug/*=false*/) const +{ //os << "TypeRef("; - #define _(VAR, ...) case TypeData::TAG_##VAR: { const auto &ent = tr.m_data.as_##VAR(); (void)&ent; __VA_ARGS__ } break; - switch(tr.m_data.tag()) + #define _(VAR, ...) case TypeData::TAG_##VAR: { const auto &ent = this->m_data.as_##VAR(); (void)&ent; __VA_ARGS__ } break; + switch(this->m_data.tag()) { case TypeData::TAGDEAD: throw ""; _(None, @@ -224,49 +225,59 @@ Ordering TypeRef::ord(const TypeRef& x) const os << "()"; ) _(Primitive, - os << tr.m_data.as_Primitive().core_type; + os << ent.core_type; ) _(Function, if( ent.info.m_abi != "" ) os << "extern \"" << ent.info.m_abi << "\" "; os << "fn ("; for( const auto& arg : ent.info.m_arg_types ) - os << arg << ", "; + { + arg.print(os, is_debug); + os << ", "; + } os << ") -> " << *ent.info.m_rettype; ) _(Tuple, - //os << "TagTuple, {" << tr.m_inner_types << "}"; os << "( "; for( const auto& it : ent.inner_types ) - os << it << ", "; + { + it.print(os, is_debug); + os << ", "; + } os << ")"; ) _(Borrow, - //os << "TagReference, " << (tr.m_is_inner_mutable ? "mut" : "const") << ", " << tr.m_inner_types[0]; - os << "&" << (ent.is_mut ? "mut " : "") << *ent.inner; + os << "&" << (ent.is_mut ? "mut " : ""); + ent.inner->print(os, is_debug); ) _(Pointer, - //os << "TagPointer, " << (tr.m_is_inner_mutable ? "mut" : "const") << ", " << tr.m_inner_types[0]; - os << "*" << (ent.is_mut ? "mut" : "const") << " " << *ent.inner; + os << "*" << (ent.is_mut ? "mut" : "const"); + ent.inner->print(os, is_debug); ) _(Array, - os << "[" << *ent.inner; + os << "["; + ent.inner->print(os, is_debug); if( ent.size.get() ) os << "; " << *ent.size; os << "]"; ) _(Generic, - os << "/* arg */ " << ent.name << "/*"<<ent.index<<"*/"; + if(is_debug) + os << "/* arg */ "; + os << ent.name; + if(is_debug) + os << "/*"<<ent.index<<"*/"; ) _(Path, - os << ent.path; + ent.path.print_pretty(os, true, is_debug); ) _(TraitObject, os << "("; for( const auto& it : ent.traits ) { if( &it != &ent.traits.front() ) os << "+"; - os << it; + it.print_pretty(os, true, is_debug); } os << ")"; ) @@ -275,71 +286,20 @@ Ordering TypeRef::ord(const TypeRef& x) const for( const auto& it : ent.traits ) { if( &it != &ent.traits.front() ) os << "+"; - os << it; + it.print_pretty(os, true, is_debug); } os << ""; ) } #undef _ - //os << ")"; - return os; } - -void PrettyPrintType::print(::std::ostream& os) const -{ - #if 1 - os << m_type; - #else - switch(m_type.m_class) - { - case TypeRef::ANY: - os << "_"; - if( m_type.m_inner_types.size() ) { - os << "/* : " << m_type.m_inner_types << "*/"; - } - break; - case TypeRef::UNIT: - os << "()"; - break; - case TypeRef::PRIMITIVE: - os << m_type.m_core_type; - break; - case TypeRef::FUNCTION: - if( m_type.m_path[0].name() != "" ) - os << "extern \"" << m_type.m_path[0].name() << "\" "; - os << "fn ("; - for( unsigned int i = 0; i < m_type.m_inner_types.size()-1; i ++ ) - os << m_type.m_inner_types[i].print_pretty() << ", "; - os << ") -> " << m_type.m_inner_types.back().print_pretty(); - break; - case TypeRef::TUPLE: - os << "("; - for(const auto& t : m_type.m_inner_types) - os << t.print_pretty() << ","; - os << ")"; - break; - case TypeRef::REFERENCE: - os << "&" << (m_type.m_is_inner_mutable ? "mut " : "") << m_type.m_inner_types[0].print_pretty(); - break; - case TypeRef::POINTER: - os << "*" << (m_type.m_is_inner_mutable ? "mut" : "const") << " " << m_type.m_inner_types[0].print_pretty(); - break; - case TypeRef::ARRAY: - os << "[" << m_type.m_inner_types[0].print_pretty() << ", " << m_type.m_size_expr << "]"; - break; - case TypeRef::GENERIC: - os << m_type.m_path[0].name(); - break; - case TypeRef::PATH: - os << m_type.m_path; - break; - } - #endif +::std::ostream& operator<<(::std::ostream& os, const TypeRef& tr) { + tr.print(os, true); + return os; } - -::std::ostream& operator<<(::std::ostream& os, const PrettyPrintType& v) -{ - v.print(os); +::std::ostream& operator<<(::std::ostream& os, const PrettyPrintType& x) { + x.m_type.print(os, false); return os; } + diff --git a/src/ast/types.hpp b/src/ast/types.hpp index 314ab828..820bcf27 100644 --- a/src/ast/types.hpp +++ b/src/ast/types.hpp @@ -266,6 +266,8 @@ public: bool operator!=(const TypeRef& x) const { return ord(x) != OrdEqual; } bool operator<(const TypeRef& x) const { return ord(x) == OrdLess; }; + void print(::std::ostream& os, bool is_debug=false) const; + PrettyPrintType print_pretty() const { return PrettyPrintType(*this); } friend class PrettyPrintType; diff --git a/src/parse/token.cpp b/src/parse/token.cpp index 53ea584f..d626659c 100644 --- a/src/parse/token.cpp +++ b/src/parse/token.cpp @@ -281,6 +281,7 @@ struct EscapedString { ::std::string Token::to_str() const { + ::std::stringstream ss; switch(m_type) { case TOK_NULL: return "/*null*/"; @@ -289,9 +290,15 @@ struct EscapedString { case TOK_NEWLINE: return "\n"; case TOK_WHITESPACE: return " "; case TOK_COMMENT: return "/*" + m_data.as_String() + "*/"; - case TOK_INTERPOLATED_TYPE: return FMT( *reinterpret_cast<const ::TypeRef*>(m_data.as_Fragment()) ); - case TOK_INTERPOLATED_PATH: return FMT( *reinterpret_cast<const ::AST::Path*>(m_data.as_Fragment()) ); - case TOK_INTERPOLATED_PATTERN: return FMT( *reinterpret_cast<const ::AST::Pattern*>(m_data.as_Fragment()) ); + case TOK_INTERPOLATED_TYPE: + reinterpret_cast<const ::TypeRef*>(m_data.as_Fragment())->print(ss, false); + return ss.str(); + case TOK_INTERPOLATED_PATH: + reinterpret_cast<const ::AST::Path*>(m_data.as_Fragment())->print_pretty(ss, true); + return ss.str(); + case TOK_INTERPOLATED_PATTERN: + // TODO: Use a configurable print + return FMT( *reinterpret_cast<const ::AST::Pattern*>(m_data.as_Fragment()) ); case TOK_INTERPOLATED_STMT: case TOK_INTERPOLATED_BLOCK: case TOK_INTERPOLATED_EXPR: { |