Clean up, some small additions.
This commit is contained in:
@@ -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 ;
|
||||
|
||||
|
||||
4
main.cpp
4
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);
|
||||
}
|
||||
|
||||
|
||||
@@ -250,22 +250,22 @@ std::string ASTTransformation::concatSymbolTree(NodeTree<Symbol>* root) {
|
||||
NodeTree<ASTData>* ASTTransformation::scopeLookup(NodeTree<ASTData>* 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;
|
||||
}
|
||||
|
||||
@@ -23,17 +23,24 @@ std::string CGenerator::generate(NodeTree<ASTData>* 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<ASTData>* declaration = i->second;
|
||||
std::vector<NodeTree<ASTData>*> 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<ASTData>* 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<ASTData>* 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]);
|
||||
|
||||
Reference in New Issue
Block a user