Permalink
Browse files

- Fixed issue #174

  • Loading branch information...
1 parent 4872fff commit 0a09b96331427bfe7afbdb57c3ad215ee836816a @felipensp committed Oct 3, 2012
Showing with 28 additions and 9 deletions.
  1. +4 −3 compiler/compiler.cc
  2. +12 −4 compiler/pkgmanager.cc
  3. +1 −1 compiler/pkgmanager.h
  4. +11 −1 compiler/typechecker.cc
View
@@ -172,8 +172,8 @@ void Compiler::buildIR() {
* Direct the driver to parser a new file and return its AST tree
* to be appended to the main tree
*/
-ast::UnscopedBlockNode* Compiler::importFile(Driver& driver, const CString* file,
- ast::Identifier* alias) {
+ast::UnscopedBlockNode* Compiler::importFile(Driver& driver,
+ const CString* file, ast::Identifier* alias) {
if (driver.getFile() == file) {
Compiler::warningf("Recursive import of file `%S' detected!",
@@ -183,7 +183,8 @@ ast::UnscopedBlockNode* Compiler::importFile(Driver& driver, const CString* file
driver.parseFile(file->str());
- return new ast::UnscopedBlockNode(alias, static_cast<ast::BlockNode*>(getAST()));
+ return new ast::UnscopedBlockNode(alias,
+ static_cast<ast::BlockNode*>(getAST()));
}
void Compiler::dumpOpcodes() {
View
@@ -348,9 +348,11 @@ void PackageManager::loadModule(Scope* scope, const CString* const package,
}
/**
- * Copy user-defined function in a scope to another supplied alias
+ * Copy user-defined function in a scope to another scope using the
+ * supplied alias to prefix its symbol names
*/
-void PackageManager::copyScopeToAlias(Scope* scope, const std::string& alias) {
+void PackageManager::copyScopeToAlias(Scope* scope, Scope* dscope,
+ const std::string& alias) {
SymbolMap& symbols = scope->getSymbols();
SymbolMap::const_iterator it2(symbols.begin()), end2(symbols.end());
std::vector<ValuePair> callables;
@@ -381,17 +383,23 @@ void PackageManager::copyScopeToAlias(Scope* scope, const std::string& alias) {
std::vector<ValuePair>::const_iterator it_call(callables.begin()),
it_end(callables.end());
+ /*
+ * Pushing the new symbols (functions) to the scope destination
+ */
while (it_call != it_end) {
- scope->pushValue(it_call->first, it_call->second);
+ dscope->pushValue(it_call->first, it_call->second);
it_call->second->addRef();
++it_call;
}
std::vector<TypePair>::const_iterator it_alias(type_alias.begin()),
it_end2(type_alias.end());
+ /*
+ * Pushing the new symbols (types) to the scope destination
+ */
while (it_alias != it_end2) {
- scope->pushType(it_alias->first, it_alias->second);
+ dscope->pushType(it_alias->first, it_alias->second);
const_cast<Type*>(it_alias->second)->addRef();
++it_alias;
}
View
@@ -99,7 +99,7 @@ class PackageManager {
const CString* const);
// Copy function in the scope to another alias
- void copyScopeToAlias(Scope*, const std::string&);
+ void copyScopeToAlias(Scope*, Scope*, const std::string&);
const PackageMap& getPackages() const { return m_packages; }
private:
View
@@ -812,6 +812,14 @@ AST_VISITOR(TypeChecker, UnscopedBlockNode) {
const CString* alias = expr->getAlias();
const NodeList& nodes = expr->getBlock()->getNodes();
NodeList::const_iterator it = nodes.begin(), end = nodes.end();
+
+ if (alias) {
+ /*
+ * When an alias is supplied, we have to create a new scope and
+ * so copy everything with aliased name to the parent scope
+ */
+ m_scope = m_scope->newChild();
+ }
// Iterates over statements inside the block
while (EXPECTED(it != end)) {
@@ -826,7 +834,9 @@ AST_VISITOR(TypeChecker, UnscopedBlockNode) {
if (alias) {
std::string prefix = alias->str() + "::";
- g_pkgmanager.copyScopeToAlias(m_scope, prefix);
+ g_pkgmanager.copyScopeToAlias(m_scope, m_scope->getParent(), prefix);
+
+ m_scope = m_scope->getParent();
}
}

0 comments on commit 0a09b96

Please sign in to comment.