Initial work on standard library
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2019-06-29 16:18:59 +02:00
parent ecfc1ae3b7
commit 24c560b52d
12 changed files with 213 additions and 22 deletions

View File

@@ -0,0 +1,56 @@
#ifndef PORYGONLANG_BASICLIBRARY_HPP
#define PORYGONLANG_BASICLIBRARY_HPP
#include <string>
#include <map>
#include "../Evaluator/EvaluationException.hpp"
#include "../Evaluator/EvalValues/EvalValue.hpp"
#include "../Evaluator/EvalValues/NilEvalValue.hpp"
#include "../Utilities/StringUtils.hpp"
#include "../Binder/BoundVariables/BoundVariable.hpp"
#include "../UserData/UserDataFunction.hpp"
#include "../UserData/UserDataFunctionType.hpp"
namespace Porygon::StandardLibraries{
class BasicLibrary{
static Evaluation::EvalValue* _error(void*, Evaluation::EvalValue* parameters[], int parameterCount){
auto message = parameters[0]->EvaluateString();
auto conv = Utilities::StringUtils::FromUTF8(message);
throw Evaluation::EvaluationException(conv);
}
static Evaluation::EvalValue* _assert(void*, Evaluation::EvalValue* parameters[], int parameterCount){
auto assertion = parameters[0]->EvaluateBool();
if (!assertion){
throw Evaluation::EvaluationException("assertion failed!");
}
return new Evaluation::BooleanEvalValue(true);
}
public:
static void RegisterVariables(std::map<Utilities::HashedString, Binder::BoundVariable *>* bound,
std::map<Utilities::HashedString, shared_ptr<Evaluation::EvalValue>>* values){
// Register error function
auto errorFuncType = make_shared<UserData::UserDataFunctionType>(
make_shared<ScriptType>(TypeClass::Nil),
vector<shared_ptr<ScriptType>>{make_shared<StringScriptType>(false, 0)});
auto errorFunc = make_shared<UserData::UserDataFunction>(_error, nullptr);
auto errorLookup = Utilities::HashedString::CreateLookup(u"error");
bound->insert({errorLookup, new Binder::BoundVariable(errorFuncType)});
values->insert({errorLookup, errorFunc});
// Register assert function
auto assertFuncType = make_shared<UserData::UserDataFunctionType>(
make_shared<ScriptType>(TypeClass::Bool),
vector<shared_ptr<ScriptType>>{make_shared<ScriptType>(TypeClass::Bool)});
auto assertFunc = make_shared<UserData::UserDataFunction>(_assert, nullptr);
auto assertLookup = Utilities::HashedString::CreateLookup(u"assert");
bound->insert({assertLookup, new Binder::BoundVariable(assertFuncType)});
values->insert({assertLookup, assertFunc});
}
};
}
#endif //PORYGONLANG_BASICLIBRARY_HPP

View File

@@ -0,0 +1,3 @@
#include "StaticScope.hpp"
Porygon::StandardLibraries::StaticScope::InternalScope Porygon::StandardLibraries::StaticScope::_internal;

View File

@@ -0,0 +1,51 @@
#include <utility>
#ifndef PORYGONLANG_STATICSCOPE_HPP
#define PORYGONLANG_STATICSCOPE_HPP
#include <map>
#include "../Utilities/HashedString.hpp"
#include "../Binder/BoundVariables/BoundVariable.hpp"
#include "../Evaluator/EvalValues/EvalValue.hpp"
#include "BasicLibrary.hpp"
using namespace std;
namespace Porygon::StandardLibraries{
/*!
\class StaticScope
\brief The static shared scope for all scripts. Variables registered in here should be stateless.
*/
class StaticScope {
class InternalScope{
public:
map<Utilities::HashedString, Binder::BoundVariable *> _boundVariables;
map<Utilities::HashedString, shared_ptr<Evaluation::EvalValue>> _variables;
InternalScope(){
BasicLibrary::RegisterVariables(&_boundVariables, &_variables);
}
};
static InternalScope _internal;
public:
static void RegisterVariable(const Utilities::HashedString& identifier, shared_ptr<ScriptType> type, Evaluation::EvalValue* value){
_internal._boundVariables.insert({identifier, new Binder::BoundVariable(std::move(type))});
_internal._variables.insert({identifier, shared_ptr<Evaluation::EvalValue>(value)});
}
static Binder::BoundVariable* GetBoundVariable(const Utilities::HashedString &identifier){
auto found = _internal._boundVariables.find(identifier);
if (found != _internal._boundVariables.end()) {
return found->second;
}
return nullptr;
}
static shared_ptr<Evaluation::EvalValue> GetVariable(const Utilities::HashedString &identifier){
return _internal._variables[identifier];
}
};
}
#endif //PORYGONLANG_STATICSCOPE_HPP