From e77af0d5845d7304e34e5b1d891ae68cd128d8e3 Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Fri, 19 Jun 2015 12:48:18 -0400 Subject: [PATCH] topLevelVarInit finally works, even with infrencing --- src/ASTTransformation.cpp | 12 +----------- src/CGenerator.cpp | 8 +++++++- tests/test_newScoping.expected_results | 4 ++-- tests/test_topLevelVarInit.expected_results | 1 + tests/test_topLevelVarInit.krak | 2 ++ 5 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/ASTTransformation.cpp b/src/ASTTransformation.cpp index cfbbad1..e3c018e 100644 --- a/src/ASTTransformation.cpp +++ b/src/ASTTransformation.cpp @@ -210,17 +210,7 @@ void ASTTransformation::secondPassDoClassInsides(NodeTree* typeDef, std //This function may need to partially instantiate a class template NodeTree* ASTTransformation::secondPassDeclaration(NodeTree* from, NodeTree* scope, std::map templateTypeReplacements) { - //Check here for method call (an error here) - NodeTree* decStmt = addToScope("~enclosing_scope", scope, new NodeTree("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* newIdentifier = addToScope("~enclosing_scope", decStmt, new NodeTree("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; + return transform(from, scope, std::vector(), false, templateTypeReplacements); } //This function may need to partially instantiate a class template diff --git a/src/CGenerator.cpp b/src/CGenerator.cpp index b6e68a3..9f1dff8 100644 --- a/src/CGenerator.cpp +++ b/src/CGenerator.cpp @@ -182,9 +182,15 @@ std::pair CGenerator::generateTranslationUnit(std::str ASTData declarationData = declaration->getData(); switch(declarationData.type) { case identifier: - variableDeclarations += ValueTypeToCType(declarationData.valueType, scopePrefix(declaration) + declarationData.symbol.getName()) + "; /*identifier*/\n"; + { + auto parent = declaration->getDataRef()->scope["~enclosing_scope"][0]; + if (parent->getChildren().size() == 1) + 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"; break; + } case function: { if (declarationData.valueType->baseType == template_type) diff --git a/tests/test_newScoping.expected_results b/tests/test_newScoping.expected_results index 2d170cc..1e96910 100644 --- a/tests/test_newScoping.expected_results +++ b/tests/test_newScoping.expected_results @@ -1,12 +1,12 @@ Qualified io! -0 +7 9 11 Qualified Container! Even template functions qualified! Unqualified io! -0 +8 10 12 Unqualified Container! diff --git a/tests/test_topLevelVarInit.expected_results b/tests/test_topLevelVarInit.expected_results index d81cc07..69cdcf0 100644 --- a/tests/test_topLevelVarInit.expected_results +++ b/tests/test_topLevelVarInit.expected_results @@ -1 +1,2 @@ 42 +hi diff --git a/tests/test_topLevelVarInit.krak b/tests/test_topLevelVarInit.krak index 9a0eeb8..1390dcd 100644 --- a/tests/test_topLevelVarInit.krak +++ b/tests/test_topLevelVarInit.krak @@ -1,8 +1,10 @@ import io; var a: int = 42; +var b = "hi"; fun main(): int { io::println(a); + io::println(b); return 0; }