From 100c843c70d9ee4c8e142cb45ba9b177b7da158d Mon Sep 17 00:00:00 2001 From: John Hodge Date: Thu, 26 Mar 2015 09:48:23 +0800 Subject: Cleaning up rust source output --- src/ast/ast.hpp | 5 +++++ src/ast/expr.cpp | 1 + src/ast/path.cpp | 2 +- src/convert/ast_iterate.cpp | 10 +++++++++- src/convert/resolve.cpp | 43 ++++++++++++++++++++++++++++++++++++++++++- src/dump_as_rust.cpp | 21 ++++++++++++++++++++- 6 files changed, 78 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/ast/ast.hpp b/src/ast/ast.hpp index 9a640024..341b860e 100644 --- a/src/ast/ast.hpp +++ b/src/ast/ast.hpp @@ -50,6 +50,8 @@ public: const ::std::string& name() const { return m_name; } const TypeRef& get_default() const { return m_default; } + TypeRef& get_default() { return m_default; } + bool is_type() const { return m_class == TYPE; } friend ::std::ostream& operator<<(::std::ostream& os, const TypeParam& tp); @@ -278,6 +280,9 @@ public: const TypeRef& type() const { return m_type; } const Expr& value() const { return m_value; } + TypeRef& type() { return m_type; } + Expr& value() { return m_value; } + SERIALISABLE_PROTOTYPES(); }; diff --git a/src/ast/expr.cpp b/src/ast/expr.cpp index 97d951fc..6adec104 100644 --- a/src/ast/expr.cpp +++ b/src/ast/expr.cpp @@ -616,6 +616,7 @@ NV(ExprNode_String, {}) NV(ExprNode_Closure, { + visit(node.m_code); }); NV(ExprNode_StructLiteral, { diff --git a/src/ast/path.cpp b/src/ast/path.cpp index fed46d0c..2edb4fad 100644 --- a/src/ast/path.cpp +++ b/src/ast/path.cpp @@ -33,7 +33,7 @@ bool PathNode::operator==(const PathNode& x) const os << pn.m_name; if( pn.m_params.size() ) { - os << "<"; + os << "::<"; os << pn.m_params; os << ">"; } diff --git a/src/convert/ast_iterate.cpp b/src/convert/ast_iterate.cpp index 3e92e410..a38bcb9c 100644 --- a/src/convert/ast_iterate.cpp +++ b/src/convert/ast_iterate.cpp @@ -27,8 +27,10 @@ void CASTIterator::handle_params(AST::TypeParams& params) DEBUG("params"); for( auto& param : params.params() ) { - if( param.is_type() ) + if( param.is_type() ) { + handle_type(param.get_default()); local_type( param.name(), TypeRef(TypeRef::TagArg(), param.name()) ); + } } DEBUG("Bounds"); for( auto& bound : params.bounds() ) @@ -217,6 +219,12 @@ void CASTIterator::handle_module(AST::Path path, AST::Module& mod) DEBUG("Handling alias " << item.name); handle_alias(path + item.name, item.data); } + for( auto& stat : mod.statics() ) + { + DEBUG("handling static " << stat.name); + handle_type(stat.data.type()); + handle_expr(stat.data.value().node()); + } for( auto& fcn : mod.functions() ) { diff --git a/src/convert/resolve.cpp b/src/convert/resolve.cpp index 62eb0bbf..d0edc165 100644 --- a/src/convert/resolve.cpp +++ b/src/convert/resolve.cpp @@ -187,9 +187,46 @@ public: DEBUG("ExprNode_LetBinding"); AST::NodeVisitor::visit(node.m_value); - + m_res.handle_type(node.m_type); m_res.handle_pattern(node.m_pat, TypeRef()); } + + void visit(AST::ExprNode_StructLiteral& node) override + { + DEBUG("ExprNode_StructLiteral"); + + m_res.handle_path(node.m_path, CASTIterator::MODE_EXPR); + AST::NodeVisitorDef::visit(node); + } + + void visit(AST::ExprNode_Closure& node) override + { + DEBUG("ExprNode_Closure"); + m_res.start_scope(); + for( auto& param : node.m_args ) + { + m_res.handle_type(param.second); + m_res.handle_pattern(param.first, param.second); + } + m_res.handle_type(node.m_return); + AST::NodeVisitor::visit(node.m_code); + m_res.end_scope(); + } + + void visit(AST::ExprNode_Cast& node) override + { + DEBUG("ExprNode_Cast"); + m_res.handle_type(node.m_type); + AST::NodeVisitorDef::visit(node); + } + + void visit(AST::ExprNode_CallMethod& node) override + { + DEBUG("ExprNode_CallMethod"); + for( auto& arg : node.m_method.args() ) + m_res.handle_type(arg); + AST::NodeVisitorDef::visit(node); + } }; CPathResolver::CPathResolver(const AST::Crate& crate): @@ -360,6 +397,10 @@ void CPathResolver::handle_path(AST::Path& path, CASTIterator::PathMode mode) // - Switch the path to be a "LOCAL" path.set_local(); return ; + // Binding is valid + case MODE_BIND: + //break ; + throw ParseError::Todo("TODO: MODE_BIND, but local hit, what do?"); } // TODO: What about sub-types and methods on type params? diff --git a/src/dump_as_rust.cpp b/src/dump_as_rust.cpp index f3fd8f83..20dc3ab5 100644 --- a/src/dump_as_rust.cpp +++ b/src/dump_as_rust.cpp @@ -34,6 +34,10 @@ public: virtual void visit(AST::ExprNode_Block& n) override { m_os << "{"; inc_indent(); + if( n.m_inner_mod.get() ) + { + handle_module(*n.m_inner_mod); + } bool is_first = true; for( auto& child : n.m_nodes ) { @@ -89,13 +93,28 @@ public: m_expr_root = false; m_os << "let "; print_pattern(n.m_pat); + m_os << ": "; + print_type(n.m_type); m_os << " = "; AST::NodeVisitor::visit(n.m_value); } virtual void visit(AST::ExprNode_Assign& n) override { m_expr_root = false; AST::NodeVisitor::visit(n.m_slot); - m_os << " = "; + switch(n.m_op) + { + case AST::ExprNode_Assign::NONE: m_os << " = "; break; + case AST::ExprNode_Assign::ADD: m_os << " += "; break; + case AST::ExprNode_Assign::SUB: m_os << " -= "; break; + case AST::ExprNode_Assign::MUL: m_os << " *= "; break; + case AST::ExprNode_Assign::DIV: m_os << " /= "; break; + case AST::ExprNode_Assign::MOD: m_os << " %= "; break; + case AST::ExprNode_Assign::AND: m_os << " &= "; break; + case AST::ExprNode_Assign::OR: m_os << " |= "; break; + case AST::ExprNode_Assign::XOR: m_os << " ^= "; break; + case AST::ExprNode_Assign::SHR: m_os << " >>= "; break; + case AST::ExprNode_Assign::SHL: m_os << " <<= "; break; + } AST::NodeVisitor::visit(n.m_value); } virtual void visit(AST::ExprNode_CallPath& n) override { -- cgit v1.2.3