From 3044ae80284fb958051ffd2926fcbff470ff8591 Mon Sep 17 00:00:00 2001 From: "John Hodge (sonata)" Date: Thu, 15 Jan 2015 11:44:12 +0800 Subject: Type alias bound to path, considering removing std hack and implementing AST read --- src/ast/ast.cpp | 37 ++++++++++++++++++++++++++----------- src/ast/path.cpp | 9 ++++++++- src/ast/path.hpp | 7 ++++++- src/convert/resolve.cpp | 15 ++++++++++++--- src/parse/expr.cpp | 7 ++----- 5 files changed, 54 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/ast/ast.cpp b/src/ast/ast.cpp index 0c33dcb9..c2686787 100644 --- a/src/ast/ast.cpp +++ b/src/ast/ast.cpp @@ -116,16 +116,6 @@ ExternCrate ExternCrate_std() } )); std_mod.add_submod(true, ::std::move(option)); - // - io - Module io(crate.crate(), "io"); - io.add_typealias(true, "IoResult", TypeAlias( - { TypeParam(false, "T") }, - TypeRef( Path("std", { - PathNode("result",{}), - PathNode("Result", {TypeRef("T"),TypeRef(Path("std", {PathNode("io"), PathNode("IoError")}))}) - }) ) - )); - std_mod.add_submod(true, ::std::move(io)); // - result Module result(crate.crate(), "result"); result.add_enum(true, "Result", Enum( @@ -139,13 +129,38 @@ ExternCrate ExternCrate_std() } )); std_mod.add_submod(true, ::std::move(result)); + // - io + Module io(crate.crate(), "io"); + io.add_typealias(true, "IoResult", TypeAlias( + { TypeParam(false, "T") }, + TypeRef( Path("std", { + PathNode("result",{}), + PathNode("Result", {TypeRef("T"),TypeRef(Path("std", {PathNode("io"), PathNode("IoError")}))}) + }) ) + )); + std_mod.add_submod(true, ::std::move(io)); + // - iter + { + Module iter(crate.crate(), "iter"); + { + Trait iterator; + iterator.add_type("Item", TypeRef()); + //iterator.add_function("next", Function({}, Function::CLASS_REFMETHOD, "Option<::Item>", {}, Expr()); + iter.add_trait(true, "Iterator", ::std::move(iterator)); + } + std_mod.add_submod(true, ::std::move(iter)); + } + // - prelude Module prelude(crate.crate(), "prelude"); // Re-exports - #define USE(mod, name, ...) do{ Path p({__VA_ARGS__}); p.set_crate("std"); mod.add_alias(true, ::std::move(p), name); } while(0) + #define USE(mod, name, ...) do{ Path p("std", {__VA_ARGS__}); mod.add_alias(true, ::std::move(p), name); } while(0) USE(prelude, "Option", PathNode("option", {}), PathNode("Option",{}) ); USE(prelude, "Some", PathNode("option", {}), PathNode("Option",{}), PathNode("Some",{}) ); USE(prelude, "None", PathNode("option", {}), PathNode("Option",{}), PathNode("None",{}) ); + USE(prelude, "Result", PathNode("result", {}), PathNode("Result",{}) ); + USE(prelude, "Ok", PathNode("result", {}), PathNode("Result",{}), PathNode("Ok",{}) ); + USE(prelude, "Err", PathNode("result", {}), PathNode("Result",{}), PathNode("Err",{}) ); std_mod.add_submod(true, prelude); return crate; diff --git a/src/ast/path.cpp b/src/ast/path.cpp index e6700696..636c4166 100644 --- a/src/ast/path.cpp +++ b/src/ast/path.cpp @@ -103,7 +103,14 @@ void Path::resolve(const Crate& root_crate) throw ParseError::Generic("Param count mismatch when referencing type alias"); // Make a copy of the path, replace params with it, then replace *this? // - Maybe leave that up to other code? - throw ParseError::Todo("Path::resolve() type alias"); + if( is_last ) { + m_binding_type = ALIAS; + m_binding.alias = &it->data; + return ; + } + else { + throw ParseError::Todo("Path::resolve() type method"); + } } } diff --git a/src/ast/path.hpp b/src/ast/path.hpp index c6baf607..f4818764 100644 --- a/src/ast/path.hpp +++ b/src/ast/path.hpp @@ -17,6 +17,7 @@ namespace AST { class Crate; class Module; +class TypeAlias; class Enum; class Struct; class Static; @@ -55,10 +56,12 @@ public: enum BindingType { UNBOUND, MODULE, + ALIAS, ENUM, - ENUM_VAR, STRUCT, + STRUCT_METHOD, + ENUM_VAR, FUNCTION, STATIC, }; @@ -90,6 +93,7 @@ private: const Enum* enum_; unsigned int idx; } enumvar; + const TypeAlias* alias; } m_binding; public: Path(): @@ -150,6 +154,7 @@ public: void resolve(const Crate& crate); + bool is_absolute() const { return m_class == ABSOLUTE; } bool is_relative() const { return m_class == RELATIVE; } size_t size() const { return m_nodes.size(); } diff --git a/src/convert/resolve.cpp b/src/convert/resolve.cpp index a696e802..bba534a3 100644 --- a/src/convert/resolve.cpp +++ b/src/convert/resolve.cpp @@ -125,6 +125,15 @@ public: m_res.pop_scope(); } } + + void visit(AST::ExprNode_LetBinding& node) + { + DEBUG("ExprNode_LetBinding"); + + AST::NodeVisitor::visit(node.m_value); + + m_res.handle_pattern(node.m_pat); + } }; CPathResolver::CPathResolver(const AST::Crate& crate, const AST::Module& mod, AST::Path module_path): @@ -148,7 +157,7 @@ void CPathResolver::resolve_path(AST::Path& path, ResolvePathMode mode) const } // Convert to absolute - if( !path.is_relative() ) + if( path.is_absolute() ) { DEBUG("Absolute - binding"); // Already absolute, our job is done @@ -156,8 +165,8 @@ void CPathResolver::resolve_path(AST::Path& path, ResolvePathMode mode) const if( !path.is_bound() ) { path.resolve(m_crate); } - } - else + } + else if( path.is_relative() ) { // If there's a single node, and we're in expresion mode, look for a variable // Otherwise, search for a type diff --git a/src/parse/expr.cpp b/src/parse/expr.cpp index fa9d43e9..fdf35740 100644 --- a/src/parse/expr.cpp +++ b/src/parse/expr.cpp @@ -487,11 +487,8 @@ ExprNodeP Parse_ExprVal(TokenStream& lex) return NEWNODE( AST::ExprNode_Integer, tok.intval(), tok.datatype() ); case TOK_FLOAT: throw ParseError::Todo("Float"); - case TOK_RWORD_SELF: { - AST::Path path; - path.append( AST::PathNode("self", ::std::vector()) ); - return NEWNODE( AST::ExprNode_NamedValue, ::std::move(path) ); - } + case TOK_RWORD_SELF: + return NEWNODE( AST::ExprNode_NamedValue, AST::Path(AST::Path::TagLocal(), "self") ); case TOK_PAREN_OPEN: { ExprNodeP rv = Parse_Expr0(lex); GET_CHECK_TOK(tok, lex, TOK_PAREN_CLOSE); -- cgit v1.2.3