2013-09-26 15:16:58 -04:00
|
|
|
#include "ASTTransformation.h"
|
|
|
|
|
|
|
|
|
|
ASTTransformation::ASTTransformation() {
|
|
|
|
|
//
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ASTTransformation::~ASTTransformation() {
|
|
|
|
|
//
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-02 03:15:20 -04:00
|
|
|
NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from) {
|
2013-10-16 01:43:18 -04:00
|
|
|
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;
|
2013-09-26 15:16:58 -04:00
|
|
|
}
|