Fixes for creating variable in local scope

This commit is contained in:
Deukhoofd 2019-05-29 15:10:16 +02:00
parent 188d89db94
commit bda561b775
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
4 changed files with 20 additions and 5 deletions

View File

@ -67,12 +67,12 @@ BoundVariable *BoundScope::GetVariable(int scope, int identifier) {
} }
VariableAssignment BoundScope::CreateExplicitLocal(int identifier, const ScriptType& type) { VariableAssignment BoundScope::CreateExplicitLocal(int identifier, const ScriptType& type) {
auto scope = this->_localScope.at(this->_currentScope); auto scope = this->_localScope.at(this->_currentScope - 1);
if (scope -> find(identifier) != scope -> end()){ if (scope -> find(identifier) != scope -> end()){
return VariableAssignment(VariableAssignmentResult::ExplicitLocalVariableExists, nullptr); return VariableAssignment(VariableAssignmentResult::ExplicitLocalVariableExists, nullptr);
} }
scope -> insert({identifier, new BoundVariable(type)}); scope -> insert({identifier, new BoundVariable(type)});
return VariableAssignment(VariableAssignmentResult::Ok, new BoundVariableKey(identifier, this->_currentScope, true)); return VariableAssignment(VariableAssignmentResult::Ok, new BoundVariableKey(identifier, this->_currentScope - 1, true));
} }
VariableAssignment BoundScope::AssignVariable(int identifier, const ScriptType& type) { VariableAssignment BoundScope::AssignVariable(int identifier, const ScriptType& type) {

View File

@ -57,3 +57,12 @@ void Script::Parse(string script) {
delete parseResult; delete parseResult;
} }
EvalValue *Script::GetVariable(const string &key) {
return _scriptVariables -> at(HashedString(key).GetHash());
}
bool Script::HasVariable(const string &key) {
auto f = _scriptVariables->find(HashedString(key).GetHash());
return f != _scriptVariables->end();
}

View File

@ -39,9 +39,8 @@ public:
return _lastValue; return _lastValue;
}; };
EvalValue* GetVariable(const string& key){ EvalValue* GetVariable(const string& key);
return _scriptVariables -> at(HashedString(key).GetHash()); bool HasVariable(const string& key);
}
}; };

View File

@ -14,5 +14,12 @@ TEST_CASE( "Create script variable", "[integration]" ) {
delete script; delete script;
} }
TEST_CASE( "Create local variable", "[integration]" ) {
Script* script = Script::Create("local foo = true");
REQUIRE(!script->Diagnostics -> HasErrors());
REQUIRE_FALSE(script->HasVariable("foo"));
script->Evaluate();
delete script;
}
#endif #endif