PorygonLang/src/Script.cpp

112 lines
3.1 KiB
C++
Raw Normal View History

#include <utility>
#include <vector>
#include <iterator>
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"
#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() {
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;
2019-06-12 13:19:28 +00:00
_scriptVariables = new unordered_map<size_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
}
2019-05-21 10:59:15 +00:00
void Script::Parse(string script) {
2019-06-05 17:11:56 +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();
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);
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 string &key) {
return _scriptVariables -> at(HashedString(key).GetHash()).get();
}
bool Script::HasVariable(const string &key) {
auto f = _scriptVariables->find(HashedString(key).GetHash());
return f != _scriptVariables->end();
}
EvalValue *Script::GetLastValue() {
return _evaluator->GetLastValue();
}
bool Script::HasFunction(const string &key) {
auto f = _scriptVariables->find(HashedString(key).GetHash());
return f != _scriptVariables->end() && f.operator->()->second->GetType()->GetClass() == TypeClass ::Function;
}
2019-06-07 13:23:13 +00:00
shared_ptr<EvalValue> Script::CallFunction(const string &key, vector<EvalValue *> variables) {
auto var = (ScriptFunctionEvalValue*)GetVariable(key);
return this->_evaluator->EvaluateFunction(var, std::move(variables));
}
extern "C" {
Script* CreateScript(char * s){
return Script::Create(s);
}
void EvaluateScript(Script* script){
script->Evaluate();
}
EvalValue* GetLastValue(Script* script){
return script->GetLastValue();
}
bool HasVariable(Script* script, const char* key){
return script->HasVariable(key);
}
EvalValue* GetVariable(Script* script, const char* key){
return script->GetVariable(key);
}
bool HasFunction(Script* script, const char* key){
return script->HasFunction(key);
}
EvalValue* CallFunction(Script* script, const char* 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();
}
}