2013-09-26 15:16:58 -04:00
|
|
|
#include "ASTTransformation.h"
|
|
|
|
|
|
2013-12-31 23:43:49 -06:00
|
|
|
ASTTransformation::ASTTransformation(Importer *importerIn) {
|
2013-09-26 15:16:58 -04:00
|
|
|
//
|
2013-12-31 23:43:49 -06:00
|
|
|
importer = importerIn;
|
2013-09-26 15:16:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ASTTransformation::~ASTTransformation() {
|
|
|
|
|
//
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-02 03:15:20 -04:00
|
|
|
NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from) {
|
2013-12-27 13:05:07 -06:00
|
|
|
//Set up top scope
|
|
|
|
|
return transform(from, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree<ASTData>* scope) {
|
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();
|
2013-10-26 11:47:34 -04:00
|
|
|
std::set<int> skipChildren;
|
2013-10-16 01:43:18 -04:00
|
|
|
|
|
|
|
|
if (name == "translation_unit") {
|
|
|
|
|
newNode = new NodeTree<ASTData>(name, ASTData(translation_unit));
|
2013-12-27 13:05:07 -06:00
|
|
|
scope = newNode;
|
|
|
|
|
//Temporary scope fix
|
|
|
|
|
scope->getDataRef()->scope["+"] = new NodeTree<ASTData>();
|
|
|
|
|
scope->getDataRef()->scope["-"] = new NodeTree<ASTData>();
|
|
|
|
|
scope->getDataRef()->scope["*"] = new NodeTree<ASTData>();
|
|
|
|
|
scope->getDataRef()->scope["&"] = new NodeTree<ASTData>();
|
|
|
|
|
scope->getDataRef()->scope["--"] = new NodeTree<ASTData>();
|
|
|
|
|
scope->getDataRef()->scope["++"] = new NodeTree<ASTData>();
|
2013-12-31 23:43:49 -06:00
|
|
|
scope->getDataRef()->scope["=="] = new NodeTree<ASTData>();
|
2013-12-27 13:05:07 -06:00
|
|
|
scope->getDataRef()->scope["<="] = new NodeTree<ASTData>();
|
|
|
|
|
scope->getDataRef()->scope[">="] = new NodeTree<ASTData>();
|
2013-12-31 23:43:49 -06:00
|
|
|
scope->getDataRef()->scope["<"] = new NodeTree<ASTData>();
|
|
|
|
|
scope->getDataRef()->scope[">"] = new NodeTree<ASTData>();
|
|
|
|
|
scope->getDataRef()->scope["&&"] = new NodeTree<ASTData>();
|
|
|
|
|
scope->getDataRef()->scope["||"] = new NodeTree<ASTData>();
|
|
|
|
|
scope->getDataRef()->scope["!"] = new NodeTree<ASTData>();
|
2013-12-27 13:05:07 -06:00
|
|
|
scope->getDataRef()->scope["*="] = new NodeTree<ASTData>();
|
|
|
|
|
scope->getDataRef()->scope["+="] = new NodeTree<ASTData>();
|
|
|
|
|
scope->getDataRef()->scope["-="] = new NodeTree<ASTData>();
|
2013-12-31 23:43:49 -06:00
|
|
|
|
2013-10-26 11:47:34 -04:00
|
|
|
} else if (name == "interpreter_directive") {
|
|
|
|
|
newNode = new NodeTree<ASTData>(name, ASTData(interpreter_directive));
|
2013-10-16 01:43:18 -04:00
|
|
|
} else if (name == "import" && !current.isTerminal()) {
|
2013-12-31 23:43:49 -06:00
|
|
|
std::string toImport = concatSymbolTree(children[0]);
|
|
|
|
|
newNode = new NodeTree<ASTData>(name, ASTData(import, Symbol(toImport, true)));
|
|
|
|
|
//Do the imported file too
|
|
|
|
|
NodeTree<ASTData>* outsideTranslationUnit = importer->import(toImport + ".krak");
|
|
|
|
|
scope->getDataRef()->scope[toImport] = outsideTranslationUnit; //Put this transation_unit in the scope as it's files name
|
|
|
|
|
//Now add it to scope
|
|
|
|
|
for (auto i = outsideTranslationUnit->getDataRef()->scope.begin(); i != outsideTranslationUnit->getDataRef()->scope.end(); i++)
|
|
|
|
|
scope->getDataRef()->scope[i->first] = i->second;
|
2013-10-16 01:43:18 -04:00
|
|
|
return newNode; // Don't need children of import
|
2013-10-26 11:47:34 -04:00
|
|
|
} else if (name == "identifier") {
|
2013-12-27 13:05:07 -06:00
|
|
|
std::string lookupName = concatSymbolTree(children[0]);
|
2014-01-01 17:29:19 -06:00
|
|
|
//std::cout << "scope lookup from identifier" << std::endl;
|
2013-12-27 13:05:07 -06:00
|
|
|
newNode = scopeLookup(scope, lookupName);
|
|
|
|
|
if (newNode == NULL) {
|
|
|
|
|
std::cout << "scope lookup error! Could not find " << lookupName << std::endl;
|
|
|
|
|
throw "LOOKUP ERROR: " + lookupName;
|
|
|
|
|
}
|
|
|
|
|
//newNode = new NodeTree<ASTData>(name, ASTData(identifier, Symbol(concatSymbolTree(children[0]), true)));
|
2014-01-07 13:14:58 -05:00
|
|
|
} else if (name == "type_def") {
|
|
|
|
|
std::string typeAlias = concatSymbolTree(children[0]);
|
|
|
|
|
newNode = new NodeTree<ASTData>(name, ASTData(type_def, Symbol(typeAlias, true, typeAlias), typeFromString(concatSymbolTree(children[1]), scope)));
|
|
|
|
|
scope->getDataRef()->scope[typeAlias] = newNode;
|
|
|
|
|
return newNode;
|
2013-10-16 01:43:18 -04:00
|
|
|
} else if (name == "function") {
|
2013-12-27 13:05:07 -06:00
|
|
|
std::string functionName = concatSymbolTree(children[1]);
|
2014-01-07 13:14:58 -05:00
|
|
|
newNode = new NodeTree<ASTData>(name, ASTData(function, Symbol(functionName, true), typeFromString(concatSymbolTree(children[0]), scope)));
|
2013-10-26 11:47:34 -04:00
|
|
|
skipChildren.insert(0);
|
|
|
|
|
skipChildren.insert(1);
|
2013-12-27 13:05:07 -06:00
|
|
|
scope->getDataRef()->scope[functionName] = newNode;
|
|
|
|
|
newNode->getDataRef()->scope["~enclosing_scope"] = scope;
|
|
|
|
|
scope = newNode;
|
2013-10-26 11:47:34 -04:00
|
|
|
} else if (name == "code_block") {
|
|
|
|
|
newNode = new NodeTree<ASTData>(name, ASTData(code_block));
|
2013-12-27 13:05:07 -06:00
|
|
|
newNode->getDataRef()->scope["~enclosing_scope"] = scope;
|
|
|
|
|
scope = newNode;
|
2013-10-26 11:47:34 -04:00
|
|
|
} else if (name == "typed_parameter") {
|
2013-12-27 13:05:07 -06:00
|
|
|
//newNode = transform(children[1]); //Transform to get the identifier
|
|
|
|
|
std::string parameterName = concatSymbolTree(children[1]);
|
|
|
|
|
std::string typeString = concatSymbolTree(children[0]);//Get the type (left child) and set our new identifer to be that type
|
2014-01-07 13:14:58 -05:00
|
|
|
newNode = new NodeTree<ASTData>("identifier", ASTData(identifier, Symbol(parameterName, true), typeFromString(typeString, scope)));
|
2013-12-27 13:05:07 -06:00
|
|
|
scope->getDataRef()->scope[parameterName] = newNode;
|
2013-11-01 02:52:18 -04:00
|
|
|
return newNode;
|
2013-12-27 13:05:07 -06:00
|
|
|
} else if (name == "boolean_expression" || name == "and_boolean_expression" || name == "bool_exp") {
|
2013-11-07 22:19:33 -05:00
|
|
|
//If this is an actual part of an expression, not just a premoted term
|
|
|
|
|
if (children.size() > 1) {
|
|
|
|
|
std::string functionCallName = concatSymbolTree(children[1]);
|
2014-01-01 17:29:19 -06:00
|
|
|
//std::cout << "scope lookup from boolen_expression or similar" << std::endl;
|
2013-12-27 13:05:07 -06:00
|
|
|
NodeTree<ASTData>* function = scopeLookup(scope, functionCallName);
|
|
|
|
|
if (function == NULL) {
|
|
|
|
|
std::cout << "scope lookup error! Could not find " << functionCallName << std::endl;
|
|
|
|
|
throw "LOOKUP ERROR: " + functionCallName;
|
|
|
|
|
}
|
2013-11-07 22:19:33 -05:00
|
|
|
newNode = new NodeTree<ASTData>(functionCallName, ASTData(function_call, Symbol(functionCallName, true)));
|
2013-12-27 13:05:07 -06:00
|
|
|
newNode->addChild(function); // First child of function call is a link to the function definition
|
2013-11-07 22:19:33 -05:00
|
|
|
skipChildren.insert(1);
|
|
|
|
|
} else {
|
2014-01-01 17:29:19 -06:00
|
|
|
//std::cout << children.size() << std::endl;
|
2013-12-27 13:05:07 -06:00
|
|
|
if (children.size() == 0)
|
|
|
|
|
return new NodeTree<ASTData>();
|
|
|
|
|
return transform(children[0], scope); //Just a promoted term, so do child
|
2013-11-07 22:19:33 -05:00
|
|
|
}
|
2013-12-18 18:05:21 -06:00
|
|
|
//Here's the order of ops stuff
|
2013-12-23 01:26:24 -06:00
|
|
|
} else if (name == "expression" || name == "shiftand" || name == "term" || name == "unarad") { //unarad can ride through, it should always just be a promoted child
|
2013-12-18 18:05:21 -06:00
|
|
|
//If this is an actual part of an expression, not just a premoted child
|
2013-12-23 01:26:24 -06:00
|
|
|
if (children.size() > 2) {
|
2013-10-26 15:05:42 -04:00
|
|
|
std::string functionCallName = concatSymbolTree(children[1]);
|
2014-01-01 17:29:19 -06:00
|
|
|
//std::cout << "scope lookup from expression or similar" << std::endl;
|
2013-12-27 13:05:07 -06:00
|
|
|
NodeTree<ASTData>* function = scopeLookup(scope, functionCallName);
|
|
|
|
|
if (function == NULL) {
|
|
|
|
|
std::cout << "scope lookup error! Could not find " << functionCallName << std::endl;
|
|
|
|
|
throw "LOOKUP ERROR: " + functionCallName;
|
|
|
|
|
}
|
2013-10-26 15:05:42 -04:00
|
|
|
newNode = new NodeTree<ASTData>(functionCallName, ASTData(function_call, Symbol(functionCallName, true)));
|
2013-12-27 13:05:07 -06:00
|
|
|
newNode->addChild(function); // First child of function call is a link to the function definition
|
2013-10-26 15:05:42 -04:00
|
|
|
skipChildren.insert(1);
|
|
|
|
|
} else {
|
2013-12-27 13:05:07 -06:00
|
|
|
return transform(children[0], scope); //Just a promoted child, so do it instead
|
2013-10-26 11:47:34 -04:00
|
|
|
}
|
2013-12-23 01:26:24 -06:00
|
|
|
} else if (name == "factor") { //Do factor here, as it has all the weird unary operators
|
|
|
|
|
//If this is an actual part of an expression, not just a premoted child
|
|
|
|
|
//NO SUPPORT FOR CASTING YET
|
|
|
|
|
if (children.size() == 2) {
|
|
|
|
|
std::string funcName = concatSymbolTree(children[0]);
|
|
|
|
|
int funcNum;
|
|
|
|
|
if (funcName == "*" || funcName == "&" || funcName == "++" || funcName == "--" || funcName == "-" || funcName == "!" || funcName == "~")
|
|
|
|
|
funcNum = 0;
|
|
|
|
|
else
|
|
|
|
|
funcName = concatSymbolTree(children[1]), funcNum = 1;
|
|
|
|
|
|
2014-01-01 17:29:19 -06:00
|
|
|
//std::cout << "scope lookup from factor" << std::endl;
|
2013-12-27 13:05:07 -06:00
|
|
|
NodeTree<ASTData>* function = scopeLookup(scope, funcName);
|
|
|
|
|
if (function == NULL) {
|
|
|
|
|
std::cout << "scope lookup error! Could not find " << funcName << std::endl;
|
|
|
|
|
throw "LOOKUP ERROR: " + funcName;
|
|
|
|
|
}
|
2013-12-23 01:26:24 -06:00
|
|
|
newNode = new NodeTree<ASTData>(funcName, ASTData(function_call, Symbol(funcName, true)));
|
2013-12-27 13:05:07 -06:00
|
|
|
newNode->addChild(function);
|
2013-12-23 01:26:24 -06:00
|
|
|
skipChildren.insert(funcNum);
|
|
|
|
|
} else {
|
2013-12-27 13:05:07 -06:00
|
|
|
return transform(children[0], scope); //Just a promoted child, so do it instead
|
2013-12-23 01:26:24 -06:00
|
|
|
}
|
2013-10-26 11:47:34 -04:00
|
|
|
} else if (name == "statement") {
|
|
|
|
|
newNode = new NodeTree<ASTData>(name, ASTData(statement));
|
|
|
|
|
} else if (name == "if_statement") {
|
|
|
|
|
newNode = new NodeTree<ASTData>(name, ASTData(if_statement));
|
2013-12-18 18:05:21 -06:00
|
|
|
} else if (name == "while_loop") {
|
|
|
|
|
newNode = new NodeTree<ASTData>(name, ASTData(while_loop));
|
|
|
|
|
} else if (name == "for_loop") {
|
|
|
|
|
newNode = new NodeTree<ASTData>(name, ASTData(for_loop));
|
2013-10-26 11:47:34 -04:00
|
|
|
} else if (name == "return_statement") {
|
|
|
|
|
newNode = new NodeTree<ASTData>(name, ASTData(return_statement));
|
|
|
|
|
} else if (name == "assignment_statement") {
|
|
|
|
|
newNode = new NodeTree<ASTData>(name, ASTData(assignment_statement));
|
2013-12-18 18:05:21 -06:00
|
|
|
std::string assignFuncName = concatSymbolTree(children[1]);
|
|
|
|
|
if (assignFuncName == "=") {
|
2013-12-27 13:05:07 -06:00
|
|
|
newNode->addChild(transform(children[0], scope));
|
|
|
|
|
newNode->addChild(transform(children[2], scope));
|
2013-12-18 18:05:21 -06:00
|
|
|
} else {
|
|
|
|
|
//For assignments like += or *=, expand the syntatic sugar.
|
2013-12-27 13:05:07 -06:00
|
|
|
NodeTree<ASTData>* lhs = transform(children[0], scope);
|
|
|
|
|
std::string functionName = assignFuncName.substr(0,1);
|
|
|
|
|
NodeTree<ASTData>* childCall = new NodeTree<ASTData>(functionName, ASTData(function_call, Symbol(functionName, true)));
|
|
|
|
|
NodeTree<ASTData>* functionDef = scopeLookup(scope, functionName);
|
|
|
|
|
if (functionDef == NULL) {
|
|
|
|
|
std::cout << "scope lookup error! Could not find " << functionName << std::endl;
|
|
|
|
|
throw "LOOKUP ERROR: " + functionName;
|
|
|
|
|
}
|
|
|
|
|
childCall->addChild(functionDef); //First child of function call is definition of the function
|
2013-12-18 18:05:21 -06:00
|
|
|
childCall->addChild(lhs);
|
2013-12-27 13:05:07 -06:00
|
|
|
childCall->addChild(transform(children[2], scope));
|
2013-12-18 18:05:21 -06:00
|
|
|
newNode->addChild(lhs);
|
|
|
|
|
newNode->addChild(childCall);
|
|
|
|
|
}
|
|
|
|
|
return newNode;
|
2013-11-01 02:52:18 -04:00
|
|
|
} else if (name == "declaration_statement") {
|
|
|
|
|
newNode = new NodeTree<ASTData>(name, ASTData(declaration_statement));
|
2013-12-27 13:05:07 -06:00
|
|
|
|
|
|
|
|
// NodeTree<ASTData>* newIdentifier = transform(children[1], scope); //Transform the identifier
|
|
|
|
|
// newIdentifier->getDataRef()->valueType = Type(concatSymbolTree(children[0]));//set the type of the identifier
|
|
|
|
|
std::string newIdentifierStr = concatSymbolTree(children[1]);
|
|
|
|
|
std::string typeString = concatSymbolTree(children[0]);//Get the type (left child) and set our new identifer to be that type
|
2014-01-07 13:14:58 -05:00
|
|
|
NodeTree<ASTData>* newIdentifier = new NodeTree<ASTData>("identifier", ASTData(identifier, Symbol(newIdentifierStr, true), typeFromString(typeString, scope)));
|
2013-12-27 13:05:07 -06:00
|
|
|
scope->getDataRef()->scope[newIdentifierStr] = newIdentifier;
|
|
|
|
|
|
2013-11-01 02:52:18 -04:00
|
|
|
newNode->addChild(newIdentifier);
|
|
|
|
|
skipChildren.insert(0); //These, the type and the identifier, have been taken care of.
|
|
|
|
|
skipChildren.insert(1);
|
2013-12-22 01:34:59 -06:00
|
|
|
} else if (name == "if_comp") {
|
|
|
|
|
newNode = new NodeTree<ASTData>(name, ASTData(if_comp));
|
2013-12-27 13:05:07 -06:00
|
|
|
newNode->addChild(new NodeTree<ASTData>("identifier", ASTData(identifier, Symbol(concatSymbolTree(children[0]),true))));
|
|
|
|
|
skipChildren.insert(0); //Don't do the identifier. The identifier lookup will fail. That's why we do it here.
|
2013-12-22 01:34:59 -06:00
|
|
|
} else if (name == "simple_passthrough") {
|
|
|
|
|
newNode = new NodeTree<ASTData>(name, ASTData(simple_passthrough));
|
2013-10-26 11:47:34 -04:00
|
|
|
} else if (name == "function_call") {
|
2013-10-26 15:05:42 -04:00
|
|
|
//children[0] is scope
|
|
|
|
|
std::string functionCallName = concatSymbolTree(children[1]);
|
|
|
|
|
newNode = new NodeTree<ASTData>(functionCallName, ASTData(function_call, Symbol(functionCallName, true)));
|
2014-01-01 17:29:19 -06:00
|
|
|
//std::cout << "scope lookup from function_call" << std::endl;
|
2013-12-27 13:05:07 -06:00
|
|
|
NodeTree<ASTData>* function = scopeLookup(scope, functionCallName);
|
|
|
|
|
if (function == NULL) {
|
|
|
|
|
std::cout << "scope lookup error! Could not find " << functionCallName << std::endl;
|
|
|
|
|
throw "LOOKUP ERROR: " + functionCallName;
|
|
|
|
|
}
|
|
|
|
|
newNode->addChild(function);
|
2013-10-26 15:05:42 -04:00
|
|
|
skipChildren.insert(1);
|
|
|
|
|
} else if (name == "parameter") {
|
2013-12-27 13:05:07 -06:00
|
|
|
return transform(children[0], scope); //Don't need a parameter node, just the value
|
2013-12-23 01:26:24 -06:00
|
|
|
} else if (name == "parameter") {
|
2013-12-27 13:05:07 -06:00
|
|
|
return transform(children[0], scope); //Don't need a parameter node, just the value
|
2013-12-23 01:26:24 -06:00
|
|
|
} else if (name == "type") {
|
|
|
|
|
std::string theConcat = concatSymbolTree(from); //We have no symbol, so this will concat our children
|
2014-01-07 13:14:58 -05:00
|
|
|
newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(theConcat, true), typeFromString(theConcat, scope)));
|
2013-10-26 15:05:42 -04:00
|
|
|
} else if (name == "number") {
|
2013-12-27 13:05:07 -06:00
|
|
|
return transform(children[0], scope);
|
2013-10-26 15:05:42 -04:00
|
|
|
} else if (name == "integer") {
|
2014-01-07 13:14:58 -05:00
|
|
|
newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(concatSymbolTree(from), true), new Type(integer)));
|
2013-10-26 15:05:42 -04:00
|
|
|
} else if (name == "float") {
|
2014-01-07 13:14:58 -05:00
|
|
|
newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(concatSymbolTree(from), true), new Type(floating)));
|
2013-10-26 15:05:42 -04:00
|
|
|
} else if (name == "double") {
|
2014-01-07 13:14:58 -05:00
|
|
|
newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(concatSymbolTree(from), true), new Type(double_percision)));
|
2013-12-23 01:26:24 -06:00
|
|
|
} else if (name == "char") {
|
2014-01-07 13:14:58 -05:00
|
|
|
newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(concatSymbolTree(children[0]), true), new Type(character, 1))); //Indirection of 1 for array
|
2013-12-22 01:34:59 -06:00
|
|
|
} else if (name == "string" || name == "triple_quoted_string") {
|
2014-01-07 13:14:58 -05:00
|
|
|
newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(concatSymbolTree(children[0]), true), new Type(character, 1))); //Indirection of 1 for array
|
2013-10-16 01:43:18 -04:00
|
|
|
} 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++) {
|
2013-10-26 11:47:34 -04:00
|
|
|
if (skipChildren.find(i) == skipChildren.end()) {
|
2013-12-27 13:05:07 -06:00
|
|
|
NodeTree<ASTData>* transChild = transform(children[i], scope);
|
|
|
|
|
if (transChild->getDataRef()->type) //Only add the children that have a real ASTData::ASTType, that is, legit ASTData.
|
2013-10-26 11:47:34 -04:00
|
|
|
newNode->addChild(transChild);
|
|
|
|
|
else
|
|
|
|
|
delete transChild;
|
|
|
|
|
}
|
2013-10-16 01:43:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return newNode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string ASTTransformation::concatSymbolTree(NodeTree<Symbol>* root) {
|
|
|
|
|
std::string concatString;
|
2013-12-27 13:05:07 -06:00
|
|
|
std::string ourValue = root->getDataRef()->getValue();
|
2013-10-16 01:43:18 -04:00
|
|
|
if (ourValue != "NoValue")
|
|
|
|
|
concatString += ourValue;
|
|
|
|
|
std::vector<NodeTree<Symbol>*> children = root->getChildren();
|
|
|
|
|
for (int i = 0; i < children.size(); i++) {
|
2013-12-23 01:26:24 -06:00
|
|
|
concatString += concatSymbolTree(children[i]);
|
2013-10-16 01:43:18 -04:00
|
|
|
}
|
|
|
|
|
return concatString;
|
2013-09-26 15:16:58 -04:00
|
|
|
}
|
2013-12-27 13:05:07 -06:00
|
|
|
|
|
|
|
|
NodeTree<ASTData>* ASTTransformation::scopeLookup(NodeTree<ASTData>* scope, std::string lookup) {
|
|
|
|
|
//Seach the map
|
|
|
|
|
auto scopeMap = scope->getDataRef()->scope;
|
2013-12-28 21:54:22 -05:00
|
|
|
//std::cout << "scope size: " << scopeMap.size() << ", scope from " << scope->getName() << std::endl;
|
2014-01-01 17:29:19 -06:00
|
|
|
// for (auto i = scopeMap.begin(); i != scopeMap.end(); i++)
|
|
|
|
|
// std::cout << i->first << " : " << i-> second << " - " << i->second->getName() << std::endl;
|
2013-12-27 13:05:07 -06:00
|
|
|
|
|
|
|
|
auto elementIterator = scopeMap.find(lookup);
|
|
|
|
|
if (elementIterator != scopeMap.end()) {
|
2013-12-28 21:54:22 -05:00
|
|
|
// std::cout << "lookup of " << lookup << " succeded in first scope!" << std::endl;
|
2013-12-27 13:05:07 -06:00
|
|
|
return elementIterator->second;
|
|
|
|
|
}
|
2013-12-28 21:54:22 -05:00
|
|
|
//std::cout << "lookup of " << lookup << " failed in first scope, checking for upper scope" << std::endl;
|
2013-12-27 13:05:07 -06:00
|
|
|
//if it doesn't exist, try the enclosing scope if it exists.
|
|
|
|
|
auto enclosingIterator = scopeMap.find("~enclosing_scope");
|
|
|
|
|
if (enclosingIterator != scopeMap.end()) {
|
2013-12-28 21:54:22 -05:00
|
|
|
// std::cout << "upper scope exists, searching it for " << lookup << std::endl;
|
2013-12-27 13:05:07 -06:00
|
|
|
return scopeLookup(enclosingIterator->second, lookup);
|
|
|
|
|
}
|
2013-12-28 21:54:22 -05:00
|
|
|
//std::cout << "upper scope does not exist" << std::endl;
|
2013-12-27 13:05:07 -06:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2014-01-07 13:14:58 -05:00
|
|
|
|
|
|
|
|
Type* ASTTransformation::typeFromString(std::string typeIn, NodeTree<ASTData>* scope) {
|
|
|
|
|
int indirection = 0;
|
|
|
|
|
ValueType baseType;
|
|
|
|
|
NodeTree<ASTData>* typeDefinition = NULL;
|
|
|
|
|
while (typeIn[typeIn.size() - indirection - 1] == '*') indirection++;
|
|
|
|
|
std::string edited = strSlice(typeIn, 0, -(indirection + 1));
|
|
|
|
|
if (edited == "void")
|
|
|
|
|
baseType = void_type;
|
|
|
|
|
else if (edited == "bool")
|
|
|
|
|
baseType = boolean;
|
|
|
|
|
else if (edited == "int")
|
|
|
|
|
baseType = integer;
|
|
|
|
|
else if (edited == "float")
|
|
|
|
|
baseType = floating
|
|
|
|
|
; else if (edited == "double")
|
|
|
|
|
baseType = double_percision;
|
|
|
|
|
else if (edited == "char")
|
|
|
|
|
baseType = character;
|
|
|
|
|
else {
|
|
|
|
|
baseType = none;
|
|
|
|
|
typeDefinition = scopeLookup(scope, edited);
|
|
|
|
|
std::cout << "scopeLookup of type " << edited << " returned " << typeDefinition << std::endl;
|
|
|
|
|
}
|
|
|
|
|
return new Type(baseType, typeDefinition, indirection);
|
|
|
|
|
}
|