2019-05-21 10:59:15 +00:00
|
|
|
#include <utility>
|
2019-05-28 15:49:03 +00:00
|
|
|
#include <unordered_map>
|
2019-05-21 10:59:15 +00:00
|
|
|
#include "Script.hpp"
|
2019-05-21 11:56:08 +00:00
|
|
|
#include "Parser/Lexer.hpp"
|
|
|
|
#include "Parser/Parser.hpp"
|
2019-05-21 18:59:26 +00:00
|
|
|
#include "Binder/Binder.hpp"
|
2019-05-21 10:59:15 +00:00
|
|
|
|
2019-05-28 15:49:03 +00:00
|
|
|
Script* Script::Create(string script) {
|
|
|
|
auto s = new Script();
|
|
|
|
s -> Parse(std::move(script));
|
2019-05-21 10:59:15 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2019-05-23 16:50:09 +00:00
|
|
|
Script::Script() {
|
|
|
|
Diagnostics = new class Diagnostics();
|
|
|
|
_evaluator = new Evaluator(this);
|
2019-05-25 10:26:11 +00:00
|
|
|
_lastValue = nullptr;
|
|
|
|
BoundScript = nullptr;
|
2019-05-29 12:55:03 +00:00
|
|
|
_scriptVariables = new unordered_map<int, EvalValue*>(0);
|
2019-05-23 16:50:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Script::Evaluate() {
|
|
|
|
_evaluator->Evaluate(BoundScript);
|
|
|
|
}
|
|
|
|
|
2019-05-21 12:15:39 +00:00
|
|
|
Script::~Script() {
|
|
|
|
delete this -> Diagnostics;
|
2019-05-21 18:59:26 +00:00
|
|
|
delete this -> BoundScript;
|
2019-05-24 17:14:30 +00:00
|
|
|
delete this -> _lastValue;
|
|
|
|
delete this -> _evaluator;
|
2019-05-30 13:23:48 +00:00
|
|
|
for (auto v : *this->_scriptVariables){
|
2019-05-29 13:00:41 +00:00
|
|
|
delete v.second;
|
|
|
|
}
|
2019-05-29 12:55:03 +00:00
|
|
|
this->_scriptVariables->clear();
|
|
|
|
delete this->_scriptVariables;
|
2019-05-21 12:15:39 +00:00
|
|
|
}
|
|
|
|
|
2019-05-21 10:59:15 +00:00
|
|
|
void Script::Parse(string script) {
|
2019-05-21 11:56:08 +00:00
|
|
|
auto lexer = Lexer(std::move(script), this);
|
2019-05-21 10:59:15 +00:00
|
|
|
auto lexResult = lexer.Lex();
|
2019-05-21 12:00:14 +00:00
|
|
|
auto parser = Parser(lexResult, this);
|
2019-05-21 10:59:15 +00:00
|
|
|
auto parseResult = parser.Parse();
|
|
|
|
for (auto token : lexResult){
|
|
|
|
delete token;
|
|
|
|
}
|
|
|
|
lexResult.clear();
|
2019-05-21 18:59:26 +00:00
|
|
|
if (!Diagnostics->HasErrors()){
|
2019-05-28 15:49:03 +00:00
|
|
|
unordered_map<int, BoundVariable*> scriptScope;
|
|
|
|
auto bindScope = new BoundScope(&scriptScope);
|
|
|
|
this->BoundScript = Binder::Bind(this, parseResult, bindScope);
|
|
|
|
for (const auto& v : scriptScope){
|
2019-05-29 12:55:03 +00:00
|
|
|
this->_scriptVariables -> insert({v.first, nullptr});
|
2019-05-28 15:49:03 +00:00
|
|
|
delete v.second;
|
|
|
|
}
|
|
|
|
scriptScope.clear();
|
2019-05-21 18:59:26 +00:00
|
|
|
}
|
|
|
|
delete parseResult;
|
2019-05-21 10:59:15 +00:00
|
|
|
}
|
2019-05-21 12:15:39 +00:00
|
|
|
|
2019-05-29 13:10:16 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|