Fixed a lot of stuff to do with objects and method calls.

This commit is contained in:
Nathan Braswell
2014-03-08 16:13:09 -05:00
parent 51e04498d6
commit 6eee808f96
4 changed files with 17 additions and 3 deletions

View File

@@ -16,5 +16,12 @@ std::string strSlice(std::string str, int begin, int end);
int findPerenEnd(std::string str, int i);
std::vector<std::string> split(const std::string &str, char delim);
std::string join(const std::vector<std::string> &strVec, std::string joinStr);
template <typename T>
bool contains(std::vector<T> vec, T item) {
for (auto i : vec)
if (i == item)
return true;
return false;
}
#endif

View File

@@ -69,7 +69,7 @@ term = term WS forward_slash WS factor | term WS "\*" WS factor | term WS "%" WS
factor = "\+\+" WS unarad | unarad WS "\+\+" | "--" WS unarad | unarad WS "--" | "\+" WS unarad | "-" WS unarad | "!" WS unarad | "~" WS unarad | "\(" WS type WS "\)" WS unarad | "\*" WS unarad | "&" WS unarad | unarad ;
unarad = number | identifier | function_call | bool | string | character | "\(" WS boolean_expression WS "\)" | access_operation ;
number = integer | float | double ;
access_operation = expression "." identifier | expression "->" identifier ;
access_operation = unarad "." identifier | unarad "->" identifier ;
assignment_statement = factor WS "=" WS boolean_expression | factor WS "\+=" WS boolean_expression | factor WS "-=" WS boolean_expression | factor WS "\*=" WS boolean_expression | factor WS "/=" WS boolean_expression ;
declaration_statement = type WS identifier WS "=" WS boolean_expression | type WS identifier ;

View File

@@ -104,7 +104,7 @@ std::string CGenerator::generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enc
if (false)
for (int j = 0; j < children.size()-1; j++)
preName += ValueTypeToCType(children[j]->getData().valueType) + "_";
return data.symbol.getName();
return preName + data.symbol.getName();
}
case type_def:
if (children.size() == 0) {
@@ -235,7 +235,13 @@ std::string CGenerator::generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enc
std::string nameDecoration;
for (int i = 0; i < (functionDefChildren.size() > 0 ? functionDefChildren.size()-1 : 0); i++)
nameDecoration += "_" + ValueTypeToCTypeDecoration(functionDefChildren[i]->getData().valueType);
//Check to see if we're inside of an object and this is a method call
bool isSelfObjectMethod = enclosingObject && contains(enclosingObject->getChildren(), children[0]);
if (isSelfObjectMethod)
output += enclosingObject->getDataRef()->symbol.getName() +"__";
/*HERE*/ output += name + nameDecoration + "(";
if (isSelfObjectMethod)
output += children.size() > 1 ? "self," : "self";
}
} else {
//This part handles cases where our definition isn't the function definition (that is, it is probabally the return from another function)
@@ -277,7 +283,7 @@ std::string CGenerator::generateObjectMethod(NodeTree<ASTData>* enclosingObject,
parameters += ", " + ValueTypeToCType(children[i]->getData().valueType) + " " + generate(children[i]);
nameDecoration += "_" + ValueTypeToCTypeDecoration(children[i]->getData().valueType);
}
output += "\n" + ValueTypeToCType(data.valueType) + " " + enclosingObject->getDataRef()->symbol.getName() +"__"
output += "\n" + ValueTypeToCType(data.valueType) + " " + enclosingObject->getDataRef()->symbol.getName() +"__"
+ data.symbol.getName() + nameDecoration + "(" + ValueTypeToCType(&enclosingObjectType) + " self" + parameters + ")\n"
+ generate(children[children.size()-1], enclosingObject); //Pass in the object so we can
return output;

View File

@@ -71,3 +71,4 @@ std::string join(const std::vector<std::string> &strVec, std::string joinStr) {
return joinedStr;
}