Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds character #78

Merged
merged 8 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler+runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ add_library(
src/cpp/jank/runtime/obj/multi_function.cpp
src/cpp/jank/runtime/obj/symbol.cpp
src/cpp/jank/runtime/obj/keyword.cpp
src/cpp/jank/runtime/obj/character.cpp
src/cpp/jank/runtime/obj/persistent_list.cpp
src/cpp/jank/runtime/obj/persistent_vector.cpp
src/cpp/jank/runtime/obj/persistent_vector_sequence.cpp
Expand Down
31 changes: 17 additions & 14 deletions compiler+runtime/include/cpp/jank/native_persistent_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,13 @@ namespace jank
}
}

[[gnu::nonnull(2)]]
constexpr native_persistent_string(const_pointer_type const s) noexcept
[[gnu::nonnull(2)]] constexpr native_persistent_string(const_pointer_type const s) noexcept
: native_persistent_string{ s, traits_type::length(s) }
{
}

[[gnu::nonnull(2)]]
constexpr native_persistent_string(const_pointer_type const s, size_type const size) noexcept
[[gnu::nonnull(2)]] constexpr native_persistent_string(const_pointer_type const s,
size_type const size) noexcept
{
if(size <= max_small_size)
{
Expand All @@ -109,11 +108,10 @@ namespace jank
}
}

[[gnu::nonnull(2, 4)]]
constexpr native_persistent_string(const_pointer_type const lhs,
size_type const lhs_size,
const_pointer_type const rhs,
size_type const rhs_size) noexcept
[[gnu::nonnull(2, 4)]] constexpr native_persistent_string(const_pointer_type const lhs,
size_type const lhs_size,
const_pointer_type const rhs,
size_type const rhs_size) noexcept
{
auto const combined_size(lhs_size + rhs_size);
if(combined_size <= max_small_size)
Expand Down Expand Up @@ -232,7 +230,8 @@ namespace jank
}

[[gnu::const]]
constexpr value_type operator[](size_t const index) const noexcept
constexpr value_type
operator[](size_t const index) const noexcept
{
return data()[index];
}
Expand Down Expand Up @@ -481,27 +480,31 @@ namespace jank

/*** Comparisons. ***/
[[gnu::const]]
constexpr native_bool operator!=(native_persistent_string const &s) const noexcept
constexpr native_bool
operator!=(native_persistent_string const &s) const noexcept
{
auto const length(size());
return length != s.size() || traits_type::compare(data(), s.data(), length);
}

[[gnu::const]]
constexpr native_bool operator==(native_persistent_string const &s) const noexcept
constexpr native_bool
operator==(native_persistent_string const &s) const noexcept
{
return !(*this != s);
}

[[gnu::const, gnu::nonnull(2)]]
constexpr native_bool operator!=(const_pointer_type const s) const noexcept
constexpr native_bool
operator!=(const_pointer_type const s) const noexcept
{
auto const length(traits_type::length(s));
return size() != length || traits_type::compare(data(), s, length);
}

[[gnu::const, gnu::nonnull(2)]]
constexpr native_bool operator==(const_pointer_type const s) const noexcept
constexpr native_bool
operator==(const_pointer_type const s) const noexcept
{
return !(*this != s);
}
Expand Down
2 changes: 2 additions & 0 deletions compiler+runtime/include/cpp/jank/read/lex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ namespace jank::read::lex
/* Has bool data. */
boolean,
/* Has string data. */
character,
/* Has string data. */
symbol,
/* Has string data. */
keyword,
Expand Down
1 change: 1 addition & 0 deletions compiler+runtime/include/cpp/jank/read/parse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ namespace jank::read::parse
object_result parse_vector();
object_result parse_map();
object_result parse_quote();
object_result parse_character();
object_result parse_meta_hint();
object_result parse_reader_macro();
object_result parse_reader_macro_set();
Expand Down
16 changes: 12 additions & 4 deletions compiler+runtime/include/cpp/jank/runtime/behavior/callable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,15 @@ namespace jank::runtime
/* TODO: Is this needed? A non-callable function-like would need to define all call overloads? :( */
template <typename T>
concept function_like = requires(T * const t) {
{ t->call(object_ptr{}) } -> std::convertible_to<object_ptr>;
{ t->call(object_ptr{}, object_ptr{}) } -> std::convertible_to<object_ptr>;
{ t->call(object_ptr{}, object_ptr{}, object_ptr{}) } -> std::convertible_to<object_ptr>;
{
t->call(object_ptr{})
} -> std::convertible_to<object_ptr>;
{
t->call(object_ptr{}, object_ptr{})
} -> std::convertible_to<object_ptr>;
{
t->call(object_ptr{}, object_ptr{}, object_ptr{})
} -> std::convertible_to<object_ptr>;
{
t->call(object_ptr{}, object_ptr{}, object_ptr{}, object_ptr{})
} -> std::convertible_to<object_ptr>;
Expand Down Expand Up @@ -243,7 +249,9 @@ namespace jank::runtime
object_ptr{})
} -> std::convertible_to<object_ptr>;

{ t->get_arity_flags() } -> std::convertible_to<size_t>;
{
t->get_arity_flags()
} -> std::convertible_to<size_t>;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace jank
using persistent_string = static_object<object_type::persistent_string>;
using persistent_list = static_object<object_type::persistent_list>;
using symbol = static_object<object_type::symbol>;
using character = static_object<object_type::character>;
}

/* TODO: Constexpr these. */
Samy-33 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -65,6 +66,12 @@ namespace jank
return make_box<runtime::obj::integer>(i);
}

[[gnu::always_inline, gnu::flatten, gnu::hot]]
inline auto make_box(char const i)
{
return make_box<runtime::obj::character>(i);
}

[[gnu::always_inline, gnu::flatten, gnu::hot]]
inline auto make_box(size_t const i)
{
Expand Down
6 changes: 6 additions & 0 deletions compiler+runtime/include/cpp/jank/runtime/erasure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <jank/runtime/obj/persistent_string.hpp>
#include <jank/runtime/obj/keyword.hpp>
#include <jank/runtime/obj/symbol.hpp>
#include <jank/runtime/obj/character.hpp>
#include <jank/runtime/obj/persistent_vector.hpp>
#include <jank/runtime/obj/persistent_list.hpp>
#include <jank/runtime/obj/persistent_set.hpp>
Expand Down Expand Up @@ -172,6 +173,11 @@ namespace jank::runtime
return fn(expect_object<obj::symbol>(erased), std::forward<Args>(args)...);
}
break;
case object_type::character:
{
return fn(expect_object<obj::character>(erased), std::forward<Args>(args)...);
}
break;
case object_type::persistent_vector:
{
return fn(expect_object<obj::persistent_vector>(erased), std::forward<Args>(args)...);
Expand Down
35 changes: 35 additions & 0 deletions compiler+runtime/include/cpp/jank/runtime/obj/character.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include <jank/runtime/object.hpp>

namespace jank::runtime
{
template <>
struct static_object<object_type::character> : gc
{
static constexpr native_bool pointer_free{ false };

static_object() = default;
static_object(static_object &&) = default;
static_object(static_object const &) = default;
static_object(native_persistent_string const &);
static_object(char);

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

object base{ object_type::character };

/* Holds the litereal form of the character as it's written eg. "\\tab" */
native_persistent_string data;
Samy-33 marked this conversation as resolved.
Show resolved Hide resolved
};

namespace obj
{
using character = static_object<object_type::character>;
using character_ptr = native_box<character>;
}
}
21 changes: 16 additions & 5 deletions compiler+runtime/include/cpp/jank/runtime/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace jank::runtime
persistent_string,
keyword,
symbol,
character,
persistent_list,
persistent_vector,
persistent_array_map,
Expand Down Expand Up @@ -69,11 +70,21 @@ namespace jank::runtime
{
template <typename T>
concept objectable = requires(T * const t) {
{ t->equal(std::declval<object const &>()) } -> std::convertible_to<native_bool>;
{ t->to_string() } -> std::convertible_to<native_persistent_string>;
{ t->to_string(std::declval<fmt::memory_buffer &>()) } -> std::same_as<void>;
{ t->to_hash() } -> std::convertible_to<native_integer>;
{ t->base } -> std::same_as<object &>;
{
t->equal(std::declval<object const &>())
} -> std::convertible_to<native_bool>;
{
t->to_string()
} -> std::convertible_to<native_persistent_string>;
{
t->to_string(std::declval<fmt::memory_buffer &>())
} -> std::same_as<void>;
{
t->to_hash()
} -> std::convertible_to<native_integer>;
{
t->base
} -> std::same_as<object &>;
};
}
}
Loading
Loading