Mostly implemented Simultaneous Declaration, only template instantation during pass 2 remains to be implemented

This commit is contained in:
Nathan Braswell
2014-05-24 14:04:32 -04:00
parent 0f6b6c0c67
commit ae9e652f1e
10 changed files with 461 additions and 30 deletions

View File

@@ -4,7 +4,9 @@ Importer::Importer(Parser* parserIn, std::vector<std::string> includePaths) {
//constructor
parser = parserIn;
this->includePaths = includePaths;
ASTTransformer = new ASTTransformation(this);
removeSymbols.push_back(Symbol("$NULL$", true));
removeSymbols.push_back(Symbol("WS", false));
removeSymbols.push_back(Symbol("\\(", true));
removeSymbols.push_back(Symbol("\\)", true));
@@ -37,10 +39,16 @@ Importer::Importer(Parser* parserIn, std::vector<std::string> includePaths) {
Importer::~Importer() {
//destructor
delete ASTTransformer;
}
NodeTree<ASTData>* Importer::import(std::string fileName) {
void Importer::registerAST(std::string name, NodeTree<ASTData>* ast, NodeTree<Symbol>* syntaxTree) {
imported[name] = ast;
importedTrips.push_back({name, ast, syntaxTree});
std::cout << "REGISTERD " << name << std::endl;
}
NodeTree<ASTData>* Importer::getUnit(std::string fileName) {
std::cout << "\n\nImporting " << fileName << " ";
//Check to see if we've already done it
if (imported.find(fileName) != imported.end()) {
@@ -49,8 +57,65 @@ NodeTree<ASTData>* Importer::import(std::string fileName) {
}
std::cout << "Not yet imported" << std::endl;
return NULL;
}
NodeTree<ASTData>* Importer::importFirstPass(std::string fileName) {
NodeTree<ASTData>* ast = getUnit(fileName);
if (ast == NULL) {
NodeTree<Symbol>* parseTree = parseAndTrim(fileName);
//Call with ourself to allow the transformation to call us to import files that it needs
ast = ASTTransformer->firstPass(fileName, parseTree); //This firstPass will register itself
}
return ast;
}
void Importer::import(std::string fileName) {
//Start the ball rolling by importing and running the first pass on the first file.
//This will import, first pass and register all the other files too.
std::cout << "\n\n =====FIRST PASS===== \n\n" << std::endl;
importFirstPass(fileName); //First pass defines all objects
std::cout << "\n\n =====SECOND PASS===== \n\n" << std::endl;
for (importTriplet i : importedTrips) //Second pass defines data inside objects, outside declaration statements,
std::cout << "\n\nSecond pass for: " << i.name << std::endl, ASTTransformer->secondPass(i.ast, i.syntaxTree); //function prototypes, and identifiers (as we now have all type defs)
std::cout << "\n\n =====THIRD PASS===== \n\n" << std::endl;
for (importTriplet i : importedTrips) //Third pass redoes all imports to import the new function prototypes and identifiers
std::cout << "\n\nThird pass for: " << i.name << std::endl, ASTTransformer->thirdPass(i.ast);
std::cout << "\n\n =====FOURTH PASS===== \n\n" << std::endl;
for (importTriplet i : importedTrips) //Fourth pass finishes up by doing all function bodies
std::cout << "\n\nFourth pass for: " << i.name << std::endl, ASTTransformer->fourthPass(i.ast, i.syntaxTree); //With that, we're done
//Note that class template instantiation can happen in the second or fourth passes and that function template instantion
//can happen in the fourth pass.
std::ofstream outFileAST;
for (importTriplet i : importedTrips) {
std::string outputName = i.name + "out";
outFileAST.open((outputName + ".AST.dot").c_str());
if (!outFileAST.is_open()) {
std::cout << "Problem opening second output file " << outputName + ".AST.dot" << "\n";
return;
}
if (i.ast) {
outFileAST << i.ast->DOTGraphString() << std::endl;
} else {
std::cout << "Tree returned from ASTTransformation is NULL!" << std::endl;
}
outFileAST.close();
}
}
NodeTree<Symbol>* Importer::parseAndTrim(std::string fileName) {
std::ifstream programInFile;
std::ofstream outFile, outFileTransformed, outFileAST;
std::ofstream outFile, outFileTransformed;
std::string outputName = fileName + "out";
@@ -78,12 +143,6 @@ NodeTree<ASTData>* Importer::import(std::string fileName) {
return NULL;
}
outFileAST.open((outputName + ".AST.dot").c_str());
if (!outFileAST.is_open()) {
std::cout << "Probelm opening second output file " << outputName + ".AST.dot" << "\n";
return NULL;
}
std::string programInputFileString, line;
while(programInFile.good()) {
getline(programInFile, line);
@@ -119,21 +178,7 @@ NodeTree<ASTData>* Importer::import(std::string fileName) {
}
outFileTransformed.close();
//Call with ourself to allow the transformation to call us to import files that it needs
NodeTree<ASTData>* AST = ASTTransformation(this).transform(parseTree);
if (AST) {
outFileAST << AST->DOTGraphString() << std::endl;
} else {
std::cout << "Tree returned from ASTTransformation is NULL!" << std::endl;
}
outFileAST.close();
imported[fileName] = AST;
std::cout << "Done importing " << fileName << "\n\n" << std::endl;
return AST;
return parseTree;
}
std::map<std::string, NodeTree<ASTData>*> Importer::getASTMap() {