diff options
-rw-r--r-- | src/ast/ast.cpp | 32 | ||||
-rw-r--r-- | src/ast/path.hpp | 9 | ||||
-rw-r--r-- | src/convert/resolve.cpp | 24 | ||||
-rw-r--r-- | src/parse/expr.cpp | 8 |
4 files changed, 69 insertions, 4 deletions
diff --git a/src/ast/ast.cpp b/src/ast/ast.cpp index 994d3999..438aa2cd 100644 --- a/src/ast/ast.cpp +++ b/src/ast/ast.cpp @@ -4,10 +4,12 @@ #include "../types.hpp"
#include <iostream>
#include "../parse/parseerror.hpp"
+#include <algorithm>
namespace AST {
+// --- AST::PathNode
PathNode::PathNode(::std::string name, ::std::vector<TypeRef> args):
m_name(name),
m_params(args)
@@ -22,6 +24,36 @@ const ::std::vector<TypeRef>& PathNode::args() const return m_params;
}
+// --- AST::Path
+void Path::resolve(const Crate& crate)
+{
+ DEBUG("*this = " << *this);
+ if(m_class != ABSOLUTE)
+ throw ParseError::BugCheck("Calling Path::resolve on non-absolute path");
+
+ const Module* mod = &crate.root_module();
+ for(int i = 0; i < m_nodes.size(); i ++ )
+ {
+ const PathNode& node = m_nodes[i];
+ auto& sms = mod->submods();
+
+ auto it = ::std::find_if(sms.begin(), sms.end(), [&node](const ::std::pair<Module,bool>& x) {
+ return x.first.name() == node.name();
+ });
+ if( it != sms.end() )
+ continue;
+
+ // Start searching for:
+ // - Re-exports
+ // - Functions
+ // - Structs
+ // - Enums (and variants)
+
+ }
+
+ throw ParseError::Todo("Path::resolve");
+}
+
Path& Path::operator+=(const Path& other)
{
for(auto& node : other.m_nodes)
diff --git a/src/ast/path.hpp b/src/ast/path.hpp index 182802f2..05439e06 100644 --- a/src/ast/path.hpp +++ b/src/ast/path.hpp @@ -11,6 +11,8 @@ class TypeRef; namespace AST { +class Crate; + class TypeParam { public: @@ -73,6 +75,11 @@ public: ret.m_nodes.push_back(ent); return ret; } + Path operator+(PathNode&& pn) const { + Path tmp; + tmp.append( ::std::move(pn) ); + return Path(*this) += tmp; + } Path operator+(const ::std::string& s) const { Path tmp; tmp.append(PathNode(s, {})); @@ -87,6 +94,8 @@ public: m_nodes.push_back(node); } + void resolve(const Crate& crate); + bool is_relative() const { return m_class == RELATIVE; } size_t size() const { return m_nodes.size(); } ::std::vector<PathNode>& nodes() { return m_nodes; } diff --git a/src/convert/resolve.cpp b/src/convert/resolve.cpp index b32b10a5..6ea3fcc6 100644 --- a/src/convert/resolve.cpp +++ b/src/convert/resolve.cpp @@ -248,21 +248,35 @@ void ResolvePaths_HandleFunction(const AST::Crate& crate, const AST::Module& mod pr.handle_function(fcn);
}
-void ResolvePaths_HandleModule(const AST::Crate& crate, const AST::Path& modpath, AST::Module& mod)
+void ResolvePaths_HandleModule_Use(const AST::Crate& crate, const AST::Path& modpath, AST::Module& mod)
{
- // TODO: Handle 'use' statements in an earlier pass, to avoid dependency issues?
- // - Maybe resolve wildcards etc when used?
for( auto& imp : mod.imports() )
{
// TODO: Handle 'super' and 'self' imports
+ // - Any other type of import will be absolute
+
if( imp.name == "" )
{
+ if( imp.is_pub ) {
+ throw ParseError::Generic("Wildcard uses can't be public");
+ }
+
// Wildcard import
+ AST::Path& basepath = imp.data;
+ basepath.resolve(crate);
throw ParseError::Todo("ResolvePaths_HandleModule - wildcard use");
}
}
+ for( auto& submod : mod.submods() )
+ {
+ ResolvePaths_HandleModule_Use(crate, modpath + submod.first.name(), submod.first);
+ }
+}
+
+void ResolvePaths_HandleModule(const AST::Crate& crate, const AST::Path& modpath, AST::Module& mod)
+{
for( auto& fcn : mod.functions() )
{
ResolvePaths_HandleFunction(crate, mod, fcn.first);
@@ -276,5 +290,9 @@ void ResolvePaths_HandleModule(const AST::Crate& crate, const AST::Path& modpath void ResolvePaths(AST::Crate& crate)
{
+ // Handle 'use' statements in an initial parss
+ ResolvePaths_HandleModule_Use(crate, AST::Path(AST::Path::TagAbsolute()), crate.root_module());
+
+ // Then do path resolution on all other item
ResolvePaths_HandleModule(crate, AST::Path(AST::Path::TagAbsolute()), crate.root_module());
}
diff --git a/src/parse/expr.cpp b/src/parse/expr.cpp index fd6f14b8..478673e3 100644 --- a/src/parse/expr.cpp +++ b/src/parse/expr.cpp @@ -402,7 +402,7 @@ ExprNodeP Parse_ExprFC(TokenStream& lex) switch(GET_TOK(tok, lex))
{
case TOK_PAREN_OPEN:
- // Function call
+ // Expression method call
lex.putback(tok);
val = NEWNODE( AST::ExprNode_CallObject, ::std::move(val), Parse_ParenList(lex) );
break;
@@ -411,6 +411,12 @@ ExprNodeP Parse_ExprFC(TokenStream& lex) // TODO: What about tuple indexing?
GET_CHECK_TOK(tok, lex, TOK_IDENT);
val = NEWNODE( AST::ExprNode_Field, ::std::move(val), ::std::string(tok.str()) );
+ //if( GET_TOK(tok, lex) == TOK_PAREN_OPEN || tok.type() == TOK_DOUBLE_COLON ) {
+ // throw ParseError::Todo("method calls");
+ //}
+ //else {
+ // lex.putback(tok);
+ //}
break;
default:
lex.putback(tok);
|