topLevelVarInit finally works, even with infrencing

This commit is contained in:
Nathan Braswell
2015-06-19 12:48:18 -04:00
parent 3a87970eb3
commit e77af0d584
5 changed files with 13 additions and 14 deletions

View File

@@ -210,17 +210,7 @@ void ASTTransformation::secondPassDoClassInsides(NodeTree<ASTData>* typeDef, std
//This function may need to partially instantiate a class template //This function may need to partially instantiate a class template
NodeTree<ASTData>* ASTTransformation::secondPassDeclaration(NodeTree<Symbol>* from, NodeTree<ASTData>* scope, std::map<std::string, Type*> templateTypeReplacements) { NodeTree<ASTData>* ASTTransformation::secondPassDeclaration(NodeTree<Symbol>* from, NodeTree<ASTData>* scope, std::map<std::string, Type*> templateTypeReplacements) {
//Check here for method call (an error here) return transform(from, scope, std::vector<Type>(), false, templateTypeReplacements);
NodeTree<ASTData>* decStmt = addToScope("~enclosing_scope", scope, new NodeTree<ASTData>("declaration_statement", ASTData(declaration_statement)));
std::string newIdentifierStr = concatSymbolTree(from->getChildren()[0]);
Type* identifierType = typeFromTypeNode(from->getChildren()[2], scope, templateTypeReplacements);
std::cout << "Declaring an identifier " << newIdentifierStr << " to be of type " << identifierType->toString() << std::endl;
NodeTree<ASTData>* newIdentifier = addToScope("~enclosing_scope", decStmt, new NodeTree<ASTData>("identifier", ASTData(identifier, Symbol(newIdentifierStr, true), identifierType)));
//scope->getDataRef()->scope[newIdentifierStr].push_back(newIdentifier);
addToScope(newIdentifierStr, newIdentifier, scope); // NEW WAY!
decStmt->addChild(newIdentifier);
return decStmt;
} }
//This function may need to partially instantiate a class template //This function may need to partially instantiate a class template

View File

@@ -182,9 +182,15 @@ std::pair<std::string, std::string> CGenerator::generateTranslationUnit(std::str
ASTData declarationData = declaration->getData(); ASTData declarationData = declaration->getData();
switch(declarationData.type) { switch(declarationData.type) {
case identifier: case identifier:
{
auto parent = declaration->getDataRef()->scope["~enclosing_scope"][0];
if (parent->getChildren().size() == 1)
variableDeclarations += ValueTypeToCType(declarationData.valueType, scopePrefix(declaration) + declarationData.symbol.getName()) + "; /*identifier*/\n"; variableDeclarations += ValueTypeToCType(declarationData.valueType, scopePrefix(declaration) + declarationData.symbol.getName()) + "; /*identifier*/\n";
else
variableDeclarations += ValueTypeToCType(declarationData.valueType, generate(parent->getChildren()[0], nullptr, true).oneString()) + " = " + generate(parent->getChildren()[1], nullptr, true).oneString() + ";";
variableExternDeclarations += "extern " + ValueTypeToCType(declarationData.valueType, declarationData.symbol.getName()) + "; /*extern identifier*/\n"; variableExternDeclarations += "extern " + ValueTypeToCType(declarationData.valueType, declarationData.symbol.getName()) + "; /*extern identifier*/\n";
break; break;
}
case function: case function:
{ {
if (declarationData.valueType->baseType == template_type) if (declarationData.valueType->baseType == template_type)

View File

@@ -1,12 +1,12 @@
Qualified io! Qualified io!
0 7
9 9
11 11
Qualified Container! Qualified Container!
Even template functions qualified! Even template functions qualified!
Unqualified io! Unqualified io!
0 8
10 10
12 12
Unqualified Container! Unqualified Container!

View File

@@ -1 +1,2 @@
42 42
hi

View File

@@ -1,8 +1,10 @@
import io; import io;
var a: int = 42; var a: int = 42;
var b = "hi";
fun main(): int { fun main(): int {
io::println(a); io::println(a);
io::println(b);
return 0; return 0;
} }