-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3afd224
commit 864dc5a
Showing
7 changed files
with
209 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#pragma once | ||
|
||
#include <jank/runtime/behavior/seqable.hpp> | ||
|
||
namespace jank::runtime | ||
{ | ||
template <> | ||
struct static_object<object_type::repeat> : gc | ||
{ | ||
static constexpr native_bool pointer_free{ false }; | ||
static constexpr native_integer INFINITE{ -1 }; | ||
using bounds_check_t = native_bool (*)(object_ptr); | ||
|
||
static_object() = default; | ||
static_object(object_ptr value); | ||
static_object(object_ptr count, object_ptr value); | ||
|
||
static object_ptr create(object_ptr value); | ||
static object_ptr create(object_ptr count, object_ptr value); | ||
|
||
/* behavior::object_like */ | ||
native_bool equal(object const &) const; | ||
native_persistent_string to_string(); | ||
void to_string(fmt::memory_buffer &buff); | ||
native_persistent_string to_code_string(); | ||
native_hash to_hash() const; | ||
|
||
/* behavior::seqable */ | ||
native_box<static_object> seq(); | ||
native_box<static_object> fresh_seq() const; | ||
|
||
/* behavior::sequenceable */ | ||
object_ptr first() const; | ||
native_box<static_object> next() const; | ||
|
||
/* behavior::sequenceable_in_place */ | ||
native_box<static_object> next_in_place(); | ||
|
||
/* behavior::conjable */ | ||
obj::cons_ptr conj(object_ptr head) const; | ||
|
||
/* behavior::metadatable */ | ||
native_box<static_object> with_meta(object_ptr m) const; | ||
|
||
object base{ object_type::repeat }; | ||
object_ptr value{}; | ||
object_ptr count{}; | ||
option<object_ptr> meta{}; | ||
}; | ||
|
||
namespace obj | ||
{ | ||
using repeat = static_object<object_type::repeat>; | ||
using repeat_ptr = native_box<repeat>; | ||
} | ||
} |
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,134 @@ | ||
#include <jank/runtime/obj/repeat.hpp> | ||
|
||
namespace jank::runtime | ||
{ | ||
|
||
obj::repeat::static_object(object_ptr const value) | ||
: value{ value } | ||
, count{ make_box(INFINITE) } | ||
{ | ||
} | ||
|
||
obj::repeat::static_object(object_ptr const count, object_ptr const value) | ||
: value{ value } | ||
, count{ count } | ||
{ | ||
} | ||
|
||
object_ptr obj::repeat::create(object_ptr const value) | ||
{ | ||
return make_box<obj::repeat>(value); | ||
} | ||
|
||
object_ptr obj::repeat::create(object_ptr const count, object_ptr const value) | ||
{ | ||
if(lte(count, make_box(0))) | ||
{ | ||
return obj::persistent_list::empty(); | ||
} | ||
return make_box<obj::repeat>(count, value); | ||
} | ||
|
||
obj::repeat_ptr obj::repeat::seq() | ||
{ | ||
return this; | ||
} | ||
|
||
obj::repeat_ptr obj::repeat::fresh_seq() const | ||
{ | ||
return make_box<obj::repeat>(count, value); | ||
} | ||
|
||
object_ptr obj::repeat::first() const | ||
{ | ||
return value; | ||
} | ||
|
||
obj::repeat_ptr obj::repeat::next() const | ||
{ | ||
if(runtime::equal(count, make_box(INFINITE))) | ||
{ | ||
return this; | ||
} | ||
if(lt(count, make_box(1))) | ||
{ | ||
return nullptr; | ||
} | ||
return make_box<obj::repeat>(make_box(add(count, make_box(-1))), value); | ||
} | ||
|
||
obj::repeat_ptr obj::repeat::next_in_place() | ||
{ | ||
if(runtime::equal(count, make_box(INFINITE))) | ||
{ | ||
return this; | ||
} | ||
if(lte(count, make_box(1))) | ||
{ | ||
return nullptr; | ||
} | ||
count = add(count, make_box(-1)); | ||
return this; | ||
} | ||
|
||
obj::cons_ptr obj::repeat::conj(object_ptr const head) const | ||
{ | ||
return make_box<obj::cons>(head, this); | ||
} | ||
|
||
native_bool obj::repeat::equal(object const &o) const | ||
{ | ||
return visit_object( | ||
[this](auto const typed_o) { | ||
using T = typename decltype(typed_o)::value_type; | ||
|
||
if constexpr(!behavior::seqable<T>) | ||
{ | ||
return false; | ||
} | ||
else | ||
{ | ||
auto seq(typed_o->fresh_seq()); | ||
/* TODO: This is common code; can it be shared? */ | ||
for(auto it(fresh_seq()); it != nullptr; | ||
it = runtime::next_in_place(it), seq = runtime::next_in_place(seq)) | ||
{ | ||
if(seq == nullptr || !runtime::equal(it, seq->first())) | ||
{ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
}, | ||
&o); | ||
} | ||
|
||
void obj::repeat::to_string(fmt::memory_buffer &buff) | ||
{ | ||
runtime::to_string(seq(), buff); | ||
} | ||
|
||
native_persistent_string obj::repeat::to_string() | ||
{ | ||
return runtime::to_string(seq()); | ||
} | ||
|
||
native_persistent_string obj::repeat::to_code_string() | ||
{ | ||
return runtime::to_code_string(seq()); | ||
} | ||
|
||
native_hash obj::repeat::to_hash() const | ||
{ | ||
return hash::ordered(&base); | ||
} | ||
|
||
obj::repeat_ptr obj::repeat::with_meta(object_ptr const m) const | ||
{ | ||
auto const meta(behavior::detail::validate_meta(m)); | ||
auto ret(fresh_seq()); | ||
ret->meta = meta; | ||
return ret; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -1182,4 +1182,4 @@ namespace jank::read::parse | |
} | ||
} | ||
} | ||
} | ||
} |