From dd9c8059ff245ea9ecb54d390dab66818679f908 Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Sat, 28 Dec 2013 21:54:22 -0500 Subject: [PATCH] Clean up, some small additions. --- krakenGrammer.kgm | 2 +- main.cpp | 4 ++-- src/ASTTransformation.cpp | 10 +++++----- src/CGenerator.cpp | 19 +++++++++++++------ 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/krakenGrammer.kgm b/krakenGrammer.kgm index 9ea5314..b7e2fe6 100644 --- a/krakenGrammer.kgm +++ b/krakenGrammer.kgm @@ -1,6 +1,6 @@ Goal = translation_unit ; translation_unit = interpreter_directive WS unorderd_list_part WS ; -unorderd_list_part = import_list WS unorderd_list_part | function WS unorderd_list_part | if_comp WS unorderd_list_part | simple_passthrough WS unorderd_list_part | import_list | function | if_comp | simple_passthrough ; +unorderd_list_part = import_list WS unorderd_list_part | function WS unorderd_list_part | if_comp WS unorderd_list_part | simple_passthrough WS unorderd_list_part | declaration_statement WS ";" WS unorderd_list_part | import_list | function | if_comp | simple_passthrough | declaration_statement WS ";" ; type = type WS "\*" | "void" | "int" | "float" | "double" | "char" | identifier ; diff --git a/main.cpp b/main.cpp index 8a7986f..360c2f5 100644 --- a/main.cpp +++ b/main.cpp @@ -36,9 +36,9 @@ int main(int argc, char* argv[]) { return(1); } - grammerInFile.open(argv[2]); + grammerInFile.open(grammerFileString); if (!grammerInFile.is_open()) { - std::cout << "Problem opening grammerInFile " << argv[2] << "\n"; + std::cout << "Problem opening grammerInFile " << grammerFileString << "\n"; return(1); } diff --git a/src/ASTTransformation.cpp b/src/ASTTransformation.cpp index 118e44c..3097243 100644 --- a/src/ASTTransformation.cpp +++ b/src/ASTTransformation.cpp @@ -250,22 +250,22 @@ std::string ASTTransformation::concatSymbolTree(NodeTree* root) { NodeTree* ASTTransformation::scopeLookup(NodeTree* scope, std::string lookup) { //Seach the map auto scopeMap = scope->getDataRef()->scope; - std::cout << "scope size: " << scopeMap.size() << ", scope from " << scope->getName() << std::endl; + //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; auto elementIterator = scopeMap.find(lookup); if (elementIterator != scopeMap.end()) { - std::cout << "lookup of " << lookup << " succeded in first scope!" << std::endl; + // std::cout << "lookup of " << lookup << " succeded in first scope!" << std::endl; return elementIterator->second; } - std::cout << "lookup of " << lookup << " failed in first scope, checking for upper scope" << std::endl; + //std::cout << "lookup of " << lookup << " failed in first scope, checking for upper scope" << std::endl; //if it doesn't exist, try the enclosing scope if it exists. auto enclosingIterator = scopeMap.find("~enclosing_scope"); if (enclosingIterator != scopeMap.end()) { - std::cout << "upper scope exists, searching it for " << lookup << std::endl; + // std::cout << "upper scope exists, searching it for " << lookup << std::endl; return scopeLookup(enclosingIterator->second, lookup); } - std::cout << "upper scope does not exist" << std::endl; + //std::cout << "upper scope does not exist" << std::endl; return NULL; } diff --git a/src/CGenerator.cpp b/src/CGenerator.cpp index 260a12d..007180a 100644 --- a/src/CGenerator.cpp +++ b/src/CGenerator.cpp @@ -23,17 +23,24 @@ std::string CGenerator::generate(NodeTree* from) { //Declare everything in translation unit scope here. (allows stuff from other files, automatic forward declarations) for (auto i = data.scope.begin(); i != data.scope.end(); i++) { NodeTree* declaration = i->second; + std::vector*> decChildren = declaration->getChildren(); ASTData declarationData = i->second->getData(); switch(declarationData.type) { case identifier: output += ValueTypeToCType(declarationData.valueType) + " " + declarationData.symbol.getName() + "; /*identifier*/\n"; break; case function: - output += "/*func*/\n"; + output += "\n" + ValueTypeToCType(declarationData.valueType) + " " + declarationData.symbol.getName() + "("; + for (int j = 0; j < decChildren.size()-1; j++) { + if (j > 0) + output += ", "; + output += ValueTypeToCType(decChildren[j]->getData().valueType) + " " + generate(decChildren[j]); + } + output+= "); /*func*/\n"; break; default: - std::cout << "Declaration? of unknown type " << ASTData::ASTTypeToString(declarationData.type) << " in translation unit scope" << std::endl; - output += "/*unknown declaration*/\n"; + 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"; } } break; @@ -77,8 +84,8 @@ std::string CGenerator::generate(NodeTree* from) { output += "while (" + generate(children[0]) + ")\n\t" + generate(children[1]); return output; case for_loop: - //The strSlice's are there to get ride of an unwanted return and an unwanted semicolon - output += "for (" + strSlice(generate(children[0]),0,-2) + generate(children[1]) + ";" + strSlice(generate(children[2]),0,-3) + ")\n\t" + generate(children[3]); + //The strSlice's are there to get ride of an unwanted return and an unwanted semicolon(s) + output += "for (" + strSlice(generate(children[0]),0,-3) + generate(children[1]) + ";" + strSlice(generate(children[2]),0,-3) + ")\n\t" + generate(children[3]); return output; case return_statement: if (children.size()) @@ -88,7 +95,7 @@ std::string CGenerator::generate(NodeTree* from) { case assignment_statement: return generate(children[0]) + " = " + generate(children[1]); case declaration_statement: - return ValueTypeToCType(children[0]->getData().valueType) + " " + generate(children[0]) + " = " + generate(children[1]); + return ValueTypeToCType(children[0]->getData().valueType) + " " + generate(children[0]) + " = " + generate(children[1]) + ";"; case if_comp: if (generate(children[0]) == generatorString) return generate(children[1]);