Further work on AST transformation

This commit is contained in:
Nathan Braswell
2013-10-16 01:43:18 -04:00
parent b9ffe33d0b
commit 02fd878c92
9 changed files with 195 additions and 5 deletions

View File

@@ -9,5 +9,38 @@ ASTTransformation::~ASTTransformation() {
}
NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from) {
return NULL;
Symbol current = from->getData();
std::string name = current.getName();
NodeTree<ASTData>* newNode;
std::vector<NodeTree<Symbol>*> children = from->getChildren();
if (name == "translation_unit") {
newNode = new NodeTree<ASTData>(name, ASTData(translation_unit));
} else if (name == "import" && !current.isTerminal()) {
newNode = new NodeTree<ASTData>(name, ASTData(import, Symbol(concatSymbolTree(children[0]), true)));
return newNode; // Don't need children of import
} else if (name == "function") {
newNode = new NodeTree<ASTData>(name, ASTData(function, Symbol(concatSymbolTree(children[1]), true), ASTData::strToType(concatSymbolTree(children[0]))));
} else {
return new NodeTree<ASTData>();
}
// In general, iterate through children and do them. Might not do this for all children.
for (int i = 0; i < children.size(); i++) {
newNode->addChild(transform(children[i]));
}
return newNode;
}
std::string ASTTransformation::concatSymbolTree(NodeTree<Symbol>* root) {
std::string concatString;
std::string ourValue = root->getData().getValue();
if (ourValue != "NoValue")
concatString += ourValue;
std::vector<NodeTree<Symbol>*> children = root->getChildren();
for (int i = 0; i < children.size(); i++) {
concatString = concatSymbolTree(children[i]);
}
return concatString;
}