From 0446c1098b978210d9aa492f1566d3ee97140311 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Thu, 4 Jul 2019 17:18:07 +0200 Subject: [PATCH] Implements toint function --- src/Parser/Lexer.cpp | 2 +- src/StandardLibraries/BasicLibrary.hpp | 19 +++++++++++++++++++ src/Utilities/StringUtils.hpp | 4 ++++ tests/standardLibraries/BasicLibrary.cpp | 7 +++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Parser/Lexer.cpp b/src/Parser/Lexer.cpp index 43d3699..64a5691 100644 --- a/src/Parser/Lexer.cpp +++ b/src/Parser/Lexer.cpp @@ -127,7 +127,7 @@ namespace Porygon::Parser { } } - int CharToInt(char16_t c) { + static int CharToInt(char16_t c) { switch (c) { case '0': return 0; diff --git a/src/StandardLibraries/BasicLibrary.hpp b/src/StandardLibraries/BasicLibrary.hpp index 3f2089e..bbfaa7b 100644 --- a/src/StandardLibraries/BasicLibrary.hpp +++ b/src/StandardLibraries/BasicLibrary.hpp @@ -5,6 +5,7 @@ #include #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 GetFuncType(const shared_ptr& result, const vector>>& options){ auto funcType = make_shared(); for (const auto& o: options){ @@ -62,6 +69,11 @@ namespace Porygon::StandardLibraries{ return GetFuncType(make_shared(TypeClass::Nil), {{make_shared(false, 0)}}); } + static shared_ptr GetToIntFuncType(){ + return GetFuncType(make_shared(true, false), {{make_shared(false, 0)}}); + } + + static shared_ptr GetFuncEvalValue( Evaluation::EvalValue* (*func)(void* obj, Evaluation::EvalValue* parameters[], int parameterCount), shared_ptr 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}); } }; } diff --git a/src/Utilities/StringUtils.hpp b/src/Utilities/StringUtils.hpp index 533c7dd..b00c06a 100644 --- a/src/Utilities/StringUtils.hpp +++ b/src/Utilities/StringUtils.hpp @@ -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; + } }; } diff --git a/tests/standardLibraries/BasicLibrary.cpp b/tests/standardLibraries/BasicLibrary.cpp index a6717b1..8cb8678 100644 --- a/tests/standardLibraries/BasicLibrary.cpp +++ b/tests/standardLibraries/BasicLibrary.cpp @@ -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