diff --git a/include/util.h b/include/util.h index 6ca6b1e..2be7301 100644 --- a/include/util.h +++ b/include/util.h @@ -16,5 +16,12 @@ std::string strSlice(std::string str, int begin, int end); int findPerenEnd(std::string str, int i); std::vector split(const std::string &str, char delim); std::string join(const std::vector &strVec, std::string joinStr); +template +bool contains(std::vector vec, T item) { + for (auto i : vec) + if (i == item) + return true; + return false; +} #endif diff --git a/krakenGrammer.kgm b/krakenGrammer.kgm index 610816d..f396a63 100644 --- a/krakenGrammer.kgm +++ b/krakenGrammer.kgm @@ -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 ; diff --git a/src/CGenerator.cpp b/src/CGenerator.cpp index 78b8c7d..962e664 100644 --- a/src/CGenerator.cpp +++ b/src/CGenerator.cpp @@ -104,7 +104,7 @@ std::string CGenerator::generate(NodeTree* from, NodeTree* 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* from, NodeTree* 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* 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; diff --git a/src/util.cpp b/src/util.cpp index cb1fa4c..a3c7802 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -71,3 +71,4 @@ std::string join(const std::vector &strVec, std::string joinStr) { return joinedStr; } +