diff --git a/src/Binder/BoundVariables/BoundScope.cpp b/src/Binder/BoundVariables/BoundScope.cpp index 289adff..fb91d1d 100644 --- a/src/Binder/BoundVariables/BoundScope.cpp +++ b/src/Binder/BoundVariables/BoundScope.cpp @@ -67,12 +67,12 @@ BoundVariable *BoundScope::GetVariable(int scope, int identifier) { } 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()){ return VariableAssignment(VariableAssignmentResult::ExplicitLocalVariableExists, nullptr); } 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) { diff --git a/src/Script.cpp b/src/Script.cpp index 9d2a4c2..27a51ae 100644 --- a/src/Script.cpp +++ b/src/Script.cpp @@ -57,3 +57,12 @@ void Script::Parse(string script) { 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(); +} + diff --git a/src/Script.hpp b/src/Script.hpp index 626982c..756713a 100644 --- a/src/Script.hpp +++ b/src/Script.hpp @@ -39,9 +39,8 @@ public: return _lastValue; }; - EvalValue* GetVariable(const string& key){ - return _scriptVariables -> at(HashedString(key).GetHash()); - } + EvalValue* GetVariable(const string& key); + bool HasVariable(const string& key); }; diff --git a/tests/integration/Variables.cpp b/tests/integration/Variables.cpp index d391166..bd4971e 100644 --- a/tests/integration/Variables.cpp +++ b/tests/integration/Variables.cpp @@ -14,5 +14,12 @@ TEST_CASE( "Create script variable", "[integration]" ) { 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