Rework of variable scope to reduce load on evaluator

This commit is contained in:
2019-06-13 15:16:41 +02:00
parent 0339c44a89
commit 813ab1e90b
9 changed files with 65 additions and 91 deletions

View File

@@ -202,7 +202,7 @@ BoundExpression* Binder::BindVariableExpression(VariableExpression* expression){
}
auto var = this->_scope->GetVariable(scope, key.GetHash());
auto type = var->GetType();
return new BoundVariableExpression(scope, key.GetHash(), type, expression->GetStartPosition(), expression->GetLength());
return new BoundVariableExpression(new BoundVariableKey(key.GetHash(), scope, false), type, expression->GetStartPosition(), expression->GetLength());
}
BoundExpression* Binder::BindBinaryOperator(BinaryExpression* expression){

View File

@@ -134,13 +134,11 @@ public:
};
class BoundVariableExpression : public BoundExpression{
int _scope;
int _id;
BoundVariableKey* _key;
public:
BoundVariableExpression(int scope, int id, shared_ptr<ScriptType> type, unsigned int start, unsigned int length)
BoundVariableExpression(BoundVariableKey* key, shared_ptr<ScriptType> type, unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(type)){
_scope = scope;
_id = id;
_key = key;
}
~BoundVariableExpression() override = default;
@@ -149,12 +147,8 @@ public:
return BoundExpressionKind ::Variable;
}
int GetScope(){
return _scope;
}
int GetId(){
return _id;
BoundVariableKey* GetKey(){
return _key;
}
};

View File

@@ -7,6 +7,7 @@ BoundScope::BoundScope(unordered_map<int, BoundVariable *> *tableScope) {
_tableScope = tableScope;
_currentScope = 1;
_deepestScope = 1;
_lastCreatedScope = 1;
auto localUpmostScope = new unordered_map<int, BoundVariable*>();
_localScope.push_back(localUpmostScope);
}
@@ -21,14 +22,16 @@ BoundScope::~BoundScope() {
}
void BoundScope::GoInnerScope() {
_currentScope++;
_lastCreatedScope++;
_currentScope = _lastCreatedScope;
if (_localScope.size() < _currentScope){
auto innerScope = new unordered_map<int, BoundVariable*>();
_localScope.push_back(innerScope);
}
/*
if (_deepestScope < _currentScope){
_deepestScope = _currentScope;
}
}*/
}
void BoundScope::GoOuterScope() {

View File

@@ -17,6 +17,7 @@ class BoundScope {
unordered_map<int, BoundVariable*>* _tableScope;
vector<unordered_map<int, BoundVariable*>*> _localScope;
int _currentScope;
int _lastCreatedScope;
int _deepestScope;
public:
explicit BoundScope(unordered_map<int, BoundVariable*> *tableScope);

View File

@@ -10,13 +10,23 @@ class BoundVariableKey{
int _identifier;
unsigned int _scopeId;
bool _isCreation;
u_int64_t _hash;
static u_int64_t KnuthsHash(int i1, int i2)
{
u_int64_t ret = i1;
ret *= 2654435761U;
return ret ^ i2;
}
public:
BoundVariableKey(int id, unsigned int scope, bool creation){
_identifier = std::move(id);
_identifier = id;
_scopeId = scope;
_hash = KnuthsHash(scope, id);
_isCreation = creation;
}
int GetIdentifier(){
return _identifier;
}
@@ -28,6 +38,10 @@ public:
bool IsCreation(){
return _isCreation;
}
u_int64_t GetHash(){
return _hash;
}
};
#endif //PORYGONLANG_BOUNDVARIABLEKEY_HPP