diff options
author | John Hodge <tpg@mutabah.net> | 2015-03-26 20:30:30 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2015-03-26 20:30:30 +0800 |
commit | ab649f75cfe79fd91ca60f55fb93d9fe5c9e3a53 (patch) | |
tree | 9449cad89a115a5968d49636f698495220da566d /src | |
parent | 7634647e1fcb7b1b6bcefd992d65b6b25277dfcc (diff) | |
download | mrust-ab649f75cfe79fd91ca60f55fb93d9fe5c9e3a53.tar.gz |
Integer literal output, signed pattern hackery
Diffstat (limited to 'src')
-rw-r--r-- | src/dump_as_rust.cpp | 48 | ||||
-rw-r--r-- | src/parse/pattern.cpp | 8 |
2 files changed, 35 insertions, 21 deletions
diff --git a/src/dump_as_rust.cpp b/src/dump_as_rust.cpp index 20dc3ab5..6155caec 100644 --- a/src/dump_as_rust.cpp +++ b/src/dump_as_rust.cpp @@ -92,7 +92,7 @@ public: virtual void visit(AST::ExprNode_LetBinding& n) override { m_expr_root = false; m_os << "let "; - print_pattern(n.m_pat); + print_pattern(n.m_pat, false); m_os << ": "; print_type(n.m_type); m_os << " = "; @@ -183,13 +183,13 @@ public: break; case AST::ExprNode_Loop::WHILELET: m_os << "while let "; - print_pattern(n.m_pattern); + print_pattern(n.m_pattern, true); m_os << " = "; AST::NodeVisitor::visit(n.m_cond); break; case AST::ExprNode_Loop::FOR: m_os << "while for "; - print_pattern(n.m_pattern); + print_pattern(n.m_pattern, true); m_os << " in "; AST::NodeVisitor::visit(n.m_cond); break; @@ -232,7 +232,7 @@ public: if(!is_first) m_os << "|"; is_first = false; - print_pattern(pat); + print_pattern(pat, true); } if( arm.m_cond ) { @@ -269,7 +269,7 @@ public: bool expr_root = m_expr_root; m_expr_root = false; m_os << "if let "; - print_pattern(n.m_pattern); + print_pattern(n.m_pattern, true); m_os << " = "; AST::NodeVisitor::visit(n.m_value); @@ -317,7 +317,7 @@ public: { if(!is_first) m_os << ", "; is_first = false; - print_pattern(arg.first); + print_pattern(arg.first, false); m_os << ": "; print_type(arg.second); } @@ -352,7 +352,8 @@ public: case CORETYPE_I32: case CORETYPE_I64: case CORETYPE_INT: - m_os << n.m_value; + m_os << (int64_t)n.m_value; + //m_os << "0x" << ::std::hex << n.m_value << ::std::dec; break; } } @@ -520,7 +521,7 @@ private: void print_params(const AST::TypeParams& params); void print_bounds(const AST::TypeParams& params); - void print_pattern(const AST::Pattern& p); + void print_pattern(const AST::Pattern& p, bool is_refutable); void print_type(const TypeRef& t); void inc_indent(); @@ -703,10 +704,17 @@ void RustPrinter::print_bounds(const AST::TypeParams& params) } } -void RustPrinter::print_pattern(const AST::Pattern& p) +void RustPrinter::print_pattern(const AST::Pattern& p, bool is_refutable) { - if( p.binding() != "" ) - m_os << p.binding() << " @ "; + if( p.binding() != "" ) { + m_os << p.binding(); + // If binding is irrefutable, and would be binding against a wildcard, just emit the name + if( !is_refutable && p.data().tag() == AST::Pattern::Data::Any ) + { + return ; + } + m_os << " @ "; + } switch(p.data().tag()) { case AST::Pattern::Data::Any: @@ -718,19 +726,21 @@ void RustPrinter::print_pattern(const AST::Pattern& p) case AST::Pattern::Data::Ref: { const auto& v = p.data().as_Ref(); m_os << "& "; - print_pattern(*v.sub); + print_pattern(*v.sub, is_refutable); break; } case AST::Pattern::Data::Value: { auto& v = p.data().as_Value(); - m_os << *v.start; - if( v.end.get() ) - m_os << " ... " << *v.end; + v.start->visit(*this); + if( v.end.get() ) { + m_os << " ... "; + v.end->visit(*this); + } break; } case AST::Pattern::Data::StructTuple: { const auto& v = p.data().as_StructTuple(); m_os << v.path << "("; for(const auto& sp : v.sub_patterns) { - print_pattern(sp); + print_pattern(sp, is_refutable); m_os << ","; } m_os << ")"; @@ -740,7 +750,7 @@ void RustPrinter::print_pattern(const AST::Pattern& p) m_os << v.path << "("; for(const auto& sp : v.sub_patterns) { m_os << sp.first << ": "; - print_pattern(sp.second); + print_pattern(sp.second, is_refutable); m_os << ","; } m_os << ")"; @@ -749,7 +759,7 @@ void RustPrinter::print_pattern(const AST::Pattern& p) const auto& v = p.data().as_Tuple(); m_os << "("; for(const auto& sp : v.sub_patterns) { - print_pattern(sp); + print_pattern(sp, is_refutable); m_os << ","; } m_os << ")"; @@ -857,7 +867,7 @@ void RustPrinter::handle_function(const AST::Item<AST::Function>& f) { if( !is_first ) m_os << ", "; - print_pattern( a.first ); + print_pattern( a.first, false ); m_os << ": " << a.second.print_pretty(); is_first = false; } diff --git a/src/parse/pattern.cpp b/src/parse/pattern.cpp index 97571bc2..1da59951 100644 --- a/src/parse/pattern.cpp +++ b/src/parse/pattern.cpp @@ -167,9 +167,13 @@ AST::Pattern Parse_PatternReal1(TokenStream& lex, bool is_refutable) case TOK_DOUBLE_COLON: // 2. Paths are enum/struct names return Parse_PatternReal_Path( lex, Parse_Path(lex, true, PATH_GENERIC_EXPR), is_refutable ); - case TOK_DASH: + case TOK_DASH: { GET_CHECK_TOK(tok, lex, TOK_INTEGER); - return AST::Pattern( AST::Pattern::TagValue(), NEWNODE(AST::ExprNode_Integer, -tok.intval(), tok.datatype()) ); + auto dt = tok.datatype(); + if(dt == CORETYPE_ANY) + dt = CORETYPE_I32; + return AST::Pattern( AST::Pattern::TagValue(), NEWNODE(AST::ExprNode_Integer, -tok.intval(), dt) ); + } case TOK_INTEGER: return AST::Pattern( AST::Pattern::TagValue(), NEWNODE(AST::ExprNode_Integer, tok.intval(), tok.datatype()) ); case TOK_RWORD_TRUE: |