Skip to content

Commit

Permalink
case switch support
Browse files Browse the repository at this point in the history
  • Loading branch information
jianlingzhong committed Jan 5, 2025
1 parent 4a01d6f commit c08f727
Show file tree
Hide file tree
Showing 18 changed files with 715 additions and 91 deletions.
1 change: 1 addition & 0 deletions compiler+runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ if(jank_tests)
test/cpp/jank/read/lex.cpp
test/cpp/jank/read/parse.cpp
test/cpp/jank/analyze/box.cpp
test/cpp/jank/analyze/processor.cpp
test/cpp/jank/runtime/detail/native_persistent_list.cpp
test/cpp/jank/runtime/obj/ratio.cpp
test/cpp/jank/jit/processor.cpp
Expand Down
71 changes: 71 additions & 0 deletions compiler+runtime/include/cpp/jank/analyze/expr/case.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#pragma once

#include <jank/detail/to_runtime_data.hpp>
#include <jank/analyze/expression_base.hpp>
#include <jank/runtime/obj/persistent_sorted_map.hpp>
#include <jank/runtime/obj/persistent_hash_set.hpp>

namespace jank::analyze::expr
{
using namespace jank::runtime;

template <typename E>
struct case_ : expression_base
{
enum switch_type
{
integers,
hashes,
hash_equiv,
hash_identity,
};

native_box<E> value_expr{};
native_integer shift{};
native_integer mask{};
native_box<E> default_expr{};
std::vector<native_integer> keys{};
// // native_vector<native_integer> keys{};
// obj::persistent_vector_ptr exprs{};
std::vector<native_box<E>> exprs{};
native_bool is_compact{};
switch_type switch_type{};
obj::persistent_hash_set_ptr collided_keys{};

void propagate_position(expression_position const pos)
{
std::cout << "set case expr position to " << expression_position_str(pos) << std::endl;
default_expr->propagate_position(pos);
for(auto &expr : exprs)
{
expr->propagate_position(pos);
}
position = pos;
}

object_ptr to_runtime_data() const
{
return merge(static_cast<expression_base const *>(this)->to_runtime_data(),
obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::case"),
make_box("value_expr"),
value_expr->to_runtime_data(),
make_box("shift"),
make_box(shift),
make_box("mask"),
make_box(mask),
make_box("default_expr"),
default_expr->to_runtime_data(),
// make_box("keys"),
// keys,
// make_box("body_exprs"),
// exprs,
make_box("is_compact"),
make_box(is_compact),
make_box("switch_type"),
make_box(switch_type),
make_box("collided_keys"),
collided_keys));
}
};
}
1 change: 1 addition & 0 deletions compiler+runtime/include/cpp/jank/analyze/expr/if.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace jank::analyze::expr

void propagate_position(expression_position const pos)
{
std::cout << "set if expr position to" << expression_position_str(pos) << "\n";
position = pos;
if(then)
{
Expand Down
4 changes: 3 additions & 1 deletion compiler+runtime/include/cpp/jank/analyze/expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <jank/analyze/expr/if.hpp>
#include <jank/analyze/expr/throw.hpp>
#include <jank/analyze/expr/try.hpp>
#include <jank/analyze/expr/case.hpp>

namespace jank::analyze
{
Expand All @@ -46,7 +47,8 @@ namespace jank::analyze
expr::do_<E>,
expr::if_<E>,
expr::throw_<E>,
expr::try_<E>>;
expr::try_<E>,
expr::case_<E>>;

static constexpr native_bool pointer_free{ false };

Expand Down
24 changes: 24 additions & 0 deletions compiler+runtime/include/cpp/jank/analyze/processor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,33 @@ namespace jank::analyze
option<expr::function_context_ptr> const &,
native_bool needs_box);

expression_result analyze_case(runtime::obj::persistent_list_ptr const &,
local_frame_ptr &,
expression_position,
option<expr::function_context_ptr> const &,
native_bool needs_box);

/* Returns whether or not the form is a special symbol. */
native_bool is_special(runtime::object_ptr form);

struct keys_and_exprs
{
std::vector<native_integer> keys{};
std::vector<expression_ptr> exprs{};
};

result<keys_and_exprs, error>
get_keys_and_exprs_from_array_map(native_box<obj::persistent_array_map> imap,
local_frame_ptr &f,
expression_position,
option<expr::function_context_ptr> const &fc,
native_bool needs_box);
result<keys_and_exprs, error>
get_keys_and_exprs_from_sorted_map(native_box<obj::persistent_sorted_map> imap,
local_frame_ptr &f,
expression_position,
option<expr::function_context_ptr> const &fc,
native_bool needs_box);
using special_function_type
= std::function<expression_result(runtime::obj::persistent_list_ptr const &,
local_frame_ptr &,
Expand Down
1 change: 1 addition & 0 deletions compiler+runtime/include/cpp/jank/c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ extern "C"
jank_native_bool jank_truthy(jank_object_ptr o);
jank_native_bool jank_equal(jank_object_ptr l, jank_object_ptr r);
jank_native_hash jank_to_hash(jank_object_ptr o);
jank_native_integer jank_to_integer(jank_object_ptr o);

void jank_set_meta(jank_object_ptr o, jank_object_ptr meta);

Expand Down
2 changes: 2 additions & 0 deletions compiler+runtime/include/cpp/jank/codegen/llvm_processor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ namespace jank::codegen
analyze::expr::function_arity<analyze::expression> const &);
llvm::Value *gen(analyze::expr::try_<analyze::expression> const &,
analyze::expr::function_arity<analyze::expression> const &);
llvm::Value *gen(analyze::expr::case_<analyze::expression> const &,
analyze::expr::function_arity<analyze::expression> const &);

llvm::Value *gen_var(obj::symbol_ptr qualified_name) const;
llvm::Value *gen_c_string(native_persistent_string const &s) const;
Expand Down
1 change: 1 addition & 0 deletions compiler+runtime/include/cpp/jank/evaluate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ namespace jank::evaluate
runtime::object_ptr eval(analyze::expr::if_<analyze::expression> const &);
runtime::object_ptr eval(analyze::expr::throw_<analyze::expression> const &);
runtime::object_ptr eval(analyze::expr::try_<analyze::expression> const &);
runtime::object_ptr eval(analyze::expr::case_<analyze::expression> const &);
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ namespace jank::runtime
else
{
/* TODO: Better error. */
std::cout << "(invoke) object is not callable: " << **f << std::endl;
std::cout << "(invoke) object is not callable: " << **f << "\n";
throw std::runtime_error{ "object is not callable" };
}
}
Expand Down
Loading

0 comments on commit c08f727

Please sign in to comment.