From 2e1bdcf3a40787da36fd18076047d53caacdc7ca Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 7 Sep 2019 13:04:42 +0200 Subject: [PATCH] Better error handling when unable to index an eval value --- src/Evaluator/EvalValues/EvalValue.hpp | 18 +++++++++++++++--- tests/TreeStringTests.cpp | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/Evaluator/EvalValues/EvalValue.hpp b/src/Evaluator/EvalValues/EvalValue.hpp index 6129054..f4f2ad3 100644 --- a/src/Evaluator/EvalValues/EvalValue.hpp +++ b/src/Evaluator/EvalValues/EvalValue.hpp @@ -61,16 +61,22 @@ namespace Porygon::Evaluation { [[nodiscard]] virtual const EvalValue* IndexValue(const EvalValue *val) const { - throw EvaluationException("Can't index this EvalValue"); + std::stringstream err; + err << "Can't index this EvalValue: " << ToString() << " with key: " << val->ToString(); + throw EvaluationException(err.str()); } [[nodiscard]] virtual const EvalValue* IndexValue(uint32_t hash) const { - throw EvaluationException("Can't index this EvalValue"); + std::stringstream err; + err << "Can't index this EvalValue: " << ToString() << " with key hash: " << hash; + throw EvaluationException(err.str()); } virtual void SetIndexValue(const EvalValue *key, const EvalValue* value) const { - throw EvaluationException("Can't index this EvalValue"); + std::stringstream err; + err << "Can't index this EvalValue: " << ToString() << " with key: " << value->ToString(); + throw EvaluationException(err.str()); } [[nodiscard]] @@ -92,6 +98,12 @@ namespace Porygon::Evaluation { virtual EvalValue* Cast(shared_ptr castType) const{ throw new EvaluationException("Casting to invalid type."); } + + virtual std::string ToString() const{ + std::stringstream s; + s << "Type: " << ScriptType::ToString(this->GetTypeClass()); + return s.str(); + } }; class BooleanEvalValue : public EvalValue { diff --git a/tests/TreeStringTests.cpp b/tests/TreeStringTests.cpp index cac98d4..683c051 100644 --- a/tests/TreeStringTests.cpp +++ b/tests/TreeStringTests.cpp @@ -47,7 +47,7 @@ TEST_CASE( "Assignment Statement To String", "[BoundTreeString]" ) { const BoundVariableKey *keyObj = new BoundVariableKey(HashedString(key), 0, true); auto s = new BoundAssignmentStatement(keyObj, new BoundLiteralIntegerExpression(5, 0,0)); s->GetTreeString(stream, 1); - REQUIRE(stream.str() == "\tAssignment -> key\n\t\tLiteralInteger: 5 (number)"); + REQUIRE(stream.str() == "\tAssignment -> global key\n\t\tLiteralInteger: 5 (number)"); delete s; }