Initial work on iterators, rework of variable handling by including actual string
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2019-06-23 15:58:14 +02:00
parent 1a84661c79
commit 76b8ba3ebc
25 changed files with 185 additions and 78 deletions

View File

@@ -72,8 +72,8 @@ namespace Porygon::Binder {
auto boundExpression = this->BindExpression(s->GetExpression());
VariableAssignment assignment =
s->IsLocal() ?
this->_scope->CreateExplicitLocal(s->GetIdentifier().GetHash(), boundExpression->GetType())
: this->_scope->AssignVariable(s->GetIdentifier().GetHash(), boundExpression->GetType());
this->_scope->CreateExplicitLocal(s->GetIdentifier(), boundExpression->GetType())
: this->_scope->AssignVariable(s->GetIdentifier(), boundExpression->GetType());
if (assignment.GetResult() == VariableAssignmentResult::Ok) {
auto key = assignment.GetKey();
return new BoundAssignmentStatement(key, boundExpression);
@@ -139,7 +139,7 @@ namespace Porygon::Binder {
return new BoundBadStatement();
}
parameterTypes.at(i) = parsedType;
auto parameterAssignment = this->_scope->CreateExplicitLocal(var->GetIdentifier().GetHash(), parsedType);
auto parameterAssignment = this->_scope->CreateExplicitLocal(var->GetIdentifier(), parsedType);
if (parameterAssignment.GetResult() == VariableAssignmentResult::Ok) {
parameterKeys.at(i) = std::shared_ptr<BoundVariableKey>(parameterAssignment.GetKey());
} else {
@@ -153,7 +153,7 @@ namespace Porygon::Binder {
auto type = make_shared<FunctionScriptType>(returnType, parameterTypes, parameterKeys, scopeIndex);
this->_currentFunction = type;
auto assignment = this->_scope->AssignVariable(identifier.GetHash(), type);
auto assignment = this->_scope->AssignVariable(identifier, type);
if (assignment.GetResult() != VariableAssignmentResult::Ok) {
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::CantAssignVariable, statement->GetStartPosition(),
statement->GetLength());
@@ -242,7 +242,7 @@ namespace Porygon::Binder {
}
this -> _scope ->GoInnerScope();
auto variableKey = this -> _scope ->CreateExplicitLocal(identifier.GetHash(), make_shared<NumericScriptType>(true, false));
auto variableKey = this -> _scope ->CreateExplicitLocal(identifier, make_shared<NumericScriptType>(true, false));
if (variableKey.GetResult() != VariableAssignmentResult::Ok){
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::CantAssignVariable, statement->GetStartPosition(),
statement->GetLength());
@@ -300,15 +300,15 @@ namespace Porygon::Binder {
BoundExpression *Binder::BindVariableExpression(const VariableExpression *expression) {
auto key = expression->GetValue();
auto scope = this->_scope->Exists(key.GetHash());
auto scope = this->_scope->Exists(key);
if (scope == -1) {
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::VariableNotFound, expression->GetStartPosition(),
expression->GetLength());
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
}
auto var = this->_scope->GetVariable(scope, key.GetHash());
auto var = this->_scope->GetVariable(scope, key);
auto type = var->GetType();
return new BoundVariableExpression(new BoundVariableKey(key.GetHash(), scope, false), type,
return new BoundVariableExpression(new BoundVariableKey(key, scope, false), type,
expression->GetStartPosition(), expression->GetLength());
}
@@ -616,7 +616,7 @@ namespace Porygon::Binder {
}
BoundExpression *Binder::BindTableExpression(const ParsedTableExpression *expression) {
auto tableScope = new unordered_map<uint32_t, BoundVariable *>();
auto tableScope = new map<Utilities::HashedString, BoundVariable *>();
auto innerScope = new BoundScope(tableScope);
auto currentScope = this->_scope;
this->_scope = innerScope;

View File

@@ -4,11 +4,11 @@
#include "BoundScope.hpp"
namespace Porygon::Binder {
BoundScope::BoundScope(unordered_map<uint32_t, BoundVariable *> *tableScope) {
BoundScope::BoundScope(map<Utilities::HashedString, BoundVariable *> *tableScope) {
_tableScope = tableScope;
_currentScope = 1;
_lastCreatedScope = 1;
auto localUpmostScope = new unordered_map<uint32_t, BoundVariable *>();
auto localUpmostScope = new map<Utilities::HashedString, BoundVariable *>();
_localScope.push_back(localUpmostScope);
}
@@ -25,7 +25,7 @@ namespace Porygon::Binder {
_lastCreatedScope++;
_currentScope = _lastCreatedScope;
if (_localScope.size() < _currentScope) {
auto innerScope = new unordered_map<uint32_t, BoundVariable *>();
auto innerScope = new map<Utilities::HashedString, BoundVariable *>();
_localScope.push_back(innerScope);
}
}
@@ -39,7 +39,7 @@ namespace Porygon::Binder {
_currentScope--;
}
int BoundScope::Exists(int key) {
int BoundScope::Exists(Utilities::HashedString key) {
auto found = this->_tableScope->find(key);
if (found != _tableScope->end()) {
return 0;
@@ -54,7 +54,7 @@ namespace Porygon::Binder {
return -1;
}
BoundVariable *BoundScope::GetVariable(uint32_t scope, uint32_t identifier) {
BoundVariable *BoundScope::GetVariable(uint32_t scope, Utilities::HashedString identifier) {
if (scope == 0) {
auto find = this->_tableScope->find(identifier);
if (find != _tableScope->end()) {
@@ -71,7 +71,7 @@ namespace Porygon::Binder {
}
}
VariableAssignment BoundScope::CreateExplicitLocal(uint32_t identifier, std::shared_ptr<ScriptType> type) {
VariableAssignment BoundScope::CreateExplicitLocal(Utilities::HashedString identifier, std::shared_ptr<ScriptType> type) {
auto scope = this->_localScope.at(this->_currentScope - 1);
if (scope->find(identifier) != scope->end()) {
return VariableAssignment(VariableAssignmentResult::ExplicitLocalVariableExists, nullptr);
@@ -81,7 +81,7 @@ namespace Porygon::Binder {
new BoundVariableKey(identifier, this->_currentScope, true));
}
VariableAssignment BoundScope::AssignVariable(uint32_t identifier, const std::shared_ptr<ScriptType> &type) {
VariableAssignment BoundScope::AssignVariable(Utilities::HashedString identifier, const std::shared_ptr<ScriptType> &type) {
int exists = this->Exists(identifier);
if (exists == -1) {
// Creation

View File

@@ -3,7 +3,7 @@
#define PORYGONLANG_BOUNDSCOPE_HPP
#include <string>
#include <unordered_map>
#include <map>
#include <vector>
#include <memory>
#include "BoundVariable.hpp"
@@ -15,12 +15,12 @@ using namespace std;
namespace Porygon::Binder {
class BoundScope {
unordered_map<uint32_t, BoundVariable *> *_tableScope;
vector<unordered_map<uint32_t, BoundVariable *> *> _localScope;
map<Utilities::HashedString, BoundVariable *> *_tableScope;
vector<map<Utilities::HashedString, BoundVariable *> *> _localScope;
int _currentScope;
int _lastCreatedScope;
public:
explicit BoundScope(unordered_map<uint32_t, BoundVariable *> *tableScope);
explicit BoundScope(map<Utilities::HashedString, BoundVariable *> *tableScope);
~BoundScope();
@@ -28,13 +28,13 @@ namespace Porygon::Binder {
void GoOuterScope();
int Exists(int key);
int Exists(Utilities::HashedString key);
BoundVariable *GetVariable(uint32_t scope, uint32_t identifier);
BoundVariable *GetVariable(uint32_t scope, Utilities::HashedString identifier);
VariableAssignment CreateExplicitLocal(uint32_t identifier, std::shared_ptr<ScriptType> type);
VariableAssignment CreateExplicitLocal(Utilities::HashedString identifier, std::shared_ptr<ScriptType> type);
VariableAssignment AssignVariable(uint32_t identifier, const std::shared_ptr<ScriptType> &type);
VariableAssignment AssignVariable(Utilities::HashedString identifier, const std::shared_ptr<ScriptType> &type);
size_t GetLocalVariableCount() {
return _localScope.size();

View File

@@ -3,10 +3,11 @@
#define PORYGONLANG_BOUNDVARIABLEKEY_HPP
#include <string>
#include "../../Utilities/HashedString.hpp"
namespace Porygon::Binder {
class BoundVariableKey {
const int _identifier;
const Utilities::HashedString _identifier;
const unsigned int _scopeId;
const bool _isCreation;
const uint64_t _hash;
@@ -18,14 +19,14 @@ namespace Porygon::Binder {
}
public:
BoundVariableKey(int id, unsigned int scope, bool creation)
BoundVariableKey(Utilities::HashedString id, unsigned int scope, bool creation)
: _identifier(id),
_scopeId(scope),
_isCreation(creation),
_hash(KnuthsHash(id, scope)) {}
_hash(KnuthsHash(id.GetHash(), scope)) {}
const int GetIdentifier() const {
const Utilities::HashedString GetIdentifier() const {
return _identifier;
}