From 8f078e580ed4dddd3a86a67b99fe0225efd8b5e8 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 7 Sep 2019 15:33:26 +0200 Subject: [PATCH] Include type in BoundVariableKey for debugging purposes --- src/Binder/Binder.cpp | 4 ++-- src/Binder/BoundStatements/BoundStatement.hpp | 5 ++--- src/Binder/BoundVariables/BoundScope.cpp | 6 +++--- src/Binder/BoundVariables/BoundVariableKey.hpp | 9 ++++++++- .../EvaluationScope/EvaluationScope.hpp | 1 + src/ScriptTypes/ScriptType.hpp | 4 ---- tests/TreeStringTests.cpp | 17 ++++++++--------- 7 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/Binder/Binder.cpp b/src/Binder/Binder.cpp index 53ba075..8ccab07 100644 --- a/src/Binder/Binder.cpp +++ b/src/Binder/Binder.cpp @@ -178,7 +178,7 @@ namespace Porygon::Binder { } type = dynamic_pointer_cast(varType); type->RegisterFunctionOption(option); - assignmentKey = new BoundVariableKey(identifier, scope, false); + assignmentKey = new BoundVariableKey(identifier, scope, false, type); } else { type = make_shared(); type->RegisterFunctionOption(option); @@ -409,7 +409,7 @@ namespace Porygon::Binder { } auto var = this->_scope->GetVariable(scope, key); auto type = var->GetType(); - return new BoundVariableExpression(new BoundVariableKey(key, scope, false), type, + return new BoundVariableExpression(new BoundVariableKey(key, scope, false, type), type, expression->GetStartPosition(), expression->GetLength()); } diff --git a/src/Binder/BoundStatements/BoundStatement.hpp b/src/Binder/BoundStatements/BoundStatement.hpp index d1ea1f0..5823ad5 100644 --- a/src/Binder/BoundStatements/BoundStatement.hpp +++ b/src/Binder/BoundStatements/BoundStatement.hpp @@ -6,7 +6,6 @@ #include "../BoundExpressions/BoundExpression.hpp" #include "../BoundVariables/BoundVariableKey.hpp" - using namespace std; namespace Porygon::Binder { @@ -437,9 +436,9 @@ namespace Porygon::Binder { DrawIndents(stream, indents); stream << "GenericForLoopStatement" << endl; DrawIndents(stream, indents); - stream << "Key: " << _keyIdentifier->GetIdentifier()->GetDebugString() << endl; + stream << "Key: " << _keyIdentifier->GetIdentifier()->GetDebugString() << " (" << _keyIdentifier->GetType()->ToString() << ")" << endl; DrawIndents(stream, indents); - stream << "Value: " << _valueIdentifier->GetIdentifier()->GetDebugString() << endl; + stream << "Value: " << _valueIdentifier->GetIdentifier()->GetDebugString() << " (" << _keyIdentifier->GetType()->ToString() << ")" << endl; DrawIndents(stream, indents); stream << "Iterator:" << endl; _iterator->GetTreeString(stream, indents + 1); diff --git a/src/Binder/BoundVariables/BoundScope.cpp b/src/Binder/BoundVariables/BoundScope.cpp index e373ea0..b52dc77 100644 --- a/src/Binder/BoundVariables/BoundScope.cpp +++ b/src/Binder/BoundVariables/BoundScope.cpp @@ -86,7 +86,7 @@ namespace Porygon::Binder { } scope->insert({identifier, new BoundVariable(std::move(type))}); return VariableAssignment(VariableAssignmentResult::Ok, - new BoundVariableKey(identifier, this->_currentScope, true)); + new BoundVariableKey(identifier, this->_currentScope, true, type)); } VariableAssignment BoundScope::AssignVariable(const Utilities::HashedString& identifier, const std::shared_ptr &type) { @@ -97,14 +97,14 @@ namespace Porygon::Binder { if (_tableVariableTypes != nullptr){ _tableVariableTypes->insert({identifier, type}); } - return VariableAssignment(VariableAssignmentResult::Ok, new BoundVariableKey(identifier, 0, true)); + return VariableAssignment(VariableAssignmentResult::Ok, new BoundVariableKey(identifier, 0, true, type)); } else { // Assigning auto var = this->GetVariable(exists, identifier); if (var->GetType()->operator!=(type)) { return VariableAssignment(VariableAssignmentResult::VariableDefinedWithDifferentType, nullptr); } - return VariableAssignment(VariableAssignmentResult::Ok, new BoundVariableKey(identifier, exists, false)); + return VariableAssignment(VariableAssignmentResult::Ok, new BoundVariableKey(identifier, exists, false, type)); } } } diff --git a/src/Binder/BoundVariables/BoundVariableKey.hpp b/src/Binder/BoundVariables/BoundVariableKey.hpp index fd58263..6c7078b 100644 --- a/src/Binder/BoundVariables/BoundVariableKey.hpp +++ b/src/Binder/BoundVariables/BoundVariableKey.hpp @@ -4,6 +4,7 @@ #include #include "../../Utilities/HashedString.hpp" +#include "../../ScriptTypes/ScriptType.hpp" namespace Porygon::Binder { class BoundVariableKey { @@ -11,6 +12,7 @@ namespace Porygon::Binder { const unsigned int _scopeId; const bool _isCreation; const uint64_t _hash; + shared_ptr _type; static uint64_t KnuthsHash(unsigned int i1, unsigned int i2) { uint64_t ret = i1; @@ -19,10 +21,11 @@ namespace Porygon::Binder { } public: - BoundVariableKey(const Utilities::HashedString& id, unsigned int scope, bool creation) + BoundVariableKey(const Utilities::HashedString& id, unsigned int scope, bool creation, shared_ptr type) : _identifier(id), _scopeId(scope), _isCreation(creation), + _type(type), _hash(KnuthsHash(id.GetHash(), scope)) {} @@ -45,6 +48,10 @@ namespace Porygon::Binder { inline uint64_t GetHash() const { return _hash; } + + inline shared_ptr GetType() const{ + return _type; + } }; } diff --git a/src/Evaluator/EvaluationScope/EvaluationScope.hpp b/src/Evaluator/EvaluationScope/EvaluationScope.hpp index 5ab2a7b..692f347 100644 --- a/src/Evaluator/EvaluationScope/EvaluationScope.hpp +++ b/src/Evaluator/EvaluationScope/EvaluationScope.hpp @@ -6,6 +6,7 @@ #include #include "../EvalValues/EvalValue.hpp" #include "../EvalValuePointer.hpp" +#include "../../Binder/BoundVariables/BoundVariableKey.hpp" using namespace Porygon::Binder; diff --git a/src/ScriptTypes/ScriptType.hpp b/src/ScriptTypes/ScriptType.hpp index 84e1874..3f9f60f 100644 --- a/src/ScriptTypes/ScriptType.hpp +++ b/src/ScriptTypes/ScriptType.hpp @@ -1,13 +1,9 @@ -#include - - #ifndef PORYGONLANG_SCRIPTTYPE_HPP #define PORYGONLANG_SCRIPTTYPE_HPP #include #include #include -#include "../Binder/BoundVariables/BoundVariableKey.hpp" #include "../Utilities/HashedString.hpp" #include "CastResult.hpp" diff --git a/tests/TreeStringTests.cpp b/tests/TreeStringTests.cpp index 683c051..c4d4ba6 100644 --- a/tests/TreeStringTests.cpp +++ b/tests/TreeStringTests.cpp @@ -4,7 +4,6 @@ #include "../src/ScriptTypes/ScriptType.hpp" #include "../src/Binder/BoundStatements/BoundStatement.hpp" -#include "../src/Utilities/HashedString.hpp" using namespace Porygon; using namespace Porygon::Binder; using namespace Porygon::Utilities; @@ -44,7 +43,7 @@ TEST_CASE( "Expression Statement To String", "[BoundTreeString]" ) { TEST_CASE( "Assignment Statement To String", "[BoundTreeString]" ) { std::stringstream stream; auto key = new u16string(u"key"); - const BoundVariableKey *keyObj = new BoundVariableKey(HashedString(key), 0, true); + const BoundVariableKey *keyObj = new BoundVariableKey(HashedString(key), 0, true, ScriptType::NilType); auto s = new BoundAssignmentStatement(keyObj, new BoundLiteralIntegerExpression(5, 0,0)); s->GetTreeString(stream, 1); REQUIRE(stream.str() == "\tAssignment -> global key\n\t\tLiteralInteger: 5 (number)"); @@ -94,7 +93,7 @@ TEST_CASE( "Conditional To String", "[BoundTreeString]" ) { TEST_CASE( "Numerical For to String", "[BoundTreeString]" ) { std::stringstream stream; auto key = new u16string(u"i"); - const BoundVariableKey *keyObj = new BoundVariableKey(HashedString(key), 0, true); + const BoundVariableKey *keyObj = new BoundVariableKey(HashedString(key), 0, true, NumericScriptType::AwareInt); auto s = new BoundNumericalForStatement(keyObj, new BoundLiteralIntegerExpression(0,0,0), new BoundLiteralIntegerExpression(5,0,0), new BoundLiteralIntegerExpression(1,0,0), @@ -116,15 +115,15 @@ TEST_CASE( "Numerical For to String", "[BoundTreeString]" ) { TEST_CASE( "Generic For to String", "[BoundTreeString]" ) { std::stringstream stream; auto key = new u16string(u"k"); - const BoundVariableKey *keyObj = new BoundVariableKey(HashedString(key), 0, true); + const BoundVariableKey *keyObj = new BoundVariableKey(HashedString(key), 0, true, NumericScriptType::AwareInt); auto valueKey = new u16string(u"v"); - const BoundVariableKey *valueKeyObj = new BoundVariableKey(HashedString(valueKey), 0, true); + const BoundVariableKey *valueKeyObj = new BoundVariableKey(HashedString(valueKey), 0, true, NumericScriptType::AwareInt); auto s = new BoundGenericForStatement(keyObj, valueKeyObj, new BoundNilExpression(0,0), new BoundBadStatement()); s->GetTreeString(stream, 1); REQUIRE(stream.str() == R"( GenericForLoopStatement - Key: k - Value: v + Key: k (number) + Value: v (number) Iterator: NilExpression (nil) Do: @@ -150,7 +149,7 @@ TEST_CASE( "Function Declaration To String", "[BoundTreeString]" ) { std::stringstream stream; auto t = make_shared(); auto key = new u16string(u"func"); - const BoundVariableKey *keyObj = new BoundVariableKey(HashedString(key), 0, true); + const BoundVariableKey *keyObj = new BoundVariableKey(HashedString(key), 0, true, NumericScriptType::AwareInt); auto s = new BoundFunctionDeclarationStatement(t, keyObj, new BoundBlockStatement({})); s->GetTreeString(stream, 1); REQUIRE(stream.str() == @@ -213,7 +212,7 @@ TEST_CASE( "Nil Expression To String", "[BoundTreeString]" ) { TEST_CASE( "Variable Expression To String", "[BoundTreeString]" ) { std::stringstream stream; auto type = ScriptType::BoolType; - auto key = new BoundVariableKey(HashedString(new u16string(u"var")), 0, false); + auto key = new BoundVariableKey(HashedString(new u16string(u"var")), 0, false, NumericScriptType::AwareInt); auto s = new BoundVariableExpression(key, type,0,0); s->GetTreeString(stream, 1); REQUIRE(stream.str() == "\tVariableExpression: var (bool)");