Skip to content

Commit

Permalink
Add support for try/catch/finall/y
Browse files Browse the repository at this point in the history
  • Loading branch information
jeaye committed Jan 27, 2024
1 parent 540771b commit 97e843b
Show file tree
Hide file tree
Showing 34 changed files with 519 additions and 148 deletions.
2 changes: 1 addition & 1 deletion include/cpp/jank/analyze/expr/do.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace jank::analyze::expr
runtime::object_ptr to_runtime_data() const
{
using namespace runtime::obj;
runtime::object_ptr body_maps(make_box<runtime::obj::persistent_vector>());
runtime::object_ptr body_maps{ make_box<runtime::obj::persistent_vector>() };
for(auto const &e : body)
{
body_maps = runtime::conj(body_maps, e->to_runtime_data());
Expand Down
3 changes: 0 additions & 3 deletions include/cpp/jank/analyze/expr/throw.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#pragma once

#include <memory>

#include <jank/option.hpp>
#include <jank/analyze/expression_base.hpp>
#include <jank/detail/to_runtime_data.hpp>

Expand Down
50 changes: 50 additions & 0 deletions include/cpp/jank/analyze/expr/try.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#pragma once

#include <jank/analyze/expression_base.hpp>
#include <jank/detail/to_runtime_data.hpp>

namespace jank::analyze::expr
{
template <typename E>
struct catch_
{
runtime::obj::symbol_ptr sym{};
do_<E> body{};

runtime::object_ptr to_runtime_data() const
{
using namespace runtime::obj;

return persistent_array_map::create_unique(make_box("__type"),
make_box("expr::try::catch"),
make_box("body"),
body.to_runtime_data(),
make_box("sym"),
sym);
}
};

template <typename E>
struct try_ : expression_base
{
do_<E> body{};
catch_<E> catch_body{};
option<do_<E>> finally_body{};

runtime::object_ptr to_runtime_data() const
{
using namespace runtime::obj;

return runtime::merge(
static_cast<expression_base const *>(this)->to_runtime_data(),
persistent_array_map::create_unique(make_box("__type"),
make_box("expr::try"),
make_box("body"),
body.to_runtime_data(),
make_box("catch"),
jank::detail::to_runtime_data(catch_body),
make_box("finally"),
jank::detail::to_runtime_data(finally_body)));
}
};
}
2 changes: 2 additions & 0 deletions include/cpp/jank/analyze/expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <jank/analyze/expr/do.hpp>
#include <jank/analyze/expr/if.hpp>
#include <jank/analyze/expr/throw.hpp>
#include <jank/analyze/expr/try.hpp>
#include <jank/analyze/expr/native_raw.hpp>

namespace jank::analyze
Expand All @@ -39,6 +40,7 @@ namespace jank::analyze
expr::do_<E>,
expr::if_<E>,
expr::throw_<E>,
expr::try_<E>,
expr::native_raw<E>>;

static constexpr bool pointer_free{ false };
Expand Down
3 changes: 2 additions & 1 deletion include/cpp/jank/analyze/local_frame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ namespace jank::analyze
{
root,
fn,
let
let,
catch_
};

static constexpr bool pointer_free{ false };
Expand Down
5 changes: 5 additions & 0 deletions include/cpp/jank/analyze/processor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ namespace jank::analyze
expression_type,
option<expr::function_context_ptr> const &,
native_bool needs_box);
expression_result analyze_try(runtime::obj::persistent_list_ptr const &,
local_frame_ptr &,
expression_type,
option<expr::function_context_ptr> const &,
native_bool needs_box);
expression_result analyze_native_raw(runtime::obj::persistent_list_ptr const &,
local_frame_ptr &,
expression_type,
Expand Down
3 changes: 3 additions & 0 deletions include/cpp/jank/codegen/processor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ namespace jank::codegen
option<handle> gen(analyze::expr::throw_<analyze::expression> const &,
analyze::expr::function_arity<analyze::expression> const &,
native_bool box_needed);
option<handle> gen(analyze::expr::try_<analyze::expression> const &,
analyze::expr::function_arity<analyze::expression> const &,
native_bool box_needed);
option<handle> gen(analyze::expr::native_raw<analyze::expression> const &,
analyze::expr::function_arity<analyze::expression> const &,
native_bool box_needed);
Expand Down
3 changes: 3 additions & 0 deletions include/cpp/jank/evaluate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ namespace jank::evaluate
runtime::object_ptr eval(runtime::context &,
jit::processor const &,
analyze::expr::throw_<analyze::expression> const &);
runtime::object_ptr eval(runtime::context &,
jit::processor const &,
analyze::expr::try_<analyze::expression> const &);
runtime::object_ptr eval(runtime::context &,
jit::processor const &,
analyze::expr::native_raw<analyze::expression> const &);
Expand Down
1 change: 1 addition & 0 deletions include/cpp/jank/prelude.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <jank/type.hpp>
#include <jank/profile/time.hpp>
#include <jank/util/make_array.hpp>
#include <jank/util/scope_exit.hpp>
#include <jank/runtime/object.hpp>
#include <jank/runtime/detail/type.hpp>
#include <jank/runtime/erasure.hpp>
Expand Down
1 change: 1 addition & 0 deletions include/cpp/jank/runtime/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ namespace jank::runtime
var_ptr compile_files_var{};
var_ptr current_module_var{};
var_ptr assert_var{};
var_ptr no_recur_var{};

static thread_local native_unordered_map<context const *, std::list<thread_binding_frame>>
thread_binding_frames;
Expand Down
Loading

0 comments on commit 97e843b

Please sign in to comment.