PorygonLang/src/Script.cpp

121 lines
3.6 KiB
C++
Raw Normal View History

#include <utility>
#include <vector>
#include <iterator>
#include <locale>
2019-05-28 15:49:03 +00:00
#include <unordered_map>
#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"
#include "Binder/Binder.hpp"
2019-05-21 10:59:15 +00:00
Porygon::Script* Porygon::Script::Create(const u16string& script) {
2019-05-28 15:49:03 +00:00
auto s = new Script();
s -> Parse(script);
2019-05-21 10:59:15 +00:00
return s;
}
std::u16string To_UTF16(const string &s)
{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
return conv.from_bytes(s);
}
Porygon::Script *Porygon::Script::Create(const string &script) {
return Script::Create(To_UTF16(script));
}
Porygon::Script::Script() {
2019-06-06 17:01:54 +00:00
Diagnostics = new DiagnosticsHolder();
_boundScript = nullptr;
_scriptVariables = new unordered_map<uint32_t, shared_ptr<EvalValue>>(0);
_evaluator = new Evaluator(this -> _scriptVariables);
2019-05-23 16:50:09 +00:00
}
EvalValue* Porygon::Script::Evaluate() {
return _evaluator->Evaluate(_boundScript);
2019-05-23 16:50:09 +00:00
}
Porygon::Script::~Script() {
2019-05-21 12:15:39 +00:00
delete this -> Diagnostics;
delete this -> _boundScript;
delete this -> _evaluator;
this->_scriptVariables->clear();
delete this->_scriptVariables;
2019-05-21 12:15:39 +00:00
}
void Porygon::Script::Parse(const u16string& script) {
auto lexer = Lexer(script, this);
2019-05-21 10:59:15 +00:00
auto lexResult = lexer.Lex();
auto parser = Parser::Parser(lexResult, this);
2019-05-21 10:59:15 +00:00
auto parseResult = parser.Parse();
for (auto token : lexResult){
delete token;
}
lexResult.clear();
if (!Diagnostics->HasErrors()){
unordered_map<uint32_t, BoundVariable*> scriptScope;
2019-05-28 15:49:03 +00:00
auto bindScope = new BoundScope(&scriptScope);
this->_boundScript = Binder::Binder::Bind(this, parseResult, bindScope);
2019-05-28 15:49:03 +00:00
for (const auto& v : scriptScope){
this->_scriptVariables -> insert({v.first, nullptr});
2019-05-28 15:49:03 +00:00
delete v.second;
}
scriptScope.clear();
}
delete parseResult;
2019-05-21 10:59:15 +00:00
}
2019-05-21 12:15:39 +00:00
EvalValue *Porygon::Script::GetVariable(const u16string &key) {
return _scriptVariables -> at(HashedString(key).GetHash()).get();
}
bool Porygon::Script::HasVariable(const u16string &key) {
auto f = _scriptVariables->find(HashedString(key).GetHash());
return f != _scriptVariables->end();
}
EvalValue *Porygon::Script::GetLastValue() {
return _evaluator->GetLastValue();
}
bool Porygon::Script::HasFunction(const u16string &key) {
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;
}
shared_ptr<EvalValue> Porygon::Script::CallFunction(const u16string &key, const vector<EvalValue *>& variables) {
auto var = (ScriptFunctionEvalValue*)GetVariable(key);
return this->_evaluator->EvaluateFunction(var, variables);
}
extern "C" {
Porygon::Script* CreateScript(char16_t * s){
return Porygon::Script::Create(s);
}
void EvaluateScript(Porygon::Script* script){
script->Evaluate();
}
EvalValue* GetLastValue(Porygon::Script* script){
return script->GetLastValue();
}
bool HasVariable(Porygon::Script* script, const char16_t* key){
return script->HasVariable(key);
}
EvalValue* GetVariable(Porygon::Script* script, const char16_t* key){
return script->GetVariable(key);
}
bool HasFunction(Porygon::Script* script, const char16_t* key){
return script->HasFunction(key);
}
EvalValue* CallFunction(Porygon::Script* script, const char16_t* key, EvalValue* parameters[], int parameterCount){
std::vector<EvalValue*> v(parameters, parameters + parameterCount);
2019-06-07 13:23:13 +00:00
return script->CallFunction(key, v).get();
}
}