From ad0e90f74a8eeaabbb50be3ca455925613f0fd81 Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Wed, 7 Jan 2015 03:03:14 -0500 Subject: [PATCH] Fixed up vector and make template searching functions use std::set to remove false positives on multiple matching templates --- src/ASTTransformation.cpp | 12 ++++++------ stdlib/vector.krak | 10 +++++----- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/ASTTransformation.cpp b/src/ASTTransformation.cpp index fae16d4..20117d5 100644 --- a/src/ASTTransformation.cpp +++ b/src/ASTTransformation.cpp @@ -873,7 +873,7 @@ NodeTree* ASTTransformation::functionLookup(NodeTree* scope, s //Lookup class templates. It evaluates possible matches on traits NodeTree* ASTTransformation::templateClassLookup(NodeTree* scope, std::string lookup, std::vector templateInstantiationTypes) { - std::vector*> mostFittingTemplates; + std::set*> mostFittingTemplates; int bestNumTraitsSatisfied = -1; auto possibleMatches = scopeLookup(scope, lookup); std::cout << "Template Class instantiation has " << possibleMatches.size() << " possible matches." << std::endl; @@ -919,7 +919,7 @@ NodeTree* ASTTransformation::templateClassLookup(NodeTree* sco bestNumTraitsSatisfied = currentTraitsSatisfied; } else if (currentTraitsSatisfied < bestNumTraitsSatisfied) continue; - mostFittingTemplates.push_back(i); + mostFittingTemplates.insert(i); std::cout << "Current class fits, satisfying " << currentTraitsSatisfied << " traits" << std::endl; } if (!mostFittingTemplates.size()) { @@ -929,12 +929,12 @@ NodeTree* ASTTransformation::templateClassLookup(NodeTree* sco std::cout << "Multiple template classes fit with equal number of traits satisfied for " << lookup << "!" << std::endl; throw "Multiple matching template classes"; } - return mostFittingTemplates[0]; + return *mostFittingTemplates.begin(); } //Lookup function for template functions. It has some extra concerns compared to function lookup, namely traits NodeTree* ASTTransformation::templateFunctionLookup(NodeTree* scope, std::string lookup, std::vector templateInstantiationTypes, std::vector types) { - std::vector*> mostFittingTemplates; + std::set*> mostFittingTemplates; int bestNumTraitsSatisfied = -1; auto possibleMatches = scopeLookup(scope, lookup); std::cout << "Template Function instantiation has " << possibleMatches.size() << " possible matches." << std::endl; @@ -1004,7 +1004,7 @@ NodeTree* ASTTransformation::templateFunctionLookup(NodeTree* bestNumTraitsSatisfied = currentTraitsSatisfied; } else if (currentTraitsSatisfied < bestNumTraitsSatisfied) continue; - mostFittingTemplates.push_back(i); + mostFittingTemplates.insert(i); std::cout << "Current function fits, satisfying " << currentTraitsSatisfied << " traits" << std::endl; } if (!mostFittingTemplates.size()) { @@ -1014,7 +1014,7 @@ NodeTree* ASTTransformation::templateFunctionLookup(NodeTree* std::cout << "Multiple template functions fit with equal number of traits satisfied for " << lookup << "!" << std::endl; throw "Multiple matching template functions"; } - return mostFittingTemplates[0]; + return *mostFittingTemplates.begin(); } //Extract pairs of type names and traits diff --git a/stdlib/vector.krak b/stdlib/vector.krak index 0cf12df..d3e9dbc 100644 --- a/stdlib/vector.krak +++ b/stdlib/vector.krak @@ -1,6 +1,6 @@ -import mem; -import util; -import io; +import mem:*; +import util:*; +import io:*; typedef template vector (Destructable) { |T*| data; @@ -27,11 +27,11 @@ typedef template vector (Destructable) { delete(data, 0); return true; } - + |T| at(|int| index) { return get(index); } - + |T| get(|int| index) { if (index < 0 || index >= size) { println("Vector access out of bounds! Retuning 0th element as sanest option");