More valgrind fixes

Signed-off-by: Deukhoofd <deukhoofd@gmail.com>
This commit is contained in:
Deukhoofd 2019-06-24 15:33:29 +02:00
parent 907e444389
commit 97be8cded0
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
2 changed files with 27 additions and 7 deletions

View File

@ -1,3 +1,5 @@
#include <utility>
#include <utility>
#include <vector>
#include <iterator>
@ -23,7 +25,7 @@ Porygon::Script *Porygon::Script::Create(const string &script) {
}
Porygon::Script::Script(const u16string& s) {
Diagnostics = new Diagnostics::DiagnosticsHolder(s);
Diagnostics = make_shared<Diagnostics::DiagnosticsHolder>(s);
_boundScript = nullptr;
_scriptVariables = new map<Utilities::HashedString, shared_ptr<EvalValue>>();
@ -33,12 +35,10 @@ Porygon::Script::Script(const u16string& s) {
}
EvalValue* Porygon::Script::Evaluate() {
return _evaluator->Evaluate(_boundScript);
return _evaluator->Evaluate(_boundScript.get());
}
Porygon::Script::~Script() {
delete this -> Diagnostics;
delete this -> _boundScript;
delete this -> _evaluator;
this->_scriptVariables->clear();
delete this->_scriptVariables;
@ -56,7 +56,7 @@ void Porygon::Script::Parse(const u16string& script) {
if (!Diagnostics->HasErrors()){
map<Utilities::HashedString, BoundVariable*> scriptScope;
auto bindScope = new BoundScope(&scriptScope);
this->_boundScript = Binder::Binder::Bind(this, parseResult, bindScope);
this->_boundScript = shared_ptr<BoundScriptStatement>(Binder::Binder::Bind(this, parseResult, bindScope));
for (const auto& v : scriptScope){
this->_scriptVariables -> insert({v.first, nullptr});
delete v.second;
@ -89,6 +89,23 @@ shared_ptr<EvalValue> Porygon::Script::CallFunction(const u16string &key, const
return this->_evaluator->EvaluateFunction(var, variables);
}
Porygon::Script *Porygon::Script::Clone(const Porygon::Script *script) {
auto s = new Script(script->_boundScript, script->Diagnostics);
for (auto v: *script->_scriptVariables){
s->_scriptVariables->insert({v.first, nullptr});
}
s->_returnType = script->_returnType;
return s;
}
Porygon::Script::Script(shared_ptr<BoundScriptStatement> boundScript,
shared_ptr<Porygon::Diagnostics::DiagnosticsHolder> diagnostics) {
_boundScript = std::move(boundScript);
Diagnostics = std::move(diagnostics);
_scriptVariables = new map<Utilities::HashedString, shared_ptr<EvalValue>>();
_evaluator = new Evaluator(_scriptVariables);
}
extern "C" {
Porygon::Script* CreateScript(char16_t * s){

View File

@ -19,15 +19,18 @@ namespace Porygon{
class Script {
Evaluator* _evaluator;
map<Utilities::HashedString, shared_ptr<EvalValue>>* _scriptVariables;
Binder::BoundScriptStatement* _boundScript;
shared_ptr<Binder::BoundScriptStatement> _boundScript;
shared_ptr<ScriptType> _returnType;
explicit Script(const u16string&);
Script(shared_ptr<Binder::BoundScriptStatement> boundScript, shared_ptr<Diagnostics::DiagnosticsHolder> diagnostics);
void Parse(const u16string& script);
public:
shared_ptr<Diagnostics::DiagnosticsHolder> Diagnostics;
static Script* Create(const u16string& script);
static Script* Create(const string& script);
Diagnostics::DiagnosticsHolder* Diagnostics;
static Script* Clone(const Script* script);
~Script();