Skip to content

Commit

Permalink
addresses comments for refactoring and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Samy-33 committed Mar 22, 2024
1 parent c5160b5 commit 9d89d8e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 44 deletions.
1 change: 1 addition & 0 deletions include/cpp/jank/prelude.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@
#include <jank/runtime/behavior/numberable.hpp>
#include <jank/runtime/behavior/nameable.hpp>
#include <jank/runtime/behavior/transientable.hpp>
#include <jank/runtime/behavior/consable.hpp>
#include <jank/runtime/behavior/associatively_writable.hpp>
6 changes: 5 additions & 1 deletion include/cpp/jank/runtime/behavior/associatively_writable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ namespace jank::runtime::behavior
template <typename T>
concept associatively_writable_in_place = requires(T * const t) {
{
t->assoc_in_place(object_ptr{}, object_ptr{}), t->dissoc_in_place(object_ptr{})
t->assoc_in_place(object_ptr{}, object_ptr{})
} -> std::convertible_to<object_ptr>;

{
t->dissoc_in_place(object_ptr{})
} -> std::convertible_to<object_ptr>;
};
}
4 changes: 2 additions & 2 deletions include/cpp/jank/runtime/obj/transient_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ namespace jank::runtime
return ret;
}

native_box<static_object> pop_in_place();

/* behavior::objectable */
native_bool equal(object const &) const;
native_persistent_string to_string() const;
Expand All @@ -49,6 +47,8 @@ namespace jank::runtime
object_ptr get_entry(object_ptr const idx) const;
native_bool contains(object_ptr const elem) const;

native_box<static_object> pop_in_place();

void assert_active() const;

object base{ object_type::transient_vector };
Expand Down
20 changes: 10 additions & 10 deletions src/cpp/jank/runtime/obj/transient_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,6 @@ namespace jank::runtime
return static_cast<native_hash>(reinterpret_cast<uintptr_t>(this));

Check warning on line 40 in src/cpp/jank/runtime/obj/transient_vector.cpp

View check run for this annotation

Codecov / codecov/patch

src/cpp/jank/runtime/obj/transient_vector.cpp#L40

Added line #L40 was not covered by tests
}

obj::transient_vector_ptr obj::transient_vector::pop_in_place()
{
assert_active();
if(data.empty())
{ throw std::runtime_error{ "Can't pop empty vector" }; }

data.take(data.size() - 1);
return this;
}

size_t obj::transient_vector::count() const
{
assert_active();
Expand Down Expand Up @@ -164,6 +154,16 @@ namespace jank::runtime
}
}

obj::transient_vector_ptr obj::transient_vector::pop_in_place()
{
assert_active();
if(data.empty())
{ throw std::runtime_error{ "Can't pop empty vector" }; }

Check warning on line 161 in src/cpp/jank/runtime/obj/transient_vector.cpp

View check run for this annotation

Codecov / codecov/patch

src/cpp/jank/runtime/obj/transient_vector.cpp#L159-L161

Added lines #L159 - L161 were not covered by tests

data.take(data.size() - 1);
return this;

Check warning on line 164 in src/cpp/jank/runtime/obj/transient_vector.cpp

View check run for this annotation

Codecov / codecov/patch

src/cpp/jank/runtime/obj/transient_vector.cpp#L163-L164

Added lines #L163 - L164 were not covered by tests
}

void obj::transient_vector::assert_active() const
{
if(!active)

Check warning on line 169 in src/cpp/jank/runtime/obj/transient_vector.cpp

View check run for this annotation

Codecov / codecov/patch

src/cpp/jank/runtime/obj/transient_vector.cpp#L169

Added line #L169 was not covered by tests
Expand Down
56 changes: 25 additions & 31 deletions src/jank/clojure/core.jank
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,9 @@
if constexpr(behavior::transientable<T>)
{ return typed_o->to_transient(); }
else
{ throw #{ (ex-info :not-transientable {:o o}) }#; }
{ throw ~{ (ex-info :not-transientable {:o o}) }; }
},
#{ o }#
~{ o }
);"))

; Returns a new, persistent version of the transient collection, in
Expand All @@ -426,9 +426,9 @@
if constexpr(behavior::persistentable<T>)
{ return typed_o->to_persistent(); }
else
{ throw #{ (ex-info :not-persistentable {:o o}) }#; }
{ throw ~{ (ex-info :not-persistentable {:o o}) }; }
},
#{ o }#
~{ o }
);"))

(defn conj!
Expand All @@ -439,17 +439,17 @@
([coll x]
(native/raw "__value = visit_object
(
[=](auto const typed_t, auto const head) -> object_ptr
[=](auto const typed_coll, auto const head) -> object_ptr
{
using T = typename decltype(typed_t)::value_type;
using T = typename decltype(typed_coll)::value_type;
if constexpr(behavior::persistentable<T>)
{ return typed_t->cons_in_place(head); }
if constexpr(behavior::consable_in_place<T>)
{ return typed_coll->cons_in_place(head); }
else
{ throw #{ (ex-info :not-persistentable {:o coll}) }#; }
{ throw ~{ (ex-info :not-persistentable {:o coll}) }; }
},
#{ coll }#,
#{ x }#
~{ coll },
~{ x }
);
")))

Expand All @@ -464,11 +464,11 @@
if constexpr(behavior::associatively_writable_in_place<T>)
{ return typed_t->assoc_in_place(key, val); }
else
{ throw #{ (ex-info :not-associatively_writable_in_place {:o coll}) }#; }
{ throw ~{ (ex-info :not-associatively_writable_in_place {:o coll}) }; }
},
#{ coll }#,
#{ k }#,
#{ v }#
~{ coll },
~{ k },
~{ v }
);
"))
([coll k v & kvs]
Expand All @@ -488,10 +488,10 @@
if constexpr(behavior::associatively_writable_in_place<T>)
{ return typed_t->dissoc_in_place(key); }
else
{ throw #{ (ex-info :not-associatively_writable_in_place {:o coll}) }#; }
{ throw ~{ (ex-info :not-associatively_writable_in_place {:o coll}) }; }
},
#{ coll }#,
#{ k }#
~{ coll },
~{ k }
);
"))
([coll k & ks]
Expand All @@ -502,19 +502,13 @@

(defn pop!
([coll]
(native/raw "__value = visit_object
(
[=](auto const typed_t) -> object_ptr
{
using T = typename decltype(typed_t)::value_type;
if constexpr(std::same_as<T, obj::transient_vector>)
{ return typed_t->pop_in_place(); }
else
{ throw #{ (ex-info :not-transient-vector {:o coll}) }#; }
},
#{ coll }#
);
(native/raw "if( ~{ coll }->type == object_type::transient_vector )
{
auto typed_coll = expect_object<obj::transient_vector>(~{ coll });
__value = typed_coll->pop_in_place();
}
else
{ throw ~{ (ex-info :not-transient-vector {:o coll}) }; }
")))

; Functions.
Expand Down

0 comments on commit 9d89d8e

Please sign in to comment.