Include type in BoundVariableKey for debugging purposes
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2019-09-07 15:33:26 +02:00
parent 629adafeaf
commit 8f078e580e
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
7 changed files with 24 additions and 22 deletions

View File

@ -178,7 +178,7 @@ namespace Porygon::Binder {
}
type = dynamic_pointer_cast<const GenericFunctionScriptType>(varType);
type->RegisterFunctionOption(option);
assignmentKey = new BoundVariableKey(identifier, scope, false);
assignmentKey = new BoundVariableKey(identifier, scope, false, type);
} else {
type = make_shared<const GenericFunctionScriptType>();
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());
}

View File

@ -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);

View File

@ -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<const ScriptType> &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));
}
}
}

View File

@ -4,6 +4,7 @@
#include <string>
#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<const ScriptType> _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<const ScriptType> 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<const ScriptType> GetType() const{
return _type;
}
};
}

View File

@ -6,6 +6,7 @@
#include <vector>
#include "../EvalValues/EvalValue.hpp"
#include "../EvalValuePointer.hpp"
#include "../../Binder/BoundVariables/BoundVariableKey.hpp"
using namespace Porygon::Binder;

View File

@ -1,13 +1,9 @@
#include <utility>
#ifndef PORYGONLANG_SCRIPTTYPE_HPP
#define PORYGONLANG_SCRIPTTYPE_HPP
#include <utility>
#include <vector>
#include <memory>
#include "../Binder/BoundVariables/BoundVariableKey.hpp"
#include "../Utilities/HashedString.hpp"
#include "CastResult.hpp"

View File

@ -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<const GenericFunctionScriptType>();
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)");