2019-06-05 16:44:23 +00:00
|
|
|
#include <utility>
|
2019-06-07 11:51:49 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <iterator>
|
2019-06-15 15:20:27 +00:00
|
|
|
#include <locale>
|
2019-05-28 15:49:03 +00:00
|
|
|
#include <unordered_map>
|
2019-06-15 15:20:27 +00:00
|
|
|
#include <codecvt>
|
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-06-15 15:20:27 +00:00
|
|
|
Script* Script::Create(const u16string& script) {
|
2019-05-28 15:49:03 +00:00
|
|
|
auto s = new Script();
|
2019-06-13 15:37:23 +00:00
|
|
|
s -> Parse(script);
|
2019-05-21 10:59:15 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2019-06-15 15:20:27 +00:00
|
|
|
std::u16string To_UTF16(const string &s)
|
|
|
|
{
|
|
|
|
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
|
|
|
|
return conv.from_bytes(s);
|
|
|
|
}
|
|
|
|
Script *Script::Create(const string &script) {
|
|
|
|
return Script::Create(To_UTF16(script));
|
|
|
|
}
|
|
|
|
|
2019-05-23 16:50:09 +00:00
|
|
|
Script::Script() {
|
2019-06-06 17:01:54 +00:00
|
|
|
Diagnostics = new DiagnosticsHolder();
|
2019-05-23 16:50:09 +00:00
|
|
|
_evaluator = new Evaluator(this);
|
2019-06-05 15:46:46 +00:00
|
|
|
_boundScript = nullptr;
|
2019-06-14 20:21:33 +00:00
|
|
|
_scriptVariables = new unordered_map<uint32_t, shared_ptr<EvalValue>>(0);
|
2019-05-23 16:50:09 +00:00
|
|
|
}
|
|
|
|
|
2019-06-12 16:45:47 +00:00
|
|
|
EvalValue* Script::Evaluate() {
|
|
|
|
return _evaluator->Evaluate(_boundScript);
|
2019-05-23 16:50:09 +00:00
|
|
|
}
|
|
|
|
|
2019-05-21 12:15:39 +00:00
|
|
|
Script::~Script() {
|
|
|
|
delete this -> Diagnostics;
|
2019-06-05 15:46:46 +00:00
|
|
|
delete this -> _boundScript;
|
2019-05-24 17:14:30 +00:00
|
|
|
delete this -> _evaluator;
|
2019-05-29 12:55:03 +00:00
|
|
|
this->_scriptVariables->clear();
|
|
|
|
delete this->_scriptVariables;
|
2019-05-21 12:15:39 +00:00
|
|
|
}
|
|
|
|
|
2019-06-15 15:20:27 +00:00
|
|
|
void Script::Parse(const u16string& script) {
|
2019-06-13 15:37:23 +00:00
|
|
|
auto lexer = Lexer(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-06-14 20:21:33 +00:00
|
|
|
unordered_map<uint32_t, BoundVariable*> scriptScope;
|
2019-05-28 15:49:03 +00:00
|
|
|
auto bindScope = new BoundScope(&scriptScope);
|
2019-06-05 15:46:46 +00:00
|
|
|
this->_boundScript = Binder::Bind(this, parseResult, bindScope);
|
2019-05-28 15:49:03 +00:00
|
|
|
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-06-15 15:20:27 +00:00
|
|
|
EvalValue *Script::GetVariable(const u16string &key) {
|
2019-06-01 19:38:39 +00:00
|
|
|
return _scriptVariables -> at(HashedString(key).GetHash()).get();
|
2019-05-29 13:10:16 +00:00
|
|
|
}
|
|
|
|
|
2019-06-15 15:20:27 +00:00
|
|
|
bool Script::HasVariable(const u16string &key) {
|
2019-05-29 13:10:16 +00:00
|
|
|
auto f = _scriptVariables->find(HashedString(key).GetHash());
|
|
|
|
return f != _scriptVariables->end();
|
|
|
|
}
|
|
|
|
|
2019-06-05 15:46:46 +00:00
|
|
|
EvalValue *Script::GetLastValue() {
|
|
|
|
return _evaluator->GetLastValue();
|
|
|
|
}
|
|
|
|
|
2019-06-15 15:20:27 +00:00
|
|
|
bool Script::HasFunction(const u16string &key) {
|
2019-06-05 16:44:23 +00:00
|
|
|
auto f = _scriptVariables->find(HashedString(key).GetHash());
|
2019-06-13 14:26:10 +00:00
|
|
|
return f != _scriptVariables->end() && f.operator->()->second->GetTypeClass() == TypeClass ::Function;
|
2019-06-05 16:44:23 +00:00
|
|
|
}
|
|
|
|
|
2019-06-15 15:20:27 +00:00
|
|
|
shared_ptr<EvalValue> Script::CallFunction(const u16string &key, const vector<EvalValue *>& variables) {
|
2019-06-05 16:44:23 +00:00
|
|
|
auto var = (ScriptFunctionEvalValue*)GetVariable(key);
|
2019-06-14 20:21:33 +00:00
|
|
|
return this->_evaluator->EvaluateFunction(var, variables);
|
2019-06-05 16:44:23 +00:00
|
|
|
}
|
|
|
|
|
2019-06-15 15:20:27 +00:00
|
|
|
|
2019-06-05 15:46:46 +00:00
|
|
|
extern "C" {
|
2019-06-15 15:20:27 +00:00
|
|
|
Script* CreateScript(char16_t * s){
|
2019-06-05 15:46:46 +00:00
|
|
|
return Script::Create(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EvaluateScript(Script* script){
|
|
|
|
script->Evaluate();
|
|
|
|
}
|
|
|
|
|
|
|
|
EvalValue* GetLastValue(Script* script){
|
|
|
|
return script->GetLastValue();
|
|
|
|
}
|
|
|
|
|
2019-06-15 15:20:27 +00:00
|
|
|
bool HasVariable(Script* script, const char16_t* key){
|
2019-06-05 15:46:46 +00:00
|
|
|
return script->HasVariable(key);
|
|
|
|
}
|
|
|
|
|
2019-06-15 15:20:27 +00:00
|
|
|
EvalValue* GetVariable(Script* script, const char16_t* key){
|
2019-06-05 15:46:46 +00:00
|
|
|
return script->GetVariable(key);
|
|
|
|
}
|
2019-06-07 11:51:49 +00:00
|
|
|
|
2019-06-15 15:20:27 +00:00
|
|
|
bool HasFunction(Script* script, const char16_t* key){
|
2019-06-07 11:51:49 +00:00
|
|
|
return script->HasFunction(key);
|
|
|
|
}
|
|
|
|
|
2019-06-15 15:20:27 +00:00
|
|
|
EvalValue* CallFunction(Script* script, const char16_t* key, EvalValue* parameters[], int parameterCount){
|
2019-06-07 11:51:49 +00:00
|
|
|
std::vector<EvalValue*> v(parameters, parameters + parameterCount);
|
2019-06-07 13:23:13 +00:00
|
|
|
return script->CallFunction(key, v).get();
|
2019-06-07 11:51:49 +00:00
|
|
|
}
|
2019-06-05 15:46:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|