Now generates for all files that have been imported. CGenerator uses this to generate all files AND a shell script with the compile command to compile the generated C file.

This commit is contained in:
Nathan Braswell
2014-01-01 17:29:19 -06:00
parent dbf1820428
commit 53b45f360d
11 changed files with 78 additions and 36 deletions

View File

@@ -57,7 +57,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
return newNode; // Don't need children of import
} else if (name == "identifier") {
std::string lookupName = concatSymbolTree(children[0]);
std::cout << "scope lookup from identifier" << std::endl;
//std::cout << "scope lookup from identifier" << std::endl;
newNode = scopeLookup(scope, lookupName);
if (newNode == NULL) {
std::cout << "scope lookup error! Could not find " << lookupName << std::endl;
@@ -87,7 +87,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
//If this is an actual part of an expression, not just a premoted term
if (children.size() > 1) {
std::string functionCallName = concatSymbolTree(children[1]);
std::cout << "scope lookup from boolen_expression or similar" << std::endl;
//std::cout << "scope lookup from boolen_expression or similar" << std::endl;
NodeTree<ASTData>* function = scopeLookup(scope, functionCallName);
if (function == NULL) {
std::cout << "scope lookup error! Could not find " << functionCallName << std::endl;
@@ -97,7 +97,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
newNode->addChild(function); // First child of function call is a link to the function definition
skipChildren.insert(1);
} else {
std::cout << children.size() << std::endl;
//std::cout << children.size() << std::endl;
if (children.size() == 0)
return new NodeTree<ASTData>();
return transform(children[0], scope); //Just a promoted term, so do child
@@ -107,7 +107,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
//If this is an actual part of an expression, not just a premoted child
if (children.size() > 2) {
std::string functionCallName = concatSymbolTree(children[1]);
std::cout << "scope lookup from expression or similar" << std::endl;
//std::cout << "scope lookup from expression or similar" << std::endl;
NodeTree<ASTData>* function = scopeLookup(scope, functionCallName);
if (function == NULL) {
std::cout << "scope lookup error! Could not find " << functionCallName << std::endl;
@@ -130,7 +130,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
else
funcName = concatSymbolTree(children[1]), funcNum = 1;
std::cout << "scope lookup from factor" << std::endl;
//std::cout << "scope lookup from factor" << std::endl;
NodeTree<ASTData>* function = scopeLookup(scope, funcName);
if (function == NULL) {
std::cout << "scope lookup error! Could not find " << funcName << std::endl;
@@ -199,7 +199,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
//children[0] is scope
std::string functionCallName = concatSymbolTree(children[1]);
newNode = new NodeTree<ASTData>(functionCallName, ASTData(function_call, Symbol(functionCallName, true)));
std::cout << "scope lookup from function_call" << std::endl;
//std::cout << "scope lookup from function_call" << std::endl;
NodeTree<ASTData>* function = scopeLookup(scope, functionCallName);
if (function == NULL) {
std::cout << "scope lookup error! Could not find " << functionCallName << std::endl;
@@ -260,8 +260,8 @@ NodeTree<ASTData>* ASTTransformation::scopeLookup(NodeTree<ASTData>* scope, std:
//Seach the map
auto scopeMap = scope->getDataRef()->scope;
//std::cout << "scope size: " << scopeMap.size() << ", scope from " << scope->getName() << std::endl;
for (auto i = scopeMap.begin(); i != scopeMap.end(); i++)
std::cout << i->first << " : " << i-> second << " - " << i->second->getName() << std::endl;
// for (auto i = scopeMap.begin(); i != scopeMap.end(); i++)
// std::cout << i->first << " : " << i-> second << " - " << i->second->getName() << std::endl;
auto elementIterator = scopeMap.find(lookup);
if (elementIterator != scopeMap.end()) {

View File

@@ -7,6 +7,27 @@ CGenerator::~CGenerator() {
}
void CGenerator::generateCompSet(std::map<std::string, NodeTree<ASTData>*> ASTs, std::string outputName) {
//Generate an entire set of files
std::string buildString = "#!/bin/sh\ncc -std=c99 ";
for (auto i = ASTs.begin(); i != ASTs.end(); i++) {
buildString += i->first + ".c ";
std::ofstream outputCFile;
outputCFile.open(i->first + ".c");
if (outputCFile.is_open()) {
outputCFile << generate(i->second);
} else {
std::cout << "Cannot open file " << i->first << ".c" << std::endl;
}
outputCFile.close();
}
buildString += "-o " + outputName;
std::ofstream outputBuild;
outputBuild.open(outputName + ".sh");
outputBuild << buildString;
outputBuild.close();
}
std::string CGenerator::tabs() {
std::string returnTabs;
for (int i = 0; i < tabLevel; i++)
@@ -39,7 +60,7 @@ std::string CGenerator::generate(NodeTree<ASTData>* from) {
output+= "); /*func*/\n";
break;
default:
std::cout << "Declaration? named " << declaration->getName() << " of unknown type " << ASTData::ASTTypeToString(declarationData.type) << " in translation unit scope" << std::endl;
//std::cout << "Declaration? named " << declaration->getName() << " of unknown type " << ASTData::ASTTypeToString(declarationData.type) << " in translation unit scope" << std::endl;
output += "/*unknown declaration named " + declaration->getName() + "*/\n";
}
}
@@ -109,7 +130,7 @@ std::string CGenerator::generate(NodeTree<ASTData>* from) {
//Handle operators specially for now. Will later replace with
//Inlined functions in the standard library
std::string name = data.symbol.getName();
std::cout << name << " == " << children[0]->getData().symbol.getName() << std::endl;
//std::cout << name << " == " << children[0]->getData().symbol.getName() << std::endl;
if (name == "++" || name == "--")
return generate(children[1]) + name;
if (name == "*" && children.size() == 2) //Is dereference, not multiplication

View File

@@ -36,6 +36,10 @@ Importer::~Importer() {
}
NodeTree<ASTData>* Importer::import(std::string fileName) {
//Check to see if we've already done it
if (imported.find(fileName) != imported.end())
return imported[fileName];
std::ifstream programInFile;
std::ofstream outFile, outFileTransformed, outFileAST;
@@ -64,7 +68,7 @@ NodeTree<ASTData>* Importer::import(std::string fileName) {
std::cout << "Probelm opening second output file " << outputName + ".AST.dot" << "\n";
return NULL;
}
//ljklj
std::string programInputFileString, line;
while(programInFile.good()) {
getline(programInFile, line);
@@ -72,7 +76,7 @@ NodeTree<ASTData>* Importer::import(std::string fileName) {
}
programInFile.close();
std::cout << programInputFileString << std::endl;
//std::cout << programInputFileString << std::endl;
NodeTree<Symbol>* parseTree = parser->parseInput(programInputFileString);
if (parseTree) {
@@ -109,5 +113,11 @@ NodeTree<ASTData>* Importer::import(std::string fileName) {
}
outFileAST.close();
imported[fileName] = AST;
return AST;
}
}
std::map<std::string, NodeTree<ASTData>*> Importer::getASTMap() {
return imported;
}

View File

@@ -78,7 +78,7 @@ void Parser::loadGrammer(std::string grammerInputString) {
//Get next token
currToken = reader.word();
}
std::cout << "Parsed!\n";
//std::cout << "Parsed!\n";
// for (std::vector<ParseRule*>::size_type i = 0; i < loadedGrammer.size(); i++)
// std::cout << loadedGrammer[i]->toString() << std::endl;

View File

@@ -50,8 +50,8 @@ NodeTree<Symbol>* RNGLRParser::parseInput(std::string inputString) {
input.push_back(currentToken);
}
std::cout << "\nDone with Lexing, length:" << input.size() << std::endl;
std::cout << input[0].toString() << std::endl;
// std::cout << "\nDone with Lexing, length:" << input.size() << std::endl;
// std::cout << input[0].toString() << std::endl;
// for (int i = 0; i < input.size(); i++)
@@ -59,13 +59,13 @@ NodeTree<Symbol>* RNGLRParser::parseInput(std::string inputString) {
// std::cout << std::endl;
std::cout << "Setting up 0th frontier, first actions, toShift, toReduce" << std::endl;
//std::cout << "Setting up 0th frontier, first actions, toShift, toReduce" << std::endl;
//Frontier 0, new node with state 0
NodeTree<int>* v0 = gss.newNode(0);
gss.addToFrontier(0,v0);
std::cout << "Done setting up new frontier" << std::endl;
//std::cout << "Done setting up new frontier" << std::endl;
std::vector<ParseAction*> firstActions = *(table.get(0, input[0]));
for (std::vector<ParseAction*>::size_type i = 0; i < firstActions.size(); i++) {
@@ -80,7 +80,7 @@ NodeTree<Symbol>* RNGLRParser::parseInput(std::string inputString) {
// std::cout << "GSS:\n" << gss.toString() << std::endl;
std::cout << "Starting parse loop" << std::endl;
//std::cout << "Starting parse loop" << std::endl;
for (int i = 0; i < input.size(); i++) {
// std::cout << "Checking if frontier " << i << " is empty" << std::endl;
@@ -110,7 +110,7 @@ NodeTree<Symbol>* RNGLRParser::parseInput(std::string inputString) {
shifter(i);
//std::cout << "GSS:\n" << gss.toString() << std::endl;
}
std::cout << "Done with parsing loop, checking for acceptance" << std::endl;
//std::cout << "Done with parsing loop, checking for acceptance" << std::endl;
NodeTree<int>* accState = gss.frontierGetAccState(input.size()-1);
if (accState) {
std::cout << "Accepted!" << std::endl;
@@ -143,7 +143,7 @@ void RNGLRParser::reducer(int i) {
//The end of the current path
NodeTree<int>* currentReached = currentPath[currentPath.size()-1];
std::cout << "Getting the shfit state for state " << currentReached->getData() << " and symbol " << reduction.symbol.toString() << std::endl;
//std::cout << "Getting the shift state for state " << currentReached->getData() << " and symbol " << reduction.symbol.toString() << std::endl;
int toState = table.getShift(currentReached->getData(), reduction.symbol)->shiftState;
//If reduction length is 0, then we make the new label the appropriate nullable parts
@@ -189,7 +189,7 @@ void RNGLRParser::reducer(int i) {
//std::cout << "Adding shifts and reductions for a state that did not exist" << std::endl;
std::vector<ParseAction*> actions = *(table.get(toState, input[i]));
for (std::vector<ParseAction*>::size_type k = 0; k < actions.size(); k++) {
std::cout << "Action is " << actions[k]->toString() << std::endl;
//std::cout << "Action is " << actions[k]->toString() << std::endl;
if (actions[k]->action == ParseAction::SHIFT) {
toShift.push(std::make_pair(toStateNode, actions[k]->shiftState));
} else if (actions[k]->action == ParseAction::REDUCE && fullyReducesToNull(actions[k]->reduceRule)) {
@@ -213,7 +213,7 @@ void RNGLRParser::shifter(int i) {
while (!toShift.empty()) {
std::pair<NodeTree<int>*, int> shift = toShift.front();
toShift.pop();
std::cout << "Current potential shift from " << shift.first->getData() << " to " << shift.second << std::endl;
//std::cout << "Current potential shift from " << shift.first->getData() << " to " << shift.second << std::endl;
NodeTree<int>* shiftTo = gss.inFrontier(i+1, shift.second);
if (shiftTo) {
//std::cout << "State already existed, just adding edge" << std::endl;
@@ -232,7 +232,7 @@ void RNGLRParser::shifter(int i) {
gss.addEdge(shiftTo, shift.first, newLabel);
std::vector<ParseAction*> actions = *(table.get(shift.second, input[i+1]));
for (std::vector<ParseAction*>::size_type j = 0; j < actions.size(); j++) {
std::cout << "Adding action " << actions[j]->toString() << " to either nextShifts or toReduce" << std::endl;
//std::cout << "Adding action " << actions[j]->toString() << " to either nextShifts or toReduce" << std::endl;
//Shift
if (actions[j]->action == ParseAction::SHIFT) {
nextShifts.push(std::make_pair(shiftTo, actions[j]->shiftState));

View File

@@ -75,7 +75,7 @@ std::string StringReader::getTokens(const char *stop_chars, bool truncateEnd)
{
//End of String
end_reached = true;
std::cout << "Reached end of file!\n";
//std::cout << "Reached end of file!\n";
return "";
} else {

View File

@@ -303,7 +303,7 @@ std::vector<ParseAction*>* Table::get(int state, Symbol token) {
return NULL;
}
std::cout << "Get for state: " << state << ", and Symbol: " << token.toString() << std::endl;
//std::cout << "Get for state: " << state << ", and Symbol: " << token.toString() << std::endl;
if (state < 0 || state >= table.size()) {
std::cout << "State bad: " << state << std::endl;
return NULL;
@@ -312,7 +312,7 @@ std::vector<ParseAction*>* Table::get(int state, Symbol token) {
std::vector<ParseAction*>* action = NULL;
if (symbolIndex < 0 || symbolIndex >= table[state]->size()) {
std::cout << "Symbol bad for this state: " << token.toString() << ". This is a reject." << std::endl;
//std::cout << "Symbol bad for this state: " << token.toString() << ". This is a reject." << std::endl;
} else {
action = (*(table[state]))[symbolIndex];
}

View File

@@ -33,7 +33,7 @@ Type::Type(std::string typeIn) {
baseType = character;
else
baseType = none;
std::cout << ":ALKJF:LSKDJF:SDJF:LKSJDF\t\t\t" << typeIn << "\t" << edited << std::endl;
//std::cout << ":ALKJF:LSKDJF:SDJF:LKSJDF\t\t\t" << typeIn << "\t" << edited << std::endl;
}