diff --git a/src/Script.cpp b/src/Script.cpp index 92a623c..3fdd55b 100644 --- a/src/Script.cpp +++ b/src/Script.cpp @@ -1,3 +1,5 @@ +#include + #include #include #include @@ -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(s); _boundScript = nullptr; _scriptVariables = new map>(); @@ -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 scriptScope; auto bindScope = new BoundScope(&scriptScope); - this->_boundScript = Binder::Binder::Bind(this, parseResult, bindScope); + this->_boundScript = shared_ptr(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 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 boundScript, + shared_ptr diagnostics) { + _boundScript = std::move(boundScript); + Diagnostics = std::move(diagnostics); + _scriptVariables = new map>(); + _evaluator = new Evaluator(_scriptVariables); +} + extern "C" { Porygon::Script* CreateScript(char16_t * s){ diff --git a/src/Script.hpp b/src/Script.hpp index 7441b60..d5a74fd 100644 --- a/src/Script.hpp +++ b/src/Script.hpp @@ -19,15 +19,18 @@ namespace Porygon{ class Script { Evaluator* _evaluator; map>* _scriptVariables; - Binder::BoundScriptStatement* _boundScript; + shared_ptr _boundScript; shared_ptr _returnType; explicit Script(const u16string&); + Script(shared_ptr boundScript, shared_ptr diagnostics); void Parse(const u16string& script); public: + shared_ptr Diagnostics; + static Script* Create(const u16string& script); static Script* Create(const string& script); - Diagnostics::DiagnosticsHolder* Diagnostics; + static Script* Clone(const Script* script); ~Script();