Clean up, some small additions.
This commit is contained in:
@@ -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