Implements toint function
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2019-07-04 17:18:07 +02:00
parent 5e02b6b389
commit 0446c1098b
4 changed files with 31 additions and 1 deletions

View File

@@ -5,6 +5,7 @@
#include <map>
#include "../Evaluator/EvaluationException.hpp"
#include "../Evaluator/EvalValues/EvalValue.hpp"
#include "../Evaluator/EvalValues/NumericEvalValue.hpp"
#include "../Evaluator/EvalValues/NilEvalValue.hpp"
#include "../Utilities/StringUtils.hpp"
#include "../Binder/BoundVariables/BoundVariable.hpp"
@@ -39,6 +40,12 @@ namespace Porygon::StandardLibraries{
return new Evaluation::NilEvalValue();
}
static Evaluation::EvalValue* _toInt(void*, Evaluation::EvalValue* parameters[], int parameterCount){
auto parameter = parameters[0]->EvaluateString();
auto parsed = Utilities::StringUtils::ParseInteger(parameter);
return new Evaluation::IntegerEvalValue(parsed);
}
static shared_ptr<GenericFunctionScriptType> GetFuncType(const shared_ptr<ScriptType>& result, const vector<vector<shared_ptr<ScriptType>>>& options){
auto funcType = make_shared<GenericFunctionScriptType>();
for (const auto& o: options){
@@ -62,6 +69,11 @@ namespace Porygon::StandardLibraries{
return GetFuncType(make_shared<ScriptType>(TypeClass::Nil), {{make_shared<StringScriptType>(false, 0)}});
}
static shared_ptr<GenericFunctionScriptType> GetToIntFuncType(){
return GetFuncType(make_shared<NumericScriptType>(true, false), {{make_shared<StringScriptType>(false, 0)}});
}
static shared_ptr<Evaluation::EvalValue> GetFuncEvalValue(
Evaluation::EvalValue* (*func)(void* obj, Evaluation::EvalValue* parameters[], int parameterCount),
shared_ptr<GenericFunctionScriptType> type, size_t optionLength){
@@ -97,6 +109,13 @@ namespace Porygon::StandardLibraries{
auto printFunc = BasicLibrary::GetFuncEvalValue(_print, printFuncType, 1);
bound->insert({printLookup, new Binder::BoundVariable(printFuncType)});
values->insert({printLookup, printFunc});
// Register toInt function
auto toIntFuncType = BasicLibrary::GetToIntFuncType();
auto toIntLookup = Utilities::HashedString::CreateLookup(u"toint");
auto toIntFunc = BasicLibrary::GetFuncEvalValue(_toInt, toIntFuncType, 1);
bound->insert({toIntLookup, new Binder::BoundVariable(toIntFuncType)});
values->insert({toIntLookup, toIntFunc});
}
};
}