-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: sequence multiplication to own function (#118)
- Loading branch information
Showing
6 changed files
with
87 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include <type_traits> | ||
|
||
#include "util.hpp" | ||
|
||
#include <gc.hpp> | ||
|
||
#include "object.hpp" | ||
|
||
namespace | ||
{ | ||
template<class T, class U> | ||
concept Derived = std::is_base_of_v<U, T>; | ||
|
||
template<typename T> | ||
concept IsSequenceObject = requires(T obj) { | ||
{ | ||
obj.is_sequence() | ||
} -> std::same_as<bool>; | ||
} && requires(T obj) { | ||
{ | ||
obj.is_sequence() == true | ||
}; | ||
}; | ||
|
||
template<typename T> | ||
concept SequenceObject = Derived<T, object> && IsSequenceObject<T>; | ||
|
||
template<SequenceObject T> | ||
auto multiply_sequence(const T* source, integer_object::value_type count) -> T* | ||
{ | ||
typename T::value_type target; | ||
for (integer_object::value_type i = 0; i < count; i++) { | ||
std::copy(source->value.cbegin(), source->value.cend(), std::back_inserter(target)); | ||
} | ||
return make<T>(std::move(target)); | ||
} | ||
|
||
template<SequenceObject T> | ||
auto try_mul(const object* lhs, const object* rhs) -> const object* | ||
{ | ||
using enum object::object_type; | ||
const auto seq_type = T {}.type(); | ||
if (lhs->is(integer) && rhs->is(seq_type)) { | ||
return multiply_sequence(rhs->as<T>(), lhs->as<integer_object>()->value); | ||
} | ||
if (lhs->is(seq_type) && rhs->is(integer)) { | ||
return multiply_sequence(lhs->as<T>(), rhs->as<integer_object>()->value); | ||
} | ||
return nullptr; | ||
} | ||
|
||
} // namespace | ||
|
||
auto evaluate_sequence_mul(const object* lhs, const object* rhs) -> const object* | ||
{ | ||
if (const auto* result = try_mul<array_object>(lhs, rhs)) { | ||
return result; | ||
} | ||
if (const auto* result = try_mul<string_object>(lhs, rhs)) { | ||
return result; | ||
} | ||
return nullptr; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#include "object.hpp" | ||
|
||
auto evaluate_sequence_mul(const object* lhs, const object* rhs) -> const object*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters