Small fixes to the grammer, ASTTransformation and CGenerator. Should now be ready to begin implementation of multiple files, conditional inclusion, and code passthrough.

This commit is contained in:
Nathan Braswell
2013-12-19 10:39:36 -06:00
parent f273deaedc
commit 6ad406e42d
7 changed files with 28 additions and 13 deletions

View File

@@ -62,17 +62,16 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from) {
return transform(children[0]); //Just a promoted bool_exp, so do child
}
//Here's the order of ops stuff
} else if (name == "expression" || name == "shiftand" || name == "term" || name == "unarad") {
} else if (name == "expression" || name == "shiftand" || name == "term" || name == "factor" || name == "unarad") {
//If this is an actual part of an expression, not just a premoted child
if (children.size() > 1) {
std::string functionCallName = concatSymbolTree(children[1]);
std::cout << functionCallName << std::endl;
newNode = new NodeTree<ASTData>(functionCallName, ASTData(function_call, Symbol(functionCallName, true)));
skipChildren.insert(1);
} else {
return transform(children[0]); //Just a promoted child, so do it instead
}
} else if (name == "factor") {
return transform(children[0]); //Just a premoted number or function call or something, so use it instead
} else if (name == "statement") {
newNode = new NodeTree<ASTData>(name, ASTData(statement));
} else if (name == "if_statement") {
@@ -92,7 +91,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from) {
} else {
//For assignments like += or *=, expand the syntatic sugar.
NodeTree<ASTData>* lhs = transform(children[0]);
NodeTree<ASTData>* childCall = new NodeTree<ASTData>(assignFuncName, ASTData(function_call, Symbol(assignFuncName, true)));
NodeTree<ASTData>* childCall = new NodeTree<ASTData>(assignFuncName.substr(0,1), ASTData(function_call, Symbol(assignFuncName.substr(0,1), true)));
childCall->addChild(lhs);
childCall->addChild(transform(children[2]));
newNode->addChild(lhs);

View File

@@ -58,10 +58,11 @@ std::string CGenerator::generate(NodeTree<ASTData>* from) {
output += " else " + generate(children[2]);
return output;
case while_loop:
output += "if (" + generate(children[0]) + ")\n\t" + generate(children[1]);
output += "while (" + generate(children[0]) + ")\n\t" + generate(children[1]);
return output;
case for_loop:
output += "if (" + generate(children[0]) + ")\n\t" + generate(children[1]);
//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]);
return output;
case return_statement:
return "return " + generate(children[0]);
@@ -74,7 +75,9 @@ 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();
if (name == "+" || name == "-" || name == "*" || name == "/" || name == "==" || name == ">=" || name == "<=" || name == "!=" || name == "%") {
if (name == "++" || name == "--")
return generate(children[0]) + name;
if (name == "+" || name == "-" || name == "*" || name == "/" || name == "==" || name == ">=" || name == "<=" || name == "!=" || name == "<" || name == ">" || name == "%" || name == "+=" || name == "-=" || name == "*=" || name == "/=") {
return "((" + generate(children[0]) + ")" + name + "(" + generate(children[1]) + "))";
}
output += data.symbol.getName() + "(";

View File

@@ -31,3 +31,12 @@ std::string replaceExEscape(std::string first, std::string search, std::string r
}
return first;
}
//String slicing is crazy useful. substr isn't bad, but slicing with negative indicies is wonderful
std::string strSlice(std::string str, int begin, int end) {
if (begin < 0)
begin += str.length()+1;
if (end < 0)
end += str.length()+1;
return str.substr(begin, end-begin);
}