Some template implementation/tests

This commit is contained in:
Nathan Braswell
2014-05-08 01:07:57 -04:00
parent 6c3267a8b2
commit 5de2ff58bb
3 changed files with 25 additions and 3 deletions

View File

@@ -26,10 +26,13 @@ class ASTData {
~ASTData();
std::string toString();
static std::string ASTTypeToString(ASTType type);
void addTemplateTypeDefToReplace(NodeTree<ASTData>* typeDefToReplace);
std::vector<NodeTree<ASTData>*> getTemplateTypeDefsToReplace();
ASTType type;
Type* valueType;
Symbol symbol;
std::map<std::string, std::vector<NodeTree<ASTData>*>> scope;
std::vector<NodeTree<ASTData>*> templateTypeDefsToReplace; //Used only by template classes/functions to keep track of pointers to the typedefs that need to be changed
private:
};

View File

@@ -74,3 +74,12 @@ std::string ASTData::ASTTypeToString(ASTType type) {
return "unknown_ASTType";
}
}
void ASTData::addTemplateTypeDefToReplace(NodeTree<ASTData>* typeDefToReplace) {
templateTypeDefsToReplace.push_back(typeDefToReplace);
}
std::vector<NodeTree<ASTData>*> ASTData::getTemplateTypeDefsToReplace() {
return templateTypeDefsToReplace;
}

View File

@@ -77,20 +77,30 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
if (children[1]->getData().getName() == "type") {
typeAlias = concatSymbolTree(children[0]);
newNode = new NodeTree<ASTData>(name, ASTData(type_def, Symbol(typeAlias, true, typeAlias), typeFromTypeNode(children[1], scope)));
skipChildren.insert(1); //Don't want any children, it's unnecessary for ailising
skipChildren.insert(0); //Don't want any children, it's unnecessary for ailising
skipChildren.insert(1);
} else { //Is a struct or class
Type* objectType = NULL;
if (children[0]->getData().getName() == "template_dec") {
typeAlias = concatSymbolTree(children[1]);
newNode = new NodeTree<ASTData>(name, ASTData(type_def, Symbol(typeAlias, true, typeAlias)));
objectType = new Type(newNode), std::cout << "Template Type!"<<std::endl;
skipChildren.insert(0); //Don't handle template just yet
//So we get the name of the template type and create a new typedef node for it. This goes into the class's scope so that
//lookups of this type will work. Later, on instantiation, the entire class tree will be copied and this typedef copied
//and modified to point to the right type.
std::string templateTypeName = concatSymbolTree(children[0]->getChildren()[1]);
NodeTree<ASTData>* templateTypeDef = new NodeTree<ASTData>(name, ASTData(type_def, Symbol(templateTypeName, true, templateTypeName), NULL));
newNode->getDataRef()->scope[templateTypeName].push_back(templateTypeDef);
newNode->getDataRef()->addTemplateTypeDefToReplace(templateTypeDef);
skipChildren.insert(0); //Don't try to transform the template
skipChildren.insert(1); //Identifier lookup will be ourselves, as we just added ourselves to the scope
} else {
typeAlias = concatSymbolTree(children[0]);
newNode = new NodeTree<ASTData>(name, ASTData(type_def, Symbol(typeAlias, true, typeAlias)));
objectType = new Type(newNode);
skipChildren.insert(0); //Identifier lookup will be ourselves, as we just added ourselves to the scope
}
newNode = new NodeTree<ASTData>(name, ASTData(type_def, Symbol(typeAlias, true, typeAlias)));
newNode->getDataRef()->valueType = objectType; //Type is self-referential since this is the definition
}
scope->getDataRef()->scope[typeAlias].push_back(newNode);