Skip to content

Commit

Permalink
Merge pull request #106 from Samy-33/feat/obj-code-representation
Browse files Browse the repository at this point in the history
feat(runtime-objs): to_code_string
  • Loading branch information
jeaye authored Oct 11, 2024
2 parents 0723ef0 + d127580 commit f76e142
Show file tree
Hide file tree
Showing 79 changed files with 445 additions and 65 deletions.
62 changes: 62 additions & 0 deletions compiler+runtime/include/cpp/jank/runtime/core/to_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ namespace jank::runtime
void to_string(char ch, fmt::memory_buffer &buff);
void to_string(object_ptr o, fmt::memory_buffer &buff);

native_persistent_string to_code_string(object const *o);
void to_code_string(char ch, fmt::memory_buffer &buff);
void to_code_string(object_ptr o, fmt::memory_buffer &buff);

template <typename It>
void to_string(It const &begin,
It const &end,
Expand Down Expand Up @@ -63,4 +67,62 @@ namespace jank::runtime
runtime::to_string(s, buff);
return native_persistent_string{ buff.data(), buff.size() };
}

template <typename It>
void to_code_string(It const &begin,
It const &end,
native_persistent_string_view const open,
char const close,
fmt::memory_buffer &buff)
{
auto inserter(std::back_inserter(buff));
for(auto const c : open)
{
inserter = c;
}
for(auto i(begin); i != end; ++i)
{
runtime::to_code_string(*i, buff);
auto n(i);
if(++n != end)
{
inserter = ' ';
}
}
inserter = close;
}

template <typename T>
requires behavior::sequenceable<T>
void to_code_string(native_box<T> const s, fmt::memory_buffer &buff)
{
auto inserter(std::back_inserter(buff));
if(!s)
{
fmt::format_to(inserter, "()");
return;
}

fmt::format_to(inserter, "(");
native_bool needs_space{};
for(auto i(s->fresh_seq()); i != nullptr; i = i->next_in_place())
{
if(needs_space)
{
fmt::format_to(inserter, " ");
}
runtime::to_code_string(i->first(), buff);
needs_space = true;
}
fmt::format_to(inserter, ")");
}

template <typename T>
requires behavior::sequenceable<T>
native_persistent_string to_code_string(native_box<T> const s)
{
fmt::memory_buffer buff;
runtime::to_code_string(s, buff);
return native_persistent_string{ buff.data(), buff.size() };
}
}
1 change: 1 addition & 0 deletions compiler+runtime/include/cpp/jank/runtime/ns.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ namespace jank::runtime
/* behavior::object_like */
native_bool equal(object const &) const;
native_persistent_string to_string() const;
native_persistent_string to_code_string() const;
void to_string(fmt::memory_buffer &buff) const;
native_hash to_hash() const;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace jank::runtime
native_bool equal(object const &) const;
native_persistent_string to_string() const;
void to_string(fmt::memory_buffer &buff) const;
native_persistent_string to_code_string() const;
native_hash to_hash() const;

/* behavior::chunk_like */
Expand Down
1 change: 1 addition & 0 deletions compiler+runtime/include/cpp/jank/runtime/obj/atom.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace jank::runtime
native_bool equal(object const &) const;
native_persistent_string to_string() const;
void to_string(fmt::memory_buffer &buff) const;
native_persistent_string to_code_string() const;
native_hash to_hash() const;

/* behavior::derefable */
Expand Down
3 changes: 2 additions & 1 deletion compiler+runtime/include/cpp/jank/runtime/obj/character.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ namespace jank::runtime

/* behavior::object_like */
native_bool equal(object const &) const;
native_persistent_string const &to_string() const;
native_persistent_string to_string() const;
native_persistent_string to_code_string() const;
void to_string(fmt::memory_buffer &buff) const;
native_hash to_hash() const;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace jank::runtime
native_bool equal(object const &) const;
native_persistent_string to_string() const;
void to_string(fmt::memory_buffer &buff) const;
native_persistent_string to_code_string() const;
native_hash to_hash() const;

/* behavior::countable */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace jank::runtime
native_bool equal(object const &) const;
void to_string(fmt::memory_buffer &buff) const;
native_persistent_string to_string() const;
native_persistent_string to_code_string() const;
native_hash to_hash() const;

/* behavior::metadatable */
Expand Down
1 change: 1 addition & 0 deletions compiler+runtime/include/cpp/jank/runtime/obj/cons.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace jank::runtime
native_bool equal(object const &) const;
native_persistent_string to_string() const;
void to_string(fmt::memory_buffer &buff) const;
native_persistent_string to_code_string() const;
native_hash to_hash() const;

/* behavior::metadatable */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,32 @@ namespace jank::runtime::obj::detail

static void to_string_impl(typename V::const_iterator const &begin,
typename V::const_iterator const &end,
fmt::memory_buffer &buff)
fmt::memory_buffer &buff,
native_bool const to_code)
{
auto inserter(std::back_inserter(buff));
inserter = '{';
for(auto i(begin); i != end; ++i)
{
auto const pair(*i);
runtime::to_string(pair.first, buff);
if(to_code)
{
runtime::to_code_string(pair.first, buff);
}
else
{
runtime::to_string(pair.first, buff);
}
inserter = ' ';
runtime::to_string(pair.second, buff);

if(to_code)
{
runtime::to_code_string(pair.second, buff);
}
else
{
runtime::to_string(pair.second, buff);
}
auto n(i);
if(++n != end)
{
Expand All @@ -70,15 +86,27 @@ namespace jank::runtime::obj::detail
{
to_string_impl(static_cast<parent_type const *>(this)->data.begin(),
static_cast<parent_type const *>(this)->data.end(),
buff);
buff,
false);
}

native_persistent_string to_string() const
{
fmt::memory_buffer buff;
to_string_impl(static_cast<parent_type const *>(this)->data.begin(),
static_cast<parent_type const *>(this)->data.end(),
buff);
buff,
false);
return native_persistent_string{ buff.data(), buff.size() };
}

native_persistent_string to_code_string() const
{
fmt::memory_buffer buff;
to_string_impl(static_cast<parent_type const *>(this)->data.begin(),
static_cast<parent_type const *>(this)->data.end(),
buff,
true);
return native_persistent_string{ buff.data(), buff.size() };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,30 @@ namespace jank::runtime::obj::detail
&o);
}

void to_string_impl(fmt::memory_buffer &buff) const
void to_string_impl(fmt::memory_buffer &buff, native_bool const to_code) const
{
auto inserter(std::back_inserter(buff));
fmt::format_to(inserter, "(");
for(auto i(begin); i != end; ++i)
{
fmt::format_to(inserter, "[");
runtime::to_string((*i).first, buff);
if(to_code)
{
runtime::to_code_string((*i).first, buff);
}
else
{
runtime::to_string((*i).first, buff);
}
fmt::format_to(inserter, " ");
runtime::to_string((*i).second, buff);
if(to_code)
{
runtime::to_code_string((*i).second, buff);
}
else
{
runtime::to_string((*i).second, buff);
}
fmt::format_to(inserter, "]");
auto n(i);
if(++n != end)
Expand All @@ -74,13 +88,20 @@ namespace jank::runtime::obj::detail

void to_string(fmt::memory_buffer &buff) const
{
return to_string_impl(buff);
return to_string_impl(buff, false);
}

native_persistent_string to_string() const
{
fmt::memory_buffer buff;
to_string_impl(buff);
to_string_impl(buff, false);
return native_persistent_string{ buff.data(), buff.size() };
}

native_persistent_string to_code_string() const
{
fmt::memory_buffer buff;
to_string_impl(buff, true);
return native_persistent_string{ buff.data(), buff.size() };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ namespace jank::runtime::obj::detail
return native_persistent_string{ buff.data(), buff.size() };
}

native_persistent_string to_code_string() const
{
fmt::memory_buffer buff;
runtime::to_code_string(begin, end, "(", ')', buff);
return native_persistent_string{ buff.data(), buff.size() };
}

native_hash to_hash() const
{
return hash::ordered(begin, end);
Expand Down
1 change: 1 addition & 0 deletions compiler+runtime/include/cpp/jank/runtime/obj/iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace jank::runtime
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 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace jank::runtime
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::metadatable */
Expand Down
1 change: 1 addition & 0 deletions compiler+runtime/include/cpp/jank/runtime/obj/keyword.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace jank::runtime
native_bool equal(object const &) const;
native_persistent_string to_string() const;
void to_string(fmt::memory_buffer &buff) const;
native_persistent_string to_code_string() const;
native_hash to_hash() const;

/* behavior::comparable */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace jank::runtime
native_bool equal(object const &) const;
native_persistent_string to_string() const;
void to_string(fmt::memory_buffer &buff) const;
native_persistent_string to_code_string() const;
native_hash to_hash() const;

/* behavior::seqable */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace jank::runtime
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::callable */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace jank::runtime
native_bool equal(object const &o) const;
void to_string(fmt::memory_buffer &buff) const;
native_persistent_string to_string() const;
native_persistent_string to_code_string() const;
native_hash to_hash() const;

/* behavior::seqable */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ namespace jank::runtime
native_bool equal(object const &) const;
native_persistent_string to_string() const;
void to_string(fmt::memory_buffer &buff) const;
native_persistent_string to_code_string() const;
native_hash to_hash() const;

/* behavior::callable */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace jank::runtime
native_bool equal(object const &o) const;
void to_string(fmt::memory_buffer &buff) const;
native_persistent_string to_string() const;
native_persistent_string to_code_string() const;
native_hash to_hash();

/* behavior::seqable */
Expand Down
1 change: 1 addition & 0 deletions compiler+runtime/include/cpp/jank/runtime/obj/nil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace jank::runtime
/* behavior::object_like */
native_bool equal(object const &) const;
native_persistent_string const &to_string() const;
native_persistent_string const &to_code_string() const;
void to_string(fmt::memory_buffer &buff) const;
native_hash to_hash() const;

Expand Down
3 changes: 3 additions & 0 deletions compiler+runtime/include/cpp/jank/runtime/obj/number.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace jank::runtime
native_bool equal(object const &) const;
native_persistent_string to_string() const;
void to_string(fmt::memory_buffer &buff) const;
native_persistent_string to_code_string() const;
native_hash to_hash() const;

/* behavior::comparable */
Expand All @@ -45,6 +46,7 @@ namespace jank::runtime
native_bool equal(object const &) const;
native_persistent_string to_string() const;
void to_string(fmt::memory_buffer &buff) const;
native_persistent_string to_code_string() const;
native_hash to_hash() const;

/* behavior::comparable */
Expand Down Expand Up @@ -75,6 +77,7 @@ namespace jank::runtime
native_bool equal(object const &) const;
native_persistent_string to_string() const;
void to_string(fmt::memory_buffer &buff) const;
native_persistent_string to_code_string() const;
native_hash to_hash() const;

/* behavior::comparable */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ namespace jank::runtime
native_bool equal(object const &) const;
native_persistent_string to_string() const;
void to_string(fmt::memory_buffer &buff) const;
native_persistent_string to_code_string() const;
native_hash to_hash() const;

/* behavior::metadatable */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ namespace jank::runtime
native_bool equal(object const &) const;
native_persistent_string to_string() const;
void to_string(fmt::memory_buffer &buff) const;
native_persistent_string to_code_string() const;
native_hash to_hash() const;

/* behavior::metadatable */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ namespace jank::runtime
native_bool equal(object const &) const;
native_persistent_string to_string() const;
void to_string(fmt::memory_buffer &buff) const;
native_persistent_string to_code_string() const;
native_hash to_hash() const;

/* behavior::metadatable */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace jank::runtime
native_bool equal(object const &) const;
native_persistent_string const &to_string() const;
void to_string(fmt::memory_buffer &buff) const;
native_persistent_string to_code_string() const;
native_hash to_hash() const;

/* behavior::comparable */
Expand Down
Loading

0 comments on commit f76e142

Please sign in to comment.