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

Add support for runtime ratio object #137

Merged
merged 4 commits into from
Dec 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 .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
test.jank
jank-generated.hpp
.jank-repl-history
a.out
.jank-native-repl-history
.jank-repl-history
Expand Down
2 changes: 2 additions & 0 deletions compiler+runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ add_library(
src/cpp/jank/runtime/obj/cons.cpp
src/cpp/jank/runtime/obj/range.cpp
src/cpp/jank/runtime/obj/repeat.cpp
src/cpp/jank/runtime/obj/ratio.cpp
src/cpp/jank/runtime/obj/iterator.cpp
src/cpp/jank/runtime/obj/lazy_sequence.cpp
src/cpp/jank/runtime/obj/chunk_buffer.cpp
Expand Down Expand Up @@ -444,6 +445,7 @@ if(jank_tests)
test/cpp/jank/read/parse.cpp
test/cpp/jank/analyze/box.cpp
test/cpp/jank/runtime/detail/list_type.cpp
test/cpp/jank/runtime/obj/ratio.cpp
test/cpp/jank/jit/processor.cpp
)
add_executable(jank::test_exe ALIAS jank_test_exe)
Expand Down
10 changes: 10 additions & 0 deletions compiler+runtime/bin/format
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

set -euo pipefail

git_root=$(git rev-parse --show-toplevel)

for i in $(git status | grep -E "modified:" | sed 's/modified:\s*//'); do
"$git_root"/compiler+runtime/build/llvm-install/usr/local/bin/clang-format -i "$i"
echo "formatted" "$i"
done
6 changes: 6 additions & 0 deletions compiler+runtime/include/cpp/jank/runtime/core/make_box.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ namespace jank::runtime
return make_box<runtime::obj::real>(r);
}

[[gnu::always_inline, gnu::flatten, gnu::hot]]
inline auto make_box(obj::ratio_data const r)
{
return make_box<runtime::obj::ratio>(r);
}

[[gnu::always_inline, gnu::flatten, gnu::hot]]
inline auto make_box(native_persistent_string_view const &s)
{
Expand Down
6 changes: 6 additions & 0 deletions compiler+runtime/include/cpp/jank/runtime/core/math.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#pragma once

#include <jank/runtime/obj/number.hpp>
#include <jank/runtime/obj/ratio.hpp>

namespace jank::runtime
{
object_ptr add(object_ptr l, object_ptr r);
object_ptr add(obj::integer_ptr l, object_ptr r);
object_ptr add(object_ptr l, obj::integer_ptr r);
object_ptr add(object_ptr l, obj::ratio_ptr r);
native_integer add(obj::integer_ptr l, obj::integer_ptr r);
native_real add(obj::real_ptr l, obj::real_ptr r);
native_real add(obj::real_ptr l, object_ptr r);
Expand All @@ -25,6 +27,10 @@ namespace jank::runtime
object_ptr add(native_integer l, object_ptr r);
native_integer add(native_integer l, native_integer r);

obj::ratio_ptr add(obj::ratio_ptr l, obj::ratio_ptr r);
object_ptr add(obj::ratio_ptr l, obj::integer_ptr r);
obj::ratio_ptr add(obj::integer_ptr l, obj::ratio_ptr r);

object_ptr sub(object_ptr l, object_ptr r);
object_ptr sub(obj::integer_ptr l, object_ptr r);
object_ptr sub(object_ptr l, obj::integer_ptr r);
Expand Down
11 changes: 11 additions & 0 deletions compiler+runtime/include/cpp/jank/runtime/erasure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <jank/runtime/obj/chunked_cons.hpp>
#include <jank/runtime/obj/range.hpp>
#include <jank/runtime/obj/repeat.hpp>
#include <jank/runtime/obj/ratio.hpp>
#include <jank/runtime/obj/jit_function.hpp>
#include <jank/runtime/obj/multi_function.hpp>
#include <jank/runtime/obj/native_function_wrapper.hpp>
Expand Down Expand Up @@ -293,6 +294,11 @@ namespace jank::runtime
return fn(expect_object<obj::repeat>(erased), std::forward<Args>(args)...);
}
break;
case object_type::ratio:
{
return fn(expect_object<obj::ratio>(erased), std::forward<Args>(args)...);
}
break;
case object_type::native_array_sequence:
{
return fn(expect_object<obj::native_array_sequence>(erased), std::forward<Args>(args)...);
Expand Down Expand Up @@ -648,6 +654,11 @@ namespace jank::runtime
return fn(expect_object<obj::real>(erased), std::forward<Args>(args)...);
}
break;
case object_type::ratio:
{
return fn(expect_object<obj::ratio>(erased), std::forward<Args>(args)...);
}
break;
default:
return else_fn();
}
Expand Down
139 changes: 139 additions & 0 deletions compiler+runtime/include/cpp/jank/runtime/obj/ratio.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#pragma once

namespace jank::runtime
{
namespace obj
{
struct ratio_data
{
ratio_data(native_integer const, native_integer const);
ratio_data(ratio_data const &);
native_real to_real() const;
native_integer to_integer() const;
native_integer numerator{};
native_integer denominator{};
};
}

template <>
struct static_object<object_type::ratio> : gc
{
static constexpr native_bool pointer_free{ true };

static_object() = default;
static_object(static_object &&) = default;
static_object(static_object const &) = default;
static_object(obj::ratio_data const &);

static object_ptr create(native_integer const, native_integer const);
/* behavior::object_like */
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 */
native_integer compare(object const &) const;

/* behavior::comparable extended */
native_integer compare(static_object const &) const;

/* behavior::number_like */
native_integer to_integer() const;
native_real to_real() const;

object base{ object_type::ratio };
obj::ratio_data data;
};

namespace obj
{
using ratio = static_object<object_type::ratio>;
using ratio_ptr = native_box<ratio>;

object_ptr operator+(obj::ratio_data const &l, obj::ratio_data const &r);
obj::ratio_ptr operator+(obj::integer_ptr l, obj::ratio_data const &r);
obj::ratio_ptr operator+(obj::ratio_data const &l, obj::integer_ptr r);
native_real operator+(obj::real_ptr l, obj::ratio_data const &r);
native_real operator+(obj::ratio_data const &l, obj::real_ptr r);
native_real operator+(obj::ratio_data const &l, native_real r);
native_real operator+(native_real l, obj::ratio_data const &r);
obj::ratio_ptr operator+(obj::ratio_data const &l, native_integer r);
obj::ratio_ptr operator+(native_integer l, obj::ratio_data const &r);
object_ptr operator-(obj::ratio_data const &l, obj::ratio_data const &r);
obj::ratio_ptr operator-(obj::integer_ptr l, obj::ratio_data const &r);
obj::ratio_ptr operator-(obj::ratio_data const &l, obj::integer_ptr r);
native_real operator-(obj::real_ptr l, obj::ratio_data const &r);
native_real operator-(obj::ratio_data const &l, obj::real_ptr r);
native_real operator-(obj::ratio_data const &l, native_real r);
native_real operator-(native_real l, obj::ratio_data const &r);
obj::ratio_ptr operator-(obj::ratio_data const &l, native_integer r);
obj::ratio_ptr operator-(native_integer l, obj::ratio_data const &r);
object_ptr operator*(obj::ratio_data const &l, obj::ratio_data const &r);
object_ptr operator*(obj::integer_ptr l, obj::ratio_data const &r);
object_ptr operator*(obj::ratio_data const &l, obj::integer_ptr r);
native_real operator*(obj::real_ptr l, obj::ratio_data const &r);
native_real operator*(obj::ratio_data const &l, obj::real_ptr r);
native_real operator*(obj::ratio_data const &l, native_real r);
native_real operator*(native_real l, obj::ratio_data const &r);
object_ptr operator*(obj::ratio_data const &l, native_integer r);
object_ptr operator*(native_integer l, obj::ratio_data const &r);
object_ptr operator/(obj::ratio_data const &l, obj::ratio_data const &r);
object_ptr operator/(obj::integer_ptr l, obj::ratio_data const &r);
obj::ratio_ptr operator/(obj::ratio_data const &l, obj::integer_ptr r);
native_real operator/(obj::real_ptr l, obj::ratio_data const &r);
native_real operator/(obj::ratio_data const &l, obj::real_ptr r);
native_real operator/(obj::ratio_data const &l, native_real r);
native_real operator/(native_real l, obj::ratio_data const &r);
obj::ratio_ptr operator/(obj::ratio_data const &l, native_integer r);
object_ptr operator/(native_integer l, obj::ratio_data const &r);
native_bool operator==(obj::ratio_data const &l, obj::ratio_data const &r);
native_bool operator==(obj::integer_ptr l, obj::ratio_data const &r);
native_bool operator==(obj::ratio_data const &l, obj::integer_ptr r);
native_bool operator==(obj::real_ptr l, obj::ratio_data const &r);
native_bool operator==(obj::ratio_data const &l, obj::real_ptr r);
native_bool operator==(obj::ratio_data const &l, native_real r);
native_bool operator==(native_real l, obj::ratio_data const &r);
native_bool operator==(obj::ratio_data const &l, native_integer r);
native_bool operator==(native_integer l, obj::ratio_data const &r);
native_bool operator<(obj::ratio_data const &l, obj::ratio_data const &r);
native_bool operator<(obj::integer_ptr l, obj::ratio_data const &r);
native_bool operator<(obj::ratio_data const &l, obj::integer_ptr r);
native_bool operator<(obj::real_ptr l, obj::ratio_data const &r);
native_bool operator<(obj::ratio_data const &l, obj::real_ptr r);
native_bool operator<(obj::ratio_data const &l, native_real r);
native_bool operator<(native_real l, obj::ratio_data const &r);
native_bool operator<(obj::ratio_data const &l, native_integer r);
native_bool operator<(native_integer l, obj::ratio_data const &r);
native_bool operator<(native_bool l, obj::ratio_data const &r);
native_bool operator<=(obj::ratio_data const &l, obj::ratio_data const &r);
native_bool operator<=(obj::integer_ptr l, obj::ratio_data const &r);
native_bool operator<=(obj::ratio_data const &l, obj::integer_ptr r);
native_bool operator<=(obj::real_ptr l, obj::ratio_data const &r);
native_bool operator<=(obj::ratio_data const &l, obj::real_ptr r);
native_bool operator<=(obj::ratio_data const &l, native_real r);
native_bool operator<=(native_real l, obj::ratio_data const &r);
native_bool operator<=(obj::ratio_data const &l, native_integer r);
native_bool operator<=(native_integer l, obj::ratio_data const &r);
native_bool operator>(obj::ratio_data const &l, obj::ratio_data const &r);
native_bool operator>(obj::integer_ptr l, obj::ratio_data const &r);
native_bool operator>(obj::ratio_data const &l, obj::integer_ptr r);
native_bool operator>(obj::real_ptr l, obj::ratio_data const &r);
native_bool operator>(obj::ratio_data const &l, obj::real_ptr r);
native_bool operator>(obj::ratio_data const &l, native_real r);
native_bool operator>(native_real l, obj::ratio_data const &r);
native_bool operator>(obj::ratio_data const &l, native_integer r);
native_bool operator>(native_integer l, obj::ratio_data const &r);
native_bool operator>(native_bool l, obj::ratio_data const &r);
native_bool operator>=(obj::ratio_data const &l, obj::ratio_data const &r);
native_bool operator>=(obj::integer_ptr l, obj::ratio_data const &r);
native_bool operator>=(obj::ratio_data const &l, obj::integer_ptr r);
native_bool operator>=(obj::real_ptr l, obj::ratio_data const &r);
native_bool operator>=(obj::ratio_data const &l, obj::real_ptr r);
native_bool operator>=(obj::ratio_data const &l, native_real r);
native_bool operator>=(native_real l, obj::ratio_data const &r);
native_bool operator>=(obj::ratio_data const &l, native_integer r);
native_bool operator>=(native_integer l, obj::ratio_data const &r);
}
}
1 change: 1 addition & 0 deletions compiler+runtime/include/cpp/jank/runtime/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace jank::runtime
boolean,
integer,
real,
ratio,

persistent_string,
persistent_string_sequence,
Expand Down
18 changes: 14 additions & 4 deletions compiler+runtime/src/cpp/jank/read/parse.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <magic_enum.hpp>

#include <jank/runtime/obj/number.hpp>
#include <jank/runtime/obj/ratio.hpp>
#include <jank/runtime/obj/persistent_vector.hpp>
#include <jank/runtime/obj/persistent_list.hpp>
#include <jank/runtime/obj/persistent_array_map.hpp>
Expand Down Expand Up @@ -1277,10 +1278,19 @@ namespace jank::read::parse
{
return err(error{ token.pos, "Divide by zero" });
}
return object_source_info{ make_box<obj::real>(static_cast<native_real>(ratio_data.numerator)
/ ratio_data.denominator),
token,
token };
auto ratio = obj::ratio::create(ratio_data.numerator, ratio_data.denominator);
jianlingzhong marked this conversation as resolved.
Show resolved Hide resolved
if(ratio->type == object_type::ratio)
{
return object_source_info{ expect_object<obj::ratio>(ratio), token, token };
jianlingzhong marked this conversation as resolved.
Show resolved Hide resolved
}
else if(ratio->type == object_type::integer)
{
return object_source_info{ expect_object<obj::integer>(ratio), token, token };
}
else
{
return err(error{ token.pos, "Internal parsing error" });
}
}

processor::object_result processor::parse_real()
Expand Down
Loading
Loading