Skip to content

Commit

Permalink
Add stronger, better hashing to all objects
Browse files Browse the repository at this point in the history
Along the way, this improves AST runtime data, reworks native_array_map,
and fixes a bug in collection equality with nil.
  • Loading branch information
jeaye committed Jan 19, 2024
1 parent e2836c8 commit 6e771a8
Show file tree
Hide file tree
Showing 85 changed files with 938 additions and 609 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ set_target_properties(jank_cling_lib PROPERTIES LINK_FLAGS -s)
add_library(
jank_lib STATIC
src/cpp/jank/native_box.cpp
src/cpp/jank/hash.cpp
src/cpp/jank/util/cli.cpp
src/cpp/jank/util/mapped_file.cpp
src/cpp/jank/util/scope_exit.cpp
Expand All @@ -154,6 +155,7 @@ add_library(
src/cpp/jank/runtime/seq.cpp
src/cpp/jank/runtime/object.cpp
src/cpp/jank/runtime/detail/object_util.cpp
src/cpp/jank/runtime/detail/native_persistent_array_map.cpp
src/cpp/jank/runtime/context.cpp
src/cpp/jank/runtime/ns.cpp
src/cpp/jank/runtime/var.cpp
Expand Down
18 changes: 10 additions & 8 deletions include/cpp/jank/analyze/expr/call.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ namespace jank::analyze::expr
arg_expr_maps = runtime::conj(arg_expr_maps, e->to_runtime_data());
}

return runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::call"),
make_box("source_expr"),
source_expr->to_runtime_data(),
make_box("args"),
args,
make_box("arg_exprs"),
arg_expr_maps);
return runtime::merge(
static_cast<expression_base const *>(this)->to_runtime_data(),
runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::call"),
make_box("source_expr"),
source_expr->to_runtime_data(),
make_box("args"),
args,
make_box("arg_exprs"),
arg_expr_maps));
}
};
}
14 changes: 8 additions & 6 deletions include/cpp/jank/analyze/expr/def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ namespace jank::analyze::expr

runtime::object_ptr to_runtime_data() const
{
return runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::def"),
make_box("name"),
name,
make_box("value"),
detail::to_runtime_data(value));
return runtime::merge(
static_cast<expression_base const *>(this)->to_runtime_data(),
runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::def"),
make_box("name"),
name,
make_box("value"),
detail::to_runtime_data(value)));
}
};
}
11 changes: 6 additions & 5 deletions include/cpp/jank/analyze/expr/do.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ namespace jank::analyze::expr

runtime::object_ptr to_runtime_data() const
{
using namespace runtime::obj;
runtime::object_ptr body_maps(make_box<runtime::obj::vector>());
for(auto const &e : body)
{
body_maps = runtime::conj(body_maps, e->to_runtime_data());
}

return runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::do"),
make_box("body"),
body_maps);
return runtime::merge(static_cast<expression_base const *>(this)->to_runtime_data(),
persistent_array_map::create_unique(make_box("__type"),
make_box("expr::do"),
make_box("body"),
body_maps));
}
};
}
17 changes: 9 additions & 8 deletions include/cpp/jank/analyze/expr/function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <list>

#include <jank/runtime/obj/symbol.hpp>
#include <jank/runtime/hash.hpp>
#include <jank/analyze/local_frame.hpp>
#include <jank/analyze/expr/do.hpp>
#include <jank/analyze/expression_base.hpp>
Expand Down Expand Up @@ -78,12 +77,14 @@ namespace jank::analyze::expr
arity_maps = runtime::conj(arity_maps, e.to_runtime_data());
}

return runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::function"),
make_box("name"),
detail::to_runtime_data(name),
make_box("arities"),
arity_maps);
return runtime::merge(
static_cast<expression_base const *>(this)->to_runtime_data(),
runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::function"),
make_box("name"),
detail::to_runtime_data(name),
make_box("arities"),
arity_maps));
}
};
}
Expand All @@ -96,7 +97,7 @@ namespace std
size_t operator()(jank::analyze::expr::arity_key const &k) const noexcept
{
static auto hasher(std::hash<decltype(jank::analyze::expr::arity_key::param_count)>{});
return jank::runtime::detail::hash_combine(hasher(k.param_count), k.is_variadic);
return jank::hash::combine(hasher(k.param_count), k.is_variadic);
}
};
}
18 changes: 10 additions & 8 deletions include/cpp/jank/analyze/expr/if.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ namespace jank::analyze::expr

runtime::object_ptr to_runtime_data() const
{
return runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::if"),
make_box("condition"),
detail::to_runtime_data(*condition),
make_box("then"),
detail::to_runtime_data(*then),
make_box("else"),
detail::to_runtime_data(else_));
return runtime::merge(
static_cast<expression_base const *>(this)->to_runtime_data(),
runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::if"),
make_box("condition"),
detail::to_runtime_data(*condition),
make_box("then"),
detail::to_runtime_data(*then),
make_box("else"),
detail::to_runtime_data(else_)));
}
};
}
14 changes: 8 additions & 6 deletions include/cpp/jank/analyze/expr/let.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ namespace jank::analyze::expr
e.second->to_runtime_data()));
}

return runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::let"),
make_box("pairs"),
pair_maps,
make_box("body"),
detail::to_runtime_data(body));
return runtime::merge(
static_cast<expression_base const *>(this)->to_runtime_data(),
runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::let"),
make_box("pairs"),
pair_maps,
make_box("body"),
detail::to_runtime_data(body)));
}
};
}
14 changes: 8 additions & 6 deletions include/cpp/jank/analyze/expr/local_reference.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ namespace jank::analyze::expr

runtime::object_ptr to_runtime_data() const
{
return runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::local_reference"),
make_box("name"),
name,
make_box("binding"),
detail::to_runtime_data(binding));
return runtime::merge(
static_cast<expression_base const *>(this)->to_runtime_data(),
runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::local_reference"),
make_box("name"),
name,
make_box("binding"),
detail::to_runtime_data(binding)));
}
};
}
10 changes: 6 additions & 4 deletions include/cpp/jank/analyze/expr/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ namespace jank::analyze::expr
make_box<runtime::obj::vector>(e.first->to_runtime_data(), e.second->to_runtime_data()));
}

return runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::map"),
make_box("data_exprs"),
pair_maps);
return runtime::merge(
static_cast<expression_base const *>(this)->to_runtime_data(),
runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::map"),
make_box("data_exprs"),
pair_maps));
}
};
}
10 changes: 6 additions & 4 deletions include/cpp/jank/analyze/expr/native_raw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ namespace jank::analyze::expr
e));
}

return runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::native_raw"),
make_box("chunks"),
chunk_maps);
return runtime::merge(
static_cast<expression_base const *>(this)->to_runtime_data(),
runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::native_raw"),
make_box("chunks"),
chunk_maps));
}
};
}
10 changes: 6 additions & 4 deletions include/cpp/jank/analyze/expr/primitive_literal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ namespace jank::analyze::expr

runtime::object_ptr to_runtime_data() const
{
return runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::primitive_literal"),
make_box("data"),
data);
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::primitive_literal"),
make_box("data"),
data));
}
};
}
14 changes: 8 additions & 6 deletions include/cpp/jank/analyze/expr/recur.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ namespace jank::analyze::expr
arg_expr_maps = runtime::conj(arg_expr_maps, e->to_runtime_data());
}

return runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::recur"),
make_box("args"),
args,
make_box("arg_exprs"),
arg_expr_maps);
return runtime::merge(
static_cast<expression_base const *>(this)->to_runtime_data(),
runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::recur"),
make_box("args"),
args,
make_box("arg_exprs"),
arg_expr_maps));
}
};
}
10 changes: 6 additions & 4 deletions include/cpp/jank/analyze/expr/throw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ namespace jank::analyze::expr

runtime::object_ptr to_runtime_data() const
{
return runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::throw"),
make_box("value"),
detail::to_runtime_data(*value));
return runtime::merge(
static_cast<expression_base const *>(this)->to_runtime_data(),
runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::throw"),
make_box("value"),
detail::to_runtime_data(*value)));
}
};
}
14 changes: 8 additions & 6 deletions include/cpp/jank/analyze/expr/var_deref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ namespace jank::analyze::expr

runtime::object_ptr to_runtime_data() const
{
return runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::var_deref"),
make_box("qualified_name"),
qualified_name,
make_box("var"),
var);
return runtime::merge(
static_cast<expression_base const *>(this)->to_runtime_data(),
runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::var_deref"),
make_box("qualified_name"),
qualified_name,
make_box("var"),
var));
}
};
}
14 changes: 8 additions & 6 deletions include/cpp/jank/analyze/expr/var_ref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ namespace jank::analyze::expr

runtime::object_ptr to_runtime_data() const
{
return runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::var_ref"),
make_box("qualified_name"),
qualified_name,
make_box("var"),
var);
return runtime::merge(
static_cast<expression_base const *>(this)->to_runtime_data(),
runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::var_ref"),
make_box("qualified_name"),
qualified_name,
make_box("var"),
var));
}
};
}
10 changes: 6 additions & 4 deletions include/cpp/jank/analyze/expr/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ namespace jank::analyze::expr
pair_maps = runtime::conj(pair_maps, e->to_runtime_data());
}

return runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::vector"),
make_box("data_exprs"),
pair_maps);
return runtime::merge(
static_cast<expression_base const *>(this)->to_runtime_data(),
runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::vector"),
make_box("data_exprs"),
pair_maps));
}
};
}
9 changes: 9 additions & 0 deletions include/cpp/jank/analyze/expression_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ namespace jank::analyze
/* Common base class for every expression. */
struct expression_base : gc
{
runtime::object_ptr to_runtime_data() const
{
using namespace runtime::obj;
return persistent_array_map::create_unique(make_box("expr_type"),
make_box(magic_enum::enum_name(expr_type)),
make_box("needs_box"),
make_box(needs_box));
}

expression_type expr_type{};
local_frame_ptr frame{};
native_bool needs_box{ true };
Expand Down
Loading

0 comments on commit 6e771a8

Please sign in to comment.