Working on objects and scoping. To finish, need to actually implement decent propogation of types

This commit is contained in:
Nathan Braswell
2014-02-03 11:41:25 -05:00
parent 62933af8f8
commit 1791738cd9
3 changed files with 161 additions and 79 deletions

View File

@@ -17,8 +17,9 @@ class CGenerator {
CGenerator(); CGenerator();
~CGenerator(); ~CGenerator();
void generateCompSet(std::map<std::string, NodeTree<ASTData>*> ASTs, std::string outputName); void generateCompSet(std::map<std::string, NodeTree<ASTData>*> ASTs, std::string outputName);
std::string generate(NodeTree<ASTData>* from); std::string generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enclosingObject = NULL);
static std::string ValueTypeToCType(Type *type); static std::string ValueTypeToCType(Type *type);
std::string generateObjectMethod(NodeTree<ASTData>* enclosingObject, NodeTree<ASTData>* from);
std::string generatorString; std::string generatorString;
private: private:

View File

@@ -17,7 +17,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from) {
NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree<ASTData>* scope) { NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree<ASTData>* scope) {
Symbol current = from->getData(); Symbol current = from->getData();
std::string name = current.getName(); std::string name = current.getName();
NodeTree<ASTData>* newNode; NodeTree<ASTData>* newNode = NULL;
std::vector<NodeTree<Symbol>*> children = from->getChildren(); std::vector<NodeTree<Symbol>*> children = from->getChildren();
std::set<int> skipChildren; std::set<int> skipChildren;
@@ -25,25 +25,26 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
newNode = new NodeTree<ASTData>(name, ASTData(translation_unit)); newNode = new NodeTree<ASTData>(name, ASTData(translation_unit));
scope = newNode; scope = newNode;
//Temporary scope fix //Temporary scope fix
scope->getDataRef()->scope["+"] = new NodeTree<ASTData>(); Type placeholderType;
scope->getDataRef()->scope["-"] = new NodeTree<ASTData>(); scope->getDataRef()->scope["+"] = new NodeTree<ASTData>("function", ASTData(function, Symbol("+", true), &placeholderType));
scope->getDataRef()->scope["*"] = new NodeTree<ASTData>(); scope->getDataRef()->scope["-"] = new NodeTree<ASTData>("function", ASTData(function, Symbol("-", true), &placeholderType));
scope->getDataRef()->scope["&"] = new NodeTree<ASTData>(); scope->getDataRef()->scope["*"] = new NodeTree<ASTData>("function", ASTData(function, Symbol("*", true), &placeholderType));
scope->getDataRef()->scope["--"] = new NodeTree<ASTData>(); scope->getDataRef()->scope["&"] = new NodeTree<ASTData>("function", ASTData(function, Symbol("&", true), &placeholderType));
scope->getDataRef()->scope["++"] = new NodeTree<ASTData>(); scope->getDataRef()->scope["--"] = new NodeTree<ASTData>("function", ASTData(function, Symbol("--", true), &placeholderType));
scope->getDataRef()->scope["=="] = new NodeTree<ASTData>(); scope->getDataRef()->scope["++"] = new NodeTree<ASTData>("function", ASTData(function, Symbol("++", true), &placeholderType));
scope->getDataRef()->scope["<="] = new NodeTree<ASTData>(); scope->getDataRef()->scope["=="] = new NodeTree<ASTData>("function", ASTData(function, Symbol("==", true), &placeholderType));
scope->getDataRef()->scope[">="] = new NodeTree<ASTData>(); scope->getDataRef()->scope["<="] = new NodeTree<ASTData>("function", ASTData(function, Symbol("<=", true), &placeholderType));
scope->getDataRef()->scope["<"] = new NodeTree<ASTData>(); scope->getDataRef()->scope[">="] = new NodeTree<ASTData>("function", ASTData(function, Symbol(">=", true), &placeholderType));
scope->getDataRef()->scope[">"] = new NodeTree<ASTData>(); scope->getDataRef()->scope["<"] = new NodeTree<ASTData>("function", ASTData(function, Symbol("<", true), &placeholderType));
scope->getDataRef()->scope["&&"] = new NodeTree<ASTData>(); scope->getDataRef()->scope[">"] = new NodeTree<ASTData>("function", ASTData(function, Symbol(">", true), &placeholderType));
scope->getDataRef()->scope["||"] = new NodeTree<ASTData>(); scope->getDataRef()->scope["&&"] = new NodeTree<ASTData>("function", ASTData(function, Symbol("&&", true), &placeholderType));
scope->getDataRef()->scope["!"] = new NodeTree<ASTData>(); scope->getDataRef()->scope["||"] = new NodeTree<ASTData>("function", ASTData(function, Symbol("||", true), &placeholderType));
scope->getDataRef()->scope["*="] = new NodeTree<ASTData>(); scope->getDataRef()->scope["!"] = new NodeTree<ASTData>("function", ASTData(function, Symbol("!", true), &placeholderType));
scope->getDataRef()->scope["+="] = new NodeTree<ASTData>(); scope->getDataRef()->scope["*="] = new NodeTree<ASTData>("function", ASTData(function, Symbol("*=", true), &placeholderType));
scope->getDataRef()->scope["-="] = new NodeTree<ASTData>(); scope->getDataRef()->scope["+="] = new NodeTree<ASTData>("function", ASTData(function, Symbol("+=", true), &placeholderType));
scope->getDataRef()->scope["."] = new NodeTree<ASTData>(); scope->getDataRef()->scope["-="] = new NodeTree<ASTData>("function", ASTData(function, Symbol("-=", true), &placeholderType));
scope->getDataRef()->scope["->"] = new NodeTree<ASTData>(); scope->getDataRef()->scope["."] = new NodeTree<ASTData>("function", ASTData(function, Symbol(".", true), &placeholderType));
scope->getDataRef()->scope["->"] = new NodeTree<ASTData>("function", ASTData(function, Symbol("->", true), &placeholderType));
} else if (name == "interpreter_directive") { } else if (name == "interpreter_directive") {
newNode = new NodeTree<ASTData>(name, ASTData(interpreter_directive)); newNode = new NodeTree<ASTData>(name, ASTData(interpreter_directive));
@@ -63,7 +64,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
//std::cout << "scope lookup from identifier" << std::endl; //std::cout << "scope lookup from identifier" << std::endl;
newNode = scopeLookup(scope, lookupName); newNode = scopeLookup(scope, lookupName);
if (newNode == NULL) { if (newNode == NULL) {
std::cout << "scope lookup error! Could not find " << lookupName << std::endl; std::cout << "scope lookup error! Could not find " << lookupName << " in identifier " << std::endl;
throw "LOOKUP ERROR: " + lookupName; throw "LOOKUP ERROR: " + lookupName;
} else if (newNode->getDataRef()->symbol.getName() !=lookupName) { } else if (newNode->getDataRef()->symbol.getName() !=lookupName) {
//This happens when the lookup name denotes a member of an object, i.e. obj.foo //This happens when the lookup name denotes a member of an object, i.e. obj.foo
@@ -81,6 +82,8 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
newNode->getDataRef()->valueType = new Type(newNode); //Type is self-referential since this is the definition newNode->getDataRef()->valueType = new Type(newNode); //Type is self-referential since this is the definition
} }
scope->getDataRef()->scope[typeAlias] = newNode; scope->getDataRef()->scope[typeAlias] = newNode;
newNode->getDataRef()->scope["~enclosing_scope"] = scope;
scope = newNode;
skipChildren.insert(0); //Identifier lookup will be ourselves, as we just added ourselves to the scope skipChildren.insert(0); //Identifier lookup will be ourselves, as we just added ourselves to the scope
//return newNode; //return newNode;
} else if (name == "function") { } else if (name == "function") {
@@ -91,6 +94,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
scope->getDataRef()->scope[functionName] = newNode; scope->getDataRef()->scope[functionName] = newNode;
newNode->getDataRef()->scope["~enclosing_scope"] = scope; newNode->getDataRef()->scope["~enclosing_scope"] = scope;
scope = newNode; scope = newNode;
std::cout << "finished function " << functionName << std::endl;
} else if (name == "code_block") { } else if (name == "code_block") {
newNode = new NodeTree<ASTData>(name, ASTData(code_block)); newNode = new NodeTree<ASTData>(name, ASTData(code_block));
newNode->getDataRef()->scope["~enclosing_scope"] = scope; newNode->getDataRef()->scope["~enclosing_scope"] = scope;
@@ -101,19 +105,19 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
std::string typeString = concatSymbolTree(children[0]);//Get the type (left child) and set our new identifer to be that type std::string typeString = concatSymbolTree(children[0]);//Get the type (left child) and set our new identifer to be that type
newNode = new NodeTree<ASTData>("identifier", ASTData(identifier, Symbol(parameterName, true), typeFromString(typeString, scope))); newNode = new NodeTree<ASTData>("identifier", ASTData(identifier, Symbol(parameterName, true), typeFromString(typeString, scope)));
scope->getDataRef()->scope[parameterName] = newNode; scope->getDataRef()->scope[parameterName] = newNode;
newNode->getDataRef()->scope["~enclosing_scope"] = scope;
return newNode; return newNode;
} else if (name == "boolean_expression" || name == "and_boolean_expression" || name == "bool_exp") { } else if (name == "boolean_expression" || name == "and_boolean_expression" || name == "bool_exp") {
//If this is an actual part of an expression, not just a premoted term //If this is an actual part of an expression, not just a premoted term
if (children.size() > 1) { if (children.size() > 1) {
std::string functionCallName = concatSymbolTree(children[1]); std::string functionCallString = concatSymbolTree(children[1]);
//std::cout << "scope lookup from boolen_expression or similar" << std::endl; NodeTree<ASTData>* function = scopeLookup(scope, functionCallString);
NodeTree<ASTData>* function = scopeLookup(scope, functionCallName);
if (function == NULL) { if (function == NULL) {
std::cout << "scope lookup error! Could not find " << functionCallName << std::endl; std::cout << "scope lookup error! Could not find " << functionCallString << " in boolean stuff " << std::endl;
throw "LOOKUP ERROR: " + functionCallName; throw "LOOKUP ERROR: " + functionCallString;
} }
newNode = new NodeTree<ASTData>(functionCallName, ASTData(function_call, Symbol(functionCallName, true))); newNode = new NodeTree<ASTData>(functionCallString, ASTData(function_call));
newNode->addChild(function); // First child of function call is a link to the function definition newNode->addChild(function); // First child of function call is a link to the function
skipChildren.insert(1); skipChildren.insert(1);
} else { } else {
//std::cout << children.size() << std::endl; //std::cout << children.size() << std::endl;
@@ -129,12 +133,25 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
//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); NodeTree<ASTData>* function = scopeLookup(scope, functionCallName);
if (function == NULL) { if (function == NULL) {
std::cout << "scope lookup error! Could not find " << functionCallName << std::endl; std::cout << "scope lookup error! Could not find " << functionCallName << " in expression " << std::endl;
throw "LOOKUP ERROR: " + functionCallName; throw "LOOKUP ERROR: " + functionCallName;
} }
newNode = new NodeTree<ASTData>(functionCallName, ASTData(function_call, Symbol(functionCallName, true))); newNode = new NodeTree<ASTData>(functionCallName, ASTData(function_call, Symbol(functionCallName, true)));
newNode->addChild(function); // First child of function call is a link to the function definition newNode->addChild(function); // First child of function call is a link to the function definition
skipChildren.insert(1); NodeTree<ASTData>* lhs = transform(children[0], scope);
NodeTree<ASTData>* rhs;// = transform(children[2], scope);
if (name == "access_operation")
rhs = transform(children[2], lhs->getDataRef()->valueType->typeDefinition); //If an access operation, then the right side will be in the lhs's type's scope
else
rhs = transform(children[2], scope);
if (name == "access_operation")
std::cout << "Access Operation: " << lhs->getDataRef()->symbol.getName() << " : " << rhs->getDataRef()->symbol.getName() << std::endl;
newNode->addChild(lhs);
newNode->addChild(rhs);
return newNode;
//skipChildren.insert(1);
} else { } else {
return transform(children[0], scope); //Just a promoted child, so do it instead return transform(children[0], scope); //Just a promoted child, so do it instead
} }
@@ -152,7 +169,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
//std::cout << "scope lookup from factor" << std::endl; //std::cout << "scope lookup from factor" << std::endl;
NodeTree<ASTData>* function = scopeLookup(scope, funcName); NodeTree<ASTData>* function = scopeLookup(scope, funcName);
if (function == NULL) { if (function == NULL) {
std::cout << "scope lookup error! Could not find " << funcName << std::endl; std::cout << "scope lookup error! Could not find " << funcName << " in factor " << std::endl;
throw "LOOKUP ERROR: " + funcName; throw "LOOKUP ERROR: " + funcName;
} }
newNode = new NodeTree<ASTData>(funcName, ASTData(function_call, Symbol(funcName, true))); newNode = new NodeTree<ASTData>(funcName, ASTData(function_call, Symbol(funcName, true)));
@@ -184,7 +201,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
NodeTree<ASTData>* childCall = new NodeTree<ASTData>(functionName, ASTData(function_call, Symbol(functionName, true))); NodeTree<ASTData>* childCall = new NodeTree<ASTData>(functionName, ASTData(function_call, Symbol(functionName, true)));
NodeTree<ASTData>* functionDef = scopeLookup(scope, functionName); NodeTree<ASTData>* functionDef = scopeLookup(scope, functionName);
if (functionDef == NULL) { if (functionDef == NULL) {
std::cout << "scope lookup error! Could not find " << functionName << std::endl; std::cout << "scope lookup error! Could not find " << functionName << " in assignment_statement " << std::endl;
throw "LOOKUP ERROR: " + functionName; throw "LOOKUP ERROR: " + functionName;
} }
childCall->addChild(functionDef); //First child of function call is definition of the function childCall->addChild(functionDef); //First child of function call is definition of the function
@@ -204,6 +221,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
Type* identifierType = typeFromString(typeString, scope); Type* identifierType = typeFromString(typeString, scope);
NodeTree<ASTData>* newIdentifier = new NodeTree<ASTData>("identifier", ASTData(identifier, Symbol(newIdentifierStr, true), identifierType)); NodeTree<ASTData>* newIdentifier = new NodeTree<ASTData>("identifier", ASTData(identifier, Symbol(newIdentifierStr, true), identifierType));
scope->getDataRef()->scope[newIdentifierStr] = newIdentifier; scope->getDataRef()->scope[newIdentifierStr] = newIdentifier;
newNode->getDataRef()->scope["~enclosing_scope"] = scope;
//Now we don't do this thing //Now we don't do this thing
// if (identifierType->typeDefinition) { // if (identifierType->typeDefinition) {
// //Is a custom type. Populate this declaration's scope with it's inner declarations // //Is a custom type. Populate this declaration's scope with it's inner declarations
@@ -226,12 +244,19 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
} else if (name == "function_call") { } else if (name == "function_call") {
std::string functionCallName = concatSymbolTree(children[0]); std::string functionCallName = concatSymbolTree(children[0]);
newNode = new NodeTree<ASTData>(functionCallName, ASTData(function_call, Symbol(functionCallName, true))); 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: " << functionCallName << std::endl;
NodeTree<ASTData>* function = scopeLookup(scope, functionCallName); for (auto i : children)
if (function == NULL) { std::cout << i << " : " << i->getName() << " : " << i->getDataRef()->getName() << std::endl;
std::cout << "scope lookup error! Could not find " << functionCallName << std::endl; //NodeTree<ASTData>* function = scopeLookup(scope, functionCallName);
throw "LOOKUP ERROR: " + functionCallName; NodeTree<ASTData>* function = transform(children[0], scope);/* scopeLookup(scope, functionCallName);*/
} std::cout << "The thing: " << function << " : " << function->getName() << std::endl;
for (auto i : function->getChildren())
std::cout << i->getName() << " ";
std::cout << std::endl;
// if (function == NULL) {
// std::cout << "scope lookup error! Could not find " << functionCallName << " in function_call " << std::endl;
// throw "LOOKUP ERROR: " + functionCallName;
// }
newNode->addChild(function); newNode->addChild(function);
skipChildren.insert(0); skipChildren.insert(0);
} else if (name == "parameter") { } else if (name == "parameter") {

View File

@@ -35,16 +35,17 @@ std::string CGenerator::tabs() {
return returnTabs; return returnTabs;
} }
std::string CGenerator::generate(NodeTree<ASTData>* from) { //The enclosing object is for when we're generating the inside of object methods. They allow us to check scope lookups against the object we're in
std::string CGenerator::generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enclosingObject) {
ASTData data = from->getData(); ASTData data = from->getData();
std::vector<NodeTree<ASTData>*> children = from->getChildren(); std::vector<NodeTree<ASTData>*> children = from->getChildren();
std::string output = ""; std::string output = "";
switch (data.type) { switch (data.type) {
case translation_unit: case translation_unit:
//Do here because we may need the typedefs before the declarations of variables //Do here because we may need the typedefs before the declarations of variables
for (int i = 0; i < children.size(); i++) for (int i = 0; i < children.size(); i++)
if (children[i]->getDataRef()->type == type_def) if (children[i]->getDataRef()->type == type_def)
output += generate(children[i]) + "\n"; output += generate(children[i], enclosingObject) + "\n";
//Declare everything in translation unit scope here. (allows stuff from other files, automatic forward declarations) //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++) { for (auto i = data.scope.begin(); i != data.scope.end(); i++) {
NodeTree<ASTData>* declaration = i->second; NodeTree<ASTData>* declaration = i->second;
@@ -55,11 +56,15 @@ std::string CGenerator::generate(NodeTree<ASTData>* from) {
output += ValueTypeToCType(declarationData.valueType) + " " + declarationData.symbol.getName() + "; /*identifier*/\n"; output += ValueTypeToCType(declarationData.valueType) + " " + declarationData.symbol.getName() + "; /*identifier*/\n";
break; break;
case function: case function:
if (decChildren.size() == 0) { //Not a real function, must be a built in passthrough {
output += "/* built in function: " + declarationData.toString() + " */\n";
break;
}
output += "\n" + ValueTypeToCType(declarationData.valueType) + " " + declarationData.symbol.getName() + "("; output += "\n" + ValueTypeToCType(declarationData.valueType) + " " + declarationData.symbol.getName() + "(";
for (int j = 0; j < decChildren.size()-1; j++) { for (int j = 0; j < decChildren.size()-1; j++) {
if (j > 0) if (j > 0)
output += ", "; output += ", ";
output += ValueTypeToCType(decChildren[j]->getData().valueType) + " " + generate(decChildren[j]); output += ValueTypeToCType(decChildren[j]->getData().valueType) + " " + generate(decChildren[j], enclosingObject);
} }
output += "); /*func*/\n"; output += "); /*func*/\n";
break; break;
@@ -75,7 +80,7 @@ std::string CGenerator::generate(NodeTree<ASTData>* from) {
//Do here because we need the newlines //Do here because we need the newlines
for (int i = 0; i < children.size(); i++) for (int i = 0; i < children.size(); i++)
if (children[i]->getDataRef()->type != type_def) if (children[i]->getDataRef()->type != type_def)
output += generate(children[i]) + "\n"; output += generate(children[i], enclosingObject) + "\n";
return output; return output;
break; break;
case interpreter_directive: case interpreter_directive:
@@ -85,31 +90,47 @@ std::string CGenerator::generate(NodeTree<ASTData>* from) {
return "/* would import \"" + data.symbol.getName() + "\" but....*/\n"; return "/* would import \"" + data.symbol.getName() + "\" but....*/\n";
//return "#include <" + data.symbol.getName() + ">\n"; //return "#include <" + data.symbol.getName() + ">\n";
case identifier: case identifier:
{
//If we're in an object method, and our enclosing scope is that object, we're a member of the object and should use the self reference.
if (enclosingObject && enclosingObject->getDataRef()->scope.find(data.symbol.getName()) != enclosingObject->getDataRef()->scope.end()) {
return "self->" + data.symbol.getName();
}
return data.symbol.getName(); return data.symbol.getName();
}
case type_def: case type_def:
if (children.size() == 0) { if (children.size() == 0) {
return "typedef " + ValueTypeToCType(data.valueType) + " " + data.symbol.getName() + ";"; return "typedef " + ValueTypeToCType(data.valueType) + " " + data.symbol.getName() + ";";
} else { } else {
std::string objectString = "typedef struct __struct_dummy_" + data.symbol.getName() + "__ {\n"; std::string objectString = "typedef struct __struct_dummy_" + data.symbol.getName() + "__ {\n";
for (int i = 0; i < children.size(); i++) std::string postString; //The functions have to be outside the struct definition
objectString += generate(children[i]) + "\n"; for (int i = 0; i < children.size(); i++) {
std::cout << children[i]->getName() << std::endl;
if (children[i]->getName() == "function") //If object method
postString += generateObjectMethod(from, children[i]) + "\n";
else
objectString += generate(children[i], enclosingObject) + "\n";
}
objectString += "} " + data.symbol.getName() + ";"; objectString += "} " + data.symbol.getName() + ";";
return objectString; return objectString + postString; //Functions come after the declaration of the struct
} }
case function: case function:
output += "\n" + ValueTypeToCType(data.valueType) + " " + data.symbol.getName() + "("; output += "\n" + ValueTypeToCType(data.valueType) + " " + data.symbol.getName() + "(";
for (int i = 0; i < children.size()-1; i++) { for (int i = 0; i < children.size()-1; i++) {
if (i > 0) if (i > 0)
output += ", "; output += ", ";
output += ValueTypeToCType(children[i]->getData().valueType) + " " + generate(children[i]); output += ValueTypeToCType(children[i]->getData().valueType) + " " + generate(children[i], enclosingObject);
} }
output+= ")\n" + generate(children[children.size()-1]); output+= ")\n" + generate(children[children.size()-1], enclosingObject);
return output; return output;
case code_block: case code_block:
output += "{\n"; output += "{\n";
tabLevel++; tabLevel++;
for (int i = 0; i < children.size(); i++) for (int i = 0; i < children.size(); i++) {
output += generate(children[i]); std::cout << "Line " << i << std::endl;
std::string line = generate(children[i], enclosingObject);
std::cout << line << std::endl;
output += line;
}
tabLevel--; tabLevel--;
output += tabs() + "}"; output += tabs() + "}";
return output; return output;
@@ -118,60 +139,82 @@ std::string CGenerator::generate(NodeTree<ASTData>* from) {
case boolean_expression: case boolean_expression:
output += " " + data.symbol.getName() + " "; output += " " + data.symbol.getName() + " ";
case statement: case statement:
return tabs() + generate(children[0]) + ";\n"; return tabs() + generate(children[0], enclosingObject) + ";\n";
case if_statement: case if_statement:
output += "if (" + generate(children[0]) + ")\n\t" + generate(children[1]); output += "if (" + generate(children[0], enclosingObject) + ")\n\t" + generate(children[1], enclosingObject);
if (children.size() > 2) if (children.size() > 2)
output += " else " + generate(children[2]); output += " else " + generate(children[2], enclosingObject);
return output; return output;
case while_loop: case while_loop:
output += "while (" + generate(children[0]) + ")\n\t" + generate(children[1]); output += "while (" + generate(children[0], enclosingObject) + ")\n\t" + generate(children[1], enclosingObject);
return output; return output;
case for_loop: case for_loop:
//The strSlice's are there to get ride of an unwanted return and an unwanted semicolon(s) //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]); output += "for (" + strSlice(generate(children[0], enclosingObject),0,-3) + generate(children[1], enclosingObject) + ";" + strSlice(generate(children[2], enclosingObject),0,-3) + ")\n\t" + generate(children[3], enclosingObject);
return output; return output;
case return_statement: case return_statement:
if (children.size()) if (children.size())
return "return " + generate(children[0]); return "return " + generate(children[0], enclosingObject);
else else
return "return"; return "return";
case assignment_statement: case assignment_statement:
return generate(children[0]) + " = " + generate(children[1]); return generate(children[0], enclosingObject) + " = " + generate(children[1], enclosingObject);
case declaration_statement: case declaration_statement:
if (children.size() == 1) if (children.size() == 1)
return ValueTypeToCType(children[0]->getData().valueType) + " " + generate(children[0]) + ";"; return ValueTypeToCType(children[0]->getData().valueType) + " " + generate(children[0], enclosingObject) + ";";
else else
return ValueTypeToCType(children[0]->getData().valueType) + " " + generate(children[0]) + " = " + generate(children[1]) + ";"; return ValueTypeToCType(children[0]->getData().valueType) + " " + generate(children[0], enclosingObject) + " = " + generate(children[1], enclosingObject) + ";";
case if_comp: case if_comp:
if (generate(children[0]) == generatorString) if (generate(children[0], enclosingObject) == generatorString)
return generate(children[1]); return generate(children[1], enclosingObject);
return ""; return "";
case simple_passthrough: case simple_passthrough:
return strSlice(generate(children[0]), 3, -4); return strSlice(generate(children[0], enclosingObject), 3, -4);
case function_call: case function_call:
{ {
//NOTE: The first (0th) child of a function call node is the declaration of the function //NOTE: The first (0th) child of a function call node is the declaration of the function
//Handle operators specially for now. Will later replace with //Handle operators specially for now. Will later replace with
//Inlined functions in the standard library //Inlined functions in the standard library
std::string name = data.symbol.getName(); // 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 == "--") std::string name = children[0]->getDataRef()->symbol.getName();
return generate(children[1]) + name; ASTType funcType = children[0]->getDataRef()->type;
if (name == "*" && children.size() == 2) //Is dereference, not multiplication std::cout << "Doing function: " << name << std::endl;
return "*(" + generate(children[1]) + ")"; //Test for specail functions only if what we're testing is, indeed, the definition, not a function call that returns a callable function pointer
if (name == "+" || name == "-" || name == "*" || name == "/" || name == "==" || name == ">=" || name == "<=" || name == "!=" if (funcType == function) {
|| name == "<" || name == ">" || name == "%" || name == "+=" || name == "-=" || name == "*=" || name == "/=" || name == "||" if (name == "++" || name == "--")
|| name == "&&" || name == "!" ) return generate(children[1], enclosingObject) + name;
return "((" + generate(children[1]) + ")" + name + "(" + generate(children[2]) + "))"; if (name == "*" && children.size() == 2) //Is dereference, not multiplication
else if (name == "." || name == "->") return "*(" + generate(children[1], enclosingObject) + ")";
return "((" + generate(children[1]) + ")" + name + generate(children[2]) + ")"; if (name == "+" || name == "-" || name == "*" || name == "/" || name == "==" || name == ">=" || name == "<=" || name == "!="
output += data.symbol.getName() + "("; || name == "<" || name == ">" || name == "%" || name == "+=" || name == "-=" || name == "*=" || name == "/=" || name == "||"
|| name == "&&" || name == "!" )
return "((" + generate(children[1], enclosingObject) + ")" + name + "(" + generate(children[2], enclosingObject) + "))";
else if (name == "." || name == "->") {
if (children.size() == 1)
return "/*dot operation with one child*/" + generate(children[0], enclosingObject) + "/*end one child*/";
//If this is accessing an actual function, just output the name. No actual function definition should be in an access operation
if (children[2]->getDataRef()->type == function)
return "((" + generate(children[1], enclosingObject) + ")" + name + children[2]->getDataRef()->symbol.getName() + ")";
return "((" + generate(children[1], enclosingObject) + ")" + name + generate(children[2], enclosingObject) + ")";
}
}
//output += data.symbol.getName() + "(";
if (funcType == function) {
output += name + "(";
std::cout << "Is a function, outputting name!" << std::cout;
if (enclosingObject && enclosingObject->getDataRef()->scope.find(name) != enclosingObject->getDataRef()->scope.end()) {
//So, it is part of the enclosing object's namespace, so it's (for now) a member function and we need to pass in an implicit self reference
output += ValueTypeToCType(enclosingObject->getDataRef()->valueType) + "* self, ";
}
} else {
output += generate(children[0], enclosingObject) + "(";
}
for (int i = 1; i < children.size(); i++) //children[0] is the declaration for (int i = 1; i < children.size(); i++) //children[0] is the declaration
if (i < children.size()-1) if (i < children.size()-1)
output += generate(children[i]) + ", "; output += generate(children[i], enclosingObject) + ", ";
else output += generate(children[i]); else output += generate(children[i], enclosingObject);
output += ") "; output += ") ";
return output; return output;
} }
@@ -182,11 +225,24 @@ std::string CGenerator::generate(NodeTree<ASTData>* from) {
std::cout << "Nothing!" << std::endl; std::cout << "Nothing!" << std::endl;
} }
for (int i = 0; i < children.size(); i++) for (int i = 0; i < children.size(); i++)
output += generate(children[i]); output += generate(children[i], enclosingObject);
return output; return output;
} }
std::string CGenerator::generateObjectMethod(NodeTree<ASTData>* enclosingObject, NodeTree<ASTData>* from) {
std::string output;
ASTData data = from->getData();
Type enclosingObjectType = *(enclosingObject->getDataRef()->valueType); //Copy a new type so we can turn it into a pointer
enclosingObjectType.indirection++;
std::vector<NodeTree<ASTData>*> children = from->getChildren();
output += "\n" + ValueTypeToCType(data.valueType) + " " + enclosingObject->getDataRef()->symbol.getName() +"__" + data.symbol.getName() + "(" + ValueTypeToCType(&enclosingObjectType) + " self";
for (int i = 0; i < children.size()-1; i++)
output += ", " + ValueTypeToCType(children[i]->getData().valueType) + " " + generate(children[i]);
output+= ")\n" + generate(children[children.size()-1], enclosingObject); //Pass in the object so we can
return output;
}
std::string CGenerator::ValueTypeToCType(Type *type) { std::string CGenerator::ValueTypeToCType(Type *type) {
std::string return_type; std::string return_type;
switch (type->baseType) { switch (type->baseType) {