From 4cf8dbbd5bb0f013dbdf6920c9b6ef305e62505e Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Mon, 28 Jul 2014 01:52:15 -0700 Subject: [PATCH] vector and vector test are finally working! Also found some bugs that I don't have time to fix before bed. Added file future_features.txt to keep track of bugs and features. --- future_features.txt | 10 ++ include/CGenerator.h | 4 +- krakenGrammer.kgm | 2 +- src/ASTTransformation.cpp | 45 ++++--- src/CGenerator.cpp | 193 +++++++++++++++++++----------- stdlib/mem.krak | 18 +-- stdlib/vector.krak | 21 ++-- tests/vectorTest.expected_results | 1 - tests/vectorTest.krak | 6 +- 9 files changed, 183 insertions(+), 117 deletions(-) create mode 100644 future_features.txt diff --git a/future_features.txt b/future_features.txt new file mode 100644 index 0000000..88b0a92 --- /dev/null +++ b/future_features.txt @@ -0,0 +1,10 @@ +Declaration of a pointer and multiplication are ambigious! +( T* a; maybe either a declaration or a multiplication) + +Fix destructors being placed after return. +Fix functions before declaration? (in class? (this is from an old file)) +Template instantiation without explicit type param +Fix // comment right before top level function declaration. Something to do +with the return as /* comment */ does not have that problem +for in or for each loops +Traits on aliases, maybe that keep their old traits? diff --git a/include/CGenerator.h b/include/CGenerator.h index 84fba11..a0472fb 100644 --- a/include/CGenerator.h +++ b/include/CGenerator.h @@ -18,11 +18,13 @@ class CGenerator { CGenerator(); ~CGenerator(); void generateCompSet(std::map*> ASTs, std::string outputName); + std::string generateClassStruct(NodeTree* from); std::string generate(NodeTree* from, NodeTree* enclosingObject = NULL); + std::string generateAliasChains(NodeTree* scopeNode, NodeTree* definition); static std::string ValueTypeToCType(Type *type); static std::string ValueTypeToCTypeDecoration(Type *type); static std::string CifyName(std::string name); - std::string generateObjectMethod(NodeTree* enclosingObject, NodeTree* from); + std::string generateObjectMethod(NodeTree* enclosingObject, NodeTree* from, std::string *functionPrototype); NodeTree* getMethodsObjectType(NodeTree* scope, std::string functionName); std::string generatorString; private: diff --git a/krakenGrammer.kgm b/krakenGrammer.kgm index c1bfd26..799c1ab 100644 --- a/krakenGrammer.kgm +++ b/krakenGrammer.kgm @@ -96,7 +96,7 @@ declaration_statement = type WS identifier WS "=" WS boolean_expression | type W alphanumeric = alphanumeric numeric | alphanumeric alpha | numeric | alpha ; hexadecimal = "0x(1|2|3|4|5|6|7|8|9|a|b|c|d|e|f)+" ; sign = "\+|-" WS | ; -integer = sign numeric | sign hexadecimal | "null" ; +integer = sign numeric | sign hexadecimal ; floating_literal = sign numeric "." numeric | sign numeric "." numeric alpha ; bool = "true" | "false" | "True" | "False" ; character = "'(`|1|2|3|4|5|6|7|8|9|0|-|=| |q|w|e|r|t|y|u|i|o|p|[|]|\\|a|s|d|f|g|h|j|k|l|;|'| diff --git a/src/ASTTransformation.cpp b/src/ASTTransformation.cpp index efd6be0..c883adf 100644 --- a/src/ASTTransformation.cpp +++ b/src/ASTTransformation.cpp @@ -17,14 +17,14 @@ ASTTransformation::ASTTransformation(Importer *importerIn) { languageLevelOperators["&"].push_back(new NodeTree("function", ASTData(function, Symbol("&", true), NULL))); languageLevelOperators["--"].push_back(new NodeTree("function", ASTData(function, Symbol("--", true), NULL))); languageLevelOperators["++"].push_back(new NodeTree("function", ASTData(function, Symbol("++", true), NULL))); - languageLevelOperators["=="].push_back(new NodeTree("function", ASTData(function, Symbol("==", true), NULL))); - languageLevelOperators["<="].push_back(new NodeTree("function", ASTData(function, Symbol("<=", true), NULL))); - languageLevelOperators[">="].push_back(new NodeTree("function", ASTData(function, Symbol(">=", true), NULL))); - languageLevelOperators["<"].push_back(new NodeTree("function", ASTData(function, Symbol("<", true), NULL))); - languageLevelOperators[">"].push_back(new NodeTree("function", ASTData(function, Symbol(">", true), NULL))); - languageLevelOperators["&&"].push_back(new NodeTree("function", ASTData(function, Symbol("&&", true), NULL))); - languageLevelOperators["||"].push_back(new NodeTree("function", ASTData(function, Symbol("||", true), NULL))); - languageLevelOperators["!"].push_back(new NodeTree("function", ASTData(function, Symbol("!", true), NULL))); + languageLevelOperators["=="].push_back(new NodeTree("function", ASTData(function, Symbol("==", true), new Type(boolean)))); + languageLevelOperators["<="].push_back(new NodeTree("function", ASTData(function, Symbol("<=", true), new Type(boolean)))); + languageLevelOperators[">="].push_back(new NodeTree("function", ASTData(function, Symbol(">=", true), new Type(boolean)))); + languageLevelOperators["<"].push_back(new NodeTree("function", ASTData(function, Symbol("<", true), new Type(boolean)))); + languageLevelOperators[">"].push_back(new NodeTree("function", ASTData(function, Symbol(">", true), new Type(boolean)))); + languageLevelOperators["&&"].push_back(new NodeTree("function", ASTData(function, Symbol("&&", true), new Type(boolean)))); + languageLevelOperators["||"].push_back(new NodeTree("function", ASTData(function, Symbol("||", true), new Type(boolean)))); + languageLevelOperators["!"].push_back(new NodeTree("function", ASTData(function, Symbol("!", true), new Type(boolean)))); languageLevelOperators["*="].push_back(new NodeTree("function", ASTData(function, Symbol("*=", true), NULL))); languageLevelOperators["+="].push_back(new NodeTree("function", ASTData(function, Symbol("+=", true), NULL))); languageLevelOperators["-="].push_back(new NodeTree("function", ASTData(function, Symbol("-=", true), NULL))); @@ -651,10 +651,14 @@ NodeTree* ASTTransformation::transform(NodeTree* from, NodeTree newNode = new NodeTree(name, ASTData(value, Symbol(concatSymbolTree(children[0]), true), new Type(character, 1))); //Indirection of 1 for array } else if (name == "string" || name == "triple_quoted_string") { newNode = new NodeTree(name, ASTData(value, Symbol(concatSymbolTree(children[0]), true), new Type(character, 1))); //Indirection of 1 for array - }else if (name == "character") { + } else if (name == "character") { newNode = new NodeTree(name, ASTData(value, Symbol(concatSymbolTree(children[0]), true), new Type(character, 0))); //Indirection of 0 for character + } else if (name == "bool") { + newNode = new NodeTree(name, ASTData(value, Symbol(concatSymbolTree(children[0]), true), new Type(boolean, 0))); //Indirection of 0 for character } else if (name == "AmbiguityPackOuter" || name == "AmbiguityPackInner") { + std::cout << "///////////////////////////////////////////////////////////////////////////////" << std::endl; std::cout << "Ambigious program when parsed by this grammer! This is a bug, please report it." << std::endl; + std::cout << "///////////////////////////////////////////////////////////////////////////////" << std::endl; throw "Ambigious parse!"; } else { // Should get rid of this eventually. Right now it handles cases like sign, alpha, a comma, etc @@ -1128,10 +1132,11 @@ Type* ASTTransformation::typeFromTypeNode(NodeTree* typeNode, NodeTreegetDataRef()->scope[fullyInstantiatedName].push_back(typeDefinition); topScope->addChild(typeDefinition); //Add this object the the highest scope's - NodeTree* templateHighScope = templateDefinition->getDataRef()->scope["~enclosing_scope"][0]; - if (topScope != templateHighScope) - templateHighScope->getDataRef()->scope[fullyInstantiatedName].push_back(typeDefinition); - + //NodeTree* templateHighScope = templateDefinition->getDataRef()->scope["~enclosing_scope"][0]; + //if (topScope != templateHighScope) + //templateHighScope->getDataRef()->scope[fullyInstantiatedName].push_back(typeDefinition); + // We put it in the scope of the template so that it can find itself (as it's scope is its template definition) + templateDefinition->getDataRef()->scope[fullyInstantiatedName].push_back(typeDefinition); //Note that the instantiated template's scope is the template's definition. typeDefinition->getDataRef()->scope["~enclosing_scope"].push_back(templateDefinition); @@ -1184,7 +1189,12 @@ NodeTree* ASTTransformation::findOrInstantiateFunctionTemplate(std::vec if (instantiatedFunction) { std::cout << fullyInstantiatedName << " already exists! Returning" << std::endl; return instantiatedFunction; - } else { + } else { + instantiatedFunction = functionLookup(topScope, fullyInstantiatedName, types); + if (instantiatedFunction) { + std::cout << fullyInstantiatedName << "already exists! Found in TopScope" << std::endl; + return instantiatedFunction; + } std::cout << fullyInstantiatedName << " does NOT exist" << std::endl; } @@ -1210,13 +1220,14 @@ NodeTree* ASTTransformation::findOrInstantiateFunctionTemplate(std::vec skipChildren.insert(0); skipChildren.insert(1); skipChildren.insert(2); - scope->getDataRef()->scope[fullyInstantiatedName].push_back(instantiatedFunction); + //scope->getDataRef()->scope[fullyInstantiatedName].push_back(instantiatedFunction); instantiatedFunction->getDataRef()->scope["~enclosing_scope"].push_back(templateDefinition->getDataRef()->scope["~enclosing_scope"][0]); //Instantiated Template Function's scope is it's template's definition's scope + topScope->getDataRef()->scope[fullyInstantiatedName].push_back(instantiatedFunction); + topScope->addChild(instantiatedFunction); //Add this object the the highest scope's + std::cout << "About to do children of " << functionName << " to " << fullyInstantiatedName << std::endl; instantiatedFunction->addChildren(transformChildren(templateSyntaxTree->getChildren(), skipChildren, instantiatedFunction, std::vector(), newTemplateTypeReplacement)); - topScope->getDataRef()->scope[fullyInstantiatedName].push_back(instantiatedFunction); - topScope->addChild(instantiatedFunction); //Add this object the the highest scope's std::cout << "Fully Instantiated function " << functionName << " to " << fullyInstantiatedName << std::endl; diff --git a/src/CGenerator.cpp b/src/CGenerator.cpp index 20e875e..6234d7a 100644 --- a/src/CGenerator.cpp +++ b/src/CGenerator.cpp @@ -15,7 +15,8 @@ void CGenerator::generateCompSet(std::map*> ASTs, std::ofstream outputCFile; outputCFile.open(i->first + ".c"); if (outputCFile.is_open()) { - outputCFile << generate(i->second); + // Prequel common to all files + outputCFile << "#include \n#include \n#include \n" << generate(i->second); } else { std::cout << "Cannot open file " << i->first << ".c" << std::endl; } @@ -35,64 +36,109 @@ std::string CGenerator::tabs() { return returnTabs; } +std::string CGenerator::generateClassStruct(NodeTree* from) { + auto data = from->getData(); + auto children = from->getChildren(); + std::string objectString = "struct __struct_dummy_" + CifyName(data.symbol.getName()) + "__ {\n"; + tabLevel++; + for (int i = 0; i < children.size(); i++) { + std::cout << children[i]->getName() << std::endl; + if (children[i]->getName() != "function") + objectString += tabs() + generate(children[i], nullptr) + "\n"; + } + tabLevel--; + objectString += "};"; + return objectString; +} + +// This method recurseivly generates all aliases of some definition +std::string CGenerator::generateAliasChains(NodeTree* scopeNode, NodeTree* definition) { + auto scope = scopeNode->getDataRef()->scope; + std::string output; + for (auto i = scope.begin(); i != scope.end(); i++) { + for (auto declaration : i->second) { + auto declarationData = declaration->getDataRef(); + if (declarationData->type == type_def + && declarationData->valueType->typeDefinition != declaration + && declarationData->valueType->typeDefinition == definition) { + output += "typedef " + CifyName(definition->getDataRef()->symbol.getName()) + " " + CifyName(declarationData->symbol.getName()) + ";\n"; + // Recursively add the ones that depend on this one + output += generateAliasChains(scopeNode, declaration); + } + } + } + return output; +} + //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* from, NodeTree* enclosingObject) { ASTData data = from->getData(); std::vector*> children = from->getChildren(); - std::string output = ""; + std::string output; switch (data.type) { case translation_unit: - //Do here because we may need the typedefs before the declarations of variables - //Note that we need to be careful of the order, though, as some declarations depend on others. - //What is this then? It's a poset! Wooo posets! { + // Ok, so we've got to do this in passes to preserve mututally recursive definitions. + // + // First Pass: All classes get "struct dummy_thing; typedef struct dummy_thing thing;". + // Also, other typedefs follow after their naming. + // Second Pass: All top level variable declarations + // Third Pass: Define all actual structs of a class, in correct order (done with posets) + // Fourth Pass: Declare all function prototypes (as functions may be mutually recursive too). + // (this includes object methods) + // Fifth Pass: Define all functions (including object methods). + + // However, most of these do not actually have to be done as separate passes. First, second, fourth, and fifth + // are done simultanously, but append to different strings that are then concatinated properly, in order. + + std::string plainTypedefs = "/**\n * Plain Typedefs\n */\n\n"; + std::string variableDeclarations = "/**\n * Variable Declarations \n */\n\n"; + std::string classStructs = "/**\n * Class Structs\n */\n\n"; + std::string functionPrototypes = "/**\n * Function Prototypes\n */\n\n"; + std::string functionDefinitions = "/**\n * Function Definitions\n */\n\n"; + Poset*> typedefPoset; for (int i = 0; i < children.size(); i++) { if (children[i]->getDataRef()->type == type_def) { - typedefPoset.addVertex(children[i]); //We add this definition by itthis just in case there are no dependencies. - //If it has dependencies, there's no harm in adding it here - //Go through every child in the class looking for declaration statements. For each of these that is not a primitive type - //we will add a dependency from this definition to that definition in the poset. + // If we're an alias type, continue. We handle those differently + if (children[i]->getDataRef()->valueType->typeDefinition != children[i]) + continue; + + typedefPoset.addVertex(children[i]); // We add this definition by itself just in case there are no dependencies. + // If it has dependencies, there's no harm in adding it here + // Go through every child in the class looking for declaration statements. For each of these that is not a primitive type + // we will add a dependency from this definition to that definition in the poset. std::vector*> classChildren = children[i]->getChildren(); for (auto j : classChildren) { if (j->getDataRef()->type == declaration_statement) { - Type* decType = j->getChildren()[0]->getDataRef()->valueType; //Type of the declaration - if (decType->typeDefinition && decType->getIndirection() == 0) //If this is a custom type and not a pointer - typedefPoset.addRelationship(children[i], decType->typeDefinition); //Add a dependency + Type* decType = j->getChildren()[0]->getDataRef()->valueType; // Type of the declaration + if (decType->typeDefinition && decType->getIndirection() == 0) // If this is a custom type and not a pointer + typedefPoset.addRelationship(children[i], decType->typeDefinition); // Add a dependency } } - //In case there are pointer dependencies. We don't do this if this is an alias type - if (children[i]->getDataRef()->valueType->typeDefinition == children[i]) - output += "struct " + CifyName(children[i]->getDataRef()->symbol.getName()) + ";\n"; - else { - Type *aliasType = children[i]->getDataRef()->valueType; - if (aliasType->typeDefinition && !aliasType->templateDefinition) //Isn't uninstantiated template or 0 parameter class, so must be alias. if typeDefinition isn't null, then it's an alias of a custom, not a primitive, type. - typedefPoset.addRelationship(children[i], children[i]->getDataRef()->valueType->typeDefinition); //An alias typedef depends on the type it aliases being declared before it - } } } //Now generate the typedef's in the correct, topological order for (NodeTree* i : typedefPoset.getTopoSort()) - output += generate(i, enclosingObject) + "\n"; + classStructs += generateClassStruct(i) + "\n"; //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 overloadedMembers : i->second) { - NodeTree* declaration = overloadedMembers; + for (auto declaration : i->second) { std::vector*> decChildren = declaration->getChildren(); ASTData declarationData = declaration->getData(); switch(declarationData.type) { case identifier: - output += ValueTypeToCType(declarationData.valueType) + " " + declarationData.symbol.getName() + "; /*identifier*/\n"; + variableDeclarations += ValueTypeToCType(declarationData.valueType) + " " + declarationData.symbol.getName() + "; /*identifier*/\n"; break; case function: { if (declarationData.valueType->baseType == template_type) - output += "/* template function " + declarationData.symbol.toString() + " */\n"; + functionPrototypes += "/* template function " + declarationData.symbol.toString() + " */\n"; else if (decChildren.size() == 0) //Not a real function, must be a built in passthrough - output += "/* built in function: " + declarationData.symbol.toString() + " */\n"; + functionPrototypes += "/* built in function: " + declarationData.symbol.toString() + " */\n"; else { - output += "\n" + ValueTypeToCType(declarationData.valueType) + " "; + functionPrototypes += "\n" + ValueTypeToCType(declarationData.valueType) + " "; std::string nameDecoration, parameters; for (int j = 0; j < decChildren.size()-1; j++) { if (j > 0) @@ -100,24 +146,51 @@ std::string CGenerator::generate(NodeTree* from, NodeTree* enc parameters += ValueTypeToCType(decChildren[j]->getData().valueType) + " " + generate(decChildren[j], enclosingObject); nameDecoration += "_" + ValueTypeToCTypeDecoration(decChildren[j]->getData().valueType); } - output += CifyName(declarationData.symbol.getName() + nameDecoration) + "(" + parameters + "); /*func*/\n"; + functionPrototypes += CifyName(declarationData.symbol.getName() + nameDecoration) + "(" + parameters + "); /*func*/\n"; + // Only generate function if this is the unit it was defined in + std::cout << "Generating " << CifyName(declarationData.symbol.getName()) << std::endl; + if (contains(children, declaration)) + functionDefinitions += generate(declaration, enclosingObject); } } break; - case type_def: - //type - output += "/*typedef " + declarationData.symbol.getName() + " */\n"; - break; + case type_def: + //type + plainTypedefs += "/*typedef " + declarationData.symbol.getName() + " */\n"; + + if (declarationData.valueType->baseType == template_type) { + plainTypedefs += "/* non instantiated template " + declarationData.symbol.getName() + " */"; + } else if (declarationData.valueType->typeDefinition != declaration) { + if (declarationData.valueType->typeDefinition) + continue; // Aliases of objects are done with the thing it alises + // Otherwise, we're actually a renaming of a primitive, can generate here + plainTypedefs += "typedef " + ValueTypeToCType(declarationData.valueType) + " " + CifyName(declarationData.symbol.getName()) + ";\n"; + plainTypedefs += generateAliasChains(from, declaration); + } else { + plainTypedefs += "typedef struct __struct_dummy_" + CifyName(declarationData.symbol.getName()) + "__ " + CifyName(declarationData.symbol.getName()) + ";\n"; + functionPrototypes += "/* Method Prototypes for " + declarationData.symbol.getName() + " */\n"; + // We use a seperate string for this because we only include it if this is the file we're defined in + std::string objectFunctionDefinitions = "/* Method Definitions for " + declarationData.symbol.getName() + " */\n"; + for (int j = 0; j < decChildren.size(); j++) { + std::cout << decChildren[j]->getName() << std::endl; + if (decChildren[j]->getName() == "function") //If object method + objectFunctionDefinitions += generateObjectMethod(declaration, decChildren[j], &functionPrototypes) + "\n"; + } + // Add all aliases to the plain typedefs. This will add any alias that aliases to this object, and any alias that aliases to that, and so on + plainTypedefs += generateAliasChains(from, declaration); + functionPrototypes += "/* Done with " + declarationData.symbol.getName() + " */\n"; + // If this is the file the object is defined in, include methods + if (contains(children, declaration)) + functionDefinitions += objectFunctionDefinitions + "/* Done with " + declarationData.symbol.getName() + " */\n"; + } + break; default: //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"; } } } - //Do here because we need the newlines - for (int i = 0; i < children.size(); i++) - if (children[i]->getDataRef()->type != type_def) - output += generate(children[i], enclosingObject) + "\n"; + output += plainTypedefs + variableDeclarations + classStructs + functionPrototypes + functionDefinitions; return output; } break; @@ -139,31 +212,8 @@ std::string CGenerator::generate(NodeTree* from, NodeTree* enc std::string preName; if (enclosingObject && enclosingObject->getDataRef()->scope.find(data.symbol.getName()) != enclosingObject->getDataRef()->scope.end()) preName += "this->"; - if (false) - for (int j = 0; j < children.size()-1; j++) - preName += ValueTypeToCType(children[j]->getData().valueType) + "_"; return preName + CifyName(data.symbol.getName()); //Cifying does nothing if not an operator overload } - case type_def: - if (data.valueType->baseType == template_type) { - return "/* non instantiated template " + data.symbol.getName() + " */"; - } else if (data.valueType->typeDefinition != from) { - return "typedef " + ValueTypeToCType(data.valueType) + " " + data.symbol.getName() + ";"; - } else { - std::string objectString = "typedef struct __struct_dummy_" + CifyName(data.symbol.getName()) + "__ {\n"; - std::string postString; //The functions have to be outside the struct definition - tabLevel++; - 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 += tabs() + generate(children[i], enclosingObject) + "\n"; - } - tabLevel--; - objectString += "} " + CifyName(data.symbol.getName()) + ";"; - return objectString + postString; //Functions come after the declaration of the struct - } case function: { if (data.valueType->baseType == template_type) @@ -191,7 +241,10 @@ std::string CGenerator::generate(NodeTree* from, NodeTree* enc output += line; if (children[i]->getChildren().size() && children[i]->getChildren()[0]->getDataRef()->type == declaration_statement) { NodeTree *identifier = children[i]->getChildren()[0]->getChildren()[0]; - NodeTree *typeDefinition = identifier->getDataRef()->valueType->typeDefinition; + Type* declarationType = identifier->getDataRef()->valueType; + if (declarationType->getIndirection()) + continue; + NodeTree *typeDefinition = declarationType->typeDefinition; if (!typeDefinition) continue; if (typeDefinition->getDataRef()->scope.find("destruct") == typeDefinition->getDataRef()->scope.end()) @@ -261,13 +314,13 @@ std::string CGenerator::generate(NodeTree* from, NodeTree* enc if (funcType == function) { if (name == "++" || name == "--") return generate(children[1], enclosingObject) + name; - if ( (name == "*" || name == "&") && children.size() == 2) //Is dereference, not multiplication, or address-of + if ( (name == "*" || name == "&" || name == "!" ) && children.size() == 2) //Is dereference, not multiplication, address-of, or other unary operator return name + "(" + generate(children[1], enclosingObject) + ")"; if (name == "[]") return "(" + generate(children[1], enclosingObject) + ")[" +generate(children[2],enclosingObject) + "]"; if (name == "+" || name == "-" || name == "*" || name == "/" || name == "==" || name == ">=" || name == "<=" || name == "!=" || name == "<" || 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) @@ -310,7 +363,7 @@ std::string CGenerator::generate(NodeTree* from, NodeTree* enc //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() +"__"; + output += CifyName(enclosingObject->getDataRef()->symbol.getName()) +"__"; /*HERE*/ output += CifyName(name + nameDecoration) + "("; if (isSelfObjectMethod) output += children.size() > 1 ? "this," : "this"; @@ -349,8 +402,9 @@ NodeTree* CGenerator::getMethodsObjectType(NodeTree* scope, st scope = scope->getDataRef()->valueType->typeDefinition; return (scope->getDataRef()->scope.find(functionName) != scope->getDataRef()->scope.end()) ? scope : NULL; } -std::string CGenerator::generateObjectMethod(NodeTree* enclosingObject, NodeTree* from) { - std::string output; + +// Returns the function prototype in the out param and the full definition normally +std::string CGenerator::generateObjectMethod(NodeTree* enclosingObject, NodeTree* from, std::string *functionPrototype) { ASTData data = from->getData(); Type enclosingObjectType = *(enclosingObject->getDataRef()->valueType); //Copy a new type so we can turn it into a pointer if we need to enclosingObjectType.increaseIndirection(); @@ -360,10 +414,11 @@ 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) + " " + CifyName(enclosingObject->getDataRef()->symbol.getName()) +"__" + std::string functionSignature = "\n" + ValueTypeToCType(data.valueType) + " " + CifyName(enclosingObject->getDataRef()->symbol.getName()) +"__" + CifyName(data.symbol.getName()) + nameDecoration + "(" + ValueTypeToCType(&enclosingObjectType) - + " this" + parameters + ")\n" + generate(children[children.size()-1], enclosingObject); //Pass in the object so we can properly handle access to member stuff - return output; + + " this" + parameters + ")"; + *functionPrototype += functionSignature + ";\n"; + return functionSignature + "\n" + generate(children[children.size()-1], enclosingObject); //Pass in the object so we can properly handle access to member stuff } std::string CGenerator::ValueTypeToCType(Type *type) { @@ -407,7 +462,7 @@ std::string CGenerator::ValueTypeToCTypeDecoration(Type *type) { switch (type->baseType) { case none: if (type->typeDefinition) - return_type = type->typeDefinition->getDataRef()->symbol.getName(); + return_type = CifyName(type->typeDefinition->getDataRef()->symbol.getName()); else return_type = "none"; break; diff --git a/stdlib/mem.krak b/stdlib/mem.krak index 0c4ca92..b011463 100644 --- a/stdlib/mem.krak +++ b/stdlib/mem.krak @@ -2,10 +2,10 @@ __if_comp__ __C__ __simple_passthrough__ """ #include """ -char* nullPtr = 0; +/* we have a template versions so we don't have to cast (because we don't have that yet) */ -char* malloc(int size) { - char* memPtr = nullPtr; +template T* malloc(int size) { + T* memPtr = 0; __if_comp__ __C__ { __simple_passthrough__ """ memPtr = malloc(size); @@ -14,15 +14,6 @@ char* malloc(int size) { return memPtr; } -void free(char* memPtr) { - __if_comp__ __C__ { - __simple_passthrough__ """ - free(memPtr); - """ - } -} - -/* we have a template version so we don't have to cast */ template void free(T* memPtr) { __if_comp__ __C__ { __simple_passthrough__ """ @@ -43,7 +34,7 @@ template int sizeof() { } template T* new(int count) { - return malloc( sizeof() * count ); + return malloc( sizeof() * count ); } template T* new() { @@ -55,6 +46,7 @@ template void delete(T* toDelete, int itemCount) { delete(toDelete); } +/* Calling this with itemCount = 0 allows you to delete destructable objects without calling their destructors. */ template void delete(T* toDelete, int itemCount) { for (int i = 0; i < itemCount; i++;) toDelete[i].destruct(); diff --git a/stdlib/vector.krak b/stdlib/vector.krak index ae52fe6..ab320c2 100644 --- a/stdlib/vector.krak +++ b/stdlib/vector.krak @@ -1,15 +1,13 @@ import mem; import util; +import io; typedef template vector (Destructable) { T *data; int size; int available; - bool destroyItems; - vector* construct(bool destroyItemsIn) { - destroyItems = destroyItemsIn; - return construct(); + vector* construct() { size = 0; available = 8; data = new(8); @@ -17,17 +15,14 @@ typedef template vector (Destructable) { } void destruct() { - if (destroyItems) - delete(data, size); - else - delete(data); + delete(data); } bool resize(int newSize) { T* newData = new(newSize); if (!newData) return false; - for (int i = 0; i < lesser(size, newSize); i++;) + for (int i = 0; i < lesser(size, newSize); i++;) newData[i] = data[i]; delete(data, 0); return true; @@ -38,8 +33,10 @@ typedef template vector (Destructable) { } T get(int index) { - if (index < 0 || index >= size) - return null; + if (index < 0 || index >= size) { + println("Vector access out of bounds! Retuning 0th element as sanest option"); + return data[0]; + } return data[index]; } @@ -53,6 +50,6 @@ typedef template vector (Destructable) { size++; else resize(size*2); - data[size-1]; + data[size-1] = dataIn; } }; diff --git a/tests/vectorTest.expected_results b/tests/vectorTest.expected_results index 2239baa..c7ae395 100644 --- a/tests/vectorTest.expected_results +++ b/tests/vectorTest.expected_results @@ -1,3 +1,2 @@ 1337 Destroyed! - diff --git a/tests/vectorTest.krak b/tests/vectorTest.krak index 878745f..4ee170a 100644 --- a/tests/vectorTest.krak +++ b/tests/vectorTest.krak @@ -9,17 +9,17 @@ typedef AbleToBeDestroyed (Destructable) { }; int main() { - vector intVec.construct(false); + vector intVec.construct(); intVec.addEnd(1); intVec.addEnd(3); intVec.addEnd(3); intVec.addEnd(7); - for (int i = 0; i < intVec.size(); i++;) + for (int i = 0; i < intVec.size; i++;) print(intVec.at(i)); println(); - vector* desVec = new>()->construct(true); + vector* desVec = new>()->construct(); AbleToBeDestroyed testDestruct; desVec->addEnd(testDestruct); delete>(desVec);