Implements toint function
continuous-integration/drone/push Build is passing Details

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

View File

@ -127,7 +127,7 @@ namespace Porygon::Parser {
}
}
int CharToInt(char16_t c) {
static int CharToInt(char16_t c) {
switch (c) {
case '0':
return 0;

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});
}
};
}

View File

@ -23,6 +23,10 @@ namespace Porygon::Utilities{
return to_16.to_bytes(s);
}
static int64_t ParseInteger(const std::u16string &s){
auto parsed = std::stol(FromUTF8(s));
return parsed;
}
};
}

View File

@ -63,6 +63,13 @@ TEST_CASE( "Print func works", "[integration]" ) {
delete script;
}
TEST_CASE( "toint func works", "[integration]" ) {
Script* script = Script::Create(u"return toint('5846321')");
REQUIRE(!script->Diagnostics -> HasErrors());
auto result = script->Evaluate();
REQUIRE(result->EvaluateInteger() == 5846321);
delete script;
}
#endif