PorygonLang/src/Script.cpp

124 lines
3.4 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
Script* 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);
}
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);
_boundScript = nullptr;
_scriptVariables = new unordered_map<uint32_t, shared_ptr<EvalValue>>(0);
2019-05-23 16:50:09 +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;
delete this -> _boundScript;
delete this -> _evaluator;
this->_scriptVariables->clear();
delete this->_scriptVariables;
2019-05-21 12:15:39 +00:00
}
void Script::Parse(const u16string& script) {
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();
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::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 *Script::GetVariable(const u16string &key) {
return _scriptVariables -> at(HashedString(key).GetHash()).get();
}
bool Script::HasVariable(const u16string &key) {
auto f = _scriptVariables->find(HashedString(key).GetHash());
return f != _scriptVariables->end();
}
EvalValue *Script::GetLastValue() {
return _evaluator->GetLastValue();
}
bool 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> Script::CallFunction(const u16string &key, const vector<EvalValue *>& variables) {
auto var = (ScriptFunctionEvalValue*)GetVariable(key);
return this->_evaluator->EvaluateFunction(var, variables);
}
extern "C" {
Script* CreateScript(char16_t * s){
return Script::Create(s);
}
void EvaluateScript(Script* script){
script->Evaluate();
}
EvalValue* GetLastValue(Script* script){
return script->GetLastValue();
}
bool HasVariable(Script* script, const char16_t* key){
return script->HasVariable(key);
}
EvalValue* GetVariable(Script* script, const char16_t* key){
return script->GetVariable(key);
}
bool HasFunction(Script* script, const char16_t* key){
return script->HasFunction(key);
}
EvalValue* CallFunction(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();
}
}