diff --git a/include/Poset.h b/include/Poset.h new file mode 100644 index 0000000..29fea05 --- /dev/null +++ b/include/Poset.h @@ -0,0 +1,118 @@ + +#ifndef POSET_H +#define POSET_H + +#include +#include +#include +#include + +#include + +#include "util.h" + +template +class Poset { + public: + Poset(); + ~Poset(); + void addRelationship(T first, T second); + bool zeroDependencies(T vertex); + std::set getDependsOn(T dependency); + std::vector getTopoSort(); + static void test(); + private: + //backing data structures + std::map> adjMatrix; + std::set verticies; +}; + +template +Poset::Poset() { + //Nothing needed +} + +template +Poset::~Poset() { + //Ditto +} + + +template +void Poset::addRelationship(T first, T second) { + verticies.insert(first); + verticies.insert(second); + adjMatrix[first][second] = true; +} + +template +bool Poset::zeroDependencies(T vertex) { + auto depMapItr = adjMatrix.find(vertex); + if (depMapItr == adjMatrix.end()) + return true; + for (auto i : depMapItr->second) + if (i.second == true) + return false; + return true; +} + +template +std::set Poset::getDependsOn(T dependency) { + std::set vertsThatDependOn; + for (auto i : adjMatrix) { + auto depItr = i.second.find(dependency); + if (depItr != i.second.end() && depItr->second) + vertsThatDependOn.insert(i.first); + } + return vertsThatDependOn; +} + +template +std::vector Poset::getTopoSort() { + std::vector sorted; + std::queue toDo; + for (auto i : verticies) + if (zeroDependencies(i)) + toDo.push(i); + while(!toDo.empty()) { + T current = toDo.front(); toDo.pop(); + sorted.push_back(current); + for (T depOnCurrent : getDependsOn(current)) { + adjMatrix[depOnCurrent][current] = false; //Remove the edge to current, since current's now been taken care of + if (zeroDependencies(depOnCurrent)) + toDo.push(depOnCurrent); + } + } + return sorted; +} + + +//int specilization just for testing +template<> +void Poset::test() { + std::string result; + { + Poset poset; + for (int i = 0; i < 20; i++) + poset.addRelationship(i,i+1); + result = ""; + for (int i : poset.getTopoSort()) + result += intToString(i) + " "; + //std::cout << result << std::endl; + assert(result == "20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 "); + } + { + Poset poset; + for (int i = 0; i < 20; i+=2) + poset.addRelationship(i,i+1); + result = ""; + for (int i : poset.getTopoSort()) + result += intToString(i) + " "; + //std::cout << result << std::endl; + assert(result == "1 3 5 7 9 11 13 15 17 19 0 2 4 6 8 10 12 14 16 18 "); + } + + std::cout << "Poset tests passed" << std::endl; +} + +#endif \ No newline at end of file diff --git a/main.cpp b/main.cpp index 4f6f8b7..c5639ab 100644 --- a/main.cpp +++ b/main.cpp @@ -14,6 +14,7 @@ #include "Importer.h" #include "ASTData.h" #include "CGenerator.h" +#include "Poset.h" #include "util.h" #include "Tester.h" @@ -27,6 +28,8 @@ int main(int argc, char* argv[]) { RegEx::test(); Lexer::test(); //std::cout << strSlice("123", 0, -1) << std::endl; + Poset::test(); //int specilization just for testing. test() is actually only in the int specilization + if (argc >= 3) { std::string testResults, line; int passed = 0, failed = 0; diff --git a/src/ASTTransformation.cpp b/src/ASTTransformation.cpp index 94ac037..6d0d863 100644 --- a/src/ASTTransformation.cpp +++ b/src/ASTTransformation.cpp @@ -480,7 +480,7 @@ NodeTree* ASTTransformation::scopeLookup(NodeTree* scope, std: // the last node is actually a body node, as it may not have been generated yet if we're in the body //and this function is recursive or if this is a non-instantiated template function if (types.size() != ((children.size() > 0 && children[children.size()-1]->getDataRef()->type == code_block) ? children.size()-1 : children.size())) { - std::cout << "Type sizes do not match between two " << lookup << "(" << types.size() << "," << ((children.size() > 0) ? children.size()-1 : 0) << "), types are: "; + std::cout << "Type sizes do not match between two " << lookup << "(" << types.size() << "," << ((children.size() > 0 && children[children.size()-1]->getDataRef()->type == code_block) ? children.size()-1 : children.size()) << "), types are: "; for (auto j : types) std::cout << j.toString() << " "; std::cout << std::endl; diff --git a/src/CGenerator.cpp b/src/CGenerator.cpp index c34fe4e..b2d09bd 100644 --- a/src/CGenerator.cpp +++ b/src/CGenerator.cpp @@ -43,6 +43,8 @@ std::string CGenerator::generate(NodeTree* from, NodeTree* enc 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! for (int i = 0; i < children.size(); i++) if (children[i]->getDataRef()->type == type_def) output += generate(children[i], enclosingObject) + "\n"; diff --git a/true_triple_quoted_string_regex.txt b/true_triple_quoted_string_regex.txt deleted file mode 100644 index a14076b..0000000 --- a/true_triple_quoted_string_regex.txt +++ /dev/null @@ -1,9 +0,0 @@ -This is the true regex for triple quoted strings, but it segfaults my regex code.... - -triple_quoted_string = "\"\"\"((\"\"(`|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|;|'| -|z|x|c|v|b|n|m|,|.|/|~|!|@|#|$|%|^|&|\*|\(|\)|_|\+|Q|W|E|R|T|Y|U|I|O|P|{|}|\||A|S|D|F|G|H|J|K|L|:|Z|X|C|V|B|N|M|<|>|\?| )+)|(\"(`|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|;|'| -|z|x|c|v|b|n|m|,|.|/|~|!|@|#|$|%|^|&|\*|\(|\)|_|\+|Q|W|E|R|T|Y|U|I|O|P|{|}|\||A|S|D|F|G|H|J|K|L|:|Z|X|C|V|B|N|M|<|>|\?| )+))*(`|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|;|'| -|z|x|c|v|b|n|m|,|.|/|~|!|@|#|$|%|^|&|\*|\(|\)|_|\+|Q|W|E|R|T|Y|U|I|O|P|{|}|\||A|S|D|F|G|H|J|K|L|:|Z|X|C|V|B|N|M|<|>|\?| )*(((`|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|;|'| -|z|x|c|v|b|n|m|,|.|/|~|!|@|#|$|%|^|&|\*|\(|\)|_|\+|Q|W|E|R|T|Y|U|I|O|P|{|}|\||A|S|D|F|G|H|J|K|L|:|Z|X|C|V|B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